Skip to content

Commit 385f83c

Browse files
jongmyeong-choiJongmyeong Choi
andauthored
[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 <[email protected]>
1 parent 6b20b16 commit 385f83c

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

clang/docs/ReleaseNotes.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,7 @@ Bug Fixes to C++ Support
194194
- Fix the dynamic_cast to final class optimization to correctly handle
195195
casts that are guaranteed to fail (#GH137518).
196196
- Fix bug rejecting partial specialization of variable templates with auto NTTPs (#GH118190).
197+
- Fix a crash when using ``explicit(bool)`` in pre-C++11 language modes. (#GH152729)
197198

198199
Bug Fixes to AST Handling
199200
^^^^^^^^^^^^^^^^^^^^^^^^^

clang/lib/Sema/SemaOverload.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6275,7 +6275,9 @@ static ExprResult BuildConvertedConstantExpression(Sema &S, Expr *From,
62756275
QualType T, CCEKind CCE,
62766276
NamedDecl *Dest,
62776277
APValue &PreNarrowingValue) {
6278-
assert((S.getLangOpts().CPlusPlus11 || CCE == CCEKind::TempArgStrict) &&
6278+
[[maybe_unused]] bool isCCEAllowedPreCXX11 =
6279+
(CCE == CCEKind::TempArgStrict || CCE == CCEKind::ExplicitBool);
6280+
assert((S.getLangOpts().CPlusPlus11 || isCCEAllowedPreCXX11) &&
62796281
"converted constant expression outside C++11 or TTP matching");
62806282

62816283
if (checkPlaceholderForOverload(S, From))
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// Regression test for assertion failure when explicit(bool) is used in pre-C++20
2+
// Fixes GitHub issue #152729
3+
// RUN: %clang_cc1 -std=c++98 -verify %s
4+
// RUN: %clang_cc1 -std=c++03 -verify %s
5+
// RUN: %clang_cc1 -std=c++11 -verify %s
6+
// RUN: %clang_cc1 -std=c++14 -verify %s
7+
// RUN: %clang_cc1 -std=c++17 -verify %s
8+
9+
struct S {
10+
explicit(true) S(int);
11+
// expected-warning@-1 {{explicit(bool) is a C++20 extension}}
12+
13+
explicit(false) S(float);
14+
// expected-warning@-1 {{explicit(bool) is a C++20 extension}}
15+
};

0 commit comments

Comments
 (0)