Bug 85194 - ICE with structured binding in broken for-loop
Summary: ICE with structured binding in broken for-loop
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 8.0
: P3 normal
Target Milestone: ---
Assignee: Jakub Jelinek
URL:
Keywords: error-recovery, ice-on-invalid-code
Depends on:
Blocks:
 
Reported: 2018-04-04 10:42 UTC by Volker Reichelt
Modified: 2018-04-09 19:51 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2018-04-04 00:00:00


Attachments
gcc8-pr85194.patch (731 bytes, patch)
2018-04-04 14:02 UTC, Jakub Jelinek
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Volker Reichelt 2018-04-04 10:42:14 UTC
The following invalid code snippet (compiled with "-std=c++17") triggers
an ICE since GCC 7.1.0 (when structured bindings were introduced):

================================
struct A { int i; };

A x[2];

void foo()
{
  for (auto [i] = A() : x)
    ;
}
================================

bug.cc: In function 'void foo()':
bug.cc:7:17: error: initializer in range-based 'for' loop
   for (auto [i] = A() : x)
                 ^
bug.cc:7:23: internal compiler error: Segmentation fault
   for (auto [i] = A() : x)
                       ^
0xebccdf crash_signal
	../../gcc/gcc/toplev.c:325
0x91bd07 tree_check3(tree_node*, char const*, int, char const*, tree_code, tree_code, tree_code)
	../../gcc/gcc/tree.h:3172
0x91bd07 cp_parser_range_for
	../../gcc/gcc/cp/parser.c:11755
0x946866 cp_parser_for
	../../gcc/gcc/cp/parser.c:11685
0x946866 cp_parser_iteration_statement
	../../gcc/gcc/cp/parser.c:12242
0x923d06 cp_parser_statement
	../../gcc/gcc/cp/parser.c:10824
0x924d10 cp_parser_statement_seq_opt
	../../gcc/gcc/cp/parser.c:11276
0x924de7 cp_parser_compound_statement
	../../gcc/gcc/cp/parser.c:11230
0x93ba20 cp_parser_function_body
	../../gcc/gcc/cp/parser.c:21791
0x93ba20 cp_parser_ctor_initializer_opt_and_function_body
	../../gcc/gcc/cp/parser.c:21828
0x93c2d0 cp_parser_function_definition_after_declarator
	../../gcc/gcc/cp/parser.c:26849
0x93d056 cp_parser_function_definition_from_specifiers_and_declarator
	../../gcc/gcc/cp/parser.c:26765
0x93d056 cp_parser_init_declarator
	../../gcc/gcc/cp/parser.c:19511
0x944148 cp_parser_simple_declaration
	../../gcc/gcc/cp/parser.c:13059
0x944f58 cp_parser_block_declaration
	../../gcc/gcc/cp/parser.c:12884
0x948ee2 cp_parser_declaration
	../../gcc/gcc/cp/parser.c:12782
0x9492f1 cp_parser_declaration_seq_opt
	../../gcc/gcc/cp/parser.c:12658
0x9495e4 cp_parser_translation_unit
	../../gcc/gcc/cp/parser.c:4563
0x9495e4 c_parse_file()
	../../gcc/gcc/cp/parser.c:39026
0xa491c6 c_common_parse_file()
	../../gcc/gcc/c-family/c-opts.c:1132
Please submit a full bug report, [etc.]
Comment 1 Jakub Jelinek 2018-04-04 14:02:37 UTC
Created attachment 43844 [details]
gcc8-pr85194.patch

Untested fix.
Comment 2 Jakub Jelinek 2018-04-09 19:49:20 UTC
Author: jakub
Date: Mon Apr  9 19:48:48 2018
New Revision: 259252

URL: https://gcc.gnu.org/viewcvs?rev=259252&root=gcc&view=rev
Log:
	PR c++/85194
	* parser.c (cp_parser_simple_declaration): For structured bindings,
	if *maybe_range_for_decl is NULL after parsing it, set it to
	error_mark_node.

	* g++.dg/cpp1z/decomp43.C: New test.

Added:
    trunk/gcc/testsuite/g++.dg/cpp1z/decomp43.C
Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/parser.c
    trunk/gcc/testsuite/ChangeLog
Comment 3 Jakub Jelinek 2018-04-09 19:51:09 UTC
Fixed for GCC8.  error-recovery, not backporting to release branches.