... turns out, I can avoid fiddling with in_decl (which, I realized, is meant to be used for diagnostics). The below version also passes testing.

Thanks,
Paolo.

/////////////////////
Index: cp/pt.c
===================================================================
--- cp/pt.c     (revision 211003)
+++ cp/pt.c     (working copy)
@@ -11323,7 +11323,28 @@ tsubst_function_type (tree t,
   gcc_assert (TYPE_CONTEXT (t) == NULL_TREE);
 
   /* Substitute the return type.  */
+  tree save_ccp = current_class_ptr;
+  tree save_ccr = current_class_ref;
+  bool do_inject = (!current_class_ref
+                   && TREE_CODE (t) == METHOD_TYPE
+                   && TREE_CODE (TREE_TYPE (t)) == DECLTYPE_TYPE);
+  if (do_inject)
+    {
+      /* DR 1207: 'this' is in scope in the trailing return type.  */
+      tree this_type = (current_class_type
+                       ? current_class_type
+                       : TREE_TYPE (TREE_VALUE (TYPE_ARG_TYPES (t))));
+      inject_this_parameter (this_type, type_memfn_quals (t));
+    }
+
   return_type = tsubst (TREE_TYPE (t), args, complain, in_decl);
+
+  if (do_inject)
+    {
+      current_class_ptr = save_ccp;
+      current_class_ref = save_ccr;
+    }
+
   if (return_type == error_mark_node)
     return error_mark_node;
   /* DR 486 clarifies that creation of a function type with an
Index: testsuite/g++.dg/cpp0x/decltype59.C
===================================================================
--- testsuite/g++.dg/cpp0x/decltype59.C (revision 0)
+++ testsuite/g++.dg/cpp0x/decltype59.C (working copy)
@@ -0,0 +1,41 @@
+// PR c++/57543
+// { dg-do compile { target c++11 } }
+
+template< typename > struct X
+{
+  void foo();
+  auto bar() -> decltype( X::foo() );
+};
+
+template< typename > struct Y
+{
+  void foo();
+  template< typename >
+  auto bar() -> decltype( Y::foo() );
+};
+
+template< typename > struct Z
+{
+  void foo();
+  template< typename T >
+  auto bar() -> decltype( T::foo() );
+};
+
+template< typename > struct K
+{
+  void foo();
+  template< typename T >
+  auto bar() -> decltype( T::foo() );
+};
+
+template<>
+template<>
+auto K<int>::bar<K<int>>() -> decltype( K<int>::foo() );
+
+int main()
+{
+  X<int>().bar();
+  Y<int>().bar<double>();
+  Z<int>().bar<Z<int>>();
+  K<int>().bar<K<int>>();
+}

Reply via email to