Index: clang/include/clang/Sema/Template.h =================================================================== --- clang/include/clang/Sema/Template.h +++ clang/include/clang/Sema/Template.h @@ -232,9 +232,21 @@ /// Replaces the current 'innermost' level with the provided argument list. /// This is useful for type deduction cases where we need to get the entire /// list from the AST, but then add the deduced innermost list. - void replaceInnermostTemplateArguments(ArgList Args) { - assert(TemplateArgumentLists.size() > 0 && "Replacing in an empty list?"); - TemplateArgumentLists[0].Args = Args; + void replaceInnermostTemplateArguments(Decl *AssociatedDecl, ArgList Args) { + assert((TemplateArgumentLists.size() > 0 || NumRetainedOuterLevels) && + "Replacing in an empty list?"); + + if (TemplateArgumentLists.size() > 0) { + assert((TemplateArgumentLists[0].AssociatedDeclAndFinal.getPointer() || + TemplateArgumentLists[0].AssociatedDeclAndFinal.getPointer() == + AssociatedDecl) && + "Trying to change incorrect declaration?"); + TemplateArgumentLists[0].Args = Args; + } else { + --NumRetainedOuterLevels; + TemplateArgumentLists.push_back( + {{AssociatedDecl, /*Final=*/false}, Args}); + } } /// Add an outermost level that we are not substituting. We have no Index: clang/lib/Sema/SemaTemplateDeduction.cpp =================================================================== --- clang/lib/Sema/SemaTemplateDeduction.cpp +++ clang/lib/Sema/SemaTemplateDeduction.cpp @@ -2854,7 +2854,7 @@ template <> bool DeducedArgsNeedReplacement( ClassTemplatePartialSpecializationDecl *Spec) { - return !Spec->isClassScopeExplicitSpecialization(); + return true; } template @@ -2881,7 +2881,7 @@ // not class-scope explicit specialization, so replace with Deduced Args // instead of adding to inner-most. if (NeedsReplacement) - MLTAL.replaceInnermostTemplateArguments(CanonicalDeducedArgs); + MLTAL.replaceInnermostTemplateArguments(Template, CanonicalDeducedArgs); if (S.CheckConstraintSatisfaction(Template, AssociatedConstraints, MLTAL, Info.getLocation(), Index: clang/lib/Sema/SemaTemplateInstantiate.cpp =================================================================== --- clang/lib/Sema/SemaTemplateInstantiate.cpp +++ clang/lib/Sema/SemaTemplateInstantiate.cpp @@ -131,6 +131,38 @@ return Response::Done(); } +Response HandlePartialClassTemplateSpec( + const ClassTemplatePartialSpecializationDecl *PartialClassTemplSpec, + MultiLevelTemplateArgumentList &Result, bool SkipForSpecialization) { + // We don't want the arguments from the Partial Specialization, since + // anything instantiating here cannot access the arguments from the + // specialized template anyway, so any substitution we would do with these + // partially specialized arguments would 'wrong' and confuse constraint + // instantiation. We only do this in the case of a constraint check, since + // code elsewhere actually uses these and replaces them later with what + // they mean. + // If we know this is the 'top level', we can replace this with an + // OuterRetainedLevel, else we have to generate a set of identity arguments. + + // If this is the top-level template entity, we can just add a retained level + // and be done. + if (!PartialClassTemplSpec->getTemplateDepth()) { + if (!SkipForSpecialization) + Result.addOuterRetainedLevel(); + return Response::Done(); + } + + // Else, we can replace this with an 'empty' level, and the checking will just + // alter the 'depth', since this we don't have the 'Index' for this level. + if (!SkipForSpecialization) + Result.addOuterTemplateArguments( + const_cast( + PartialClassTemplSpec), + {}, /*Final=*/false); + + return Response::UseNextDecl(PartialClassTemplSpec); +} + // Add template arguments from a class template instantiation. Response HandleClassTemplateSpec(const ClassTemplateSpecializationDecl *ClassTemplSpec, @@ -310,6 +342,10 @@ if (const auto *VarTemplSpec = dyn_cast(CurDecl)) { R = HandleVarTemplateSpec(VarTemplSpec, Result, SkipForSpecialization); + } else if (const auto *PartialClassTemplSpec = + dyn_cast(CurDecl)) { + R = HandlePartialClassTemplateSpec(PartialClassTemplSpec, Result, + SkipForSpecialization); } else if (const auto *ClassTemplSpec = dyn_cast(CurDecl)) { R = HandleClassTemplateSpec(ClassTemplSpec, Result, Index: clang/test/SemaTemplate/concepts.cpp =================================================================== --- clang/test/SemaTemplate/concepts.cpp +++ clang/test/SemaTemplate/concepts.cpp @@ -825,3 +825,53 @@ template U> friend constexpr auto decltype(L)::operator()() const; }; } // namespace TemplateInsideNonTemplateClass + +namespace GH61959 { +template +concept C = (sizeof(T0) >= 4); + +template +struct Orig { }; + +template +struct Orig { + template requires C + void f() { } + + template requires true + void f() { } +}; + +template struct Mod {}; + +template +struct Mod { + template requires C + constexpr static int f() { return 1; } + + template requires C + constexpr static int f() { return 2; } +}; + +static_assert(Mod::f() == 1); +static_assert(Mod::f() == 2); + +template +struct Outer { + template + struct Inner {}; + + template + struct Inner { + template + void foo() requires C && C && C{} + template + void foo() requires true{} + }; +}; + +void bar() { + Outer::Inner I; + I.foo(); +} +}