From a88ebd405da67b4cebf094c5a56f9aed97875423 Mon Sep 17 00:00:00 2001 From: Matheus Izvekov Date: Wed, 26 Oct 2022 10:14:27 +0200 Subject: [PATCH] Revert "[clang] Instantiate NTTPs and template default arguments with sugar" This reverts commit 2560c1266993af6e6c15900ce673c6db23132f8b. --- clang/include/clang/Sema/Sema.h | 13 ++- clang/lib/Sema/SemaTemplate.cpp | 120 ++++++++++++--------- clang/lib/Sema/SemaTemplateDeduction.cpp | 21 ++-- clang/test/AST/ast-dump-template-decls.cpp | 8 +- clang/test/CXX/drs/dr3xx.cpp | 4 +- clang/test/CXX/expr/expr.const/p3-0x.cpp | 2 +- clang/test/Misc/diag-template-diffing.cpp | 2 +- .../test/SemaTemplate/instantiation-default-1.cpp | 2 +- clang/test/SemaTemplate/make_integer_seq.cpp | 8 +- 9 files changed, 100 insertions(+), 80 deletions(-) diff --git a/clang/include/clang/Sema/Sema.h b/clang/include/clang/Sema/Sema.h index 37ad78f..d0757f8 100644 --- a/clang/include/clang/Sema/Sema.h +++ b/clang/include/clang/Sema/Sema.h @@ -8199,11 +8199,14 @@ public: SourceLocation TemplateLoc, Declarator &D); - TemplateArgumentLoc SubstDefaultTemplateArgumentIfAvailable( - TemplateDecl *Template, SourceLocation TemplateLoc, - SourceLocation RAngleLoc, Decl *Param, - ArrayRef SugaredConverted, - ArrayRef CanonicalConverted, bool &HasDefaultArg); + TemplateArgumentLoc + SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, + SourceLocation TemplateLoc, + SourceLocation RAngleLoc, + Decl *Param, + SmallVectorImpl + &Converted, + bool &HasDefaultArg); /// Specifies the context in which a particular template /// argument is being checked. diff --git a/clang/lib/Sema/SemaTemplate.cpp b/clang/lib/Sema/SemaTemplate.cpp index 37eaa2a..73c6810 100644 --- a/clang/lib/Sema/SemaTemplate.cpp +++ b/clang/lib/Sema/SemaTemplate.cpp @@ -5261,25 +5261,27 @@ bool Sema::CheckTemplateTypeArgument( /// \param Converted the list of template arguments provided for template /// parameters that precede \p Param in the template parameter list. /// \returns the substituted template argument, or NULL if an error occurred. -static TypeSourceInfo *SubstDefaultTemplateArgument( - Sema &SemaRef, TemplateDecl *Template, SourceLocation TemplateLoc, - SourceLocation RAngleLoc, TemplateTypeParmDecl *Param, - ArrayRef SugaredConverted, - ArrayRef CanonicalConverted) { +static TypeSourceInfo * +SubstDefaultTemplateArgument(Sema &SemaRef, + TemplateDecl *Template, + SourceLocation TemplateLoc, + SourceLocation RAngleLoc, + TemplateTypeParmDecl *Param, + SmallVectorImpl &Converted) { TypeSourceInfo *ArgType = Param->getDefaultArgumentInfo(); // If the argument type is dependent, instantiate it now based // on the previously-computed template arguments. if (ArgType->getType()->isInstantiationDependentType()) { - Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Param, Template, - SugaredConverted, + Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, + Param, Template, Converted, SourceRange(TemplateLoc, RAngleLoc)); if (Inst.isInvalid()) return nullptr; // Only substitute for the innermost template argument list. - MultiLevelTemplateArgumentList TemplateArgLists(Template, SugaredConverted, - /*Final=*/true); + MultiLevelTemplateArgumentList TemplateArgLists(Template, Converted, + /*Final=*/false); for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) TemplateArgLists.addOuterTemplateArguments(None); @@ -5318,20 +5320,22 @@ static TypeSourceInfo *SubstDefaultTemplateArgument( /// parameters that precede \p Param in the template parameter list. /// /// \returns the substituted template argument, or NULL if an error occurred. -static ExprResult SubstDefaultTemplateArgument( - Sema &SemaRef, TemplateDecl *Template, SourceLocation TemplateLoc, - SourceLocation RAngleLoc, NonTypeTemplateParmDecl *Param, - ArrayRef SugaredConverted, - ArrayRef CanonicalConverted) { - Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, Param, Template, - SugaredConverted, +static ExprResult +SubstDefaultTemplateArgument(Sema &SemaRef, + TemplateDecl *Template, + SourceLocation TemplateLoc, + SourceLocation RAngleLoc, + NonTypeTemplateParmDecl *Param, + SmallVectorImpl &Converted) { + Sema::InstantiatingTemplate Inst(SemaRef, TemplateLoc, + Param, Template, Converted, SourceRange(TemplateLoc, RAngleLoc)); if (Inst.isInvalid()) return ExprError(); // Only substitute for the innermost template argument list. - MultiLevelTemplateArgumentList TemplateArgLists(Template, SugaredConverted, - /*Final=*/true); + MultiLevelTemplateArgumentList TemplateArgLists(Template, Converted, + /*Final=*/false); for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) TemplateArgLists.addOuterTemplateArguments(None); @@ -5366,21 +5370,23 @@ static ExprResult SubstDefaultTemplateArgument( /// source-location information) that precedes the template name. /// /// \returns the substituted template argument, or NULL if an error occurred. -static TemplateName SubstDefaultTemplateArgument( - Sema &SemaRef, TemplateDecl *Template, SourceLocation TemplateLoc, - SourceLocation RAngleLoc, TemplateTemplateParmDecl *Param, - ArrayRef SugaredConverted, - ArrayRef CanonicalConverted, - NestedNameSpecifierLoc &QualifierLoc) { +static TemplateName +SubstDefaultTemplateArgument(Sema &SemaRef, + TemplateDecl *Template, + SourceLocation TemplateLoc, + SourceLocation RAngleLoc, + TemplateTemplateParmDecl *Param, + SmallVectorImpl &Converted, + NestedNameSpecifierLoc &QualifierLoc) { Sema::InstantiatingTemplate Inst( - SemaRef, TemplateLoc, TemplateParameter(Param), Template, - SugaredConverted, SourceRange(TemplateLoc, RAngleLoc)); + SemaRef, TemplateLoc, TemplateParameter(Param), Template, Converted, + SourceRange(TemplateLoc, RAngleLoc)); if (Inst.isInvalid()) return TemplateName(); // Only substitute for the innermost template argument list. - MultiLevelTemplateArgumentList TemplateArgLists(Template, SugaredConverted, - /*Final=*/true); + MultiLevelTemplateArgumentList TemplateArgLists(Template, Converted, + /*Final=*/false); for (unsigned i = 0, e = Param->getDepth(); i != e; ++i) TemplateArgLists.addOuterTemplateArguments(None); @@ -5404,11 +5410,14 @@ static TemplateName SubstDefaultTemplateArgument( /// If the given template parameter has a default template /// argument, substitute into that default template argument and /// return the corresponding template argument. -TemplateArgumentLoc Sema::SubstDefaultTemplateArgumentIfAvailable( - TemplateDecl *Template, SourceLocation TemplateLoc, - SourceLocation RAngleLoc, Decl *Param, - ArrayRef SugaredConverted, - ArrayRef CanonicalConverted, bool &HasDefaultArg) { +TemplateArgumentLoc +Sema::SubstDefaultTemplateArgumentIfAvailable(TemplateDecl *Template, + SourceLocation TemplateLoc, + SourceLocation RAngleLoc, + Decl *Param, + SmallVectorImpl + &Converted, + bool &HasDefaultArg) { HasDefaultArg = false; if (TemplateTypeParmDecl *TypeParm = dyn_cast(Param)) { @@ -5416,9 +5425,11 @@ TemplateArgumentLoc Sema::SubstDefaultTemplateArgumentIfAvailable( return TemplateArgumentLoc(); HasDefaultArg = true; - TypeSourceInfo *DI = SubstDefaultTemplateArgument( - *this, Template, TemplateLoc, RAngleLoc, TypeParm, SugaredConverted, - CanonicalConverted); + TypeSourceInfo *DI = SubstDefaultTemplateArgument(*this, Template, + TemplateLoc, + RAngleLoc, + TypeParm, + Converted); if (DI) return TemplateArgumentLoc(TemplateArgument(DI->getType()), DI); @@ -5431,9 +5442,11 @@ TemplateArgumentLoc Sema::SubstDefaultTemplateArgumentIfAvailable( return TemplateArgumentLoc(); HasDefaultArg = true; - ExprResult Arg = SubstDefaultTemplateArgument( - *this, Template, TemplateLoc, RAngleLoc, NonTypeParm, SugaredConverted, - CanonicalConverted); + ExprResult Arg = SubstDefaultTemplateArgument(*this, Template, + TemplateLoc, + RAngleLoc, + NonTypeParm, + Converted); if (Arg.isInvalid()) return TemplateArgumentLoc(); @@ -5448,9 +5461,12 @@ TemplateArgumentLoc Sema::SubstDefaultTemplateArgumentIfAvailable( HasDefaultArg = true; NestedNameSpecifierLoc QualifierLoc; - TemplateName TName = SubstDefaultTemplateArgument( - *this, Template, TemplateLoc, RAngleLoc, TempTempParm, SugaredConverted, - CanonicalConverted, QualifierLoc); + TemplateName TName = SubstDefaultTemplateArgument(*this, Template, + TemplateLoc, + RAngleLoc, + TempTempParm, + Converted, + QualifierLoc); if (TName.isNull()) return TemplateArgumentLoc(); @@ -5546,13 +5562,13 @@ bool Sema::CheckTemplateArgument( !Template->getDeclContext()->isDependentContext()) { // Do substitution on the type of the non-type template parameter. InstantiatingTemplate Inst(*this, TemplateLoc, Template, NTTP, - SugaredConverted, + CanonicalConverted, SourceRange(TemplateLoc, RAngleLoc)); if (Inst.isInvalid()) return true; - MultiLevelTemplateArgumentList MLTAL(Template, SugaredConverted, - /*Final=*/true); + MultiLevelTemplateArgumentList MLTAL(Template, CanonicalConverted, + /*Final=*/false); // If the parameter is a pack expansion, expand this slice of the pack. if (auto *PET = NTTPType->getAs()) { Sema::ArgumentPackSubstitutionIndexRAII SubstIndex(*this, @@ -5717,7 +5733,7 @@ bool Sema::CheckTemplateArgument( Params = SubstTemplateParams(Params, CurContext, MultiLevelTemplateArgumentList( - Template, SugaredConverted, /*Final=*/true)); + Template, CanonicalConverted, /*Final=*/false)); if (!Params) return true; } @@ -5996,8 +6012,7 @@ bool Sema::CheckTemplateArgumentList( NewArgs); TypeSourceInfo *ArgType = SubstDefaultTemplateArgument( - *this, Template, TemplateLoc, RAngleLoc, TTP, SugaredConverted, - CanonicalConverted); + *this, Template, TemplateLoc, RAngleLoc, TTP, CanonicalConverted); if (!ArgType) return true; @@ -6010,8 +6025,7 @@ bool Sema::CheckTemplateArgumentList( NewArgs); ExprResult E = SubstDefaultTemplateArgument( - *this, Template, TemplateLoc, RAngleLoc, NTTP, SugaredConverted, - CanonicalConverted); + *this, Template, TemplateLoc, RAngleLoc, NTTP, CanonicalConverted); if (E.isInvalid()) return true; @@ -6027,8 +6041,8 @@ bool Sema::CheckTemplateArgumentList( NestedNameSpecifierLoc QualifierLoc; TemplateName Name = SubstDefaultTemplateArgument( - *this, Template, TemplateLoc, RAngleLoc, TempParm, SugaredConverted, - CanonicalConverted, QualifierLoc); + *this, Template, TemplateLoc, RAngleLoc, TempParm, CanonicalConverted, + QualifierLoc); if (Name.isNull()) return true; @@ -6042,7 +6056,7 @@ bool Sema::CheckTemplateArgumentList( // template here, we just create this object to put a note into the // context stack. InstantiatingTemplate Inst(*this, RAngleLoc, Template, *Param, - SugaredConverted, + CanonicalConverted, SourceRange(TemplateLoc, RAngleLoc)); if (Inst.isInvalid()) return true; diff --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp index e078d3d..19f76e5 100644 --- a/clang/lib/Sema/SemaTemplateDeduction.cpp +++ b/clang/lib/Sema/SemaTemplateDeduction.cpp @@ -2687,12 +2687,12 @@ static bool ConvertDeducedTemplateArgument( // itself, in case that substitution fails. if (SugaredPackedArgsBuilder.empty()) { LocalInstantiationScope Scope(S); - MultiLevelTemplateArgumentList Args(Template, SugaredOutput, - /*Final=*/true); + MultiLevelTemplateArgumentList Args(Template, CanonicalOutput, + /*Final=*/false); if (auto *NTTP = dyn_cast(Param)) { Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, - NTTP, SugaredOutput, + NTTP, CanonicalOutput, Template->getSourceRange()); if (Inst.isInvalid() || S.SubstType(NTTP->getType(), Args, NTTP->getLocation(), @@ -2700,7 +2700,7 @@ static bool ConvertDeducedTemplateArgument( return true; } else if (auto *TTP = dyn_cast(Param)) { Sema::InstantiatingTemplate Inst(S, Template->getLocation(), Template, - TTP, SugaredOutput, + TTP, CanonicalOutput, Template->getSourceRange()); if (Inst.isInvalid() || !S.SubstDecl(TTP, S.CurContext, Args)) return true; @@ -2810,7 +2810,7 @@ static Sema::TemplateDeductionResult ConvertDeducedTemplateArguments( DefArg = S.SubstDefaultTemplateArgumentIfAvailable( TD, TD->getLocation(), TD->getSourceRange().getEnd(), Param, - SugaredBuilder, CanonicalBuilder, HasDefaultArg); + CanonicalBuilder, HasDefaultArg); } // If there was no default argument, deduction is incomplete. @@ -2960,9 +2960,10 @@ FinishTemplateArgumentDeduction( PartialTemplArgInfo->RAngleLoc); if (S.SubstTemplateArguments(PartialTemplArgInfo->arguments(), - MultiLevelTemplateArgumentList(Partial, - SugaredBuilder, - /*Final=*/true), + MultiLevelTemplateArgumentList( + Partial, + CanonicalDeducedArgumentList->asArray(), + /*Final=*/false), InstArgs)) { unsigned ArgIdx = InstArgs.size(), ParamIdx = ArgIdx; if (ParamIdx >= Partial->getTemplateParameters()->size()) @@ -3302,8 +3303,8 @@ Sema::TemplateDeductionResult Sema::SubstituteExplicitTemplateArguments( ExtParameterInfoBuilder ExtParamInfos; MultiLevelTemplateArgumentList MLTAL(FunctionTemplate, - SugaredExplicitArgumentList->asArray(), - /*Final=*/true); + CanonicalExplicitArgumentList->asArray(), + /*Final=*/false); // Instantiate the types of each of the function parameters given the // explicitly-specified template arguments. If the function has a trailing diff --git a/clang/test/AST/ast-dump-template-decls.cpp b/clang/test/AST/ast-dump-template-decls.cpp index 49760d1..6b71df3 100644 --- a/clang/test/AST/ast-dump-template-decls.cpp +++ b/clang/test/AST/ast-dump-template-decls.cpp @@ -172,9 +172,11 @@ using test1 = D; // CHECK: TypeAliasDecl 0x{{[^ ]*}} col:7 test1 'D':'subst_default_argument::E>' // CHECK: TemplateSpecializationType 0x{{[^ ]*}} 'A' sugar A // CHECK-NEXT: |-TemplateArgument type 'int':'int' -// CHECK-NEXT: | `-SubstTemplateTypeParmType 0x{{[^ ]*}} 'int' sugar class depth 0 index 1 D2 -// CHECK-NEXT: | |-TypeAliasTemplate 0x{{[^ ]*}} 'D' -// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'int' +// CHECK-NEXT: | `-SubstTemplateTypeParmType 0x{{[^ ]*}} 'int' sugar class depth 0 index 0 E1 +// CHECK-NEXT: | |-ClassTemplate 0x{{[^ ]*}} 'E' +// CHECK-NEXT: | `-SubstTemplateTypeParmType 0x{{[^ ]*}} 'int' sugar class depth 0 index 1 D2 +// CHECK-NEXT: | |-TypeAliasTemplate 0x{{[^ ]*}} 'D' +// CHECK-NEXT: | `-BuiltinType 0x{{[^ ]*}} 'int' // CHECK-NEXT: `-RecordType 0x{{[^ ]*}} 'subst_default_argument::A' // CHECK-NEXT: `-ClassTemplateSpecialization 0x{{[^ ]*}} 'A' } // namespace subst_default_argument diff --git a/clang/test/CXX/drs/dr3xx.cpp b/clang/test/CXX/drs/dr3xx.cpp index 4ae56fc..e6f7328 100644 --- a/clang/test/CXX/drs/dr3xx.cpp +++ b/clang/test/CXX/drs/dr3xx.cpp @@ -924,9 +924,9 @@ namespace dr367 { // dr367: yes namespace dr368 { // dr368: yes template struct S {}; // expected-note {{here}} template int f(S *); // expected-error {{function type}} - template int g(S *); // cxx98_17-note {{type 'X'}} + template int g(S *); // cxx98_17-note {{type 'dr368::X'}} // cxx20_2b-note@-1 {{candidate function [with T = dr368::X]}} - template int g(S *); // cxx98_17-note {{type 'X'}} + template int g(S *); // cxx98_17-note {{type 'dr368::X'}} // cxx20_2b-note@-1 {{candidate function [with T = dr368::X]}} struct X {}; int n = g(0); // cxx98_17-error {{no matching}} diff --git a/clang/test/CXX/expr/expr.const/p3-0x.cpp b/clang/test/CXX/expr/expr.const/p3-0x.cpp index 47968ee..8daca7a 100644 --- a/clang/test/CXX/expr/expr.const/p3-0x.cpp +++ b/clang/test/CXX/expr/expr.const/p3-0x.cpp @@ -88,7 +88,7 @@ void noexcept_true() noexcept(true); Val remove_noexcept; Val add_noexcept; #if __cplusplus > 201402L -// expected-error@-2 {{value of type 'void (*)() noexcept(false)' is not implicitly convertible to 'decltype(&noexcept_true)' (aka 'void (*)() noexcept(true)')}} +// expected-error@-2 {{value of type 'void (*)() noexcept(false)' is not implicitly convertible to 'void (*)() noexcept'}} #endif // (no other conversions are permitted) diff --git a/clang/test/Misc/diag-template-diffing.cpp b/clang/test/Misc/diag-template-diffing.cpp index dffd571..087cdd7 100644 --- a/clang/test/Misc/diag-template-diffing.cpp +++ b/clang/test/Misc/diag-template-diffing.cpp @@ -1454,7 +1454,7 @@ void run() { D(VectorType()); } // CHECK-ELIDE-NOTREE: error: no matching function for call to 'D' -// CHECK-ELIDE-NOTREE: note: candidate function template not viable: no known conversion from 'VectorType' to 'const VectorType<(X)0>' for 1st argument +// CHECK-ELIDE-NOTREE: note: candidate function template not viable: no known conversion from 'VectorType' to 'const VectorType<(TypeAlias::X)0>' for 1st argument } namespace TypeAlias2 { diff --git a/clang/test/SemaTemplate/instantiation-default-1.cpp b/clang/test/SemaTemplate/instantiation-default-1.cpp index 3e70a21..33e01b6 100644 --- a/clang/test/SemaTemplate/instantiation-default-1.cpp +++ b/clang/test/SemaTemplate/instantiation-default-1.cpp @@ -33,7 +33,7 @@ void test_Def2(Def2 *d2) { } typedef int& int_ref_t; -Def2 *d2; // expected-note{{in instantiation of default argument for 'Def2' required here}} +Def2 *d2; // expected-note{{in instantiation of default argument for 'Def2' required here}} template<> struct Def1 { }; // expected-error{{redefinition of 'Def1'}} diff --git a/clang/test/SemaTemplate/make_integer_seq.cpp b/clang/test/SemaTemplate/make_integer_seq.cpp index 12505b0..b51eed5 100644 --- a/clang/test/SemaTemplate/make_integer_seq.cpp +++ b/clang/test/SemaTemplate/make_integer_seq.cpp @@ -69,14 +69,14 @@ template