Skip to content

Commit 5d1d2f0

Browse files
authored
[clang][ExprConst] Allow comparisons with string literals (#106733)
Don't diagnose them, but literals still have distinct addresses. Fixes #58754
1 parent 127c349 commit 5d1d2f0

File tree

3 files changed

+7
-15
lines changed

3 files changed

+7
-15
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2146,7 +2146,7 @@ static bool IsLiteralLValue(const LValue &Value) {
21462146
if (Value.getLValueCallIndex())
21472147
return false;
21482148
const Expr *E = Value.Base.dyn_cast<const Expr*>();
2149-
return E && !isa<MaterializeTemporaryExpr>(E);
2149+
return E && !isa<MaterializeTemporaryExpr, StringLiteral>(E);
21502150
}
21512151

21522152
static bool IsWeakLValue(const LValue &Value) {

clang/test/AST/ByteCode/cxx20.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -108,22 +108,16 @@ constexpr auto p2 = "test2";
108108
constexpr bool b1 = foo(p1) == foo(p1);
109109
static_assert(b1);
110110

111-
constexpr bool b2 = foo(p1) == foo(p2); // ref-error {{must be initialized by a constant expression}} \
112-
// ref-note {{comparison of addresses of literals}} \
113-
// ref-note {{declared here}}
114-
static_assert(!b2); // ref-error {{not an integral constant expression}} \
115-
// ref-note {{not a constant expression}}
111+
constexpr bool b2 = foo(p1) == foo(p2);
112+
static_assert(!b2);
116113

117114
constexpr auto name1() { return "name1"; }
118115
constexpr auto name2() { return "name2"; }
119116

120117
constexpr auto b3 = name1() == name1();
121118
static_assert(b3);
122-
constexpr auto b4 = name1() == name2(); // ref-error {{must be initialized by a constant expression}} \
123-
// ref-note {{has unspecified value}} \
124-
// ref-note {{declared here}}
125-
static_assert(!b4); // ref-error {{not an integral constant expression}} \
126-
// ref-note {{not a constant expression}}
119+
constexpr auto b4 = name1() == name2();
120+
static_assert(!b4);
127121

128122
namespace UninitializedFields {
129123
class A {

clang/test/SemaCXX/constant-expression-cxx11.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,9 @@ struct Str {
358358

359359
extern char externalvar[];
360360
constexpr bool constaddress = (void *)externalvar == (void *)0x4000UL; // expected-error {{must be initialized by a constant expression}} expected-note {{reinterpret_cast}}
361-
constexpr bool litaddress = "foo" == "foo"; // expected-error {{must be initialized by a constant expression}}
362-
// expected-note@-1 {{comparison of addresses of literals has unspecified value}}
363-
// cxx20_23-warning@-2 {{comparison between two arrays is deprecated}}
361+
constexpr bool litaddress = "foo" == "foo"; // cxx20_23-warning {{comparison between two arrays is deprecated}}
364362
static_assert(0 != "foo", "");
365-
363+
static_assert("foo" != "foo", "");// cxx20_23-warning {{comparison between two arrays is deprecated}}
366364
}
367365

368366
namespace MaterializeTemporary {

0 commit comments

Comments
 (0)