[PATCH] c++: don't ICE upon valid lookup in local anonymous structs [PR120311]
Marek Polacek
polacek@redhat.com
Fri Oct 9 13:54:32 GMT 2026
On Fri, Oct 09, 2026 at 11:46:45AM +0000, Simon Martin wrote:
> We currently ICE with the following valid input because we end up using
(valid only with GNU extensions)
> DERIVED_FROM_P with a parent that's not a type (but here the function)
>
> ===
> void foo () {
> int x = ((struct/* AN_0 */ { struct/* AN_1 */ { char c; }; }) {0}).c;
> }
> ===
>
> The problem is that cp_parser_class_head calls xref_tag both for AN_0
> and AN_1 with INNERMOST_NON_CLASS because they appear in an expression,
> while we should use CURRENT_ONLY here. We therefore run into trouble
> when figuring out what 'c' corresponds to.
>
> This patch sets in_type_id_in_expr_p to false when parsing a class
> definition and restores its value right after, like we do for a bunch of
> other parser attributes.
>
> PR c++/120311
>
> gcc/cp/ChangeLog:
>
> * parser.cc (cp_parser_class_specifier): Force
> in_type_id_in_expr_p to false when parsing class definitions.
>
> gcc/testsuite/ChangeLog:
>
> * g++.dg/parse/crash82.C: New test.
The patch looks good, but...
> ---
> gcc/cp/parser.cc | 5 +++++
> gcc/testsuite/g++.dg/parse/crash82.C | 12 ++++++++++++
> 2 files changed, 17 insertions(+)
> create mode 100644 gcc/testsuite/g++.dg/parse/crash82.C
>
> diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
> index 48e3117a71e7..2bfa651e160e 100644
> --- a/gcc/cp/parser.cc
> +++ b/gcc/cp/parser.cc
> @@ -29933,6 +29933,7 @@ cp_parser_class_specifier (cp_parser* parser)
> bool saved_in_function_body;
> unsigned char in_statement;
> bool in_switch_statement_p;
> + bool saved_in_type_id_in_expr_p;
> bool saved_in_unbraced_linkage_specification_p;
> bool saved_in_unbraced_export_declaration_p;
> bool saved_auto_is_implicit_function_template_parm_p;
> @@ -30003,6 +30004,9 @@ cp_parser_class_specifier (cp_parser* parser)
> saved_in_unbraced_export_declaration_p
> = parser->in_unbraced_export_declaration_p;
> parser->in_unbraced_export_declaration_p = false;
> + /* Or in an expression. */
> + saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
> + parser->in_type_id_in_expr_p = false;
> saved_auto_is_implicit_function_template_parm_p
> = parser->auto_is_implicit_function_template_parm_p;
> parser->auto_is_implicit_function_template_parm_p = false;
> @@ -30407,6 +30411,7 @@ cp_parser_class_specifier (cp_parser* parser)
> parser->in_switch_statement_p = in_switch_statement_p;
> parser->in_statement = in_statement;
> parser->in_function_body = saved_in_function_body;
> + parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
> parser->num_template_parameter_lists
> = saved_num_template_parameter_lists;
> parser->in_unbraced_linkage_specification_p
> diff --git a/gcc/testsuite/g++.dg/parse/crash82.C b/gcc/testsuite/g++.dg/parse/crash82.C
> new file mode 100644
> index 000000000000..e934044e3108
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/parse/crash82.C
> @@ -0,0 +1,12 @@
> +// PR c++/120311
> +// { dg-do compile { target c++11 } }
> +// { dg-options "-fpermissive" }
Neither c++11 nor -fpermissive seems to be needed.
Marek
More information about the Gcc-patches
mailing list