Skip to content

[clang] Distinguish NTTPs with deduced types in variable template partial specializations #152864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Bug Fixes to C++ Support
(``[[assume(expr)]]``) creates temporary objects.
- Fix the dynamic_cast to final class optimization to correctly handle
casts that are guaranteed to fail (#GH137518).
- Fix bug rejecting partial specialization of variable templates with auto NTTPs (#GH118190).

Bug Fixes to AST Handling
^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
20 changes: 15 additions & 5 deletions clang/lib/Sema/SemaTemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4099,7 +4099,6 @@ static bool CheckTemplateSpecializationScope(Sema &S, NamedDecl *Specialized,
static TemplateSpecializationKind getTemplateSpecializationKind(Decl *D);

static bool isTemplateArgumentTemplateParameter(const TemplateArgument &Arg,
NamedDecl *Param,
unsigned Depth,
unsigned Index) {
switch (Arg.getKind()) {
Expand Down Expand Up @@ -4139,8 +4138,9 @@ static bool isTemplateArgumentTemplateParameter(const TemplateArgument &Arg,
}

static bool isSameAsPrimaryTemplate(TemplateParameterList *Params,
TemplateParameterList *SpecParams,
ArrayRef<TemplateArgument> Args) {
if (Params->size() != Args.size())
if (Params->size() != Args.size() || Params->size() != SpecParams->size())
return false;

unsigned Depth = Params->getDepth();
Expand All @@ -4157,9 +4157,19 @@ static bool isSameAsPrimaryTemplate(TemplateParameterList *Params,
Arg = Arg.pack_begin()->getPackExpansionPattern();
}

if (!isTemplateArgumentTemplateParameter(Arg, Params->getParam(I), Depth,
I))
if (!isTemplateArgumentTemplateParameter(Arg, Depth, I))
return false;

// For NTTPs further specialization is allowed via deduced types, so
// we need to make sure to only reject here if primary template and
// specialization use the same type for the NTTP.
if (auto *SpecNTTP =
dyn_cast<NonTypeTemplateParmDecl>(SpecParams->getParam(I))) {
auto *NTTP = dyn_cast<NonTypeTemplateParmDecl>(Params->getParam(I));
if (!NTTP || NTTP->getType().getCanonicalType() !=
SpecNTTP->getType().getCanonicalType())
return false;
}
}

return true;
Expand Down Expand Up @@ -4357,7 +4367,7 @@ DeclResult Sema::ActOnVarTemplateSpecialization(
}

if (isSameAsPrimaryTemplate(VarTemplate->getTemplateParameters(),
CTAI.CanonicalConverted) &&
TemplateParams, CTAI.CanonicalConverted) &&
(!Context.getLangOpts().CPlusPlus20 ||
!TemplateParams->hasAssociatedConstraints())) {
// C++ [temp.class.spec]p9b3:
Expand Down
5 changes: 5 additions & 0 deletions clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,3 +621,8 @@ namespace GH73460 {
int j;
template struct A<int&, j, j>;
} // namespace GH73460

namespace GH118190 {
template <auto> int x;
template <int i> int x<i>;
}