https://gcc.gnu.org/bugzilla/show_bug.cgi?id=128974

            Bug ID: 128974
           Summary: ICE in tsubst (gcc_assert TYPE_P (arg)) during alias
                    template CTAD with return-type-requirement in
                    constructor constraint
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: ice-on-valid-code
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: mateusz.pusz at gmail dot com
  Target Milestone: ---

The following valid C++20 code causes an ICE. It needs class template argument
deduction through an alias template, where the aliased class template's first
template parameter is a non-type parameter fixed by the alias, and a
constructor template whose requires-clause contains a compound requirement with
a type-constraint.

template<class T> concept C = true;

template<int N, class T>
struct A {
  A(T);
  template<class U> requires requires { { U() } -> C; }
  A(U);
};

template<class T> using B = A<0, T>;

B b = 1.0;

Command: g++ -std=c++20 -fsyntax-only test.cpp

Actual output (GCC 16 snapshot r16-8246-g569ace1fa50; the same assertion fires
on today's trunk):

test.cpp:6:43: internal compiler error: in tsubst, at cp/pt.cc:17141
    6 |   template<class U> requires requires { { U() } -> C; }
      |                                         ~~^~~~~~~~~~
0xa60177 tsubst(tree_node*, tree_node*, int, tree_node*)
        ../../src/gcc/cp/pt.cc:17141
0x1f4546a tsubst_type_requirement_1(tree_node*, tree_node*, sat_info, unsigned
long) [clone .isra.0]
        ../../src/gcc/cp/constraint.cc:1427
0x1eff63a tsubst_compound_requirement
        ../../src/gcc/cp/constraint.cc:1530
0x1eff63a tsubst_requirement
        ../../src/gcc/cp/constraint.cc:1631
0x1eff63a tsubst_requires_expr
        ../../src/gcc/cp/constraint.cc:1767
0x1be69aa tsubst_expr(tree_node*, tree_node*, int, tree_node*)
        ../../src/gcc/cp/pt.cc:23133
0x222de82 tsubst_constraint(tree_node*, tree_node*, int, tree_node*)
        ../../src/gcc/cp/constraint.cc:2264
0xdc8a22 tsubst_constraint_info(tree_node*, tree_node*, int, tree_node*)
        ../../src/gcc/cp/constraint.cc:1804
0x11247f3 alias_ctad_tweaks
        ../../src/gcc/cp/pt.cc:31950
0x112504f deduction_guides_for
        ../../src/gcc/cp/pt.cc:32272
0xb3d94e do_class_deduction
        ../../src/gcc/cp/pt.cc:32444

The failing assertion is gcc_assert (TYPE_P (arg)) in the TEMPLATE_TYPE_PARM
case of tsubst. It looks as if, while alias_ctad_tweaks rewrites the
constructor's constraints, the placeholder of the return-type-requirement is
mapped to the guide argument at the position of the NTTP N. The ICE goes away
if the NTTP is not the first template parameter (template<class T, int N> with
A<T, 0>), if the requirement has no type-constraint ({ U() }; or requires
C<U>;), or if no CTAD is involved (B<double> b = 1.0;).

Current trunk, gcc version 17.0.0 20261123 (experimental) (Compiler Explorer
build 4cb25d3a3d609a2a291716bf99503a64789bde86), ICEs at pt.cc:17500; 16.2 at
pt.cc:17313; 16.1 at pt.cc:17282. GCC 15.2 ICEs at pt.cc:16721, 14.3 at
pt.cc:16472, 13.4 at pt.cc:16200, and 12.5 ICEs in add_outermost_template_args
at pt.cc:615. -std=c++23 behaves the same.

Expected: the code compiles. The guide from A(T) deduces B as A<0, double>; the
guide from the constructor template cannot deduce the alias's T, which is a
deduction failure, not an error. Clang trunk accepts the code.

Found while compiling a documentation sample of the mp-units library
(template<typename T = double> using latitude = quantity_point<si::degree,
equator, T>; with `latitude lat = ...;`).

Reply via email to