-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[clang] Fix assertion failure with explicit(bool) in pre-C++11 modes #152985
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
base: main
Are you sure you want to change the base?
[clang] Fix assertion failure with explicit(bool) in pre-C++11 modes #152985
Conversation
Allow CCEKind::ExplicitBool in BuildConvertedConstantExpression for pre-C++11 contexts, similar to the existing TempArgStrict exception. This enables explicit(bool) to work as a C++20 extension in earlier language modes without triggering assertion failures. Fixes llvm#152729
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-clang Author: Jongmyeong Choi (jongmyeong-choi) ChangesAllow CCEKind::ExplicitBool in BuildConvertedConstantExpression for pre-C++11 contexts, similar to the existing TempArgStrict exception. This enables explicit(bool) to work as a C++20 extension in earlier language modes without triggering assertion failures. Fixes #152729 Full diff: https://github.com/llvm/llvm-project/pull/152985.diff 2 Files Affected:
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 5dd5b495480d9..700c330f02427 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -6268,7 +6268,9 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
QualType T, CCEKind CCE,
NamedDecl *Dest,
APValue &PreNarrowingValue) {
- assert((S.getLangOpts().CPlusPlus11 || CCE == CCEKind::TempArgStrict) &&
+ bool isCCEAllowedPreCXX11 =
+ (CCE == CCEKind::TempArgStrict || CCE == CCEKind::ExplicitBool);
+ assert((S.getLangOpts().CPlusPlus11 || isCCEAllowedPreCXX11) &&
"converted constant expression outside C++11 or TTP matching");
if (checkPlaceholderForOverload(S, From))
diff --git a/clang/test/Parser/explicit-bool-pre-cxx17.cpp b/clang/test/Parser/explicit-bool-pre-cxx17.cpp
new file mode 100644
index 0000000000000..0a704f3ef85cd
--- /dev/null
+++ b/clang/test/Parser/explicit-bool-pre-cxx17.cpp
@@ -0,0 +1,14 @@
+// Regression test for assertion failure when explicit(bool) is used in pre-C++20
+// Fixes GitHub issue #152729
+// RUN: %clang_cc1 -std=c++03 -verify %s
+// RUN: %clang_cc1 -std=c++11 -verify %s
+// RUN: %clang_cc1 -std=c++14 -verify %s
+// RUN: %clang_cc1 -std=c++17 -verify %s
+
+struct S {
+ explicit(true) S(int);
+ // expected-warning@-1 {{explicit(bool) is a C++20 extension}}
+
+ explicit(false) S(float);
+ // expected-warning@-1 {{explicit(bool) is a C++20 extension}}
+};
\ No newline at end of file
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the fix!
You can test this locally with the following command:git-clang-format --diff HEAD~1 HEAD --extensions cpp -- clang/test/Parser/explicit-bool-pre-cxx17.cpp clang/lib/Sema/SemaOverload.cpp View the diff from clang-format here.diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index 816e95982..90bec25a8 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -6276,7 +6276,7 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
NamedDecl *Dest,
APValue &PreNarrowingValue) {
bool isCCEAllowedPreCXX11 =
- (CCE == CCEKind::TempArgStrict || CCE == CCEKind::ExplicitBool);
+ (CCE == CCEKind::TempArgStrict || CCE == CCEKind::ExplicitBool);
assert((S.getLangOpts().CPlusPlus11 || isCCEAllowedPreCXX11) &&
"converted constant expression outside C++11 or TTP matching");
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM sans small nit.
FYI the change is correct as far as if we want to allow this as an extension.
But we otherwise don't produce an extension warning here.
Usually when we allow something as an extension to earlier standards, this warning is added, which users can also either ignore it or turn it into an error, and is helpful for people who want strict standard conformance (ie want to make sure this code will compile in other compilers).
- Add [[maybe_unused]] to suppress unused variable warning - Add C++98 test case - Fix missing newline
Allow CCEKind::ExplicitBool in BuildConvertedConstantExpression for pre-C++11 contexts, similar to the existing TempArgStrict exception. This enables explicit(bool) to work as a C++20 extension in earlier language modes without triggering assertion failures.
Fixes #152729