c++: ICE with deduction guide in checking type-dep [PR99009, PR97034]
authorMarek Polacek <polacek@redhat.com>
Fri, 12 Feb 2021 17:21:15 +0000 (12:21 -0500)
committerMarek Polacek <polacek@redhat.com>
Wed, 3 Mar 2021 14:52:37 +0000 (09:52 -0500)
commit1dabbfb0f4a9fbdc77e1ea4db7302586f00895e1
tree98f220954fdc781fea26a10059280c9dfe9382bd
parent15cf7fe3556d11aa895cee0f162f6678da9daca6
c++: ICE with deduction guide in checking type-dep [PR99009, PR97034]

We represent deduction guides with FUNCTION_DECLs, but they are built
without DECL_CONTEXT, leading to an ICE in type_dependent_expression_p
on the assert that the type of a function template with no dependent
(innermost!) template arguments must be non-dependent.  Consider the
attached class-deduction79.C: we create a deduction guide:

  template<class T> G(T)-> E<Z>::G<T>

we deduce T and create a partial instantiation:

  G(T) -> E<Z>::G<T> [with T = int]

And then do_class_deduction wants to create a CALL_EXPR from the above
using build_new_function_call -> build_over_call which calls mark_used
-> maybe_instantiate_noexcept -> type_dependent_expression_p.

There, the innermost template arguments are non-dependent (<int>), but
the fntype is dependent -- the return type is a TYPENAME_TYPE, and
since we have no DECL_CONTEXT, this check holds:

  /* Otherwise, if the function decl isn't from a dependent scope, it can't be
     type-dependent.  Checking this is important for functions with auto return
     type, which looks like a dependent type.  */
  if (TREE_CODE (expression) == FUNCTION_DECL
      && !(DECL_CLASS_SCOPE_P (expression)
           && dependent_type_p (DECL_CONTEXT (expression)))

whereupon we ICE.

This patch fixes it by deferring the class deduction until the
enclosing scope is non-dependent.  build_deduction_guide and maybe_aggr_guide
needed a little tweaking to make the deduction work in a member
template.

Co-Authored-By: Jason Merrill <jason@redhat.com>
gcc/cp/ChangeLog:

PR c++/97034
PR c++/99009
* pt.c (build_deduction_guide): Use INNERMOST_TEMPLATE_ARGS.
(maybe_aggr_guide): Use the original template type where needed.  In
a class member template, partially instantiate the result of
collect_ctor_idx_types.
(do_class_deduction): Defer the deduction until the enclosing
scope is non-dependent.

gcc/testsuite/ChangeLog:

PR c++/97034
PR c++/99009
* g++.dg/cpp1z/class-deduction81.C: New test.
* g++.dg/cpp1z/class-deduction82.C: New test.
* g++.dg/cpp2a/class-deduction-aggr8.C: New test.
* g++.dg/cpp2a/class-deduction-aggr9.C: New test.
* g++.dg/cpp2a/class-deduction-aggr10.C: New test.
gcc/cp/pt.c
gcc/testsuite/g++.dg/cpp1z/class-deduction81.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp1z/class-deduction82.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr10.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr8.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/class-deduction-aggr9.C [new file with mode: 0644]