-
Notifications
You must be signed in to change notification settings - Fork 14.8k
[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
[clang] Fix assertion failure with explicit(bool) in pre-C++11 modes #152985
Conversation
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!
✅ With the latest revision this PR passed the C/C++ code formatter. |
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).
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.
This need a release note in clang/docs/ReleaseNotes.rst
. This goes back to clang-10.
Thank. Do you need us to merge that for you? |
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
- Add [[maybe_unused]] to suppress unused variable warning - Add C++98 test case - Fix missing newline
84bb335
to
a792e91
Compare
Oh.. could you please do that? I don't know what to do because the workflows awaiting. |
Looks like the documentation failure is related to some OMP documentation, so just merging this. |
@jongmyeong-choi Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
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