]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++/modules: Fix treatment of unnamed types
authorNathaniel Shead <nathanieloshead@gmail.com>
Thu, 23 May 2024 12:50:58 +0000 (22:50 +1000)
committerNathaniel Shead <nathanieloshead@gmail.com>
Fri, 24 May 2024 14:10:41 +0000 (00:10 +1000)
In r14-9530 we relaxed "depending on type with no-linkage" errors for
declarations that could actually be accessed from different TUs anyway.
However, this also enabled it for unnamed types, which never work.

In a normal module interface, an unnamed type is TU-local by
[basic.link] p15.2, and so cannot be exposed or the program is
ill-formed.  We don't yet implement this checking but we should assume
that we will later; currently supporting this actually causes ICEs when
attempting to create the mangled name in some situations.

For a header unit, by [module.import] p5.3 it is unspecified whether two
TUs importing a header unit providing such a declaration are importing
the same header unit.  In this case, we would require name mangling
changes to somehow allow the (anonymous) type exported by such a header
unit to correspond across different TUs in the presence of other
anonymous declarations, so for this patch just assume that this case
would be an ODR violation instead.

gcc/cp/ChangeLog:

* tree.cc (no_linkage_check): Anonymous types can't be accessed
in a different TU.

gcc/testsuite/ChangeLog:

* g++.dg/modules/linkage-1_a.C: Remove anonymous type test.
* g++.dg/modules/linkage-1_b.C: Likewise.
* g++.dg/modules/linkage-1_c.C: Likewise.
* g++.dg/modules/linkage-2.C: Add note about anonymous types.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
Reviewed-by: Jason Merrill <jason@redhat.com>
gcc/cp/tree.cc
gcc/testsuite/g++.dg/modules/linkage-1_a.C
gcc/testsuite/g++.dg/modules/linkage-1_b.C
gcc/testsuite/g++.dg/modules/linkage-1_c.C
gcc/testsuite/g++.dg/modules/linkage-2.C

index 0485a618c6c193d0837d1071a26facce9c1b0a83..fe3f034d0004cfed3a2a9eb27ed041fc28620d48 100644 (file)
@@ -2988,15 +2988,7 @@ 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))
-       {
-         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;
-       }
+       return t;
 
       for (r = CP_TYPE_CONTEXT (t); ; )
        {
index 750e31ff347f88f4bb0aa01b32f082affd59d9de..1d81312e94f9ddfaca6545823aa2b9f7086c3812 100644 (file)
@@ -9,7 +9,3 @@ auto f() {
 }
 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();
index f23962d76b778fb0b7feaef314eaa4fb98f75923..5bc0d1b888dd69573e6a2f2e2a16c4612f143d2e 100644 (file)
@@ -3,4 +3,3 @@
 module M;
 
 decltype(f()) g() { return {}; }
-decltype(s) h() { return {}; }
index f1406b99032da8b93e612f4747c66452a54f7c15..9ff1491b67eb6f4bdeab71e25a77986638217a5c 100644 (file)
@@ -5,5 +5,4 @@ import M;
 
 int main() {
   auto a = x;
-  auto b = y;
 }
index eb4d7b051af251d5dc2fdcf8f734f3b285f6fac0..d913d6a30fcea7c310db2bda75337d18c169a1df 100644 (file)
@@ -23,4 +23,10 @@ export void use() {
   h();
 }
 
+// Additionally, unnamed types have no linkage but are also TU-local, and thus
+// cannot be exposed in a module interface unit.  The non-TU-local entity 's'
+// here is an exposure of this type, so this should be an error; we don't yet
+// implement this checking however.
+struct {} s;  // { dg-error "TU-local" "" { xfail *-*-* } }
+
 // { dg-prune-output "not writing module" }