From 440fffad7e7231fab766c6e00e47a39ad5a9b95e Mon Sep 17 00:00:00 2001 From: Younan Zhang Date: Wed, 17 Jul 2024 19:17:01 +0800 Subject: [PATCH] [Clang][Concepts] Avoid substituting into constraints for invalid TemplateDecls (#75697) Fixes https://github.com/llvm/llvm-project/issues/73885. Substituting into constraints for invalid TemplateDecls might still yield dependent expressions and end up crashing later in evaluation. --- clang/docs/ReleaseNotes.rst | 1 + clang/lib/Sema/SemaConcept.cpp | 6 ++++++ clang/test/SemaTemplate/instantiate-requires-expr.cpp | 10 ++++++++++ 3 files changed, 17 insertions(+) diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index 8c0d1635d27568..6dc45956a9afba 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -1042,6 +1042,7 @@ Bug Fixes to C++ Support - Fixed failed assertion when resolving context of defaulted comparison method outside of struct. (#GH96043). - Clang now diagnoses explicit object parameters in member pointers and other contexts where they should not appear. Fixes (#GH85992). +- Fixed a crash-on-invalid bug involving extraneous template parameter with concept substitution. (#GH73885) Bug Fixes to AST Handling ^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp index 84c5753a46ac35..9e16b67284be45 100644 --- a/clang/lib/Sema/SemaConcept.cpp +++ b/clang/lib/Sema/SemaConcept.cpp @@ -625,6 +625,12 @@ bool Sema::CheckConstraintSatisfaction( *this, nullptr, ConstraintExprs, ConvertedConstraints, TemplateArgsLists, TemplateIDRange, OutSatisfaction); } + // Invalid templates could make their way here. Substituting them could result + // in dependent expressions. + if (Template->isInvalidDecl()) { + OutSatisfaction.IsSatisfied = false; + return true; + } // A list of the template argument list flattened in a predictible manner for // the purposes of caching. The ConstraintSatisfaction type is in AST so it diff --git a/clang/test/SemaTemplate/instantiate-requires-expr.cpp b/clang/test/SemaTemplate/instantiate-requires-expr.cpp index 516708bf4c875a..20a19d731ae169 100644 --- a/clang/test/SemaTemplate/instantiate-requires-expr.cpp +++ b/clang/test/SemaTemplate/instantiate-requires-expr.cpp @@ -227,3 +227,13 @@ struct r6 {}; using r6i = r6; // expected-error@-1 {{constraints not satisfied for class template 'r6' [with T = int]}} + +namespace GH73885 { + +template // expected-error {{extraneous}} +template requires(T{}) +constexpr bool e_v = true; + +static_assert(e_v); + +} // namespace GH73885