[PATCH] c++: don't ICE upon valid lookup in local anonymous structs [PR120311]

Marek Polacek polacek@redhat.com
Fri Oct 9 14:42:17 GMT 2026


On Fri, Oct 09, 2026 at 02:37:37PM +0000, Simon Martin wrote:
> Hi Marek,
> 
> Thanks for the review!
> 
> >> We currently ICE with the following valid input because we end up using
> >
> > (valid only with GNU extensions)
> Good point, I'll amend the commit message accordingly.
> 
> > The patch looks good, but...
> 
> >> +++ 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.
> You're right about c++11, I'll remove it. I added -fpermissive because
> otherwise the scenario errors out because it's compiled with
> -pedantic-errors and triggers the following, and it's weird to have a
> scenario for a valid case trigger errors
> 
>   error: ISO C++ prohibits anonymous structs [-Wpedantic]
>   error: ISO C++ forbids compound-literals [-Wpedantic]
> 
> That said, it's probably better to use -Wno-pedantic instead of
> -fpermissive.

Or leave the dg-options "".
 
> So I'm testing the attached updated patch. OK it testing passes?

Can't approve but LGTM.  Thanks,

> From d0eb78555982b194867207f13d82621330063126 Mon Sep 17 00:00:00 2001
> From: Simon Martin <simon@nasilyan.com>
> Date: Fri, 9 Oct 2026 16:31:35 +0200
> Subject: [PATCH] c++: don't ICE upon lookup in local anonymous structs
>  [PR120311]
> 
> We currently ICE with the following valid (with GNU extensions) input
> because we end up using 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.
> 
> ---
>  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..dbc662901a3a
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/parse/crash82.C
> @@ -0,0 +1,12 @@
> +// PR c++/120311
> +// { dg-do compile }
> +// { dg-options "-Wno-pedantic" }
> +
> +void foo ()
> +{
> +   int x = ((struct {
> +      struct {
> +            char c;
> +      };
> +   }) {0}).c;
> +}
> -- 
> 2.56.0
> 


Marek



More information about the Gcc-patches mailing list