In this testcase, we were deducing a type for g from the function f which has not yet been deduced itself. Fixed by recognizing the case of an undeduced initializer.
The change to undeduced_auto_decl is necessary because auto parameters are a different story; they act more like normal template parameters. Tested x86_64-pc-linux-gnu, applying to trunk.
commit 28ec0bede14adc98ac9cec6693f8a4c0ccf39d14 Author: Jason Merrill <ja...@redhat.com> Date: Tue Jan 23 13:47:13 2018 -0500 PR c++/83947 - ICE with auto declarations. * pt.c (do_auto_deduction): Don't deduce from an auto decl. * decl.c (undeduced_auto_decl): Limit to vars and fns. diff --git a/gcc/cp/decl.c b/gcc/cp/decl.c index f6fab422d17..e6d9289abd2 100644 --- a/gcc/cp/decl.c +++ b/gcc/cp/decl.c @@ -16189,15 +16189,17 @@ fndecl_declared_return_type (tree fn) return TREE_TYPE (TREE_TYPE (fn)); } -/* Returns true iff DECL was declared with an auto type and it has - not yet been deduced to a real type. */ +/* Returns true iff DECL is a variable or function declared with an auto type + that has not yet been deduced to a real type. */ bool undeduced_auto_decl (tree decl) { if (cxx_dialect < cxx11) return false; - return type_uses_auto (TREE_TYPE (decl)); + return ((VAR_OR_FUNCTION_DECL_P (decl) + || TREE_CODE (decl) == TEMPLATE_DECL) + && type_uses_auto (TREE_TYPE (decl))); } /* Complain if DECL has an undeduced return type. */ diff --git a/gcc/cp/pt.c b/gcc/cp/pt.c index 0296845a31b..d5d263c1c74 100644 --- a/gcc/cp/pt.c +++ b/gcc/cp/pt.c @@ -25891,6 +25891,10 @@ do_auto_deduction (tree type, tree init, tree auto_node, from ahead of time isn't worth the trouble. */ return type; + /* Similarly, we can't deduce from another undeduced decl. */ + if (init && undeduced_auto_decl (init)) + return type; + if (tree tmpl = CLASS_PLACEHOLDER_TEMPLATE (auto_node)) /* C++17 class template argument deduction. */ return do_class_deduction (type, tmpl, init, flags, complain); diff --git a/gcc/testsuite/g++.dg/cpp1y/auto-fn46.C b/gcc/testsuite/g++.dg/cpp1y/auto-fn46.C new file mode 100644 index 00000000000..120a4dd9e7c --- /dev/null +++ b/gcc/testsuite/g++.dg/cpp1y/auto-fn46.C @@ -0,0 +1,6 @@ +// PR c++/83947 +// { dg-do compile { target c++14 } } + +auto f (); +template < int > auto g (f); // { dg-error "before deduction" } +auto h = g < 0 > ();