]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Fix mangling of otherwise unattached class-scope lambdas [PR118245]
authorNathaniel Shead <nathanieloshead@gmail.com>
Thu, 23 Jan 2025 08:22:04 +0000 (19:22 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Sat, 25 Jan 2025 00:31:15 +0000 (11:31 +1100)
This is a step closer to implementing the suggested changes for
https://github.com/itanium-cxx-abi/cxx-abi/pull/85.  Most lambdas
defined within a class should have an extra scope of that class so that
uses across different TUs are properly merged by the linker.  This also
needs to happen during template instantiation.

While I was working on this I found some other cases where the mangling
of lambdas was incorrect and causing issues, notably the testcase
lambda-ctx3.C which currently emits the same mangling for the base class
and member lambdas, causing mysterious assembler errors since r14-9232.

One notable case not handled either here or in the ABI is what is
supposed to happen with such unattached lambdas declared in member
templates; see lambda-uneval22.  I believe that by the C++ standard,
such lambdas should also dedup across TUs, but this isn't currently
implemented, and it's not clear exactly how such lambdas should mangle.

Since this should only affect usage of lambdas in unevaluated contexts
(a C++20 feature) this patch does not add an ABI flag to control this
behaviour.

PR c++/118245

gcc/cp/ChangeLog:

* cp-tree.h (LAMBDA_EXPR_EXTRA_SCOPE): Adjust comment.
* parser.cc (cp_parser_class_head): Start (and do not finish)
lambda scope for all valid types.
(cp_parser_class_specifier): Finish lambda scope after parsing
members instead.
* pt.cc (instantiate_class_template): Add lambda scoping.

gcc/testsuite/ChangeLog:

* g++.dg/abi/lambda-ctx3.C: New test.
* g++.dg/cpp2a/lambda-uneval22.C: New test.
* g++.dg/cpp2a/lambda-uneval23.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/cp-tree.h
gcc/cp/parser.cc
gcc/cp/pt.cc
gcc/testsuite/g++.dg/abi/lambda-ctx3.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/lambda-uneval22.C [new file with mode: 0644]
gcc/testsuite/g++.dg/cpp2a/lambda-uneval23.C [new file with mode: 0644]

index 5e8aefc12893cb8ea87ef781e63a42225de95d30..5d79cf927d92ed7568e342c540c564c8063f59df 100644 (file)
@@ -1562,7 +1562,8 @@ enum cp_lambda_default_capture_mode_type {
   (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->locus)
 
 /* The mangling scope for the lambda: FUNCTION_DECL, PARM_DECL, VAR_DECL,
-   FIELD_DECL or NULL_TREE.  If this is NULL_TREE, we have no linkage.  */
+   FIELD_DECL, TYPE_DECL, or NULL_TREE.  If this is NULL_TREE, we have no
+   linkage.  */
 #define LAMBDA_EXPR_EXTRA_SCOPE(NODE) \
   (((struct tree_lambda_expr *)LAMBDA_EXPR_CHECK (NODE))->extra_scope)
 
index 03010089c3c625a0f625c83e0dc7ce8901c19a1f..2f88e5fcd9141d0072a5ea3543427114f7fd591f 100644 (file)
@@ -27462,6 +27462,8 @@ cp_parser_class_specifier (cp_parser* parser)
   if (!braces.require_open (parser))
     {
       pop_deferring_access_checks ();
+      if (type != error_mark_node)
+       finish_lambda_scope ();
       return error_mark_node;
     }
 
@@ -27526,7 +27528,10 @@ cp_parser_class_specifier (cp_parser* parser)
   if (cp_parser_allow_gnu_extensions_p (parser))
     attributes = cp_parser_gnu_attributes_opt (parser);
   if (type != error_mark_node)
-    type = finish_struct (type, attributes);
+    {
+      type = finish_struct (type, attributes);
+      finish_lambda_scope ();
+    }
   if (nested_name_specifier_p)
     pop_inner_scope (old_scope, scope);
 
@@ -28366,6 +28371,12 @@ cp_parser_class_head (cp_parser* parser,
   if (flag_concepts)
     type = associate_classtype_constraints (type);
 
+  /* Lambdas in bases and members must have the same mangling scope for ABI.
+     We open this scope now, and will close it in cp_parser_class_specifier
+     after parsing the member list.  */
+  if (type && type != error_mark_node)
+    start_lambda_scope (TYPE_NAME (type));
+
   /* We will have entered the scope containing the class; the names of
      base classes should be looked up in that context.  For example:
 
@@ -28380,16 +28391,10 @@ cp_parser_class_head (cp_parser* parser,
   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
     {
       if (type)
-       {
-         pushclass (type);
-         start_lambda_scope (TYPE_NAME (type));
-       }
+       pushclass (type);
       bases = cp_parser_base_clause (parser);
       if (type)
-       {
-         finish_lambda_scope ();
-         popclass ();
-       }
+       popclass ();
     }
   else
     bases = NULL_TREE;
index 28e05490d06c997d40d45db9b27c1db2ae1ef094..0f739d89b35c86435ba2bea313ba3f7f0a77ea68 100644 (file)
@@ -12606,6 +12606,10 @@ instantiate_class_template (tree type)
   gcc_assert (!DECL_CLASS_SCOPE_P (TYPE_MAIN_DECL (pattern))
              || COMPLETE_OR_OPEN_TYPE_P (TYPE_CONTEXT (type)));
 
+  /* When instantiating nested lambdas, ensure that they get the mangling
+     scope of the new class type.  */
+  start_lambda_scope (TYPE_NAME (type));
+
   base_list = NULL_TREE;
   /* Defer access checking while we substitute into the types named in
      the base-clause.  */
@@ -12967,6 +12971,8 @@ instantiate_class_template (tree type)
   finish_struct_1 (type);
   TYPE_BEING_DEFINED (type) = 0;
 
+  finish_lambda_scope ();
+
   /* Remember if instantiating this class ran into errors, so we can avoid
      instantiating member functions in limit_bad_template_recursion.  We set
      this flag even if the problem was in another instantiation triggered by
diff --git a/gcc/testsuite/g++.dg/abi/lambda-ctx3.C b/gcc/testsuite/g++.dg/abi/lambda-ctx3.C
new file mode 100644 (file)
index 0000000..f782f2c
--- /dev/null
@@ -0,0 +1,27 @@
+// { dg-do compile { target c++20 } }
+// { dg-additional-options "-fkeep-inline-functions" }
+// See also https://github.com/itanium-cxx-abi/cxx-abi/pull/85
+
+struct A {
+  decltype([]{ return 1; }) f;
+};
+
+struct B : decltype([]{ return 2; }) {
+  decltype([]{ return 3; }) f;
+};
+
+template <typename T>
+struct C : decltype([]{ return 4; }) {
+  decltype([]{ return 5; }) f;
+};
+
+template struct C<int>;
+template struct C<double>;
+
+// { dg-final { scan-assembler {_ZNK1AUlvE_clEv:} } }
+// { dg-final { scan-assembler {_ZNK1BUlvE_clEv:} } }
+// { dg-final { scan-assembler {_ZNK1BUlvE0_clEv:} } }
+// { dg-final { scan-assembler {_ZNK1CIiEUlvE_clEv:} } }
+// { dg-final { scan-assembler {_ZNK1CIiEUlvE0_clEv:} } }
+// { dg-final { scan-assembler {_ZNK1CIdEUlvE_clEv:} } }
+// { dg-final { scan-assembler {_ZNK1CIdEUlvE0_clEv:} } }
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-uneval22.C b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval22.C
new file mode 100644 (file)
index 0000000..9c0e812
--- /dev/null
@@ -0,0 +1,21 @@
+// { dg-do compile { target c++20 } }
+
+struct S {
+  template <int I>
+  using T = decltype([]{ return I; });
+
+  template <int I>
+  decltype([]{ return I; }) f() { return {}; }
+};
+
+void a(S::T<0>*);  // { dg-error "declared using local type" }
+void b(S::T<1>*);  // { dg-error "declared using local type" }
+void c(decltype(S{}.f<0>())*);  // { dg-error "declared using local type" }
+void d(decltype(S{}.f<1>())*);  // { dg-error "declared using local type" }
+
+int main() {
+  a(nullptr);
+  b(nullptr);
+  c(nullptr);
+  d(nullptr);
+}
diff --git a/gcc/testsuite/g++.dg/cpp2a/lambda-uneval23.C b/gcc/testsuite/g++.dg/cpp2a/lambda-uneval23.C
new file mode 100644 (file)
index 0000000..fa009f5
--- /dev/null
@@ -0,0 +1,7 @@
+// PR c++/118245
+// { dg-do compile { target c++20 } }
+
+template<auto> struct Cask {};
+struct T1 : Cask<[]{}> {
+  Cask<[]{}> c{};
+};