-
Notifications
You must be signed in to change notification settings - Fork 14.5k
release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. #149402
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
efriedma-quic
wants to merge
2
commits into
llvm:release/21.x
Choose a base branch
from
efriedma-quic:constexpr-unknown-potential-constant-backport
base: release/21.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
release/21.x: [clang] Fix potential constant expression checking with constexpr-unknown. #149402
efriedma-quic
wants to merge
2
commits into
llvm:release/21.x
from
efriedma-quic:constexpr-unknown-potential-constant-backport
+40
−4
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
…nown. 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 llvm#149041. Fixes llvm#149188. (cherry picked from commit 6a60f18)
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. |
@llvm/pr-subscribers-clang Author: Eli Friedman (efriedma-quic) ChangesManual backport of 6a60f18 Full diff: https://github.com/llvm/llvm-project/pull/149402.diff 3 Files Affected:
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<DynamicAllocLValue>()) {
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; }
+}
|
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
clang:frontend
Language frontend issues, e.g. anything involving "Sema"
clang
Clang issues not falling into any other category
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Manual backport of 6a60f18