]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: Constrain visibility for CNTTPs with internal types [PR118849]
authorNathaniel Shead <nathanieloshead@gmail.com>
Wed, 12 Feb 2025 12:07:43 +0000 (23:07 +1100)
committerNathaniel Shead <nathanieloshead@gmail.com>
Thu, 13 Feb 2025 11:32:15 +0000 (22:32 +1100)
While looking into PR118846 I noticed that we don't currently constrain
the linkage of functions involving CNTTPs of internal-linkage types.  It
seems to me that this would be sensible to do.

PR c++/118849

gcc/cp/ChangeLog:

* decl2.cc (min_vis_expr_r): Constrain visibility according to
the type of decl_constant_var_p decls.

gcc/testsuite/ChangeLog:

* g++.dg/template/linkage6.C: New test.

Signed-off-by: Nathaniel Shead <nathanieloshead@gmail.com>
gcc/cp/decl2.cc
gcc/testsuite/g++.dg/template/linkage6.C [new file with mode: 0644]

index 593dcaa4e2de332024858cc91151151a31c3c6c6..aa9ebca12f8fc26a08f5759b887c9802db2627dc 100644 (file)
@@ -2814,7 +2814,9 @@ min_vis_expr_r (tree *tp, int */*walk_subtrees*/, void *data)
        /* The ODR allows definitions in different TUs to refer to distinct
           constant variables with internal or no linkage, so such a reference
           shouldn't affect visibility (PR110323).  FIXME but only if the
-          lvalue-rvalue conversion is applied.  */;
+          lvalue-rvalue conversion is applied.  We still want to restrict
+          visibility according to the type of the declaration however.  */
+       tpvis = type_visibility (TREE_TYPE (t));
       else if (! TREE_PUBLIC (t))
        tpvis = VISIBILITY_ANON;
       else
diff --git a/gcc/testsuite/g++.dg/template/linkage6.C b/gcc/testsuite/g++.dg/template/linkage6.C
new file mode 100644 (file)
index 0000000..b86c986
--- /dev/null
@@ -0,0 +1,13 @@
+// PR c++/118849
+// { dg-do compile { target c++20 } }
+// { dg-final { scan-assembler-not "(weak|glob)\[^\n\]*_Z" { xfail powerpc-*-aix* } } }
+
+namespace {
+  struct A {};
+}
+
+template <auto Q> void f() { }
+
+int main() {
+  f<A{}>();
+}