Author: Richard Smith Date: 2022-10-27T05:24:23+02:00 New Revision: 27f9b0c619c6e1439532434b632ea4f9b98f9f97
URL: https://github.com/llvm/llvm-project/commit/27f9b0c619c6e1439532434b632ea4f9b98f9f97 DIFF: https://github.com/llvm/llvm-project/commit/27f9b0c619c6e1439532434b632ea4f9b98f9f97.diff LOG: [clang] Include the type of a pointer or reference non-type template 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 Added: Modified: clang/docs/ReleaseNotes.rst clang/lib/AST/ASTContext.cpp clang/lib/AST/TemplateBase.cpp clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp Removed: ################################################################################ diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst index e4ae29da4848f..cb438b596c90c 100644 --- a/clang/docs/ReleaseNotes.rst +++ b/clang/docs/ReleaseNotes.rst @@ -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 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ diff --git a/clang/lib/AST/ASTContext.cpp b/clang/lib/AST/ASTContext.cpp index e8a9c7efa3964..d7f97fffd9120 100644 --- a/clang/lib/AST/ASTContext.cpp +++ b/clang/lib/AST/ASTContext.cpp @@ -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: diff --git a/clang/lib/AST/TemplateBase.cpp b/clang/lib/AST/TemplateBase.cpp index 6d9ab2b8ca718..579684c94f80f 100644 --- a/clang/lib/AST/TemplateBase.cpp +++ b/clang/lib/AST/TemplateBase.cpp @@ -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() && diff --git a/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp b/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp index 24c486cb35ad3..c872e770fd2ff 100644 --- a/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp +++ b/clang/test/SemaTemplate/temp_arg_nontype_cxx1z.cpp @@ -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 diff erent types"); + + const I &r3 = m; + static_assert(&a<r1> == &a<r3>, "should have diff erent types"); + static_assert(&a<r2> != &a<r3>, "should have diff erent types"); +} _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits