[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
github-actions[bot] wrote: @efriedma-quic (or anyone else). If you would like to add a note about this fix in the release notes (completely optional). Please reply to this comment with a one or two sentence description of the fix. When you are done, please add the release:note label to this PR. https://github.com/llvm/llvm-project/pull/149402 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
https://github.com/tru closed https://github.com/llvm/llvm-project/pull/149402 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
https://github.com/tru updated https://github.com/llvm/llvm-project/pull/149402
>From ece444008ab4bfdb29f39ddc84469972b2e4d563 Mon Sep 17 00:00:00 2001
From: Eli Friedman
Date: Thu, 17 Jul 2025 13:14:34 -0700
Subject: [PATCH] [clang] Fix potential constant expression checking with
constexpr-unknown.
071765749a70b22fb62f2efc07a3f242ff5b4c52 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.
(cherry picked from commit 6a60f18997d62b0e2842a921fcb6beb3e52ed823)
---
clang/lib/AST/ExprConstant.cpp| 11 +---
.../SemaCXX/constant-expression-p2280r4.cpp | 26 +++
.../test/SemaCXX/constexpr-never-constant.cpp | 7 +
3 files changed, 40 insertions(+), 4 deletions(-)
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 1b33b6706e204..d7b1173283c57 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -4441,7 +4441,8 @@ static CompleteObject findCompleteObject(EvalInfo &Info,
const Expr *E,
}
} else if (!IsAccess) {
return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
- } else if (IsConstant && Info.checkingPotentialConstantExpression() &&
+ } else if ((IsConstant || BaseType->isReferenceType()) &&
+ Info.checkingPotentialConstantExpression() &&
BaseType->isLiteralType(Info.Ctx) && !VD->hasDefinition()) {
// This variable might end up being constexpr. Don't diagnose it yet.
} else if (IsConstant) {
@@ -4478,9 +4479,11 @@ static CompleteObject findCompleteObject(EvalInfo &Info,
const Expr *E,
// a null BaseVal. Any constexpr-unknown variable seen here is an error:
// we can't access a constexpr-unknown object.
if (!BaseVal) {
- Info.FFDiag(E, diag::note_constexpr_access_unknown_variable, 1)
- << AK << VD;
- Info.Note(VD->getLocation(), diag::note_declared_at);
+ if (!Info.checkingPotentialConstantExpression()) {
+Info.FFDiag(E, diag::note_constexpr_access_unknown_variable, 1)
+<< AK << VD;
+Info.Note(VD->getLocation(), diag::note_declared_at);
+ }
return CompleteObject();
}
} else if (DynamicAllocLValue DA = LVal.Base.dyn_cast())
{
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index 03fea91169787..16f5f823d26c1 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -357,3 +357,29 @@ namespace pointer_comparisons {
static_assert(!f4()); // expected-error {{static assertion expression is not
an integral constant expression}} \
// expected-note {{in call to 'f4()'}}
}
+
+namespace GH149188 {
+namespace enable_if_1 {
+ template <__SIZE_TYPE__ N>
+ constexpr void foo(const char (&Str)[N])
+ __attribute((enable_if(__builtin_strlen(Str), ""))) {}
+
+ void x() {
+ foo("1234");
+ }
+}
+
+namespace enable_if_2 {
+ constexpr const char (&f())[];
+ extern const char (&Str)[];
+ constexpr int foo()
+ __attribute((enable_if(__builtin_strlen(Str), "")))
+ {return __builtin_strlen(Str);}
+
+ constexpr const char (&f())[] {return "a";}
+ constexpr const char (&Str)[] = f();
+ void x() {
+ constexpr int x = foo();
+ }
+}
+}
diff --git a/clang/test/SemaCXX/constexpr-never-constant.cpp
b/clang/test/SemaCXX/constexpr-never-constant.cpp
index 307810ee263dd..5756bb647ce88 100644
--- a/clang/test/SemaCXX/constexpr-never-constant.cpp
+++ b/clang/test/SemaCXX/constexpr-never-constant.cpp
@@ -24,3 +24,10 @@ constexpr void other_func() {
throw 12;
}
+
+namespace GH149041 {
+ // Make sure these don't trigger the diagnostic.
+ extern const bool& b;
+ constexpr bool fun1() { return b; }
+ constexpr bool fun2(const bool& b) { return b; }
+}
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
https://github.com/cor3ntin approved this pull request. LGTM! https://github.com/llvm/llvm-project/pull/149402 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
tru wrote: @AaronBallman can you review or ask someone to do that? This seems to be blocking another fix in #150015 https://github.com/llvm/llvm-project/pull/149402 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
tru wrote: Who can review this? https://github.com/llvm/llvm-project/pull/149402 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
llvmbot wrote:
@llvm/pr-subscribers-clang
Author: Eli Friedman (efriedma-quic)
Changes
Manual backport of 6a60f18997d62b0e2842a921fcb6beb3e52ed823
---
Full diff: https://github.com/llvm/llvm-project/pull/149402.diff
3 Files Affected:
- (modified) clang/lib/AST/ExprConstant.cpp (+7-4)
- (modified) clang/test/SemaCXX/constant-expression-p2280r4.cpp (+26)
- (modified) clang/test/SemaCXX/constexpr-never-constant.cpp (+7)
``diff
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 1b33b6706e204..d7b1173283c57 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -4441,7 +4441,8 @@ static CompleteObject findCompleteObject(EvalInfo &Info,
const Expr *E,
}
} else if (!IsAccess) {
return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
- } else if (IsConstant && Info.checkingPotentialConstantExpression() &&
+ } else if ((IsConstant || BaseType->isReferenceType()) &&
+ Info.checkingPotentialConstantExpression() &&
BaseType->isLiteralType(Info.Ctx) && !VD->hasDefinition()) {
// This variable might end up being constexpr. Don't diagnose it yet.
} else if (IsConstant) {
@@ -4478,9 +4479,11 @@ static CompleteObject findCompleteObject(EvalInfo &Info,
const Expr *E,
// a null BaseVal. Any constexpr-unknown variable seen here is an error:
// we can't access a constexpr-unknown object.
if (!BaseVal) {
- Info.FFDiag(E, diag::note_constexpr_access_unknown_variable, 1)
- << AK << VD;
- Info.Note(VD->getLocation(), diag::note_declared_at);
+ if (!Info.checkingPotentialConstantExpression()) {
+Info.FFDiag(E, diag::note_constexpr_access_unknown_variable, 1)
+<< AK << VD;
+Info.Note(VD->getLocation(), diag::note_declared_at);
+ }
return CompleteObject();
}
} else if (DynamicAllocLValue DA = LVal.Base.dyn_cast())
{
diff --git a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
index 03fea91169787..16f5f823d26c1 100644
--- a/clang/test/SemaCXX/constant-expression-p2280r4.cpp
+++ b/clang/test/SemaCXX/constant-expression-p2280r4.cpp
@@ -357,3 +357,29 @@ namespace pointer_comparisons {
static_assert(!f4()); // expected-error {{static assertion expression is not
an integral constant expression}} \
// expected-note {{in call to 'f4()'}}
}
+
+namespace GH149188 {
+namespace enable_if_1 {
+ template <__SIZE_TYPE__ N>
+ constexpr void foo(const char (&Str)[N])
+ __attribute((enable_if(__builtin_strlen(Str), ""))) {}
+
+ void x() {
+ foo("1234");
+ }
+}
+
+namespace enable_if_2 {
+ constexpr const char (&f())[];
+ extern const char (&Str)[];
+ constexpr int foo()
+ __attribute((enable_if(__builtin_strlen(Str), "")))
+ {return __builtin_strlen(Str);}
+
+ constexpr const char (&f())[] {return "a";}
+ constexpr const char (&Str)[] = f();
+ void x() {
+ constexpr int x = foo();
+ }
+}
+}
diff --git a/clang/test/SemaCXX/constexpr-never-constant.cpp
b/clang/test/SemaCXX/constexpr-never-constant.cpp
index 307810ee263dd..5756bb647ce88 100644
--- a/clang/test/SemaCXX/constexpr-never-constant.cpp
+++ b/clang/test/SemaCXX/constexpr-never-constant.cpp
@@ -24,3 +24,10 @@ constexpr void other_func() {
throw 12;
}
+
+namespace GH149041 {
+ // Make sure these don't trigger the diagnostic.
+ extern const bool& b;
+ constexpr bool fun1() { return b; }
+ constexpr bool fun2(const bool& b) { return b; }
+}
``
https://github.com/llvm/llvm-project/pull/149402
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
https://github.com/efriedma-quic reopened https://github.com/llvm/llvm-project/pull/149402 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
https://github.com/efriedma-quic closed https://github.com/llvm/llvm-project/pull/149402 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
[llvm-branch-commits] [clang] release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. (PR #149402)
efriedma-quic wrote: It looks like I triggered the wrong set of bots by opening the pull request with the wrong base branch. I'm going to close/reopen. https://github.com/llvm/llvm-project/pull/149402 ___ llvm-branch-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits
