public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] c++: sizeof(expr) in non-templated requires-expr [PR108563]
@ 2023-02-09 18:17 Patrick Palka
  2023-02-09 18:22 ` Patrick Palka
  0 siblings, 1 reply; 3+ messages in thread
From: Patrick Palka @ 2023-02-09 18:17 UTC (permalink / raw)
  To: gcc-patches; +Cc: jason, Patrick Palka

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

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); });
-- 
2.39.1.418.g7876265d61


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-02-14 23:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-09 18:17 [PATCH] c++: sizeof(expr) in non-templated requires-expr [PR108563] Patrick Palka
2023-02-09 18:22 ` Patrick Palka
2023-02-14 23:02   ` Jason Merrill

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).