Skip to content

Commit 9ce04c8

Browse files
committed
[clang] Fix potential constant expression checking with constexpr-unknown.
0717657 improved constexpr-unkown diagnostics, but potential constant expression checking broke in the process: we produce diagnostics in more cases. Supress the diagnostics as appropriate. This fix affects -Winvalid-constexpr and the enable_if attribute. (The -Winvalid-constexpr diagnostic isn't really important right now, but it will become important if we allow constexpr-unknown with pre-C++23 standards.) Fixes #149041. Fixes #149188.
1 parent 703501e commit 9ce04c8

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

clang/lib/AST/ExprConstant.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4450,7 +4450,8 @@ static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
44504450
}
44514451
} else if (!IsAccess) {
44524452
return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4453-
} else if (IsConstant && Info.checkingPotentialConstantExpression() &&
4453+
} else if ((IsConstant || BaseType->isReferenceType()) &&
4454+
Info.checkingPotentialConstantExpression() &&
44544455
BaseType->isLiteralType(Info.Ctx) && !VD->hasDefinition()) {
44554456
// This variable might end up being constexpr. Don't diagnose it yet.
44564457
} else if (IsConstant) {
@@ -4491,9 +4492,11 @@ static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
44914492
// a null BaseVal. Any constexpr-unknown variable seen here is an error:
44924493
// we can't access a constexpr-unknown object.
44934494
if (AK != clang::AK_Dereference && !BaseVal) {
4494-
Info.FFDiag(E, diag::note_constexpr_access_unknown_variable, 1)
4495-
<< AK << VD;
4496-
Info.Note(VD->getLocation(), diag::note_declared_at);
4495+
if (!Info.checkingPotentialConstantExpression()) {
4496+
Info.FFDiag(E, diag::note_constexpr_access_unknown_variable, 1)
4497+
<< AK << VD;
4498+
Info.Note(VD->getLocation(), diag::note_declared_at);
4499+
}
44974500
return CompleteObject();
44984501
}
44994502
} else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,27 @@ namespace pointer_comparisons {
357357
static_assert(!f4()); // expected-error {{static assertion expression is not an integral constant expression}} \
358358
// expected-note {{in call to 'f4()'}}
359359
}
360+
361+
namespace enable_if_1 {
362+
template <__SIZE_TYPE__ N>
363+
constexpr void foo(const char (&Str)[N])
364+
__attribute((enable_if(__builtin_strlen(Str), ""))) {}
365+
366+
void x() {
367+
foo("1234");
368+
}
369+
}
370+
371+
namespace enable_if_2 {
372+
constexpr const char (&f())[];
373+
extern const char (&Str)[];
374+
constexpr int foo()
375+
__attribute((enable_if(__builtin_strlen(Str), "")))
376+
{return __builtin_strlen(Str);}
377+
378+
constexpr const char (&f())[] {return "a";}
379+
constexpr const char (&Str)[] = f();
380+
void x() {
381+
constexpr int x = foo();
382+
}
383+
}

clang/test/SemaCXX/constexpr-never-constant.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,8 @@ constexpr void other_func() {
2424

2525
throw 12;
2626
}
27+
28+
// Make sure these don't trigger the diagnostic.
29+
extern const bool& b;
30+
constexpr bool fun1() { return b; }
31+
constexpr bool fun2(const bool& b) { return b; }

0 commit comments

Comments
 (0)