]> git.ipfire.org Git - thirdparty/gcc.git/commit
c++: Fix handling of no-linkage decls for modules
authorNathaniel Shead <nathanieloshead@gmail.com>
Sat, 16 Mar 2024 11:00:29 +0000 (22:00 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Tue, 19 Mar 2024 01:21:41 +0000 (12:21 +1100)
commitc4845edfeaf44756ad9672e8d143f1c8f5c4c0f6
treea59c6a93508e213e51ae5f804f94578409dbb104
parent23409731ca4464471d4ef456d6aee50084d00c85
c++: Fix handling of no-linkage decls for modules

When testing the changes for PR c++/112631 we discovered that currently
we don't emit definitions of block-scope function declarations if
they're not used in the module interface TU, which causes issues if they
are used by importers.

This patch fixes the handling of no-linkage declarations for C++20. In
particular, a type declared in a function with vague linkage or declared
in a module CMI could potentially be accessible outside its defining TU,
and as such we can't assume that function declarations using that type
can never be defined in another TU.

A complication with handling this is that we're only strictly interested
in declarations with a module CMI, but when parsing the global module
fragment we don't yet know whether or not this module will have a CMI
until we reach the "export module" line (or not). Since this case is
IFNDR anyway (by [basic.def.odr] p11) we just tentatively assume while
parsing the GMF that this module will have a CMI; once we see (or don't
see) an 'export module' declaration we can commit to that knowledge for
future declarations.

gcc/cp/ChangeLog:

* cp-tree.h (module_maybe_has_cmi_p): New function.
* decl.cc (grokfndecl): Mark block-scope functions as public if
they could be visible in other TUs.
* decl2.cc (no_linkage_error): Don't error for declarations that
could be defined in other TUs since C++20. Suppress duplicate
errors from 'check_global_declaration'.
* tree.cc (no_linkage_check): In relaxed mode, don't consider
types in a module CMI to have no linkage.

gcc/testsuite/ChangeLog:

* g++.dg/cpp2a/linkage-1.C: New test.
* g++.dg/modules/block-decl-3.h: New test.
* g++.dg/modules/block-decl-3_a.C: New test.
* g++.dg/modules/block-decl-3_b.C: New test.
* g++.dg/modules/block-decl-3_c.C: New test.
* g++.dg/modules/linkage-1_a.C: New test.
* g++.dg/modules/linkage-1_b.C: New test.
* g++.dg/modules/linkage-1_c.C: New test.
* g++.dg/modules/linkage-2.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
13 files changed:
gcc/cp/cp-tree.h
gcc/cp/decl.cc
gcc/cp/decl2.cc
gcc/cp/tree.cc
gcc/testsuite/g++.dg/cpp2a/linkage-1.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/block-decl-3.h [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/block-decl-3_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/block-decl-3_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/block-decl-3_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/linkage-1_a.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/linkage-1_b.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/linkage-1_c.C [new file with mode: 0644]
gcc/testsuite/g++.dg/modules/linkage-2.C [new file with mode: 0644]