diff mbox series

c++: sizeof(expr) in non-templated requires-expr [PR108563]

Message ID 20230209181722.3178411-1-ppalka@redhat.com
State New
Headers show
Series c++: sizeof(expr) in non-templated requires-expr [PR108563] | expand

Commit Message

Patrick Palka Feb. 9, 2023, 6:17 p.m. UTC
When substituting into sizeof(expr), tsubst_copy_and_build elides
substitution into the operand if args is NULL_TREE, and instead
considers the TREE_TYPE of the operand.  But here the (templated)
operand is a TEMPLATE_ID_EXPR with empty TREE_TYPE, so we can't elide
substitution in this case.

Contrary to the associated comment (dating back to r69130) substituting
args=NULL_TREE should generally work since we do exactly that in e.g.
fold_non_dependent_expr, and I don't see why the operand of sizeof would
be an exception.  So this patch just removes this special case.

Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
trunk?  Diff generated with -w to ignore noisy whitespace changes.

	PR c++/108563

gcc/cp/ChangeLog:

	* pt.cc (tsubst_copy_and_build) <case SIZEOF_EXPR>: Remove
	special case for empty args.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-requires35.C: New test.
---
 gcc/cp/pt.cc                                  | 25 ++++++-------------
 .../g++.dg/cpp2a/concepts-requires35.C        | 14 +++++++++++
 2 files changed, 21 insertions(+), 18 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C

Comments

Patrick Palka Feb. 9, 2023, 6:22 p.m. UTC | #1
On Thu, 9 Feb 2023, Patrick Palka wrote:

> When substituting into sizeof(expr), tsubst_copy_and_build elides
> substitution into the operand if args is NULL_TREE, and instead
> considers the TREE_TYPE of the operand.  But here the (templated)
> operand is a TEMPLATE_ID_EXPR with empty TREE_TYPE, so we can't elide
> substitution in this case.
> 
> Contrary to the associated comment (dating back to r69130) substituting
> args=NULL_TREE should generally work since we do exactly that in e.g.
> fold_non_dependent_expr, and I don't see why the operand of sizeof would
> be an exception.  So this patch just removes this special case.
> 
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
> trunk?  Diff generated with -w to ignore noisy whitespace changes.

This time with -w actually passed to format-patch:

-- >8 --

	PR c++/108563

gcc/cp/ChangeLog:

	* pt.cc (tsubst_copy_and_build) <case SIZEOF_EXPR>: Remove
	special case for empty args.

gcc/testsuite/ChangeLog:

	* g++.dg/cpp2a/concepts-requires35.C: New test.
---
 gcc/cp/pt.cc                                     | 11 -----------
 gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C | 14 ++++++++++++++
 2 files changed, 14 insertions(+), 11 deletions(-)
 create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 9f3fc1fa089..f21d28263d1 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -20652,16 +20652,6 @@ tsubst_copy_and_build (tree t,
 	  op1 = TREE_TYPE (op1);
 	bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
 			    && ALIGNOF_EXPR_STD_P (t));
-        if (!args)
-	  {
-	    /* When there are no ARGS, we are trying to evaluate a
-	       non-dependent expression from the parser.  Trying to do
-	       the substitutions may not work.  */
-	    if (!TYPE_P (op1))
-	      op1 = TREE_TYPE (op1);
-	  }
-	else
-	  {
 	++cp_unevaluated_operand;
 	++c_inhibit_evaluation_warnings;
 	if (TYPE_P (op1))
@@ -20670,7 +20660,6 @@ tsubst_copy_and_build (tree t,
 	  op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
 	--cp_unevaluated_operand;
 	--c_inhibit_evaluation_warnings;
-	  }
         if (TYPE_P (op1))
 	  r = cxx_sizeof_or_alignof_type (input_location,
 					  op1, TREE_CODE (t), std_alignof,
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C
new file mode 100644
index 00000000000..2bb4b2b0b5d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C
@@ -0,0 +1,14 @@
+// PR c++/108563
+// { dg-do compile { target c++20 } }
+
+template<class T>
+struct foo {
+  static constexpr T value = 0;
+};
+
+template<class T>
+inline constexpr T foo_v = foo<T>::value;
+
+static_assert(requires { sizeof(foo_v<int>); });
+static_assert(requires { requires sizeof(foo_v<int*>) == sizeof(int*); });
+static_assert(requires { requires sizeof(foo_v<char>) == sizeof(char); });
Jason Merrill Feb. 14, 2023, 11:02 p.m. UTC | #2
On 2/9/23 10:22, Patrick Palka wrote:
> On Thu, 9 Feb 2023, Patrick Palka wrote:
> 
>> When substituting into sizeof(expr), tsubst_copy_and_build elides
>> substitution into the operand if args is NULL_TREE, and instead
>> considers the TREE_TYPE of the operand.  But here the (templated)
>> operand is a TEMPLATE_ID_EXPR with empty TREE_TYPE, so we can't elide
>> substitution in this case.
>>
>> Contrary to the associated comment (dating back to r69130) substituting
>> args=NULL_TREE should generally work since we do exactly that in e.g.
>> fold_non_dependent_expr, and I don't see why the operand of sizeof would
>> be an exception.  So this patch just removes this special case.
>>
>> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK for
>> trunk?  Diff generated with -w to ignore noisy whitespace changes.
> 
> This time with -w actually passed to format-patch:

OK.

> -- >8 --
> 
> 	PR c++/108563
> 
> gcc/cp/ChangeLog:
> 
> 	* pt.cc (tsubst_copy_and_build) <case SIZEOF_EXPR>: Remove
> 	special case for empty args.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/cpp2a/concepts-requires35.C: New test.
> ---
>   gcc/cp/pt.cc                                     | 11 -----------
>   gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C | 14 ++++++++++++++
>   2 files changed, 14 insertions(+), 11 deletions(-)
>   create mode 100644 gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C
> 
> diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
> index 9f3fc1fa089..f21d28263d1 100644
> --- a/gcc/cp/pt.cc
> +++ b/gcc/cp/pt.cc
> @@ -20652,16 +20652,6 @@ tsubst_copy_and_build (tree t,
>   	  op1 = TREE_TYPE (op1);
>   	bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
>   			    && ALIGNOF_EXPR_STD_P (t));
> -        if (!args)
> -	  {
> -	    /* When there are no ARGS, we are trying to evaluate a
> -	       non-dependent expression from the parser.  Trying to do
> -	       the substitutions may not work.  */
> -	    if (!TYPE_P (op1))
> -	      op1 = TREE_TYPE (op1);
> -	  }
> -	else
> -	  {
>   	++cp_unevaluated_operand;
>   	++c_inhibit_evaluation_warnings;
>   	if (TYPE_P (op1))
> @@ -20670,7 +20660,6 @@ tsubst_copy_and_build (tree t,
>   	  op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
>   	--cp_unevaluated_operand;
>   	--c_inhibit_evaluation_warnings;
> -	  }
>           if (TYPE_P (op1))
>   	  r = cxx_sizeof_or_alignof_type (input_location,
>   					  op1, TREE_CODE (t), std_alignof,
> diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C
> new file mode 100644
> index 00000000000..2bb4b2b0b5d
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C
> @@ -0,0 +1,14 @@
> +// PR c++/108563
> +// { dg-do compile { target c++20 } }
> +
> +template<class T>
> +struct foo {
> +  static constexpr T value = 0;
> +};
> +
> +template<class T>
> +inline constexpr T foo_v = foo<T>::value;
> +
> +static_assert(requires { sizeof(foo_v<int>); });
> +static_assert(requires { requires sizeof(foo_v<int*>) == sizeof(int*); });
> +static_assert(requires { requires sizeof(foo_v<char>) == sizeof(char); });
diff mbox series

Patch

diff --git a/gcc/cp/pt.cc b/gcc/cp/pt.cc
index 9f3fc1fa089..f21d28263d1 100644
--- a/gcc/cp/pt.cc
+++ b/gcc/cp/pt.cc
@@ -20652,25 +20652,14 @@  tsubst_copy_and_build (tree t,
 	  op1 = TREE_TYPE (op1);
 	bool std_alignof = (TREE_CODE (t) == ALIGNOF_EXPR
 			    && ALIGNOF_EXPR_STD_P (t));
-        if (!args)
-	  {
-	    /* When there are no ARGS, we are trying to evaluate a
-	       non-dependent expression from the parser.  Trying to do
-	       the substitutions may not work.  */
-	    if (!TYPE_P (op1))
-	      op1 = TREE_TYPE (op1);
-	  }
+	++cp_unevaluated_operand;
+	++c_inhibit_evaluation_warnings;
+	if (TYPE_P (op1))
+	  op1 = tsubst (op1, args, complain, in_decl);
 	else
-	  {
-	    ++cp_unevaluated_operand;
-	    ++c_inhibit_evaluation_warnings;
-	    if (TYPE_P (op1))
-	      op1 = tsubst (op1, args, complain, in_decl);
-	    else
-	      op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
-	    --cp_unevaluated_operand;
-	    --c_inhibit_evaluation_warnings;
-	  }
+	  op1 = tsubst_copy_and_build (op1, args, complain, in_decl);
+	--cp_unevaluated_operand;
+	--c_inhibit_evaluation_warnings;
         if (TYPE_P (op1))
 	  r = cxx_sizeof_or_alignof_type (input_location,
 					  op1, TREE_CODE (t), std_alignof,
diff --git a/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C b/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C
new file mode 100644
index 00000000000..2bb4b2b0b5d
--- /dev/null
+++ b/gcc/testsuite/g++.dg/cpp2a/concepts-requires35.C
@@ -0,0 +1,14 @@ 
+// PR c++/108563
+// { dg-do compile { target c++20 } }
+
+template<class T>
+struct foo {
+  static constexpr T value = 0;
+};
+
+template<class T>
+inline constexpr T foo_v = foo<T>::value;
+
+static_assert(requires { sizeof(foo_v<int>); });
+static_assert(requires { requires sizeof(foo_v<int*>) == sizeof(int*); });
+static_assert(requires { requires sizeof(foo_v<char>) == sizeof(char); });