[clang] 622f8de - PR49724: Fix deduction of null member pointers.

Richard Smith via cfe-commits cfe-commits at lists.llvm.org
Thu Mar 25 13:47:33 PDT 2021


Author: Richard Smith
Date: 2021-03-25T13:47:22-07:00
New Revision: 622f8de4f25136630007ce70915da4ef5321d080

URL: https://github.com/llvm/llvm-project/commit/622f8de4f25136630007ce70915da4ef5321d080
DIFF: https://github.com/llvm/llvm-project/commit/622f8de4f25136630007ce70915da4ef5321d080.diff

LOG: PR49724: Fix deduction of null member pointers.

Previously we created an implicit cast of the wrong kind, which we'd
later fail to constant-evaluate, resulting in deduction failure.

Added: 
    

Modified: 
    clang/lib/Sema/SemaTemplateDeduction.cpp
    clang/test/SemaTemplate/deduction.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Sema/SemaTemplateDeduction.cpp b/clang/lib/Sema/SemaTemplateDeduction.cpp
index 6336f3b99452..ecf0c442ad46 100644
--- a/clang/lib/Sema/SemaTemplateDeduction.cpp
+++ b/clang/lib/Sema/SemaTemplateDeduction.cpp
@@ -455,11 +455,13 @@ static Sema::TemplateDeductionResult DeduceNullPtrTemplateArgument(
     const NonTypeTemplateParmDecl *NTTP, QualType NullPtrType,
     TemplateDeductionInfo &Info,
     SmallVectorImpl<DeducedTemplateArgument> &Deduced) {
-  Expr *Value =
-      S.ImpCastExprToType(new (S.Context) CXXNullPtrLiteralExpr(
-                              S.Context.NullPtrTy, NTTP->getLocation()),
-                          NullPtrType, CK_NullToPointer)
-          .get();
+  Expr *Value = S.ImpCastExprToType(
+                     new (S.Context) CXXNullPtrLiteralExpr(S.Context.NullPtrTy,
+                                                           NTTP->getLocation()),
+                     NullPtrType,
+                     NullPtrType->isMemberPointerType() ? CK_NullToMemberPointer
+                                                        : CK_NullToPointer)
+                    .get();
   return DeduceNonTypeTemplateArgument(S, TemplateParams, NTTP,
                                        DeducedTemplateArgument(Value),
                                        Value->getType(), Info, Deduced);

diff  --git a/clang/test/SemaTemplate/deduction.cpp b/clang/test/SemaTemplate/deduction.cpp
index b9a1f0dccb24..0f48a4dc1095 100644
--- a/clang/test/SemaTemplate/deduction.cpp
+++ b/clang/test/SemaTemplate/deduction.cpp
@@ -604,3 +604,14 @@ namespace merge_size_only_deductions {
   int b = f(X<char [1], char [2]>(), Y<1, 2>(), X<id<int>, id<int>>());
 #endif
 }
+
+namespace PR49724 {
+  struct A;
+  template<int A::*> class X {};
+  template<int A::*P> void f(X<P>);
+  void g(X<nullptr> x) { f(x); }
+
+  template<void (A::*)()> class Y {};
+  template<void (A::*P)()> void f(Y<P>);
+  void g(Y<nullptr> y) { f(y); }
+}


        


More information about the cfe-commits mailing list