c++: Constrain visibility for CNTTPs with internal types [PR118849]

Message ID 67ac926e.a70a0220.d3b54.70f3@mx.google.com
State New
Headers
Series c++: Constrain visibility for CNTTPs with internal types [PR118849] |

Checks

Context Check Description
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-arm-bootstrap success Build passed
linaro-tcwg-bot/tcwg_gcc_build--master-aarch64 success Build passed
linaro-tcwg-bot/tcwg_gcc_check--master-aarch64 success Test passed
linaro-tcwg-bot/tcwg_gcc_build--master-arm success Build passed
linaro-tcwg-bot/tcwg_simplebootstrap_build--master-aarch64-bootstrap success Build passed

Commit Message

Nathaniel Shead Feb. 12, 2025, 12:22 p.m. UTC
  Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk/14?

-- >8 --

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                          |  4 +++-
 gcc/testsuite/g++.dg/template/linkage6.C | 12 ++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/template/linkage6.C
  

Comments

Jason Merrill Feb. 12, 2025, 11:32 p.m. UTC | #1
On 2/12/25 1:22 PM, Nathaniel Shead wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, OK for trunk/14?

OK.

> -- >8 --
> 
> 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                          |  4 +++-
>   gcc/testsuite/g++.dg/template/linkage6.C | 12 ++++++++++++
>   2 files changed, 15 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/template/linkage6.C
> 
> diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
> index 4415cea93e0..9a76e00dcde 100644
> --- a/gcc/cp/decl2.cc
> +++ b/gcc/cp/decl2.cc
> @@ -2820,7 +2820,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 00000000000..fb589f67874
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/linkage6.C
> @@ -0,0 +1,12 @@
> +// { 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{}>();
> +}
  

Patch

diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 4415cea93e0..9a76e00dcde 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -2820,7 +2820,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 00000000000..fb589f67874
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/linkage6.C
@@ -0,0 +1,12 @@ 
+// { 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{}>();
+}