Here the tsubst to replace the original template parameters with the
new ones was lowering the level of the 'auto' too far, so we were
confusing it with real template parameters.

Tested x86_64-pc-linux-gnu, applying to trunk.
commit 70b578584bb8efc25ab8e2d75e7169149093989c
Author: Jason Merrill <ja...@redhat.com>
Date:   Mon Mar 19 17:16:47 2018 -0400

            PR c++/84937 - ICE with class deduction and auto.
    
            * pt.c (rewrite_template_parm): Fix auto handling.

diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c
index 21a4de57745..d7c0c7bec81 100644
--- a/gcc/cp/pt.c
+++ b/gcc/cp/pt.c
@@ -25781,8 +25781,21 @@ rewrite_template_parm (tree olddecl, unsigned index, unsigned level,
 	  = TEMPLATE_TYPE_PARM_FOR_CLASS (oldtype);
     }
   else
-    newtype = tsubst (TREE_TYPE (olddecl), tsubst_args,
-		      complain, NULL_TREE);
+    {
+      newtype = TREE_TYPE (olddecl);
+      if (type_uses_auto (newtype))
+	{
+	  // Substitute once to fix references to other template parameters.
+	  newtype = tsubst (newtype, tsubst_args,
+			    complain|tf_partial, NULL_TREE);
+	  // Now substitute again to reduce the level of the auto.
+	  newtype = tsubst (newtype, current_template_args (),
+			    complain, NULL_TREE);
+	}
+      else
+	newtype = tsubst (newtype, tsubst_args,
+			  complain, NULL_TREE);
+    }
 
   tree newdecl
     = build_decl (DECL_SOURCE_LOCATION (olddecl), TREE_CODE (olddecl),
diff --git a/gcc/testsuite/g++.dg/cpp1z/class-deduction51.C b/gcc/testsuite/g++.dg/cpp1z/class-deduction51.C
new file mode 100644
index 00000000000..eba7972c3c6
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp1z/class-deduction51.C
@@ -0,0 +1,11 @@
+// PR c++/84937
+// { dg-additional-options -std=c++17 }
+
+template<int, int> struct A {};
+
+template<int I> struct B
+{
+  template<auto J> B(A<I,J>);
+};
+
+B b(A<0,0>{});

Reply via email to