Skip to content

[Clang][Parser] Fix assertion failure with explicit(bool) in pre-C++20 modes #152896

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

Closed

Conversation

jongmyeong-choi
Copy link

Summary

Fixes assertion failure when using explicit(bool) syntax in C++98 and C++11 modes. The crash occurred in
BuildConvertedConstantExpression when parsing explicit(true) or explicit(false) in these older C++ standards.

Problem

The assertion failure only occurs in C++98/C++11 modes:

// C++98/C++11 mode - CRASH
struct S {
  explicit(true) S(int);  // Assertion failure in BuildConvertedConstantExpression
};

The assertion that failed:

assert((S.getLangOpts().CPlusPlus11 || CCE == CCEKind::TempArgStrict) &&
       "converted constant expression outside C++11 or TTP matching");

Solution

  • Added Parser-level error handling for explicit(bool) in pre-C++20 modes
  • Added new diagnostic: err_explicit_bool_requires_cpp20
  • Maintains C++17 extension behavior for backward compatibility
  • Prevents reaching the problematic Sema code path that caused crashes

Implementation Strategy

While crashes only occur in C++98/C++11, the fix applies to all pre-C++20 modes (C++98/C++11/C++14) following consistent language
feature boundaries, with C++17 maintained as an extension for compatibility.

Changes

  • clang/include/clang/Basic/DiagnosticParseKinds.td: Added new error diagnostic
  • clang/lib/Parse/ParseDecl.cpp: Enhanced explicit specifier parsing with proper language version checks
  • clang/test/Parser/explicit-bool-pre-cxx17.cpp: Added regression test covering C++03/C++11/C++14

Before this fix, using explicit(bool) syntax in C++98/C++03 modes
would cause an assertion failure in BuildConvertedConstantExpression due
to the parser accepting the syntax but semantic analysis rejecting it.

This patch:
- Adds a new diagnostic error 'err_explicit_bool_requires_cpp20'
- Updates parser logic to reject explicit(bool) in pre-C++17 modes with
  proper error recovery instead of proceeding to semantic analysis
- Maintains existing behavior for C++17 (extension warning) and C++20+
  for backward compatibility with existing code

The fix prevents crashes and provides clear error messages to users
attempting to use C++20 features in earlier language modes.

Fixes assertion: (S.getLangOpts().CPlusPlus11 || CCE == CCEKind::TempArgStrict)
in BuildConvertedConstantExpression

Test: clang/test/Parser/explicit-bool-pre-cxx17.cpp
Copy link

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 @ followed by their GitHub username.

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.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Aug 10, 2025
@llvmbot
Copy link
Member

llvmbot commented Aug 10, 2025

@llvm/pr-subscribers-clang

Author: Jongmyeong Choi (jongmyeong-choi)

Changes

Summary

Fixes assertion failure when using explicit(bool) syntax in C++98 and C++11 modes. The crash occurred in
BuildConvertedConstantExpression when parsing explicit(true) or explicit(false) in these older C++ standards.

Problem

The assertion failure only occurs in C++98/C++11 modes:

// C++98/C++11 mode - CRASH
struct S {
  explicit(true) S(int);  // Assertion failure in BuildConvertedConstantExpression
};

The assertion that failed:

assert((S.getLangOpts().CPlusPlus11 || CCE == CCEKind::TempArgStrict) &&
       "converted constant expression outside C++11 or TTP matching");

Solution

  • Added Parser-level error handling for explicit(bool) in pre-C++20 modes
  • Added new diagnostic: err_explicit_bool_requires_cpp20
  • Maintains C++17 extension behavior for backward compatibility
  • Prevents reaching the problematic Sema code path that caused crashes

Implementation Strategy

While crashes only occur in C++98/C++11, the fix applies to all pre-C++20 modes (C++98/C++11/C++14) following consistent language
feature boundaries, with C++17 maintained as an extension for compatibility.

Changes

  • clang/include/clang/Basic/DiagnosticParseKinds.td: Added new error diagnostic
  • clang/lib/Parse/ParseDecl.cpp: Enhanced explicit specifier parsing with proper language version checks
  • clang/test/Parser/explicit-bool-pre-cxx17.cpp: Added regression test covering C++03/C++11/C++14

Full diff: https://github.com/llvm/llvm-project/pull/152896.diff

3 Files Affected:

  • (modified) clang/include/clang/Basic/DiagnosticParseKinds.td (+2)
  • (modified) clang/lib/Parse/ParseDecl.cpp (+58-20)
  • (added) clang/test/Parser/explicit-bool-pre-cxx17.cpp (+16)
diff --git a/clang/include/clang/Basic/DiagnosticParseKinds.td b/clang/include/clang/Basic/DiagnosticParseKinds.td
index 165f01514e2b1..ce1087319b326 100644
--- a/clang/include/clang/Basic/DiagnosticParseKinds.td
+++ b/clang/include/clang/Basic/DiagnosticParseKinds.td
@@ -842,6 +842,8 @@ def warn_cxx17_compat_explicit_bool : Warning<
   InGroup<CXXPre20Compat>, DefaultIgnore;
 def ext_explicit_bool : ExtWarn<"explicit(bool) is a C++20 extension">,
   InGroup<CXX20>;
+def err_explicit_bool_requires_cpp20
+    : Error<"explicit(bool) requires C++20 or later">;
 
 /// C++ Templates
 def err_expected_template : Error<"expected template">;
diff --git a/clang/lib/Parse/ParseDecl.cpp b/clang/lib/Parse/ParseDecl.cpp
index e47caeb855d0c..db6b979522fa6 100644
--- a/clang/lib/Parse/ParseDecl.cpp
+++ b/clang/lib/Parse/ParseDecl.cpp
@@ -4174,28 +4174,66 @@ void Parser::ParseDeclarationSpecifiers(
       ConsumedEnd = ExplicitLoc;
       ConsumeToken(); // kw_explicit
       if (Tok.is(tok::l_paren)) {
-        if (getLangOpts().CPlusPlus20 || isExplicitBool() == TPResult::True) {
-          Diag(Tok.getLocation(), getLangOpts().CPlusPlus20
-                                      ? diag::warn_cxx17_compat_explicit_bool
-                                      : diag::ext_explicit_bool);
-
-          ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
-          BalancedDelimiterTracker Tracker(*this, tok::l_paren);
-          Tracker.consumeOpen();
-
-          EnterExpressionEvaluationContext ConstantEvaluated(
-              Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
+        TPResult ExplicitBoolResult = isExplicitBool();
+        if (getLangOpts().CPlusPlus20) {
+          // C++20: Support explicit(bool) with compatibility warning
+          if (ExplicitBoolResult == TPResult::True ||
+              ExplicitBoolResult == TPResult::Ambiguous) {
+            Diag(Tok.getLocation(), diag::warn_cxx17_compat_explicit_bool);
+
+            ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
+            BalancedDelimiterTracker Tracker(*this, tok::l_paren);
+            Tracker.consumeOpen();
+
+            EnterExpressionEvaluationContext ConstantEvaluated(
+                Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
+
+            ExplicitExpr = ParseConstantExpressionInExprEvalContext();
+            ConsumedEnd = Tok.getLocation();
+            if (ExplicitExpr.isUsable()) {
+              CloseParenLoc = Tok.getLocation();
+              Tracker.consumeClose();
+              ExplicitSpec =
+                  Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
+            } else
+              Tracker.skipToEnd();
+          }
+        } else if (ExplicitBoolResult == TPResult::True) {
+          if (getLangOpts().CPlusPlus17) {
+            // C++17: Allow explicit(bool) as extension for compatibility
+            // This maintains backward compatibility with existing code that
+            // relied on this extension warning behavior
+            Diag(Tok.getLocation(), diag::ext_explicit_bool);
+
+            ExprResult ExplicitExpr(static_cast<Expr *>(nullptr));
+            BalancedDelimiterTracker Tracker(*this, tok::l_paren);
+            Tracker.consumeOpen();
+
+            EnterExpressionEvaluationContext ConstantEvaluated(
+                Actions, Sema::ExpressionEvaluationContext::ConstantEvaluated);
+
+            ExplicitExpr = ParseConstantExpressionInExprEvalContext();
+            ConsumedEnd = Tok.getLocation();
+            if (ExplicitExpr.isUsable()) {
+              CloseParenLoc = Tok.getLocation();
+              Tracker.consumeClose();
+              ExplicitSpec =
+                  Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
+            } else
+              Tracker.skipToEnd();
+          } else {
+            // C++14 and earlier: explicit(bool) causes assertion failure
+            // Emit proper error message instead of crashing
+            Diag(Tok.getLocation(), diag::err_explicit_bool_requires_cpp20);
 
-          ExplicitExpr = ParseConstantExpressionInExprEvalContext();
-          ConsumedEnd = Tok.getLocation();
-          if (ExplicitExpr.isUsable()) {
-            CloseParenLoc = Tok.getLocation();
-            Tracker.consumeClose();
-            ExplicitSpec =
-                Actions.ActOnExplicitBoolSpecifier(ExplicitExpr.get());
-          } else
+            // Error recovery: skip the parenthesized expression
+            BalancedDelimiterTracker Tracker(*this, tok::l_paren);
+            Tracker.consumeOpen();
             Tracker.skipToEnd();
-        } else {
+            ConsumedEnd = Tok.getLocation();
+          }
+        } else if (ExplicitBoolResult == TPResult::Ambiguous) {
+          // Ambiguous case: warn about potential C++20 interpretation
           Diag(Tok.getLocation(), diag::warn_cxx20_compat_explicit_bool);
         }
       }
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..c890a5c45db1d
--- /dev/null
+++ b/clang/test/Parser/explicit-bool-pre-cxx17.cpp
@@ -0,0 +1,16 @@
+// Regression test for assertion failure when explicit(bool) is used in pre-C++17
+// This test ensures no crash occurs and appropriate error messages are shown.
+// RUN: %clang_cc1 -std=c++03 -verify %s
+// RUN: %clang_cc1 -std=c++11 -verify %s
+// RUN: %clang_cc1 -std=c++14 -verify %s
+
+struct S {
+  // Before the fix, this would cause assertion failure in BuildConvertedConstantExpression
+  // Now it should produce a proper error message in C++14 and earlier modes
+  // Note: C++17 allows this as an extension for compatibility
+  explicit(true) S(int);
+  // expected-error@-1 {{explicit(bool) requires C++20 or later}}
+  
+  explicit(false) S(float);
+  // expected-error@-1 {{explicit(bool) requires C++20 or later}}
+};
\ No newline at end of file

@jongmyeong-choi
Copy link
Author

#152729 will be fixed with it.

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Er, unfortunately this is definitely not right: there’s a lot of unnecessary code duplication here, and the feature worked just fine up to and including Clang 20 (even in C++98 mode), and we definitely want to keep supporting it.

I wonder if we should just drop the assertion that causes this entirely.

@jongmyeong-choi jongmyeong-choi deleted the fix-explicit-bool-assertion branch August 11, 2025 08:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants