https://github.com/firstmoonlight created https://github.com/llvm/llvm-project/pull/191972
When the type in a CXXTypeConstructExpr is incomplete, create a RecoveryExpr with an empty QualType to avoid triggering assertion in LookupTemplateName. Fixes https://github.com/llvm/llvm-project/issues/186579 >From cfd43c4cc781cec16c7b25a1e67665959302a331 Mon Sep 17 00:00:00 2001 From: victorl <[email protected]> Date: Tue, 14 Apr 2026 14:31:01 +0800 Subject: [PATCH] [clang][Sema] fix crash on invalid expression with trailing return type and template keyword --- clang/lib/Sema/SemaExprCXX.cpp | 3 ++- clang/test/Modules/cxx20-10-2-ex5.cpp | 3 +-- clang/test/SemaCXX/recovery-expr-type.cpp | 17 ++++++++++++----- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/clang/lib/Sema/SemaExprCXX.cpp b/clang/lib/Sema/SemaExprCXX.cpp index f7e005a40363c..8de0d6c026ef4 100644 --- a/clang/lib/Sema/SemaExprCXX.cpp +++ b/clang/lib/Sema/SemaExprCXX.cpp @@ -1514,7 +1514,8 @@ Sema::ActOnCXXTypeConstructExpr(ParsedType TypeRep, RParenOrBraceLoc, ListInitialization); if (Result.isInvalid()) Result = CreateRecoveryExpr(TInfo->getTypeLoc().getBeginLoc(), - RParenOrBraceLoc, exprs, Ty); + RParenOrBraceLoc, exprs, Ty->isIncompleteType() ? QualType() : Ty); + return Result; } diff --git a/clang/test/Modules/cxx20-10-2-ex5.cpp b/clang/test/Modules/cxx20-10-2-ex5.cpp index f222568072393..b14234d44e3be 100644 --- a/clang/test/Modules/cxx20-10-2-ex5.cpp +++ b/clang/test/Modules/cxx20-10-2-ex5.cpp @@ -61,6 +61,5 @@ int main() { auto f = rootFinder(2); // OK // error: A is incomplete return A{45}.value; // expected-error {{invalid use of incomplete type 'A'}} - // expected-error@-1 {{member access into incomplete type 'A'}} - // [email protected]:12 2{{forward declaration of 'A'}} + // [email protected]:12 {{forward declaration of 'A'}} } diff --git a/clang/test/SemaCXX/recovery-expr-type.cpp b/clang/test/SemaCXX/recovery-expr-type.cpp index 5a42a11b82da5..fd38f453afc27 100644 --- a/clang/test/SemaCXX/recovery-expr-type.cpp +++ b/clang/test/SemaCXX/recovery-expr-type.cpp @@ -104,8 +104,7 @@ void test() { // verify the secondary diagnostic "cannot initialize" is emitted. namespace test8 { typedef int arr[]; -int v = arr(); // expected-error {{array types cannot be value-initialized}} \ - expected-error {{cannot initialize a variable of type 'int' with an rvalue of type 'arr'}} +int v = arr(); // expected-error {{array types cannot be value-initialized}} } namespace test9 { @@ -178,10 +177,18 @@ void f() { namespace test16 { // verify we do not crash on incomplete class type. -template<typename T, typename U> struct A; // expected-note 5{{template is declared here}} +template<typename T, typename U> struct A; // expected-note 3{{template is declared here}} A<int, int> foo() { // expected-error {{implicit instantiation of undefined template}} if (1 == 1) - return A<int, int>{1}; // expected-error 2{{implicit instantiation of undefined template}} - return A<int, int>(1); // expected-error 2{{implicit instantiation of undefined template}} + return A<int, int>{1}; // expected-error {{implicit instantiation of undefined template}} + return A<int, int>(1); // expected-error {{implicit instantiation of undefined template}} } } + +namespace test17 { +// Verify we do not crash on trailing expression with template keyword. +int a((enum b )b { } // expected-error {{ISO C++ forbids forward references to 'enum' types}} \ + // expected-error {{invalid use of incomplete type}} \ + // expected-note {{forward declaration of}} + -> template c); // expected-error {{a template argument list is expected after a name prefixed by the template keyword}} +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
