diff mbox

C++ PATCH for c++/64989 (return type deduction with abbreviated function template)

Message ID 55B33291.80605@redhat.com
State New
Headers show

Commit Message

Jason Merrill July 25, 2015, 6:54 a.m. UTC
In this testcase, the problem is that when we make_auto for the auto 
return type, we don't know yet that we're dealing with a function 
template, so the auto return type ends up with the same template 
parameter level as the implicit template parameters.

This patch fixes this by generating a new auto at the correct level in 
splice_late_return_type, after we know that we're in a template.  While 
I was messing with that function, I also tore out all the code for 
dealing with return types more complicated than plain 'auto', since the 
committee decided not to allow that.

Tested x86_64-pc-linux-gnu, applying to trunk.
diff mbox

Patch

commit ee4c93dbe9a300bb518f26dbd845de1ec8d1e1f2
Author: Jason Merrill <jason@redhat.com>
Date:   Fri Jul 24 22:58:39 2015 -0700

    	PR c++/64989
    	* pt.c (splice_late_return_type): Correct deduced return type for
    	abbreviated function template.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 5004883..971c98e 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -22459,15 +22459,19 @@  do_auto_deduction (tree type, tree init, tree auto_node)
 tree
 splice_late_return_type (tree type, tree late_return_type)
 {
-  tree argvec;
+  if (is_auto (type))
+    {
+      if (late_return_type)
+	return late_return_type;
 
-  if (late_return_type == NULL_TREE)
-    return type;
-  argvec = make_tree_vec (1);
-  TREE_VEC_ELT (argvec, 0) = late_return_type;
-  if (current_template_parms)
-    argvec = add_to_template_args (current_template_args (), argvec);
-  return tsubst (type, argvec, tf_warning_or_error, NULL_TREE);
+      tree idx = get_template_parm_index (type);
+      if (TEMPLATE_PARM_LEVEL (idx) <= processing_template_decl)
+	/* In an abbreviated function template we didn't know we were dealing
+	   with a function template when we saw the auto return type, so update
+	   it to have the correct level.  */
+	return make_auto_1 (TYPE_IDENTIFIER (type));
+    }
+  return type;
 }
 
 /* Returns true iff TYPE is a TEMPLATE_TYPE_PARM representing 'auto' or
diff --git a/gcc/testsuite/g++.dg/cpp1z/abbrev1.C b/gcc/testsuite/g++.dg/cpp1z/abbrev1.C
new file mode 100644
index 0000000..68a0bf3
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/abbrev1.C
@@ -0,0 +1,11 @@ 
+// PR c++/64969
+// { dg-options "-std=c++1z" }
+
+auto f1(auto x) { return *x; }
+decltype(auto) f2(auto x) { return *x; }
+auto f3(auto x) -> int { return *x; }
+
+int i;
+auto r1 = f1(&i);
+auto r2 = f2(&i);
+auto r3 = f3(&i);