diff mbox series

c++: Make sure fold_sizeof_expr returns the correct type [PR117775]

Message ID 0102019378575fe9-366c9ac9-4829-4fb2-b6d5-f34501a9df8a-000000@eu-west-1.amazonses.com
State New
Headers show
Series c++: Make sure fold_sizeof_expr returns the correct type [PR117775] | expand

Commit Message

Simon Martin Nov. 29, 2024, 2:33 p.m. UTC
We currently ICE upon the following code, that is valid under
-Wno-pointer-arith:

=== cut here ===
int main() {
  decltype( [](auto) { return sizeof(void); } ) x;
  return x.operator()(0);
}
=== cut here ===

The problem is that "fold_sizeof_expr (sizeof(void))" returns
size_one_node, that has a different TREE_TYPE from that of the sizeof
expression, which later triggers an assert in cxx_eval_store_expression.

This patch makes sure that fold_sizeof_expr always returns a tree with
the type requested.

Successfully tested on x86_64-pc-linux-gnu.

	PR c++/117775

gcc/cp/ChangeLog:

	* decl.cc (fold_sizeof_expr): Make sure the folded result has
	the requested TREE_TYPE.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/constexpr-117775.C: New test.

---
 gcc/cp/decl.cc                                |  1 +
 gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C | 13 +++++++++++++
 2 files changed, 14 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C

Comments

Simon Martin Dec. 11, 2024, 9:27 p.m. UTC | #1
Hi,

On 29 Nov 2024, at 15:33, Simon Martin wrote:

> We currently ICE upon the following code, that is valid under
> -Wno-pointer-arith:
>
> === cut here ===
> int main() {
>   decltype( [](auto) { return sizeof(void); } ) x;
>   return x.operator()(0);
> }
> === cut here ===
>
> The problem is that "fold_sizeof_expr (sizeof(void))" returns
> size_one_node, that has a different TREE_TYPE from that of the sizeof
> expression, which later triggers an assert in 
> cxx_eval_store_expression.
>
> This patch makes sure that fold_sizeof_expr always returns a tree with
> the type requested.
>
> Successfully tested on x86_64-pc-linux-gnu.
Ping.

Thanks!
   Simon
>
> 	PR c++/117775
>
> gcc/cp/ChangeLog:
>
> 	* decl.cc (fold_sizeof_expr): Make sure the folded result has
> 	the requested TREE_TYPE.
>
> gcc/testsuite/ChangeLog:
>
> 	* g++.dg/cpp2a/constexpr-117775.C: New test.
>
> ---
>  gcc/cp/decl.cc                                |  1 +
>  gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C | 13 +++++++++++++
>  2 files changed, 14 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
>
> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
> index 80485f0a428..fbe1407a2d2 100644
> --- a/gcc/cp/decl.cc
> +++ b/gcc/cp/decl.cc
> @@ -11686,6 +11686,7 @@ fold_sizeof_expr (tree t)
>  				    false, false);
>    if (r == error_mark_node)
>      r = size_one_node;
> +  r = cp_fold_convert (TREE_TYPE (t), r);
>    return r;
>  }
>
> diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C 
> b/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
> new file mode 100644
> index 00000000000..59fc0d332b9
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
> @@ -0,0 +1,13 @@
> +// PR c++/117775
> +// Check that we don't ICE and have sizeof(void)==1 under 
> -Wno-pointer-arith
> +// { dg-do run { target c++20 } }
> +// { dg-additional-options "-Wno-pointer-arith" }
> +
> +int main() {
> +  struct why :
> +    decltype( [](auto) {
> +		return sizeof(void);
> +	      })
> +  {} x;
> +  return 1 - x.operator()(0);
> +}
> -- 
> 2.44.0
Simon Martin Jan. 7, 2025, 3:15 p.m. UTC | #2
Hi,

On 11 Dec 2024, at 22:27, Simon Martin wrote:

> Hi,
>
> On 29 Nov 2024, at 15:33, Simon Martin wrote:
>
>> We currently ICE upon the following code, that is valid under
>> -Wno-pointer-arith:
>>
>> === cut here ===
>> int main() {
>>   decltype( [](auto) { return sizeof(void); } ) x;
>>   return x.operator()(0);
>> }
>> === cut here ===
>>
>> The problem is that "fold_sizeof_expr (sizeof(void))" returns
>> size_one_node, that has a different TREE_TYPE from that of the sizeof
>> expression, which later triggers an assert in
>> cxx_eval_store_expression.
>>
>> This patch makes sure that fold_sizeof_expr always returns a tree with
>> the type requested.
>>
>> Successfully tested on x86_64-pc-linux-gnu.
> Ping.
Ping. Thanks!

Simon
>>
>> 	PR c++/117775
>>
>> gcc/cp/ChangeLog:
>>
>> 	* decl.cc (fold_sizeof_expr): Make sure the folded result has
>> 	the requested TREE_TYPE.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 	* g++.dg/cpp2a/constexpr-117775.C: New test.
>>
>> ---
>>  gcc/cp/decl.cc                                |  1 +
>>  gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C | 13 +++++++++++++
>>  2 files changed, 14 insertions(+)
>>  create mode 100644 gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
>>
>> diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
>> index 80485f0a428..fbe1407a2d2 100644
>> --- a/gcc/cp/decl.cc
>> +++ b/gcc/cp/decl.cc
>> @@ -11686,6 +11686,7 @@ fold_sizeof_expr (tree t)
>>  				    false, false);
>>    if (r == error_mark_node)
>>      r = size_one_node;
>> +  r = cp_fold_convert (TREE_TYPE (t), r);
>>    return r;
>>  }
>>
>> diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
>> b/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
>> new file mode 100644
>> index 00000000000..59fc0d332b9
>> --- /dev/null
>> +++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
>> @@ -0,0 +1,13 @@
>> +// PR c++/117775
>> +// Check that we don't ICE and have sizeof(void)==1 under
>> -Wno-pointer-arith
>> +// { dg-do run { target c++20 } }
>> +// { dg-additional-options "-Wno-pointer-arith" }
>> +
>> +int main() {
>> +  struct why :
>> +    decltype( [](auto) {
>> +		return sizeof(void);
>> +	      })
>> +  {} x;
>> +  return 1 - x.operator()(0);
>> +}
>> -- 
>> 2.44.0
diff mbox series

Patch

diff --git a/gcc/cp/decl.cc b/gcc/cp/decl.cc
index 80485f0a428..fbe1407a2d2 100644
--- a/gcc/cp/decl.cc
+++ b/gcc/cp/decl.cc
@@ -11686,6 +11686,7 @@  fold_sizeof_expr (tree t)
 				    false, false);
   if (r == error_mark_node)
     r = size_one_node;
+  r = cp_fold_convert (TREE_TYPE (t), r);
   return r;
 }
 
diff --git a/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C b/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
new file mode 100644
index 00000000000..59fc0d332b9
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/constexpr-117775.C
@@ -0,0 +1,13 @@ 
+// PR c++/117775
+// Check that we don't ICE and have sizeof(void)==1 under -Wno-pointer-arith
+// { dg-do run { target c++20 } }
+// { dg-additional-options "-Wno-pointer-arith" }
+
+int main() {
+  struct why :
+    decltype( [](auto) {
+		return sizeof(void);
+	      })
+  {} x;
+  return 1 - x.operator()(0);
+}