On 12/15/25 4:24 PM, Ben Wu wrote:
Bootstrapped and regtested on x86_64-pc-linux-gnu. Any thoughts on this approach?

Instead, I think we want to add in_declarator_p to all the other flags that get cleared/restored in cp_parser_lambda_expression.

Please use the attached patch file if applying, thanks!

-- 8< --

Previously, constructors were not possibly parsed while in a
declarator, but we may have a constructor in a declarator as in this
PR where a lambda expression containing a class definition is a
default argument.

We can account for this by checking current_class_type when
initializing constructor_possible_p so that we allow for a constructor
inside a declarator if LOCAL_CLASS_P (current_class_type).

PR c++/121443

gcc/cp/ChangeLog:

* parser.cc (cp_parser_decl_specifier_seq): Consider LOCAL_CLASS_P
in initializing constructor_possible_p.

gcc/testsuite/ChangeLog:

* g++.dg/parse/pr121443.C: New test.
---
  gcc/cp/parser.cc                      |  4 +++-
  gcc/testsuite/g++.dg/parse/pr121443.C | 10 ++++++++++
  2 files changed, 13 insertions(+), 1 deletion(-)
  create mode 100644 gcc/testsuite/g++.dg/parse/pr121443.C

diff --git a/gcc/cp/parser.cc b/gcc/cp/parser.cc
index 214dababeb5..7683c160b14 100644
--- a/gcc/cp/parser.cc
+++ b/gcc/cp/parser.cc
@@ -17700,7 +17700,9 @@ cp_parser_decl_specifier_seq (cp_parser* parser,
        cp_decl_specifier_seq *decl_specs,
        int* declares_class_or_enum)
  {
-  bool constructor_possible_p = !parser->in_declarator_p;
+  const bool maybe_lambda_p = (current_class_type
+       && LOCAL_CLASS_P (current_class_type));
+  bool constructor_possible_p = !parser->in_declarator_p || maybe_lambda_p;
    bool found_decl_spec = false;
    cp_token *start_token = NULL;
    cp_decl_spec ds;
diff --git a/gcc/testsuite/g++.dg/parse/pr121443.C b/gcc/testsuite/g+ +.dg/parse/pr121443.C
new file mode 100644
index 00000000000..1022b3cdd90
--- /dev/null
+++ b/gcc/testsuite/g++.dg/parse/pr121443.C
@@ -0,0 +1,10 @@
+// { dg-do compile { target c++11 } }
+
+void f(int i = []() {
+  struct X {
+    int val;
+    X(int v) : val(v) { }
+  };
+  X x(2);
+  return x.val;
+}()) {}
--
2.43.0


Reply via email to