diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -7781,7 +7781,7 @@ void CheckConversionDeclarator(Declarator &D, QualType &R, StorageClass& SC); Decl *ActOnConversionDeclarator(CXXConversionDecl *Conversion); - void CheckDeductionGuideDeclarator(Declarator &D, QualType &R, + bool CheckDeductionGuideDeclarator(Declarator &D, QualType &R, StorageClass &SC); void CheckDeductionGuideTemplate(FunctionTemplateDecl *TD); diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -9199,11 +9199,13 @@ SemaRef.Diag(TrailingRequiresClause->getBeginLoc(), diag::err_trailing_requires_clause_on_deduction_guide) << TrailingRequiresClause->getSourceRange(); - SemaRef.CheckDeductionGuideDeclarator(D, R, SC); + bool IsValid = SemaRef.CheckDeductionGuideDeclarator(D, R, SC); - return CXXDeductionGuideDecl::Create(SemaRef.Context, DC, D.getBeginLoc(), + CXXDeductionGuideDecl* DGD = CXXDeductionGuideDecl::Create(SemaRef.Context, DC, D.getBeginLoc(), ExplicitSpecifier, NameInfo, R, TInfo, D.getEndLoc()); + DGD->setInvalidDecl(!IsValid); + return DGD; } else if (DC->isRecord()) { // If the name of the function is the same as the name of the record, // then this must be an invalid constructor that has a return type. diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp --- a/clang/lib/Sema/SemaDeclCXX.cpp +++ b/clang/lib/Sema/SemaDeclCXX.cpp @@ -11074,8 +11074,9 @@ /// These aren't actually declarators in the grammar, so we need to check that /// the user didn't specify any pieces that are not part of the deduction-guide /// grammar. -void Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R, +bool Sema::CheckDeductionGuideDeclarator(Declarator &D, QualType &R, StorageClass &SC) { + bool IsValid = true; TemplateName GuidedTemplate = D.getName().TemplateName.get().get(); TemplateDecl *GuidedTemplateDecl = GuidedTemplate.getAsTemplateDecl(); assert(GuidedTemplateDecl && "missing template decl for deduction guide"); @@ -11124,7 +11125,7 @@ } if (D.isInvalidType()) - return; + return IsValid; // Check the declarator is simple enough. bool FoundFunction = false; @@ -11138,6 +11139,7 @@ break; } if (!Chunk.Fun.hasTrailingReturnType()) { + IsValid = false; Diag(D.getName().getBeginLoc(), diag::err_deduction_guide_no_trailing_return_type); break; @@ -11177,6 +11179,7 @@ } if (!AcceptableReturnType) { + IsValid = false; Diag(TSI->getTypeLoc().getBeginLoc(), diag::err_deduction_guide_bad_trailing_return_type) << GuidedTemplate << TSI->getType() @@ -11191,6 +11194,7 @@ if (D.isFunctionDefinition()) Diag(D.getIdentifierLoc(), diag::err_deduction_guide_defines_function); + return IsValid; } //===----------------------------------------------------------------------===// diff --git a/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp b/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp --- a/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp +++ b/clang/test/CXX/expr/expr.prim/expr.prim.req/nested-requirement.cpp @@ -173,10 +173,10 @@ concept g = f::h; template concept i = g; -template class j { // expected-note {{candidate template ignored}} +template class j { template requires requires { requires i; } - j(); // expected-note {{candidate template ignored}} + j(); }; -template <> j(); // expected-error {{deduction guide declaration without trailing return type}} // expected-error {{no function template}} +template <> j(); // expected-error {{deduction guide declaration without trailing return type}} } diff --git a/clang/test/Parser/cxx1z-class-template-argument-deduction.cpp b/clang/test/Parser/cxx1z-class-template-argument-deduction.cpp --- a/clang/test/Parser/cxx1z-class-template-argument-deduction.cpp +++ b/clang/test/Parser/cxx1z-class-template-argument-deduction.cpp @@ -241,9 +241,8 @@ }; struct A2 { - template // expected-note {{non-deducible template parameter 'Ty'}} + template B() noexcept(false); // expected-error {{deduction guide must be declared in the same scope as template 'PR49735::B'}} \ - // expected-error {{deduction guide template contains a template parameter that cannot be deduced}} \ // expected-error {{deduction guide declaration without trailing return type}} }; diff --git a/clang/test/SemaCXX/invalid-deduction-guide-as-template-candidates.cpp b/clang/test/SemaCXX/invalid-deduction-guide-as-template-candidates.cpp new file mode 100644 --- /dev/null +++ b/clang/test/SemaCXX/invalid-deduction-guide-as-template-candidates.cpp @@ -0,0 +1,6 @@ +// RUN: %clang_cc1 -fsyntax-only -verify %s + +template class Foo {}; // expected-note {{candidate template ignored: couldn't infer template argument 'T'}} \ + // expected-note {{candidate function template not viable: requires 1 argument, but 0 were provided}} +Foo(); // expected-error {{deduction guide declaration without trailing return type}} +Foo vs; // expected-error {{no viable constructor or deduction guide for deduction of template arguments of 'Foo'}}