From 44531c6970187087fd86aced5a8e1f027ac7c80b Mon Sep 17 00:00:00 2001 From: Jongmyeong Choi Date: Mon, 11 Aug 2025 18:36:30 +0900 Subject: [PATCH 1/3] [clang] Fix assertion failure with explicit(bool) in pre-C++11 modes 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 --- clang/lib/Sema/SemaOverload.cpp | 4 +++- clang/test/Parser/explicit-bool-pre-cxx17.cpp | 14 ++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) create mode 100644 clang/test/Parser/explicit-bool-pre-cxx17.cpp diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index d593d1d74d73d..816e9598222fe 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) && + 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 From e9ac9b2b61eee3c0d73c58ed6bf6809f5e45aff9 Mon Sep 17 00:00:00 2001 From: Jongmyeong Choi Date: Tue, 12 Aug 2025 05:43:05 +0900 Subject: [PATCH 2/3] Address review feedback - Add [[maybe_unused]] to suppress unused variable warning - Add C++98 test case - Fix missing newline --- clang/lib/Sema/SemaOverload.cpp | 4 ++-- clang/test/Parser/explicit-bool-pre-cxx17.cpp | 3 ++- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/clang/lib/Sema/SemaOverload.cpp b/clang/lib/Sema/SemaOverload.cpp index 816e9598222fe..ac64dd5c9c41f 100644 --- a/clang/lib/Sema/SemaOverload.cpp +++ b/clang/lib/Sema/SemaOverload.cpp @@ -6275,8 +6275,8 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From, QualType T, CCEKind CCE, NamedDecl *Dest, APValue &PreNarrowingValue) { - bool isCCEAllowedPreCXX11 = - (CCE == CCEKind::TempArgStrict || CCE == CCEKind::ExplicitBool); + [[maybe_unused]] bool isCCEAllowedPreCXX11 = + (CCE == CCEKind::TempArgStrict || CCE == CCEKind::ExplicitBool); assert((S.getLangOpts().CPlusPlus11 || isCCEAllowedPreCXX11) && "converted constant expression outside C++11 or TTP matching"); diff --git a/clang/test/Parser/explicit-bool-pre-cxx17.cpp b/clang/test/Parser/explicit-bool-pre-cxx17.cpp index 0a704f3ef85cd..fee0889737a89 100644 --- a/clang/test/Parser/explicit-bool-pre-cxx17.cpp +++ b/clang/test/Parser/explicit-bool-pre-cxx17.cpp @@ -1,5 +1,6 @@ // 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 @@ -11,4 +12,4 @@ struct S { explicit(false) S(float); // expected-warning@-1 {{explicit(bool) is a C++20 extension}} -}; \ No newline at end of file +}; From a792e919460669975263229a13c1a3b086179448 Mon Sep 17 00:00:00 2001 From: Jongmyeong Choi Date: Wed, 13 Aug 2025 20:13:15 +0900 Subject: [PATCH 3/3] Add release note for explicit(bool) assertion fix --- clang/docs/ReleaseNotes.rst | 1 + 1 file changed, 1 insertion(+) 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 ^^^^^^^^^^^^^^^^^^^^^^^^^