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

            Bug ID: 121654
           Summary: template for ICE when referencing a variable in
                    surrounding scope
           Product: gcc
           Version: 16.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: ivan.lazaric.gcc at gmail dot com
  Target Milestone: ---

https://godbolt.org/z/W4brejxoE

Code:
```
void func(int arg){
    template for (auto c : {1}){
        arg;
    }
}
```

Compiling this with flags `-std=c++26 -O3` results in an ICE:
```
<source>: In instantiation of 'template for' iteration 1:
required from here
<source>:2:24:   
    2 |     template for (auto c : {1}){
      |                        ^
<source>:3:9: internal compiler error: in tsubst_expr, at cp/pt.cc:22327
    3 |         arg;
      |         ^~~
0x28aa705 diagnostics::context::diagnostic_impl(rich_location*,
diagnostics::metadata const*, diagnostics::option_id, char const*,
__va_list_tag (*) [1], diagnostics::kind)
        ???:0
0x289fd26 internal_error(char const*, ...)
        ???:0
0xafd6c4 fancy_abort(char const*, int, char const*)
        ???:0
0xd8e8e8 finish_expansion_stmt(tree_node*, tree_node*, int, tree_node*)
        ???:0
0xd30873 c_parse_file()
        ???:0
0xe9ffa9 c_common_parse_file()
        ???:0
```

I've also built gcc locally, on commit abf51b388692c4b69d48a60bc7bbd579241d0b7f
The backtrace might be more insightful than the godbolt one, so adding it as
well
Outcome of compiling the file:
```
$ /home/ilazaric/GCC-MASTER/bin/g++ -std=c++26 ice.cpp
ice.cpp: In instantiation of ‘template for’ iteration 1:
required from here
ice.cpp:2:24:
    2 |     template for (auto c : {1}){
      |                        ^
ice.cpp:3:9: internal compiler error: in tsubst_expr, at cp/pt.cc:22327
    3 |         arg;
      |         ^~~
0x2a5cbdf internal_error(char const*, ...)
        ../../gcc/diagnostic-global-context.cc:534
0xb78e23 fancy_abort(char const*, int, char const*)
        ../../gcc/diagnostics/context.cc:1687
0x8b64f3 tsubst_expr(tree_node*, tree_node*, int, tree_node*)
        ../../gcc/cp/pt.cc:22327
0xdee18d tsubst_expr(tree_node*, tree_node*, int, tree_node*)
        ../../gcc/cp/pt.cc:22737
0xe1c68f tsubst_stmt
        ../../gcc/cp/pt.cc:20322
0xe1d7ab tsubst_stmt
        ../../gcc/cp/pt.cc:19113
0xe22f6f tsubst_stmt
        ../../gcc/cp/pt.cc:19081
0xe22f6f finish_expansion_stmt(tree_node*, tree_node*, int, tree_node*)
        ../../gcc/cp/pt.cc:33002
0xdb7750 cp_parser_expansion_statement
        ../../gcc/cp/parser.cc:15720
0xd799ca cp_parser_statement
        ../../gcc/cp/parser.cc:13331
0xd7c87b cp_parser_statement_seq_opt
        ../../gcc/cp/parser.cc:14076
0xd7cb4f cp_parser_compound_statement
        ../../gcc/cp/parser.cc:13923
0xdaa3d5 cp_parser_function_body
        ../../gcc/cp/parser.cc:27329
0xdaa3d5 cp_parser_ctor_initializer_opt_and_function_body
        ../../gcc/cp/parser.cc:27380
0xd7cea9 cp_parser_function_definition_after_declarator
        ../../gcc/cp/parser.cc:34393
0xdb0c3e cp_parser_function_definition_from_specifiers_and_declarator
        ../../gcc/cp/parser.cc:34308
0xdb0c3e cp_parser_init_declarator
        ../../gcc/cp/parser.cc:24611
0xdb3127 cp_parser_simple_declaration
        ../../gcc/cp/parser.cc:17068
0xdc0a86 cp_parser_declaration
        ../../gcc/cp/parser.cc:16688
0xdc1fcb cp_parser_toplevel_declaration
        ../../gcc/cp/parser.cc:16709
```

Just in case it is relevant (though I suspect not very), flags for configure
for building gcc:
```
../configure --enable-languages=c,c++   --disable-multilib
--prefix=$HOME/GCC-MASTER
```

Reply via email to