Bug 110523 - [14 Regression] ICE in redeclare_class_template, at cp/pt.cc:6391 on json-3.11.2
Summary: [14 Regression] ICE in redeclare_class_template, at cp/pt.cc:6391 on json-3.11.2
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 14.0
: P3 normal
Target Milestone: 14.0
Assignee: Patrick Palka
URL:
Keywords: ice-on-valid-code
Depends on:
Blocks:
 
Reported: 2023-07-02 14:51 UTC by Sergei Trofimovich
Modified: 2023-07-10 18:07 UTC (History)
4 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2023-07-02 00:00:00


Attachments
bug.cpp.orig.xz (240.64 KB, application/x-xz)
2023-07-02 14:54 UTC, Sergei Trofimovich
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Sergei Trofimovich 2023-07-02 14:51:46 UTC
Noticed ICE on r14-2233-gfca6d9c12f5bf0 when was building json-3.11.2 package.

Extracted reproducer:

// $ cat bug.cpp
template <typename, typename> struct adl_serializer;

template <template <typename, typename> class = adl_serializer>
class basic_json;

template <typename> struct json_pointer {
  template <template <typename, typename> class> friend class basic_json;
};
template <template <typename, typename> class> struct basic_json {
  using json_pointer = json_pointer<int>;
};
basic_json<>::json_pointer __trans_tmp_1_DOCTEST_ANON_FUNC_2() {
  json_pointer<basic_json<>> ptr_j;
}

$ g++ -c bug.cpp
bug.cpp:10:9: warning: declaration of 'using basic_json< <template-parameter-1-1> >::json_pointer = struct json_pointer<int>' changes meaning of 'json_pointer' [-Wchanges-meaning]
   10 |   using json_pointer = json_pointer<int>;
      |         ^~~~~~~~~~~~
bug.cpp:10:24: note: used here to mean 'struct json_pointer<int>'
   10 |   using json_pointer = json_pointer<int>;
      |                        ^~~~~~~~~~~~~~~~~
bug.cpp:6:28: note: declared here
    6 | template <typename> struct json_pointer {
      |                            ^~~~~~~~~~~~
bug.cpp: In instantiation of 'struct json_pointer<basic_json<> >':
bug.cpp:13:30:   required from here
bug.cpp:7:63: internal compiler error: in redeclare_class_template, at cp/pt.cc:6391
    7 |   template <template <typename, typename> class> friend class basic_json;
      |                                                               ^~~~~~~~~~
0x708b6c redeclare_class_template(tree_node*, tree_node*, tree_node*)
        ../../source/gcc/cp/pt.cc:6391
0x8fe749 tsubst_friend_class
        ../../source/gcc/cp/pt.cc:11590
0x8fe749 instantiate_class_template(tree_node*)
        ../../source/gcc/cp/pt.cc:12446
0x9398ed complete_type(tree_node*)
        ../../source/gcc/cp/typeck.cc:138
0x7c49e2 start_decl_1(tree_node*, bool)
        ../../source/gcc/cp/decl.cc:5980
0x7de3e9 start_decl_1(tree_node*, bool)
        ../../source/gcc/cp/decl.cc:5946
0x7de3e9 start_decl(cp_declarator const*, cp_decl_specifier_seq*, int, tree_node*, tree_node*, tree_node**)
        ../../source/gcc/cp/decl.cc:5946
0x8b70b7 cp_parser_init_declarator
        ../../source/gcc/cp/parser.cc:22951
0x88f971 cp_parser_simple_declaration
        ../../source/gcc/cp/parser.cc:15435
0x891520 cp_parser_declaration_statement
        ../../source/gcc/cp/parser.cc:14516
0x891c39 cp_parser_statement
        ../../source/gcc/cp/parser.cc:12574
0x892d6d cp_parser_statement_seq_opt
        ../../source/gcc/cp/parser.cc:13000
0x892e0f cp_parser_compound_statement
        ../../source/gcc/cp/parser.cc:12952
0x8b5881 cp_parser_function_body
        ../../source/gcc/cp/parser.cc:25372
0x8b5881 cp_parser_ctor_initializer_opt_and_function_body
        ../../source/gcc/cp/parser.cc:25423
0x8b6a4e cp_parser_function_definition_after_declarator
        ../../source/gcc/cp/parser.cc:32058
0x8b7dcc cp_parser_function_definition_from_specifiers_and_declarator
        ../../source/gcc/cp/parser.cc:31975
0x8b7dcc cp_parser_init_declarator
        ../../source/gcc/cp/parser.cc:22831
0x88f971 cp_parser_simple_declaration
        ../../source/gcc/cp/parser.cc:15435
0x8c1ddb cp_parser_declaration
        ../../source/gcc/cp/parser.cc:15121

$ g++ -v
Using built-in specs.
COLLECT_GCC=/<<NIX>>/gcc-14.0.0/bin/g++
COLLECT_LTO_WRAPPER=/<<NIX>>/gcc-14.0.0/libexec/gcc/x86_64-unknown-linux-gnu/14.0.0/lto-wrapper
Target: x86_64-unknown-linux-gnu
Configured with:
Thread model: posix
Supported LTO compression algorithms: zlib
gcc version 14.0.0 99999999 (experimental) (GCC)
Comment 1 Sergei Trofimovich 2023-07-02 14:54:59 UTC
Created attachment 55452 [details]
bug.cpp.orig.xz

In case I reduced the file incorrectly attaching bug.cpp.orig.xz which should build with both gcc-13 and gcc-14.
Comment 2 Andrew Pinski 2023-07-02 17:07:07 UTC
Re-reducing it so it is warning free as the original was warning free.
Comment 3 Andrew Pinski 2023-07-02 19:10:21 UTC
Better reduced testcase:
```
template <typename> class v {};
template <template <typename> class a = v>
class basic_json {};
template <typename t>
struct json_pointer {
  template <template <typename> class a>
    friend class basic_json;
  using type = t;
};
json_pointer<int>::type t;
json_pointer<basic_json<>>::type t1;
```
Comment 4 David Binderman 2023-07-04 10:15:36 UTC
I see something similar when building dlib:

/home/dcb38/rpmbuild/BUILD/dlib-65bce59a1512cf222dec01d3e0f29b612dd181f5/dlib/../dlib/dnn/core.h:799:22: internal compiler error: in redeclare_class_template, at cp/pt.cc:6391
Comment 5 David Binderman 2023-07-04 10:36:43 UTC
The compiler seems to break as follows:

/home/dcb38/gcc/results.20230701/bin/gcc
/home/dcb38/gcc/results.20230703.asan.ubsan/bin/gcc
/home/dcb38/rpmbuild/BUILD/dlib-65bce59a1512cf222dec01d3e0f29b612dd181f5/dlib/../dlib/dnn/core.h:799:22: internal compiler error: in redeclare_class_template, at cp/pt.cc:6391

$ /home/dcb38/gcc/results.20230701/bin/gcc -v 2>&1 | grep exp
gcc version 14.0.0 20230630 (experimental) (c3c0ba5436170e01) 
$ /home/dcb38/gcc/results.20230703.asan.ubsan/bin/gcc -v 2>&1 | grep exp
gcc version 14.0.0 20230703 (experimental) (9a856f67ee2f13ea) 

$ git log c3c0ba5436170e01..9a856f67ee2f13ea | grep -c "^commit"
45
$
Comment 6 David Binderman 2023-07-04 11:12:56 UTC
The only C++ related commit in that range is:

commit db38b285ba61c5b888adc0d117177bfd774c1153
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Jun 30 15:05:22 2023 -0400

    c++: fix up caching of level lowered ttps
Comment 7 David Binderman 2023-07-04 12:57:52 UTC
I have tried to start a git bisect and the current range is 
g:f3d87219dd502d5c .. g:9757e4440bd8755d.

This is 15 commits.
Comment 8 David Binderman 2023-07-04 15:51:04 UTC
(In reply to David Binderman from comment #7)
> I have tried to start a git bisect and the current range is 
> g:f3d87219dd502d5c .. g:9757e4440bd8755d.
> 
> This is 15 commits.

I messed this one up and started again. As expected,
Patrick's commit seems to be the one:

$ git bisect bad db38b285ba61c5b8
db38b285ba61c5b888adc0d117177bfd774c1153 is the first bad commit
commit db38b285ba61c5b888adc0d117177bfd774c1153
Author: Patrick Palka <ppalka@redhat.com>
Date:   Fri Jun 30 15:05:22 2023 -0400

    c++: fix up caching of level lowered ttps
Comment 9 Patrick Palka 2023-07-10 14:35:17 UTC
Simplified:

template<template<class, class> class>
class basic_json;

template<class> struct json_pointer {
  template<template<class, class> class> friend class basic_json;
};

template struct json_pointer<int>;
template struct json_pointer<char>;
template struct json_pointer<long>;
Comment 10 GCC Commits 2023-07-10 15:00:07 UTC
The master branch has been updated by Patrick Palka <ppalka@gcc.gnu.org>:

https://gcc.gnu.org/g:2c60368ab5706a870a1a3be190acc4d673672c30

commit r14-2421-g2c60368ab5706a870a1a3be190acc4d673672c30
Author: Patrick Palka <ppalka@redhat.com>
Date:   Mon Jul 10 10:59:40 2023 -0400

    c++: redeclare_class_template and ttps [PR110523]
    
    Now that we cache level-lowered ttps we can end up processing the same
    ttp multiple times via (multiple calls to) redeclare_class_template, so
    we can't assume a ttp's DECL_CONTEXT is initially empty.
    
            PR c++/110523
    
    gcc/cp/ChangeLog:
    
            * pt.cc (redeclare_class_template): Relax the ttp DECL_CONTEXT
            assert, and downgrade it to a checking assert.
    
    gcc/testsuite/ChangeLog:
    
            * g++.dg/template/ttp37.C: New test.
Comment 11 Patrick Palka 2023-07-10 15:06:06 UTC
Fixed.
Comment 12 Sergei Trofimovich 2023-07-10 18:07:20 UTC
I confirm ICE gone away for json-3.11.2. json-3.11.2 still odes not compile same as in https://gcc.gnu.org/PR110580