diff mbox series

c++: Don't ICE to build private access error message [PR116323]

Message ID 01020191e0d5e45f-9128c7af-14e2-4e73-9f0d-2265af9c6c54-000000@eu-west-1.amazonses.com
State New
Headers show
Series c++: Don't ICE to build private access error message [PR116323] | expand

Commit Message

Simon Martin Sept. 11, 2024, 11:26 a.m. UTC
We currently ICE upon the following code while building the "[...] is
private within this context" error message

=== cut here ===
class A { enum Enum{}; };
template<typename E, template<typename> class Alloc>
class B : private Alloc<E>, private A {};
template<typename E, template<typename> class Alloc>
int B<E, Alloc>::foo (Enum m) { return 42; }
=== cut here ===

The problem is that since r11-6880, after detecting that Enum cannot be
accessed in B, enforce_access will access the TYPE_BINFO of all the
bases of B, which ICEs for any that is a BOUND_TEMPLATE_TEMPLATE_PARM.
This patch simply skips such bases.

Successfully tested on x86_64-pc-linux-gnu.

	PR c++/116323

gcc/cp/ChangeLog:

	* search.cc (get_parent_with_private_access): Only call access_in_type
	for RECORD_OR_UNION_TYPE_P base BINFOs.

gcc/testsuite/ChangeLog:

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

---
 gcc/cp/search.cc                         |  4 +++-
 gcc/testsuite/g++.dg/template/access43.C | 11 +++++++++++
 2 files changed, 14 insertions(+), 1 deletion(-)
 create mode 100644 gcc/testsuite/g++.dg/template/access43.C

Comments

Jason Merrill Sept. 11, 2024, 6:57 p.m. UTC | #1
On 9/11/24 7:26 AM, Simon Martin wrote:
> We currently ICE upon the following code while building the "[...] is
> private within this context" error message
> 
> === cut here ===
> class A { enum Enum{}; };
> template<typename E, template<typename> class Alloc>
> class B : private Alloc<E>, private A {};
> template<typename E, template<typename> class Alloc>
> int B<E, Alloc>::foo (Enum m) { return 42; }
> === cut here ===
> 
> The problem is that since r11-6880, after detecting that Enum cannot be
> accessed in B, enforce_access will access the TYPE_BINFO of all the
> bases of B, which ICEs for any that is a BOUND_TEMPLATE_TEMPLATE_PARM.
> This patch simply skips such bases.
> 
> Successfully tested on x86_64-pc-linux-gnu.
> 
> 	PR c++/116323
> 
> gcc/cp/ChangeLog:
> 
> 	* search.cc (get_parent_with_private_access): Only call access_in_type
> 	for RECORD_OR_UNION_TYPE_P base BINFOs.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/access43.C: New test.
> 
> ---
>   gcc/cp/search.cc                         |  4 +++-
>   gcc/testsuite/g++.dg/template/access43.C | 11 +++++++++++
>   2 files changed, 14 insertions(+), 1 deletion(-)
>   create mode 100644 gcc/testsuite/g++.dg/template/access43.C
> 
> diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc
> index 60c30ecb881..a810cf70d6a 100644
> --- a/gcc/cp/search.cc
> +++ b/gcc/cp/search.cc
> @@ -163,9 +163,11 @@ get_parent_with_private_access (tree decl, tree binfo)
>     /* Iterate through immediate parent classes.  */
>     for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
>       {
> +      tree base_binfo_type = BINFO_TYPE (base_binfo);
>         /* This parent had private access.  Therefore that's why BINFO can't
>   	  access DECL.  */
> -      if (access_in_type (BINFO_TYPE (base_binfo), decl) == ak_private)
> +      if (RECORD_OR_UNION_TYPE_P (base_binfo_type)

You might add to the comment to explain that in a template the base list 
can also contain WILDCARD_TYPE_P types.  OK either way.

Jason
Simon Martin Sept. 12, 2024, 8:25 a.m. UTC | #2
On 11 Sep 2024, at 20:57, Jason Merrill wrote:

> On 9/11/24 7:26 AM, Simon Martin wrote:
>> We currently ICE upon the following code while building the "[...] is
>> private within this context" error message
>>
>> === cut here ===
>> class A { enum Enum{}; };
>> template<typename E, template<typename> class Alloc>
>> class B : private Alloc<E>, private A {};
>> template<typename E, template<typename> class Alloc>
>> int B<E, Alloc>::foo (Enum m) { return 42; }
>> === cut here ===
>>
>> The problem is that since r11-6880, after detecting that Enum cannot 
>> be
>> accessed in B, enforce_access will access the TYPE_BINFO of all the
>> bases of B, which ICEs for any that is a 
>> BOUND_TEMPLATE_TEMPLATE_PARM.
>> This patch simply skips such bases.
>>
>> Successfully tested on x86_64-pc-linux-gnu.
>>
>> 	PR c++/116323
>>
>> gcc/cp/ChangeLog:
>>
>> 	* search.cc (get_parent_with_private_access): Only call 
>> access_in_type
>> 	for RECORD_OR_UNION_TYPE_P base BINFOs.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* g++.dg/template/access43.C: New test.
>>
>> ---
>>   gcc/cp/search.cc                         |  4 +++-
>>   gcc/testsuite/g++.dg/template/access43.C | 11 +++++++++++
>>   2 files changed, 14 insertions(+), 1 deletion(-)
>>   create mode 100644 gcc/testsuite/g++.dg/template/access43.C
>>
>> diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc
>> index 60c30ecb881..a810cf70d6a 100644
>> --- a/gcc/cp/search.cc
>> +++ b/gcc/cp/search.cc
>> @@ -163,9 +163,11 @@ get_parent_with_private_access (tree decl, tree 
>> binfo)
>>     /* Iterate through immediate parent classes.  */
>>     for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
>>       {
>> +      tree base_binfo_type = BINFO_TYPE (base_binfo);
>>         /* This parent had private access.  Therefore that's why 
>> BINFO can't
>>   	  access DECL.  */
>> -      if (access_in_type (BINFO_TYPE (base_binfo), decl) == 
>> ak_private)
>> +      if (RECORD_OR_UNION_TYPE_P (base_binfo_type)
>
> You might add to the comment to explain that in a template the base 
> list can also contain WILDCARD_TYPE_P types.  OK either way.
Thanks Jason. Pushed with the suggested extra comment as r15-3598.

Simon
diff mbox series

Patch

diff --git a/gcc/cp/search.cc b/gcc/cp/search.cc
index 60c30ecb881..a810cf70d6a 100644
--- a/gcc/cp/search.cc
+++ b/gcc/cp/search.cc
@@ -163,9 +163,11 @@  get_parent_with_private_access (tree decl, tree binfo)
   /* Iterate through immediate parent classes.  */
   for (int i = 0; BINFO_BASE_ITERATE (binfo, i, base_binfo); i++)
     {
+      tree base_binfo_type = BINFO_TYPE (base_binfo);
       /* This parent had private access.  Therefore that's why BINFO can't
 	  access DECL.  */
-      if (access_in_type (BINFO_TYPE (base_binfo), decl) == ak_private)
+      if (RECORD_OR_UNION_TYPE_P (base_binfo_type)
+	  && access_in_type (base_binfo_type, decl) == ak_private)
 	return base_binfo;
     }
 
diff --git a/gcc/testsuite/g++.dg/template/access43.C b/gcc/testsuite/g++.dg/template/access43.C
new file mode 100644
index 00000000000..ce9e6c8fbb2
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/access43.C
@@ -0,0 +1,11 @@ 
+// PR c++/116323
+// { dg-do "compile" }
+// { dg-additional-options "-Wno-template-body" }
+
+class A { enum Enum{}; };
+
+template<typename E, template<typename> class Alloc>
+class B : private Alloc<E>, private A {};
+
+template<typename E, template<typename> class Alloc>
+int B<E, Alloc>::foo (Enum m) { return 42; } // { dg-error "is private" }