diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp index 397d8b45886c2..7021131f7ff88 100644 --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -17146,6 +17146,14 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, FoldKind).isInvalid()) Failed = true; + // If the static_assert passes, only verify that + // the message is grammatically valid without evaluating it. + if (!Failed && AssertMessage && Cond.getBoolValue()) { + std::string Str; + EvaluateStaticAssertMessageAsString(AssertMessage, Str, Context, + /*ErrorOnInvalidMessage=*/false); + } + // CWG2518 // [dcl.pre]/p10 If [...] the expression is evaluated in the context of a // template definition, the declaration has no effect. @@ -17153,7 +17161,6 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, getLangOpts().CPlusPlus && CurContext->isDependentContext(); if (!Failed && !Cond && !InTemplateDefinition) { - SmallString<256> MsgBuffer; llvm::raw_svector_ostream Msg(MsgBuffer); bool HasMessage = AssertMessage; @@ -17185,8 +17192,8 @@ Decl *Sema::BuildStaticAssertDeclaration(SourceLocation StaticAssertLoc, << InnerCond->getSourceRange(); DiagnoseStaticAssertDetails(InnerCond); } else { - Diag(StaticAssertLoc, diag::err_static_assert_failed) - << !AssertMessage << Msg.str() << AssertExpr->getSourceRange(); + Diag(AssertExpr->getBeginLoc(), diag::err_static_assert_failed) + << !HasMessage << Msg.str() << AssertExpr->getSourceRange(); PrintContextStack(); } Failed = true; diff --git a/clang/test/CXX/drs/dr25xx.cpp b/clang/test/CXX/drs/dr25xx.cpp index c4e6a545c838c..a535f65e08560 100644 --- a/clang/test/CXX/drs/dr25xx.cpp +++ b/clang/test/CXX/drs/dr25xx.cpp @@ -81,40 +81,6 @@ using ::dr2521::operator""_div; #endif } // namespace dr2521 -namespace dr2518 { // dr2518: 17 review - -template -void f(T t) { - if constexpr (sizeof(T) != sizeof(int)) { - static_assert(false, "must be int-sized"); // expected-error {{must be int-size}} - } -} - -void g(char c) { - f(0); - f(c); // expected-note {{requested here}} -} - -template -struct S { - static_assert(false); // expected-error {{static assertion failed}} -}; - -template <> -struct S {}; - -template <> -struct S {}; - -int test_specialization() { - S s1; - S s2; - S s3; // expected-note {{in instantiation of template class 'dr2518::S' requested here}} -} - -} - - namespace dr2565 { // dr2565: 16 open #if __cplusplus >= 202002L template