[gcc(refs/vendors/redhat/heads/gcc-10-branch)] PR c++/92871 - bad code with xvalue and GNU ?: extension.

Jakub Jelinek jakub@gcc.gnu.org
Fri Jan 17 11:46:00 GMT 2020


https://gcc.gnu.org/g:7192b1ec12484f5ca8b20930d8dc4d28ab4a533a

commit 7192b1ec12484f5ca8b20930d8dc4d28ab4a533a
Author: Jason Merrill <jason@redhat.com>
Date:   Wed Jan 15 09:31:11 2020 -0500

    PR c++/92871 - bad code with xvalue and GNU ?: extension.
    
    I steered Jakub wrong on the desired behavior for temp-extend1.C in the
    context of bug 92831; it doesn't make sense to try to extend the lifetime of
    a temporary that we've already materialized to evaluate the test.  So this
    patch munges the stabilized expression so that it won't be subject to
    lifetime extension.
    
    	* call.c (prevent_lifetime_extension): New.
    	(build_conditional_expr_1): Use it.

Diff:
---
 gcc/cp/ChangeLog                        |  6 ++++++
 gcc/cp/call.c                           | 24 +++++++++++++++++++++++-
 gcc/testsuite/g++.dg/ext/temp-extend1.C |  2 +-
 3 files changed, 30 insertions(+), 2 deletions(-)

diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index 969827e..78eb834 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,9 @@
+2020-01-15  Jason Merrill  <jason@redhat.com>
+
+	PR c++/92871 - bad code with xvalue and GNU ?: extension.
+	* call.c (prevent_lifetime_extension): New.
+	(build_conditional_expr_1): Use it.
+
 2020-01-14  Nathan Sidwell  <nathan@acm.org>
 
 	PR c++/90916
diff --git a/gcc/cp/call.c b/gcc/cp/call.c
index 9a22398..32ccfc9 100644
--- a/gcc/cp/call.c
+++ b/gcc/cp/call.c
@@ -217,6 +217,7 @@ static conversion *merge_conversion_sequences (conversion *, conversion *);
 static tree build_temp (tree, tree, int, diagnostic_t *, tsubst_flags_t);
 static conversion *build_identity_conv (tree, tree);
 static inline bool conv_binds_to_array_of_unknown_bound (conversion *);
+static tree prevent_lifetime_extension (tree);
 
 /* Returns nonzero iff the destructor name specified in NAME matches BASETYPE.
    NAME can take many forms...  */
@@ -5078,7 +5079,10 @@ build_conditional_expr_1 (const op_location_t &loc,
 
       /* Make sure that lvalues remain lvalues.  See g++.oliva/ext1.C.  */
       if (glvalue_p (arg1))
-	arg2 = arg1 = cp_stabilize_reference (arg1);
+	{
+	  arg2 = arg1 = cp_stabilize_reference (arg1);
+	  arg2 = arg1 = prevent_lifetime_extension (arg1);
+	}
       else
 	arg2 = arg1 = cp_save_expr (arg1);
     }
@@ -12168,6 +12172,24 @@ initialize_reference (tree type, tree expr,
   return expr;
 }
 
+/* If *P is an xvalue expression, prevent temporary lifetime extension if it
+   gets used to initialize a reference.  */
+
+static tree
+prevent_lifetime_extension (tree t)
+{
+  tree *p = &t;
+  while (TREE_CODE (*p) == COMPOUND_EXPR)
+    p = &TREE_OPERAND (*p, 1);
+  while (handled_component_p (*p))
+    p = &TREE_OPERAND (*p, 0);
+  /* Change a TARGET_EXPR from prvalue to xvalue.  */
+  if (TREE_CODE (*p) == TARGET_EXPR)
+    *p = build2 (COMPOUND_EXPR, TREE_TYPE (*p), *p,
+		 move (TARGET_EXPR_SLOT (*p)));
+  return t;
+}
+
 /* Subroutine of extend_ref_init_temps.  Possibly extend one initializer,
    which is bound either to a reference or a std::initializer_list.  */
 
diff --git a/gcc/testsuite/g++.dg/ext/temp-extend1.C b/gcc/testsuite/g++.dg/ext/temp-extend1.C
index 7df9ca5..aaef115 100644
--- a/gcc/testsuite/g++.dg/ext/temp-extend1.C
+++ b/gcc/testsuite/g++.dg/ext/temp-extend1.C
@@ -21,7 +21,7 @@ baz (int i)
 {
   const bool&& a = id<S[3]>{false, true, false}[i].s
 		   ? : id<S[4]>{true, false, true, false}[i].s;
-  if (S::c != (i ? 3 : 4))
+  if (S::c != (i ? 0 : 4))
     __builtin_abort ();
 }



More information about the Gcc-cvs mailing list