Bootstrapped and regtested on x86_64-pc-linux-gnu. Any thoughts on this
approach?
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
From ea625e2ad0ca4826b84449e26602b840e7cf355b Mon Sep 17 00:00:00 2001
From: benwu25 <[email protected]>
Date: Mon, 15 Dec 2025 00:14:25 -0800
Subject: [PATCH] c++: initialize constructor_possible_p with LOCAL_CLASS_P
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