https://gcc.gnu.org/g:806d5417e0c04a15c48a7e876ebf435f06ecfccc

commit r16-4209-g806d5417e0c04a15c48a7e876ebf435f06ecfccc
Author: Egas Ribeiro <[email protected]>
Date:   Fri Oct 3 01:56:52 2025 +0100

    c++: Fix ICE with struct in function parameter containing auto [PR122112]
    
    When parsing function parameters containing a struct declaration,
    initially auto_is_implicit_function_template_parm_p is set to true by
    cp_parser_parameter_declaration_clause. However, later when we enter
    class scope and scope_kind becomes sk_class, we don't set it to false, so
    cp_parser_simple_type_specifier will call
    synthesize_implicit_template_parm, which expects that
    current_binding_level->kind == sk_function_parms, triggering a failed
    assertion. This fix ensures that auto_is_implicit_function_template_parm_p
    isn't set when parsing a struct body definition.
    
    gcc/cp/ChangeLog:
    
            PR c++/122112
            * parser.cc (cp_parser_parameter_declaration_clause): Don't
            enable auto_is_implicit_function_template_parm_p when entering
            class scope.
    
    gcc/testsuite/ChangeLog:
    
            PR c++/122112
            * g++.dg/parse/auto-struct-param.C: New test.
    
    Signed-off-by: Egas Ribeiro <[email protected]>

Diff:
---
 gcc/cp/parser.cc                               | 7 +++++++
 gcc/testsuite/g++.dg/parse/auto-struct-param.C | 4 ++++
 2 files changed, 11 insertions(+)

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 8d1187f8cdae..04031486bb3b 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -28124,6 +28124,7 @@ cp_parser_class_specifier (cp_parser* parser)
   bool in_switch_statement_p;
   bool saved_in_unbraced_linkage_specification_p;
   bool saved_in_unbraced_export_declaration_p;
+  bool saved_auto_is_implicit_function_template_parm_p;
   tree old_scope = NULL_TREE;
   tree scope = NULL_TREE;
   cp_token *closing_brace;
@@ -28181,6 +28182,10 @@ 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;
+  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;
+
   /* 'this' from an enclosing non-static member function is unavailable.  */
   tree saved_ccp = current_class_ptr;
   tree saved_ccr = current_class_ref;
@@ -28571,6 +28576,8 @@ cp_parser_class_specifier (cp_parser* parser)
     = saved_in_unbraced_linkage_specification_p;
   parser->in_unbraced_export_declaration_p
     = saved_in_unbraced_export_declaration_p;
+  parser->auto_is_implicit_function_template_parm_p
+    = saved_auto_is_implicit_function_template_parm_p;
   current_class_ptr = saved_ccp;
   current_class_ref = saved_ccr;
 
diff --git a/gcc/testsuite/g++.dg/parse/auto-struct-param.C 
b/gcc/testsuite/g++.dg/parse/auto-struct-param.C
new file mode 100644
index 000000000000..78573c685036
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/auto-struct-param.C
@@ -0,0 +1,4 @@
+// PR c++/122112
+// { dg-do compile { target c++20 } }
+
+void func(struct { auto x; });  // { dg-error "" }

Reply via email to