]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
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)
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]

index 05913861e06e10087b744217f45beeb63f5aa28d..52d53589e51d7970805a7c9dd9f9b120a9e52c65 100644 (file)
@@ -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; }
index 7a97b867199190b5a708195c64bae1aafbfb8966..65ab64885ff8bbf85c14eed8a7ae572c3fcb73cf 100644 (file)
@@ -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)
     {
index 6c9fd415d40e6163e7523931cd9279ad31e188c9..2562d8aeff6e1909efa1d637f8fc3dfef8ebb5a1 100644 (file)
@@ -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 "
index e75be9a4e665e73ff89e0117e348248842cb35b3..f1a23ffe817950570d3b36f2460b9c32d64d49dd 100644 (file)
@@ -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 (file)
index 0000000..888ed6f
--- /dev/null
@@ -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 (file)
index 0000000..4b155eb
--- /dev/null
@@ -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 (file)
index 0000000..8cb4dde
--- /dev/null
@@ -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 (file)
index 0000000..bc9b2a2
--- /dev/null
@@ -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 (file)
index 0000000..5b39e03
--- /dev/null
@@ -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 (file)
index 0000000..750e31f
--- /dev/null
@@ -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 (file)
index 0000000..f23962d
--- /dev/null
@@ -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 (file)
index 0000000..f1406b9
--- /dev/null
@@ -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 (file)
index 0000000..eb4d7b0
--- /dev/null
@@ -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" }