c++: ICE with -Wsubobject-linkage and function type [PR127672]

Message ID 20260930193420.1104915-1-polacek@redhat.com
State Committed
Commit 5f9f8711bf791130fd30bc1f04b2097f3ef25f0b
Headers
Series c++: ICE with -Wsubobject-linkage and function type [PR127672] |

Checks

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

Commit Message

Marek Polacek Sept. 30, 2026, 7:34 p.m. UTC
  Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/branches?

-- >8 --
Here we crash in decl_namespace_context because function types
(or pointer-to-data-member types) like void() have no TYPE_MAIN_DECL:

         decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));

We got there from the constrain_class_visibility TYPE_FIELDS walk
which emits the -Wsubobject-linkage warning.  And this test has:

  namespace { struct P { }; }
  struct S { void (*f) (P *); };

I'm using OVERLOAD_TYPE_P, inspired by min_vis_r.

	PR c++/127672

gcc/cp/ChangeLog:

	* decl2.cc (constrain_class_visibility): Check OVERLOAD_TYPE_P
	before calling decl_anon_ns_mem_p.

gcc/testsuite/ChangeLog:

	* g++.dg/warn/Wsubobject-linkage-6.C: New test.
---
 gcc/cp/decl2.cc                                  |  1 +
 gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C | 10 ++++++++++
 2 files changed, 11 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C


base-commit: f7094fe5ff882f0c206d51ebe93f33f0193ce636
  

Comments

Jason Merrill Oct. 3, 2026, 7:51 a.m. UTC | #1
On 9/30/26 9:34 PM, Marek Polacek wrote:
> Bootstrapped/regtested on x86_64-pc-linux-gnu, ok for trunk/branches?

OK.
> -- >8 --
> Here we crash in decl_namespace_context because function types
> (or pointer-to-data-member types) like void() have no TYPE_MAIN_DECL:
> 
>           decl = CP_DECL_CONTEXT (TYPE_MAIN_DECL (decl));
> 
> We got there from the constrain_class_visibility TYPE_FIELDS walk
> which emits the -Wsubobject-linkage warning.  And this test has:
> 
>    namespace { struct P { }; }
>    struct S { void (*f) (P *); };
> 
> I'm using OVERLOAD_TYPE_P, inspired by min_vis_r.
> 
> 	PR c++/127672
> 
> gcc/cp/ChangeLog:
> 
> 	* decl2.cc (constrain_class_visibility): Check OVERLOAD_TYPE_P
> 	before calling decl_anon_ns_mem_p.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/warn/Wsubobject-linkage-6.C: New test.
> ---
>   gcc/cp/decl2.cc                                  |  1 +
>   gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C | 10 ++++++++++
>   2 files changed, 11 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C
> 
> diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
> index 7fe567f98db..563fae5db0d 100644
> --- a/gcc/cp/decl2.cc
> +++ b/gcc/cp/decl2.cc
> @@ -3598,6 +3598,7 @@ constrain_class_visibility (tree type)
>   			       type, t, nlt);
>   		  }
>   		else if (cxx_dialect > cxx98
> +			 && OVERLOAD_TYPE_P (ftype)
>   			 && !decl_anon_ns_mem_p (ftype))
>   		  warning (OPT_Wsubobject_linkage, "\
>   %qT has a field %q#D whose type has internal linkage",
> diff --git a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C
> new file mode 100644
> index 00000000000..ea77f14386f
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C
> @@ -0,0 +1,10 @@
> +// PR c++/127672
> +// { dg-do compile { target c++11 } }
> +
> +namespace { struct P { }; }
> +
> +#line 7 "foo.C"
> +struct S { void (*f) (P *); };	  // { dg-warning "uses the anonymous namespace" }
> +struct S2 { void (P::*m) (); };	  // { dg-warning "uses the anonymous namespace" }
> +struct S3 { P (*g) (); };	  // { dg-warning "uses the anonymous namespace" }
> +struct S4 { int P::*d; };	  // { dg-warning "uses the anonymous namespace" }
> 
> base-commit: f7094fe5ff882f0c206d51ebe93f33f0193ce636
  

Patch

diff --git a/gcc/cp/decl2.cc b/gcc/cp/decl2.cc
index 7fe567f98db..563fae5db0d 100644
--- a/gcc/cp/decl2.cc
+++ b/gcc/cp/decl2.cc
@@ -3598,6 +3598,7 @@  constrain_class_visibility (tree type)
 			       type, t, nlt);
 		  }
 		else if (cxx_dialect > cxx98
+			 && OVERLOAD_TYPE_P (ftype)
 			 && !decl_anon_ns_mem_p (ftype))
 		  warning (OPT_Wsubobject_linkage, "\
 %qT has a field %q#D whose type has internal linkage",
diff --git a/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C
new file mode 100644
index 00000000000..ea77f14386f
--- /dev/null
+++ b/gcc/testsuite/g++.dg/warn/Wsubobject-linkage-6.C
@@ -0,0 +1,10 @@ 
+// PR c++/127672
+// { dg-do compile { target c++11 } }
+
+namespace { struct P { }; }
+
+#line 7 "foo.C"
+struct S { void (*f) (P *); };	  // { dg-warning "uses the anonymous namespace" }
+struct S2 { void (P::*m) (); };	  // { dg-warning "uses the anonymous namespace" }
+struct S3 { P (*g) (); };	  // { dg-warning "uses the anonymous namespace" }
+struct S4 { int P::*d; };	  // { dg-warning "uses the anonymous namespace" }