diff options
author | Nathaniel Shead <nathanieloshead@gmail.com> | 2024-03-16 22:00:29 +1100 |
---|---|---|
committer | Nathaniel Shead <nathanieloshead@gmail.com> | 2024-03-19 12:21:41 +1100 |
commit | c4845edfeaf44756ad9672e8d143f1c8f5c4c0f6 (patch) | |
tree | a59c6a93508e213e51ae5f804f94578409dbb104 /gcc/cp/tree.cc | |
parent | 23409731ca4464471d4ef456d6aee50084d00c85 (diff) | |
download | gcc-c4845edfeaf44756ad9672e8d143f1c8f5c4c0f6.zip gcc-c4845edfeaf44756ad9672e8d143f1c8f5c4c0f6.tar.gz gcc-c4845edfeaf44756ad9672e8d143f1c8f5c4c0f6.tar.bz2 |
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>
Diffstat (limited to 'gcc/cp/tree.cc')
-rw-r--r-- | gcc/cp/tree.cc | 21 |
1 files changed, 16 insertions, 5 deletions
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc index e75be9a..f1a23ff 100644 --- a/gcc/cp/tree.cc +++ b/gcc/cp/tree.cc @@ -2971,7 +2971,8 @@ verify_stmt_tree (tree t) /* Check if the type T depends on a type with no linkage and if so, return it. If RELAXED_P then do not consider a class type declared - within a vague-linkage function to have no linkage. Remember: + within a vague-linkage function or in a module CMI to have no linkage, + since it can still be accessed within a different TU. Remember: no-linkage is not the same as internal-linkage. */ tree @@ -3012,7 +3013,15 @@ no_linkage_check (tree t, bool relaxed_p) /* Only treat unnamed types as having no linkage if they're at namespace scope. This is core issue 966. */ if (TYPE_UNNAMED_P (t) && TYPE_NAMESPACE_SCOPE_P (t)) - return t; + { + if (relaxed_p + && TREE_PUBLIC (CP_TYPE_CONTEXT (t)) + && module_maybe_has_cmi_p ()) + /* This type could possibly be accessed outside this TU. */ + return NULL_TREE; + else + return t; + } for (r = CP_TYPE_CONTEXT (t); ; ) { @@ -3023,10 +3032,12 @@ no_linkage_check (tree t, bool relaxed_p) return no_linkage_check (TYPE_CONTEXT (t), relaxed_p); else if (TREE_CODE (r) == FUNCTION_DECL) { - if (!relaxed_p || !vague_linkage_p (r)) - return t; - else + if (relaxed_p + && (vague_linkage_p (r) + || (TREE_PUBLIC (r) && module_maybe_has_cmi_p ()))) r = CP_DECL_CONTEXT (r); + else + return t; } else break; |