c++: cp_stabilize_reference and non-dep exprs [PR111919]

Message ID 20231023234924.2971461-1-ppalka@redhat.com
State Accepted
Headers
Series c++: cp_stabilize_reference and non-dep exprs [PR111919] |

Checks

Context Check Description
snail/gcc-patch-check success Github commit url

Commit Message

Patrick Palka Oct. 23, 2023, 11:49 p.m. UTC
  Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
for trunk?

-- >8 --

After the removal of NON_DEPENDENT_EXPR, cp_stabilize_reference which
used to just exit early for NON_DEPENDENT_EXPR is now more prone to
passing a weird templated tree to middle-end routines, which leads to a
crash from contains_placeholder_p in the testcase below.  It seems the
best fix is to just disable cp_stabilize_reference when in a template
context like we already do for cp_save_expr; it seems SAVE_EXPR should
never appear in a templated tree (since e.g. tsubst doesn't handle it).

	PR c++/111919

gcc/cp/ChangeLog:

	* tree.cc (cp_stabilize_reference): Do nothing when
	processing_template_decl.

gcc/testsuite/ChangeLog:

	* g++.dg/template/non-dependent27.C: New test.
---
 gcc/cp/tree.cc                                  | 4 ++++
 gcc/testsuite/g++.dg/template/non-dependent27.C | 8 ++++++++
 2 files changed, 12 insertions(+)
 create mode 100644 gcc/testsuite/g++.dg/template/non-dependent27.C
  

Comments

Jason Merrill Oct. 24, 2023, 4:19 p.m. UTC | #1
On 10/23/23 19:49, Patrick Palka wrote:
> Bootstrapped and regtested on x86_64-pc-linux-gnu, does this look OK
> for trunk?
> 
> -- >8 --
> 
> After the removal of NON_DEPENDENT_EXPR, cp_stabilize_reference which
> used to just exit early for NON_DEPENDENT_EXPR is now more prone to
> passing a weird templated tree to middle-end routines, which leads to a
> crash from contains_placeholder_p in the testcase below.  It seems the
> best fix is to just disable cp_stabilize_reference when in a template
> context like we already do for cp_save_expr; it seems SAVE_EXPR should
> never appear in a templated tree (since e.g. tsubst doesn't handle it).

Hmm.  We don't want the result of cp_stabilize_reference (or 
cp_save_expr) to end up in the resulting trees in template context. 
Having a SAVE_EXPR in the result would actually be helpful for catching 
such a bug.

That said, the patch is OK.

> 	PR c++/111919
> 
> gcc/cp/ChangeLog:
> 
> 	* tree.cc (cp_stabilize_reference): Do nothing when
> 	processing_template_decl.
> 
> gcc/testsuite/ChangeLog:
> 
> 	* g++.dg/template/non-dependent27.C: New test.
> ---
>   gcc/cp/tree.cc                                  | 4 ++++
>   gcc/testsuite/g++.dg/template/non-dependent27.C | 8 ++++++++
>   2 files changed, 12 insertions(+)
>   create mode 100644 gcc/testsuite/g++.dg/template/non-dependent27.C
> 
> diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
> index a3d61d3e7c9..417c92ba76f 100644
> --- a/gcc/cp/tree.cc
> +++ b/gcc/cp/tree.cc
> @@ -408,6 +408,10 @@ bitfield_p (const_tree ref)
>   tree
>   cp_stabilize_reference (tree ref)
>   {
> +  if (processing_template_decl)
> +    /* As in cp_save_expr.  */
> +    return ref;
> +
>     STRIP_ANY_LOCATION_WRAPPER (ref);
>     switch (TREE_CODE (ref))
>       {
> diff --git a/gcc/testsuite/g++.dg/template/non-dependent27.C b/gcc/testsuite/g++.dg/template/non-dependent27.C
> new file mode 100644
> index 00000000000..cf7af6e6425
> --- /dev/null
> +++ b/gcc/testsuite/g++.dg/template/non-dependent27.C
> @@ -0,0 +1,8 @@
> +// PR c++/111919
> +
> +int i[3];
> +
> +template<class T>
> +void f() {
> +  i[42 / (int) sizeof (T)] |= 0;
> +}
  

Patch

diff --git a/gcc/cp/tree.cc b/gcc/cp/tree.cc
index a3d61d3e7c9..417c92ba76f 100644
--- a/gcc/cp/tree.cc
+++ b/gcc/cp/tree.cc
@@ -408,6 +408,10 @@  bitfield_p (const_tree ref)
 tree
 cp_stabilize_reference (tree ref)
 {
+  if (processing_template_decl)
+    /* As in cp_save_expr.  */
+    return ref;
+
   STRIP_ANY_LOCATION_WRAPPER (ref);
   switch (TREE_CODE (ref))
     {
diff --git a/gcc/testsuite/g++.dg/template/non-dependent27.C b/gcc/testsuite/g++.dg/template/non-dependent27.C
new file mode 100644
index 00000000000..cf7af6e6425
--- /dev/null
+++ b/gcc/testsuite/g++.dg/template/non-dependent27.C
@@ -0,0 +1,8 @@ 
+// PR c++/111919
+
+int i[3];
+
+template<class T>
+void f() {
+  i[42 / (int) sizeof (T)] |= 0;
+}