Index: clang/include/clang/Sema/Sema.h =================================================================== --- clang/include/clang/Sema/Sema.h +++ clang/include/clang/Sema/Sema.h @@ -7232,8 +7232,8 @@ /// at least constrained than D2, and false otherwise. /// /// \returns true if an error occurred, false otherwise. - bool IsAtLeastAsConstrained(NamedDecl *D1, ArrayRef AC1, - NamedDecl *D2, ArrayRef AC2, + bool IsAtLeastAsConstrained(NamedDecl *D1, MutableArrayRef AC1, + NamedDecl *D2, MutableArrayRef AC2, bool &Result); /// If D1 was not at least as constrained as D2, but would've been if a pair @@ -9860,7 +9860,8 @@ TemplateParameterList * SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, - const MultiLevelTemplateArgumentList &TemplateArgs); + const MultiLevelTemplateArgumentList &TemplateArgs, + bool EvaluateConstraints = true); bool SubstTemplateArguments(ArrayRef Args, Index: clang/lib/Parse/ParseTemplate.cpp =================================================================== --- clang/lib/Parse/ParseTemplate.cpp +++ clang/lib/Parse/ParseTemplate.cpp @@ -874,27 +874,39 @@ /// template parameters. /// /// type-parameter: [C++ temp.param] -/// 'template' '<' template-parameter-list '>' type-parameter-key -/// ...[opt] identifier[opt] -/// 'template' '<' template-parameter-list '>' type-parameter-key -/// identifier[opt] = id-expression +/// template-head type-parameter-key ...[opt] identifier[opt] +/// template-head type-parameter-key identifier[opt] = id-expression /// type-parameter-key: /// 'class' /// 'typename' [C++1z] -NamedDecl * -Parser::ParseTemplateTemplateParameter(unsigned Depth, unsigned Position) { +/// template-head: [C++2a] +/// 'template' '<' template-parameter-list '>' +/// requires-clause[opt] +NamedDecl *Parser::ParseTemplateTemplateParameter(unsigned Depth, + unsigned Position) { assert(Tok.is(tok::kw_template) && "Expected 'template' keyword"); // Handle the template <...> part. SourceLocation TemplateLoc = ConsumeToken(); SmallVector TemplateParams; SourceLocation LAngleLoc, RAngleLoc; + ExprResult OptionalRequiresClauseConstraintER; { MultiParseScope TemplateParmScope(*this); if (ParseTemplateParameters(TemplateParmScope, Depth + 1, TemplateParams, LAngleLoc, RAngleLoc)) { return nullptr; } + if (TryConsumeToken(tok::kw_requires)) { + OptionalRequiresClauseConstraintER = + Actions.ActOnRequiresClause(ParseConstraintLogicalOrExpression( + /*IsTrailingRequiresClause=*/false)); + if (!OptionalRequiresClauseConstraintER.isUsable()) { + SkipUntil(tok::comma, tok::greater, tok::greatergreater, + StopAtSemi | StopBeforeMatch); + return nullptr; + } + } } // Provide an ExtWarn if the C++1z feature of using 'typename' here is used. @@ -956,11 +968,9 @@ if (TryConsumeToken(tok::ellipsis, EllipsisLoc)) DiagnoseMisplacedEllipsis(EllipsisLoc, NameLoc, AlreadyHasEllipsis, true); - TemplateParameterList *ParamList = - Actions.ActOnTemplateParameterList(Depth, SourceLocation(), - TemplateLoc, LAngleLoc, - TemplateParams, - RAngleLoc, nullptr); + TemplateParameterList *ParamList = Actions.ActOnTemplateParameterList( + Depth, SourceLocation(), TemplateLoc, LAngleLoc, TemplateParams, + RAngleLoc, OptionalRequiresClauseConstraintER.get()); // Grab a default argument (if available). // Per C++0x [basic.scope.pdecl]p9, we parse the default argument before Index: clang/lib/Sema/SemaConcept.cpp =================================================================== --- clang/lib/Sema/SemaConcept.cpp +++ clang/lib/Sema/SemaConcept.cpp @@ -1279,8 +1279,10 @@ return false; } -bool Sema::IsAtLeastAsConstrained(NamedDecl *D1, ArrayRef AC1, - NamedDecl *D2, ArrayRef AC2, +bool Sema::IsAtLeastAsConstrained(NamedDecl *D1, + MutableArrayRef AC1, + NamedDecl *D2, + MutableArrayRef AC2, bool &Result) { if (AC1.empty()) { Result = AC2.empty(); @@ -1299,6 +1301,21 @@ return false; } + unsigned Depth1 = CalculateTemplateDepthForConstraints(*this, D1); + unsigned Depth2 = CalculateTemplateDepthForConstraints(*this, D2); + + for (size_t I = 0; I != AC1.size() && I != AC2.size(); ++I) { + if (Depth2 > Depth1) { + AC1[I] = AdjustConstraintDepth(*this, Depth2 - Depth1). + TransformExpr(const_cast(AC1[I])). + get(); + } else if (Depth1 > Depth2) { + AC2[I] = AdjustConstraintDepth(*this, Depth1 - Depth2). + TransformExpr(const_cast(AC2[I])). + get(); + } + } + if (subsumes(*this, D1, AC1, D2, AC2, Result, [this] (const AtomicConstraint &A, const AtomicConstraint &B) { return A.subsumes(Context, B); Index: clang/lib/Sema/SemaOverload.cpp =================================================================== --- clang/lib/Sema/SemaOverload.cpp +++ clang/lib/Sema/SemaOverload.cpp @@ -10033,13 +10033,13 @@ // parameter-type-lists, and F1 is more constrained than F2 [...], if (!Cand1IsSpecialization && !Cand2IsSpecialization && sameFunctionParameterTypeLists(S, Cand1, Cand2)) { - Expr *RC1 = Cand1.Function->getTrailingRequiresClause(); - Expr *RC2 = Cand2.Function->getTrailingRequiresClause(); + const Expr *RC1 = Cand1.Function->getTrailingRequiresClause(); + const Expr *RC2 = Cand2.Function->getTrailingRequiresClause(); if (RC1 && RC2) { bool AtLeastAsConstrained1, AtLeastAsConstrained2; - if (S.IsAtLeastAsConstrained(Cand1.Function, {RC1}, Cand2.Function, {RC2}, + if (S.IsAtLeastAsConstrained(Cand1.Function, RC1, Cand2.Function, RC2, AtLeastAsConstrained1) || - S.IsAtLeastAsConstrained(Cand2.Function, {RC2}, Cand1.Function, {RC1}, + S.IsAtLeastAsConstrained(Cand2.Function, RC2, Cand1.Function, RC1, AtLeastAsConstrained2)) return false; if (AtLeastAsConstrained1 != AtLeastAsConstrained2) Index: clang/lib/Sema/SemaTemplate.cpp =================================================================== --- clang/lib/Sema/SemaTemplate.cpp +++ clang/lib/Sema/SemaTemplate.cpp @@ -5734,7 +5734,8 @@ TemplateArgumentList TemplateArgs(TemplateArgumentList::OnStack, Converted); Params = SubstTemplateParams( Params, CurContext, - MultiLevelTemplateArgumentList(Template, TemplateArgs.asArray())); + MultiLevelTemplateArgumentList(Template, TemplateArgs.asArray()), + /*EvaluateConstraints=*/false); if (!Params) return true; } Index: clang/lib/Sema/SemaTemplateInstantiateDecl.cpp =================================================================== --- clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -4093,8 +4093,10 @@ TemplateParameterList * Sema::SubstTemplateParams(TemplateParameterList *Params, DeclContext *Owner, - const MultiLevelTemplateArgumentList &TemplateArgs) { + const MultiLevelTemplateArgumentList &TemplateArgs, + bool EvaluateConstraints) { TemplateDeclInstantiator Instantiator(*this, Owner, TemplateArgs); + Instantiator.setEvaluateConstraints(EvaluateConstraints); return Instantiator.SubstTemplateParams(Params); } Index: clang/test/CXX/temp/temp.arg/temp.arg.template/p3-2a.cpp =================================================================== --- clang/test/CXX/temp/temp.arg/temp.arg.template/p3-2a.cpp +++ clang/test/CXX/temp/temp.arg/temp.arg.template/p3-2a.cpp @@ -1,22 +1,27 @@ // RUN: %clang_cc1 -std=c++2a -frelaxed-template-template-args -verify %s -template concept C = T::f(); -// expected-note@-1{{similar constraint}} +template concept C = T::f(); // #C template concept D = C && T::g(); -template concept F = T::f(); -// expected-note@-1{{similar constraint expressions not considered equivalent}} -template class P> struct S1 { }; // expected-note 2{{'P' declared here}} +template concept F = T::f(); // #F +template class P> struct S1 { }; // #S1 template struct X { }; -template struct Y { }; // expected-note{{'Y' declared here}} +template struct Y { }; // #Y template struct Z { }; -template struct W { }; // expected-note{{'W' declared here}} - +template struct W { }; // #W S1 s11; -S1 s12; // expected-error{{template template argument 'Y' is more constrained than template template parameter 'P'}} +S1 s12; +// expected-error@-1 {{template template argument 'Y' is more constrained than template template parameter 'P'}} +// expected-note@#S1 {{'P' declared here}} +// expected-note@#Y {{'Y' declared here}} S1 s13; -S1 s14; // expected-error{{template template argument 'W' is more constrained than template template parameter 'P'}} +S1 s14; +// expected-error@-1 {{template template argument 'W' is more constrained than template template parameter 'P'}} +// expected-note@#S1 {{'P' declared here}} +// expected-note@#W {{'W' declared here}} +// expected-note@#F 1-2{{similar constraint expressions not considered equivalent}} +// expected-note@#C 1-2{{similar constraint}} template class P> struct S2 { }; @@ -32,3 +37,25 @@ using s31 = S3; using s32 = S3; + +template requires C class P> struct S4 { }; // #S4 + +S4 s41; +S4 s42; +// expected-error@-1 {{template template argument 'Y' is more constrained than template template parameter 'P'}} +// expected-note@#S4 {{'P' declared here}} +// expected-note@#Y {{'Y' declared here}} +S4 s43; +S4 s44; +// expected-error@-1 {{template template argument 'W' is more constrained than template template parameter 'P'}} +// expected-note@#S4 {{'P' declared here}} +// expected-note@#W {{'W' declared here}} + +template requires C typename U> struct S5 { + template static U V; +}; + +struct Nothing {}; + +// FIXME: Wait the standard to clarify the intent. +template<> template<> Z S5::V; Index: clang/test/SemaTemplate/concepts.cpp =================================================================== --- clang/test/SemaTemplate/concepts.cpp +++ clang/test/SemaTemplate/concepts.cpp @@ -61,9 +61,9 @@ // FIXME: This is valid under P0857R0. template concept C = true; - template requires C typename U> struct X {}; // expected-error {{requires 'class'}} expected-error 0+{{}} + template requires C typename U> struct X {}; template requires C struct Y {}; - X xy; // expected-error {{no template named 'X'}} + X xy; } namespace PR50306 {