diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp --- a/clang/lib/Sema/SemaInit.cpp +++ b/clang/lib/Sema/SemaInit.cpp @@ -6176,7 +6176,11 @@ // constructors. For example, conversion function. if (const auto *RD = dyn_cast(DestType->getAs()->getDecl()); - S.getLangOpts().CPlusPlus20 && RD && RD->isAggregate() && Failed() && + // In general, we should call isCompleteType for RD to check its + // completeness, we don't call it here as it was already called in the + // above TryConstructorInitialization. + S.getLangOpts().CPlusPlus20 && RD && RD->hasDefinition() && + RD->isAggregate() && Failed() && getFailureKind() == FK_ConstructorOverloadFailed) { // C++20 [dcl.init] 17.6.2.2: // - Otherwise, if no constructor is viable, the destination type is diff --git a/clang/test/SemaCXX/recovery-expr-type.cpp b/clang/test/SemaCXX/recovery-expr-type.cpp --- a/clang/test/SemaCXX/recovery-expr-type.cpp +++ b/clang/test/SemaCXX/recovery-expr-type.cpp @@ -1,4 +1,6 @@ -// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -frecovery-ast -frecovery-ast-type -o - %s -std=gnu++17 -fsyntax-only -verify +// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -o - %s -std=gnu++17 -fsyntax-only -verify +// RUN: %clang_cc1 -triple=x86_64-unknown-unknown -o - %s -std=gnu++20 -fsyntax-only -verify + namespace test0 { struct Indestructible { @@ -171,3 +173,13 @@ S.m(1); // no crash } } + +namespace test16 { +// verify we do not crash on incomplete class type. +template struct A; // expected-note 5{{template is declared here}} +A foo() { // expected-error {{implicit instantiation of undefined template}} + if (1 == 1) + return A{1}; // expected-error 2{{implicit instantiation of undefined template}} + return A(1); // expected-error 2{{implicit instantiation of undefined template}} +} +}