]> git.ipfire.org Git - thirdparty/gcc.git/commit
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)
commit8990070b4297b913025d564293f97c0440622976
tree539b113d7d0103dee9b13a79426ea9157d72becd
parent3fafd9cb40e40cc4cc0a201ff150530dc3da7116
c++: Fix mangling of otherwise unattached class-scope lambdas [PR118245]

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]