[clang] 385f83c - [clang] Fix assertion failure with explicit(bool) in pre-C++11 modes (#152985)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Aug 13 07:03:13 PDT 2025
Author: Jongmyeong Choi
Date: 2025-08-13T07:03:09-07:00
New Revision: 385f83c774c25d87d53903501861cb8bcc2aac2e
URL: https://github.com/llvm/llvm-project/commit/385f83c774c25d87d53903501861cb8bcc2aac2e
DIFF: https://github.com/llvm/llvm-project/commit/385f83c774c25d87d53903501861cb8bcc2aac2e.diff
LOG: [clang] Fix assertion failure with explicit(bool) in pre-C++11 modes (#152985)
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
---------
Co-authored-by: Jongmyeong Choi <cheesechoi at gmail.com>
Added:
clang/test/Parser/explicit-bool-pre-cxx17.cpp
Modified:
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaOverload.cpp
Removed:
################################################################################
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index af576f817700a..2a195ff8537c4 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -194,6 +194,7 @@ Bug Fixes to C++ Support
- Fix the dynamic_cast to final class optimization to correctly handle
casts that are guaranteed to fail (#GH137518).
- Fix bug rejecting partial specialization of variable templates with auto NTTPs (#GH118190).
+- Fix a crash when using ``explicit(bool)`` in pre-C++11 language modes. (#GH152729)
Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp
index d593d1d74d73d..ac64dd5c9c41f 100644
--- a/clang/lib/Sema/SemaOverload.cpp
+++ b/clang/lib/Sema/SemaOverload.cpp
@@ -6275,7 +6275,9 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
QualType T, CCEKind CCE,
NamedDecl *Dest,
APValue &PreNarrowingValue) {
- assert((S.getLangOpts().CPlusPlus11 || CCE == CCEKind::TempArgStrict) &&
+ [[maybe_unused]] 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..fee0889737a89
--- /dev/null
+++ b/clang/test/Parser/explicit-bool-pre-cxx17.cpp
@@ -0,0 +1,15 @@
+// Regression test for assertion failure when explicit(bool) is used in pre-C++20
+// Fixes GitHub issue #152729
+// RUN: %clang_cc1 -std=c++98 -verify %s
+// 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 at -1 {{explicit(bool) is a C++20 extension}}
+
+ explicit(false) S(float);
+ // expected-warning at -1 {{explicit(bool) is a C++20 extension}}
+};
More information about the cfe-commits
mailing list