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

            Bug ID: 129115
           Summary: [reflection] dependent splice types never compare
                    equal: ICE in unify, rejected redeclarations,
                    conflicting imported declaration
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Keywords: ice-on-valid-code, rejects-valid
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: contact.yiran.wang at gmail dot com
  Target Milestone: ---
            Target: x86_64-linux-gnu

Two occurrences of the same dependent splice type, such as "typename [: ^^T
:]", are never treated as the same
type. Any declaration that has to be matched against an earlier one through
such a type fails.

$ g++ -v
Using built-in specs.
COLLECT_GCC=g++
Target: x86_64-linux-gnu
Configured with: ../gcc-trunk-20261124/configure
--prefix=/opt/compiler-explorer/gcc-build/staging
--build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu
--disable-bootstrap --enable-multiarch
--with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib
--enable-clocale=gnu
--enable-languages=c,c++,fortran,ada,objc,obj-c++,go,d,rust,m2,cobol,algol68
--enable-ld=yes --enable-gold=yes
--enable-libstdcxx-time=yes --enable-linker-build-id --enable-lto
--enable-plugins --enable-threads=posix
--with-pkgversion=Compiler-Explorer-Build-gcc-537d251e84e6b9624d80dc434dfdb1698909c129-binutils-2.47
Thread model: posix
gcc version 17.0.0 20261124 (experimental)
(Compiler-Explorer-Build-gcc-537d251e84e6b9624d80dc434dfdb1698909c129-binutils-2.47)

Also seen with a build of snapshot gcc-17-20261122 (x86_64-w64-mingw32). None
of the tests includes a header.
Command line for tests 1 and 2: g++ -std=c++26 -freflection -fsyntax-only
test.C

1. Redeclared function template, then a call: internal compiler error

  template<typename T> int f (typename [: ^^T :] x);
  template<typename T> int f (typename [: ^^T :] x) { return 1; }
  int a = f<int> (1);

  redecl.C:6:16: internal compiler error: in unify, at cp/pt.cc:27271
      6 | int a = f<int> (1);
        |         ~~~~~~~^~~
  ... fancy_abort / more_specialized_fn / build_new_function_call /
finish_call_expr / c_parse_file

  The two declarations are taken for two overloads, and partial ordering then
reaches unify, which has no case
  for SPLICE_SCOPE.

2. Out-of-class definition of a member function: rejected

  template<typename T> struct M { typename [: ^^T :] get (typename [: ^^T :]
x); };
  template<typename T> typename [: ^^T :] M<T>::get (typename [: ^^T :] x) {
return x; }
  int m = M<int>{}.get (3);

  member.C:2:41: error: no declaration matches '[: ^^T :] M<T>::get([: ^^T :])'
    candidate is: '[: ^^T :] M<T>::get([: ^^T :])'
    parameter 1 of candidate has type '[: ^^T :]'... which does not match type
'[: ^^T :]'

3. Alias template that reaches a file through two modules: rejected

  // h.h
  template<typename T> using same = [: ^^T :];
  // a.C
  module;
  #include "h.h"
  export module a;
  export same<int> fa ();
  // b.C
  module;
  #include "h.h"
  export module b;
  export same<int> fb ();
  // c.C
  import a;
  import b;
  int x = fa () + fb ();

  g++ -std=c++26 -freflection -fmodules -c a.C; ... -c b.C; ... -c c.C

  h.h:1:28: error: conflicting imported declaration 'template<class T> using
same = [: ^^T :]'
  h.h:1:28: note: existing declaration 'template<class T> using same = [: ^^T
:]'
  c.C:3:17: note: during load of binding '::fb@b'

  The same header with "using same = T;", with a non-dependent splice, or with
the splice in a function return
  type or a class member is merged without an error.

Cause, as far as I can tell from the source (cp/reflect.cc, cp/typeck.cc,
cp/module.cc):

  make_splice_scope creates a new SPLICE_SCOPE node with cxx_make_type every
time and leaves it as its own
  TYPE_CANONICAL, and structural_comptypes has no case for SPLICE_SCOPE (it
ends in "default: return false").
  So same_type_p is false for any two such nodes; in test 3 this is the
same_type_p on DECL_ORIGINAL_TYPE in
  is_matching_decl.

A change that makes the three tests pass for me:

--- a/gcc/cp/reflect.cc
+++ b/gcc/cp/reflect.cc
@@ make_splice_scope (tree expr, bool type_p)
   tree t = cxx_make_type (SPLICE_SCOPE);
   SPLICE_SCOPE_EXPR (t) = expr;
   SPLICE_SCOPE_TYPE_P (t) = type_p;
+  SET_TYPE_STRUCTURAL_EQUALITY (t);
   return t;
--- a/gcc/cp/typeck.cc
+++ b/gcc/cp/typeck.cc
@@ structural_comptypes (tree t1, tree t2, int strict)
+    case SPLICE_SCOPE:
+      if (SPLICE_SCOPE_TYPE_P (t1) != SPLICE_SCOPE_TYPE_P (t2))
+       return false;
+      if (!cp_tree_equal (SPLICE_SCOPE_EXPR (t1), SPLICE_SCOPE_EXPR (t2)))
+       return false;
+      break;
+
     default:
       return false;

With it (cc1plus only, --enable-checking=release, no bootstrap, DejaGnu not
run): the three tests above compile;
aliases that differ ([: ^^T :] against [: ^^T* :], or against a different
expression) are still rejected when
imported from two modules; of the 241 tests in g++.dg/reflect that contain a
splice, 240 give the same output with
-fsyntax-only, and diag2.C changes from "conflicting declaration '[: R :] r'"
to "redeclaration of '[: R :] r'".

Not fixed by it: a partial specialization "template<typename T> struct
S<tag<typename [: ^^T :]>>" still gives an
internal compiler error in unify, which has no SPLICE_SCOPE case.

I searched Bugzilla (SPLICE_SCOPE; reflection + splice / unify / comptypes /
"conflicting imported declaration" /
"no declaration matches") and found no report of this. PR127316 is also about a
redeclaration, but of a function
whose return type contains ^^parm, not a splice type. PR124582 gave the same
"conflicting imported declaration"
message for typedef struct { } A and is fixed (r17-205); test 3 above still
fails after it.

This analysis and the change were prepared with the help of an AI coding
assistant (Claude); I ran the tests
above myself on my machine.

Reply via email to