Skip to content

Commit

Permalink
[clang] Include the type of a pointer or reference non-type template …
Browse files Browse the repository at this point in the history
…parameter in its notion of template argument identity.

We already did this for all the other kinds of non-type template
argument.

Note: Based on earlier reverted patch from zygoloid.

Differential Revision: https://reviews.llvm.org/D136803
  • Loading branch information
zygoloid authored and mizvekov committed Oct 27, 2022
1 parent 188b041 commit 27f9b0c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -255,6 +255,9 @@ Bug Fixes
- Reject non-type template arguments formed by casting a non-zero integer
to a pointer in pre-C++17 modes, instead of treating them as null
pointers.
- Fix template arguments of pointer and reference not taking the type as
part of their identity.
`Issue 47136 <https://github.com/llvm/llvm-project/issues/47136>`_

Improvements to Clang's diagnostics
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/AST/ASTContext.cpp
Expand Up @@ -6771,7 +6771,7 @@ ASTContext::getCanonicalTemplateArgument(const TemplateArgument &Arg) const {

case TemplateArgument::Declaration: {
auto *D = cast<ValueDecl>(Arg.getAsDecl()->getCanonicalDecl());
return TemplateArgument(D, Arg.getParamTypeForDecl());
return TemplateArgument(D, getCanonicalType(Arg.getParamTypeForDecl()));
}

case TemplateArgument::NullPtr:
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/AST/TemplateBase.cpp
Expand Up @@ -363,7 +363,8 @@ bool TemplateArgument::structurallyEquals(const TemplateArgument &Other) const {
TemplateArg.NumExpansions == Other.TemplateArg.NumExpansions;

case Declaration:
return getAsDecl() == Other.getAsDecl();
return getAsDecl() == Other.getAsDecl() &&
getParamTypeForDecl() == Other.getParamTypeForDecl();

case Integral:
return getIntegralType() == Other.getIntegralType() &&
Expand Down
19 changes: 19 additions & 0 deletions clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp
Expand Up @@ -579,3 +579,22 @@ class Derived : Base<Derived>{
}
};
} // no_crash

namespace PR47792 {
using I = int;

template<decltype(auto)> int a;
const int n = 0;
const I n2 = 0;
static_assert(&a<n> == &a<0>, "both should have type 'int'");
static_assert(&a<n2> == &a<0>, "both should have type 'int'");

int m;
const int &r1 = m;
int &r2 = m;
static_assert(&a<r1> != &a<r2>, "should have different types");

const I &r3 = m;
static_assert(&a<r1> == &a<r3>, "should have different types");
static_assert(&a<r2> != &a<r3>, "should have different types");
}

0 comments on commit 27f9b0c

Please sign in to comment.