public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc r14-9530] c++: Fix handling of no-linkage decls for modules
@ 2024-03-19 1:22 Nathaniel Shead
0 siblings, 0 replies; only message in thread
From: Nathaniel Shead @ 2024-03-19 1:22 UTC (permalink / raw)
To: gcc-cvs
https://gcc.gnu.org/g:c4845edfeaf44756ad9672e8d143f1c8f5c4c0f6
commit r14-9530-gc4845edfeaf44756ad9672e8d143f1c8f5c4c0f6
Author: Nathaniel Shead <nathanieloshead@gmail.com>
Date: Sat Mar 16 22:00:29 2024 +1100
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>
Diff:
---
gcc/cp/cp-tree.h | 6 +
gcc/cp/decl.cc | 10 +-
gcc/cp/decl2.cc | 39 ++++++-
gcc/cp/tree.cc | 21 +++-
gcc/testsuite/g++.dg/cpp2a/linkage-1.C | 18 +++
gcc/testsuite/g++.dg/modules/block-decl-3.h | 39 +++++++
gcc/testsuite/g++.dg/modules/block-decl-3_a.C | 157 ++++++++++++++++++++++++++
gcc/testsuite/g++.dg/modules/block-decl-3_b.C | 8 ++
gcc/testsuite/g++.dg/modules/block-decl-3_c.C | 30 +++++
gcc/testsuite/g++.dg/modules/linkage-1_a.C | 15 +++
gcc/testsuite/g++.dg/modules/linkage-1_b.C | 6 +
gcc/testsuite/g++.dg/modules/linkage-1_c.C | 9 ++
gcc/testsuite/g++.dg/modules/linkage-2.C | 26 +++++
13 files changed, 372 insertions(+), 12 deletions(-)
diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h
index 05913861e06..52d53589e51 100644
--- a/gcc/cp/cp-tree.h
+++ b/gcc/cp/cp-tree.h
@@ -7384,6 +7384,12 @@ inline bool named_module_purview_p ()
inline bool named_module_attach_p ()
{ return named_module_p () && module_attach_p (); }
+/* We don't know if this TU will have a CMI while parsing the GMF,
+ so tentatively assume that it might, for the purpose of determining
+ whether no-linkage decls could be used by an importer. */
+inline bool module_maybe_has_cmi_p ()
+{ return module_has_cmi_p () || (named_module_p () && !module_purview_p ()); }
+
/* We're currently exporting declarations. */
inline bool module_exporting_p ()
{ return module_kind & MK_EXPORTING; }
diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 7a97b867199..65ab64885ff 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -10756,9 +10756,15 @@ grokfndecl (tree ctype,
/* Members of anonymous types and local classes have no linkage; make
them internal. If a typedef is made later, this will be changed. */
- if (ctype && (!TREE_PUBLIC (TYPE_MAIN_DECL (ctype))
- || decl_function_context (TYPE_MAIN_DECL (ctype))))
+ if (ctype && !TREE_PUBLIC (TYPE_MAIN_DECL (ctype)))
publicp = 0;
+ else if (ctype && decl_function_context (TYPE_MAIN_DECL (ctype)))
+ /* But members of local classes in a module CMI should have their
+ definitions exported, in case they are (directly or indirectly)
+ used by an importer. We don't just use module_has_cmi_p here
+ because for entities in the GMF we don't yet know whether this
+ module will have a CMI, so we'll conservatively assume it might. */
+ publicp = module_maybe_has_cmi_p ();
if (publicp && cxx_dialect == cxx98)
{
diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 6c9fd415d40..2562d8aeff6 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -4696,8 +4696,19 @@ no_linkage_error (tree decl)
bool d = false;
auto_diagnostic_group grp;
if (cxx_dialect >= cxx11)
- d = permerror (DECL_SOURCE_LOCATION (decl), "%q#D, declared using "
- "unnamed type, is used but never defined", decl);
+ {
+ /* If t is declared in a module CMI, then decl could actually
+ be defined in a different TU, so don't warn since C++20. */
+ tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
+ if (relaxed != NULL_TREE)
+ d = permerror (DECL_SOURCE_LOCATION (decl),
+ "%q#D, declared using an unnamed type, "
+ "is used but never defined", decl);
+ else if (cxx_dialect < cxx20)
+ d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
+ "%q#D, declared using an unnamed type, "
+ "is used but not defined", decl);
+ }
else if (DECL_EXTERN_C_P (decl))
/* Allow this; it's pretty common in C. */;
else if (VAR_P (decl))
@@ -4716,13 +4727,31 @@ no_linkage_error (tree decl)
inform (DECL_SOURCE_LOCATION (TYPE_NAME (t)), "%q#D does not refer "
"to the unqualified type, so it is not used for linkage",
TYPE_NAME (t));
+ /* Suppress warning from check_global_declaration if needed. */
+ if (d)
+ suppress_warning (decl, OPT_Wunused);
}
else if (cxx_dialect >= cxx11)
{
if (VAR_P (decl) || !DECL_PURE_VIRTUAL_P (decl))
- permerror (DECL_SOURCE_LOCATION (decl),
- "%q#D, declared using local type "
- "%qT, is used but never defined", decl, t);
+ {
+ /* Similarly for local types in a function with vague linkage or
+ defined in a module CMI, then decl could actually be defined
+ in a different TU, so don't warn since C++20. */
+ bool d = false;
+ tree relaxed = no_linkage_check (t, /*relaxed_p=*/true);
+ if (relaxed != NULL_TREE)
+ d = permerror (DECL_SOURCE_LOCATION (decl),
+ "%q#D, declared using local type "
+ "%qT, is used but never defined", decl, t);
+ else if (cxx_dialect < cxx20)
+ d = pedwarn (DECL_SOURCE_LOCATION (decl), OPT_Wc__20_extensions,
+ "%q#D, declared using local type "
+ "%qT, is used but not defined here", decl, t);
+ /* Suppress warning from check_global_declaration if needed. */
+ if (d)
+ suppress_warning (decl, OPT_Wunused);
+ }
}
else if (VAR_P (decl))
warning_at (DECL_SOURCE_LOCATION (decl), 0, "type %qT with no linkage "
diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index e75be9a4e66..f1a23ffe817 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;
diff --git a/gcc/testsuite/g++.dg/cpp2a/linkage-1.C b/gcc/testsuite/g++.dg/cpp2a/linkage-1.C
new file mode 100644
index 00000000000..888ed6fa5b5
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/linkage-1.C
@@ -0,0 +1,18 @@
+// { dg-do compile { target c++11 } }
+
+inline auto f() {
+ struct A {};
+ return A{};
+}
+decltype(f()) a(); // { dg-error "used but not defined" "" { target c++17_down } }
+
+auto g() {
+ struct A {};
+ return A{};
+}
+decltype(g()) b(); // { dg-error "used but never defined" }
+
+int main() {
+ a();
+ b();
+}
diff --git a/gcc/testsuite/g++.dg/modules/block-decl-3.h b/gcc/testsuite/g++.dg/modules/block-decl-3.h
new file mode 100644
index 00000000000..4b155eb0054
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/block-decl-3.h
@@ -0,0 +1,39 @@
+// GMF
+
+// Non-inline function definitions in headers are a recipe for ODR violations,
+// but we should probably support that anyway as its not inherently wrong
+// if only ever included into the GMF of a single module.
+
+auto gmf_n_i() {
+ struct X { void f() {} };
+ return X{};
+}
+
+inline auto gmf_i_i() {
+ struct X { void f() {} };
+ return X{};
+}
+
+auto gmf_n_i_i() {
+ struct X {
+ auto f() {
+ struct Y {
+ void g() {}
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
+
+inline auto gmf_i_i_i() {
+ struct X {
+ auto f() {
+ struct Y {
+ void g() {}
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
diff --git a/gcc/testsuite/g++.dg/modules/block-decl-3_a.C b/gcc/testsuite/g++.dg/modules/block-decl-3_a.C
new file mode 100644
index 00000000000..8cb4dde74d1
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/block-decl-3_a.C
@@ -0,0 +1,157 @@
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi mod }
+
+// Test that we can link various forms of local class functions.
+// Function names use i=inline, n=non-inline, for each nesting.
+
+module;
+#include "block-decl-3.h"
+
+export module mod;
+
+namespace {
+ void internal() {}
+}
+
+// singly-nested
+
+export auto n_n() {
+ internal();
+ struct X { void f() { internal(); } };
+ return X{};
+}
+
+export auto n_i() {
+ internal();
+ struct X { inline void f() {} };
+ return X{};
+}
+
+export inline auto i_n() {
+ // `f` is not inline here, so this is OK
+ struct X { void f() { internal(); } };
+ return X{};
+}
+
+export inline auto i_i() {
+ struct X { inline void f() {} };
+ return X{};
+}
+
+
+// doubly nested
+
+export auto n_n_n() {
+ struct X {
+ auto f() {
+ struct Y {
+ void g() { internal(); }
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
+
+export auto n_i_n() {
+ struct X {
+ inline auto f() {
+ struct Y {
+ void g() { internal(); }
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
+
+export inline auto i_n_i() {
+ struct X {
+ auto f() {
+ struct Y {
+ inline void g() {}
+ };
+ return Y {};
+ }
+ };
+ return X{};
+}
+
+export inline auto i_i_i() {
+ struct X {
+ inline auto f() {
+ struct Y {
+ inline void g() {}
+ };
+ return Y{};
+ }
+ };
+ return X{};
+}
+
+
+// multiple types
+
+export auto multi_n_n() {
+ struct X {
+ void f() { internal(); }
+ };
+ struct Y {
+ X x;
+ };
+ return Y {};
+}
+
+export auto multi_n_i() {
+ struct X {
+ inline void f() {}
+ };
+ struct Y {
+ X x;
+ };
+ return Y {};
+}
+
+export inline auto multi_i_i() {
+ struct X {
+ inline void f() {}
+ };
+ struct Y {
+ X x;
+ };
+ return Y {};
+};
+
+
+// extern "C++"
+
+export extern "C++" auto extern_n_i() {
+ struct X {
+ void f() {} // implicitly inline
+ };
+ return X{};
+};
+
+export extern "C++" inline auto extern_i_i() {
+ struct X {
+ void f() {}
+ };
+ return X{};
+};
+
+
+// GMF
+
+export using ::gmf_n_i;
+export using ::gmf_i_i;
+export using ::gmf_n_i_i;
+export using ::gmf_i_i_i;
+
+
+// can access from implementation unit
+
+auto only_used_in_impl() {
+ struct X { void f() {} };
+ return X{};
+}
+export void test_from_impl_unit();
diff --git a/gcc/testsuite/g++.dg/modules/block-decl-3_b.C b/gcc/testsuite/g++.dg/modules/block-decl-3_b.C
new file mode 100644
index 00000000000..bc9b2a213f0
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/block-decl-3_b.C
@@ -0,0 +1,8 @@
+// { dg-additional-options "-fmodules-ts" }
+
+module mod;
+
+// Test that we can access (and link) to declarations from the interface
+void test_from_impl_unit() {
+ only_used_in_impl().f();
+}
diff --git a/gcc/testsuite/g++.dg/modules/block-decl-3_c.C b/gcc/testsuite/g++.dg/modules/block-decl-3_c.C
new file mode 100644
index 00000000000..5b39e038327
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/block-decl-3_c.C
@@ -0,0 +1,30 @@
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import mod;
+
+int main() {
+ n_n().f();
+ n_i().f();
+ i_n().f();
+ i_i().f();
+
+ n_n_n().f().g();
+ n_i_n().f().g();
+ i_n_i().f().g();
+ i_i_i().f().g();
+
+ multi_n_n().x.f();
+ multi_n_i().x.f();
+ multi_i_i().x.f();
+
+ extern_n_i().f();
+ extern_i_i().f();
+
+ gmf_n_i().f();
+ gmf_i_i().f();
+ gmf_n_i_i().f().g();
+ gmf_i_i_i().f().g();
+
+ test_from_impl_unit();
+}
diff --git a/gcc/testsuite/g++.dg/modules/linkage-1_a.C b/gcc/testsuite/g++.dg/modules/linkage-1_a.C
new file mode 100644
index 00000000000..750e31ff347
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/linkage-1_a.C
@@ -0,0 +1,15 @@
+// { dg-additional-options "-fmodules-ts -Wno-error=c++20-extensions" }
+// { dg-module-cmi M }
+
+export module M;
+
+auto f() {
+ struct A {};
+ return A{};
+}
+decltype(f()) g(); // { dg-warning "used but not defined" "" { target c++17_down } }
+export auto x = g();
+
+struct {} s;
+decltype(s) h(); // { dg-warning "used but not defined" "" { target c++17_down } }
+export auto y = h();
diff --git a/gcc/testsuite/g++.dg/modules/linkage-1_b.C b/gcc/testsuite/g++.dg/modules/linkage-1_b.C
new file mode 100644
index 00000000000..f23962d76b7
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/linkage-1_b.C
@@ -0,0 +1,6 @@
+// { dg-additional-options "-fmodules-ts" }
+
+module M;
+
+decltype(f()) g() { return {}; }
+decltype(s) h() { return {}; }
diff --git a/gcc/testsuite/g++.dg/modules/linkage-1_c.C b/gcc/testsuite/g++.dg/modules/linkage-1_c.C
new file mode 100644
index 00000000000..f1406b99032
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/linkage-1_c.C
@@ -0,0 +1,9 @@
+// { dg-module-do link }
+// { dg-additional-options "-fmodules-ts" }
+
+import M;
+
+int main() {
+ auto a = x;
+ auto b = y;
+}
diff --git a/gcc/testsuite/g++.dg/modules/linkage-2.C b/gcc/testsuite/g++.dg/modules/linkage-2.C
new file mode 100644
index 00000000000..eb4d7b051af
--- /dev/null
+++ b/gcc/testsuite/g++.dg/modules/linkage-2.C
@@ -0,0 +1,26 @@
+// { dg-additional-options "-fmodules-ts" }
+// { dg-module-cmi !M }
+
+export module M;
+
+// Same as a linkage-1 except within an anonymous namespace;
+// now these declarations cannot possibly be defined outside this TU,
+// so we should error.
+
+namespace {
+ auto f() {
+ struct A {};
+ return A{};
+ }
+ decltype(f()) g(); // { dg-error "used but never defined" }
+
+ struct {} s;
+ decltype(s) h(); // { dg-error "used but never defined" }
+}
+
+export void use() {
+ g();
+ h();
+}
+
+// { dg-prune-output "not writing module" }
^ permalink raw reply [flat|nested] only message in thread
only message in thread, other threads:[~2024-03-19 1:22 UTC | newest]
Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-03-19 1:22 [gcc r14-9530] c++: Fix handling of no-linkage decls for modules Nathaniel Shead
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).