https://gcc.gnu.org/g:0e485b8c85c31856b9f7c91015ac94874b100bd4

commit r15-7504-g0e485b8c85c31856b9f7c91015ac94874b100bd4
Author: Nathaniel Shead <nathanielosh...@gmail.com>
Date:   Wed Feb 12 23:07:43 2025 +1100

    c++: Constrain visibility for CNTTPs with internal types [PR118849]
    
    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 <nathanielosh...@gmail.com>

Diff:
---
 gcc/cp/decl2.cc                          |  4 +++-
 gcc/testsuite/g++.dg/template/linkage6.C | 13 +++++++++++++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 593dcaa4e2de..aa9ebca12f8f 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -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
index 000000000000..b86c986568f8
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/linkage6.C
@@ -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{}>();
+}

Reply via email to