Skip to content

Commit 6a60f18

Browse files
[clang] Fix potential constant expression checking with constexpr-unknown. (#149227)
0717657 improved constexpr-unknown diagnostics, but potential constant expression checking broke in the process: we produce diagnostics in more cases. Suppress 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 e8182fb commit 6a60f18

File tree

3 files changed

+40
-4
lines changed

3 files changed

+40
-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: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,3 +357,29 @@ 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 GH149188 {
362+
namespace enable_if_1 {
363+
template <__SIZE_TYPE__ N>
364+
constexpr void foo(const char (&Str)[N])
365+
__attribute((enable_if(__builtin_strlen(Str), ""))) {}
366+
367+
void x() {
368+
foo("1234");
369+
}
370+
}
371+
372+
namespace enable_if_2 {
373+
constexpr const char (&f())[];
374+
extern const char (&Str)[];
375+
constexpr int foo()
376+
__attribute((enable_if(__builtin_strlen(Str), "")))
377+
{return __builtin_strlen(Str);}
378+
379+
constexpr const char (&f())[] {return "a";}
380+
constexpr const char (&Str)[] = f();
381+
void x() {
382+
constexpr int x = foo();
383+
}
384+
}
385+
}

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

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

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

0 commit comments

Comments
 (0)