]> granicus.if.org Git - clang/commitdiff
[Sema] When the address of a member function is used as a template
authorAkira Hatanaka <ahatanaka@apple.com>
Wed, 13 Jun 2018 05:26:23 +0000 (05:26 +0000)
committerAkira Hatanaka <ahatanaka@apple.com>
Wed, 13 Jun 2018 05:26:23 +0000 (05:26 +0000)
argument, use the context in which it is used for checking its
accessibility.

This fixes PR32898.

rdar://problem/33737747

Differential Revision: https://reviews.llvm.org/D36918

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@334569 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaTemplateDeduction.cpp
test/SemaCXX/access.cpp

index 87968362e03d7d9c70d22858781dbabfc93aa579..fd4197ce2adaf9effbfb516b8adc282c0b45058b 100644 (file)
@@ -3803,10 +3803,16 @@ Sema::TemplateDeductionResult Sema::DeduceTemplateArguments(
       return Result;
   }
 
+  // Capture the context in which the function call is made. This is the context
+  // that is needed when the accessibility of template arguments is checked.
+  DeclContext *CallingCtx = CurContext;
+
   return FinishTemplateArgumentDeduction(
       FunctionTemplate, Deduced, NumExplicitlySpecified, Specialization, Info,
-      &OriginalCallArgs, PartialOverloading,
-      [&]() { return CheckNonDependent(ParamTypesForArgChecking); });
+      &OriginalCallArgs, PartialOverloading, [&, CallingCtx]() {
+        ContextRAII SavedContext(*this, CallingCtx);
+        return CheckNonDependent(ParamTypesForArgChecking);
+      });
 }
 
 QualType Sema::adjustCCAndNoReturn(QualType ArgFunctionType,
index 29a58a1388acf07ed8705188277545ec93ed162d..74c5f27751b9f925b13c5c36966b7a5c67739e21 100644 (file)
@@ -169,3 +169,50 @@ namespace ThisLambdaIsNotMyFriend {
   }
   void bar() { foo<void>(); }
 }
+
+namespace OverloadedMemberFunctionPointer {
+  template<class T, void(T::*pMethod)()>
+  void func0() {}
+
+  template<class T, void(T::*pMethod)(int)>
+  void func1() {}
+
+  template<class T>
+  void func2(void(*fn)()) {} // expected-note 2 {{candidate function template not viable: no overload of 'func}}
+
+  class C {
+  private:
+    friend void friendFunc();
+    void overloadedMethod();
+  protected:
+    void overloadedMethod(int);
+  public:
+    void overloadedMethod(int, int);
+    void method() {
+      func2<int>(&func0<C, &C::overloadedMethod>);
+      func2<int>(&func1<C, &C::overloadedMethod>);
+    }
+  };
+
+  void friendFunc() {
+    func2<int>(&func0<C, &C::overloadedMethod>);
+    func2<int>(&func1<C, &C::overloadedMethod>);
+  }
+
+  void nonFriendFunc() {
+    func2<int>(&func0<C, &C::overloadedMethod>); // expected-error {{no matching function for call to 'func2'}}
+    func2<int>(&func1<C, &C::overloadedMethod>); // expected-error {{no matching function for call to 'func2'}}
+  }
+
+  // r325321 caused an assertion failure when the following code was compiled.
+  class A {
+    template <typename Type> static bool foo1() { return true; }
+
+  public:
+    void init(bool c) {
+      if (c) {
+        auto f = foo1<int>;
+      }
+    }
+  };
+}