]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
c++: fixes for derived-to-base reference binding [PR107085]
authorMarek Polacek <polacek@redhat.com>
Wed, 5 Oct 2022 19:51:30 +0000 (15:51 -0400)
committerMarek Polacek <polacek@redhat.com>
Fri, 7 Oct 2022 21:18:16 +0000 (17:18 -0400)
This PR reports that

  struct Base {};
  struct Derived : Base {};
  static_assert(__reference_constructs_from_temporary(Base const&, Derived));

doesn't pass, which it should: it's just like

  const Base& b(Derived{});

where we bind 'b' to the Base subobject of a temporary object of type
Derived.  The ck_base conversion didn't have ->need_temporary_p set because
we didn't need to create a temporary object just for the base, but the whole
object is a temporary so we're still binding to a temporary.  Since the
Base subobject is an xvalue, a new function is introduced.

PR c++/107085

gcc/cp/ChangeLog:

* call.cc (conv_binds_ref_to_temporary): New.
(ref_conv_binds_directly): Rename to...
(ref_conv_binds_to_temporary): ...this.  Use
conv_binds_ref_to_temporary.
* cp-tree.h (ref_conv_binds_directly): Rename to...
(ref_conv_binds_to_temporary): ...this.
* method.cc (ref_xes_from_temporary): Use ref_conv_binds_to_temporary.
* parser.cc (warn_for_range_copy): Likewise.

gcc/testsuite/ChangeLog:

* g++.dg/ext/reference_constructs_from_temporary1.C: Adjust expected
result.
* g++.dg/ext/reference_converts_from_temporary1.C: Likewise.
* g++.dg/cpp0x/elision4.C: New test.

gcc/cp/call.cc
gcc/cp/cp-tree.h
gcc/cp/method.cc
gcc/cp/parser.cc
gcc/testsuite/g++.dg/cpp0x/elision4.C [new file with mode: 0644]
gcc/testsuite/g++.dg/ext/reference_constructs_from_temporary1.C
gcc/testsuite/g++.dg/ext/reference_converts_from_temporary1.C

index bd04a1d309a735dd08e480243f09addae995b7d9..7771d80ff316ab06714bb4cab0155574166b1c6c 100644 (file)
@@ -9210,15 +9210,47 @@ conv_binds_ref_to_prvalue (conversion *c)
   return conv_is_prvalue (next_conversion (c));
 }
 
-/* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE does
-   not involve creating a temporary.  Return tristate::TS_FALSE if converting
-   EXPR to a reference type TYPE binds the reference to a temporary.  If the
-   conversion is invalid or bad, return tristate::TS_UNKNOWN.  DIRECT_INIT_P
+/* True iff C is a conversion that binds a reference to a temporary.
+   This is a superset of conv_binds_ref_to_prvalue: here we're also
+   interested in xvalues.  */
+
+static bool
+conv_binds_ref_to_temporary (conversion *c)
+{
+  if (conv_binds_ref_to_prvalue (c))
+    return true;
+  if (c->kind != ck_ref_bind)
+    return false;
+  c = next_conversion (c);
+  /* This is the case for
+       struct Base {};
+       struct Derived : Base {};
+       const Base& b(Derived{});
+     where we bind 'b' to the Base subobject of a temporary object of type
+     Derived.  The subobject is an xvalue; the whole object is a prvalue.  */
+  if (c->kind != ck_base)
+    return false;
+  c = next_conversion (c);
+  if (c->kind == ck_identity && c->u.expr)
+    {
+      tree expr = c->u.expr;
+      while (handled_component_p (expr))
+       expr = TREE_OPERAND (expr, 0);
+      if (TREE_CODE (expr) == TARGET_EXPR)
+       return true;
+    }
+  return false;
+}
+
+/* Return tristate::TS_TRUE if converting EXPR to a reference type TYPE binds
+   the reference to a temporary.  Return tristate::TS_FALSE if converting
+   EXPR to a reference type TYPE doesn't bind the reference to a temporary.  If
+   the conversion is invalid or bad, return tristate::TS_UNKNOWN.  DIRECT_INIT_P
    says whether the conversion should be done in direct- or copy-initialization
    context.  */
 
 tristate
-ref_conv_binds_directly (tree type, tree expr, bool direct_init_p /*= false*/)
+ref_conv_binds_to_temporary (tree type, tree expr, bool direct_init_p/*=false*/)
 {
   gcc_assert (TYPE_REF_P (type));
 
@@ -9230,7 +9262,7 @@ ref_conv_binds_directly (tree type, tree expr, bool direct_init_p /*= false*/)
                                          /*c_cast_p=*/false, flags, tf_none);
   tristate ret (tristate::TS_UNKNOWN);
   if (conv && !conv->bad_p)
-    ret = tristate (!conv_binds_ref_to_prvalue (conv));
+    ret = tristate (conv_binds_ref_to_temporary (conv));
 
   /* Free all the conversions we allocated.  */
   obstack_free (&conversion_obstack, p);
index 8bc1c2dc7fd6d8884c47a1b5f2cef3f30732558d..469eb2fdb251798ebfb7a81b368eb58c2b5247ce 100644 (file)
@@ -6534,7 +6534,7 @@ extern bool sufficient_parms_p                    (const_tree);
 extern tree type_decays_to                     (tree);
 extern tree extract_call_expr                  (tree);
 extern tree build_trivial_dtor_call            (tree, bool = false);
-extern tristate ref_conv_binds_directly                (tree, tree, bool = false);
+extern tristate ref_conv_binds_to_temporary    (tree, tree, bool = false);
 extern tree build_user_type_conversion         (tree, tree, int,
                                                 tsubst_flags_t);
 extern tree build_new_function_call            (tree, vec<tree, va_gc> **,
index 55af5c43c1872db3ee7709c697767a13e4afa4b7..622e1b9802e38ecac828540d5b74b753ab41a36b 100644 (file)
@@ -2233,7 +2233,7 @@ ref_xes_from_temporary (tree to, tree from, bool direct_init_p)
   tree val = build_stub_object (from);
   if (!TYPE_REF_P (from) && TREE_CODE (from) != FUNCTION_TYPE)
     val = CLASS_TYPE_P (from) ? force_rvalue (val, tf_none) : rvalue (val);
-  return ref_conv_binds_directly (to, val, direct_init_p).is_false ();
+  return ref_conv_binds_to_temporary (to, val, direct_init_p).is_true ();
 }
 
 /* Worker for is_{,nothrow_}convertible.  Attempt to perform an implicit
index 555476e42e7bee2493dfc51d6db3ea235d8dac33..dc3d17c416cf1c9edaf4a28e55a32a9996473e60 100644 (file)
@@ -13731,7 +13731,8 @@ warn_for_range_copy (tree decl, tree expr)
 
   if (TYPE_REF_P (type))
     {
-      if (glvalue_p (expr) && ref_conv_binds_directly (type, expr).is_false ())
+      if (glvalue_p (expr)
+         && ref_conv_binds_to_temporary (type, expr).is_true ())
        {
          auto_diagnostic_group d;
          if (warning_at (loc, OPT_Wrange_loop_construct,
@@ -13762,7 +13763,7 @@ warn_for_range_copy (tree decl, tree expr)
   /* If we can initialize a reference directly, suggest that to avoid the
      copy.  */
   tree rtype = cp_build_reference_type (type, /*rval*/false);
-  if (ref_conv_binds_directly (rtype, expr).is_true ())
+  if (ref_conv_binds_to_temporary (rtype, expr).is_false ())
     {
       auto_diagnostic_group d;
       if (warning_at (loc, OPT_Wrange_loop_construct,
diff --git a/gcc/testsuite/g++.dg/cpp0x/elision4.C b/gcc/testsuite/g++.dg/cpp0x/elision4.C
new file mode 100644 (file)
index 0000000..3cc2e3a
--- /dev/null
@@ -0,0 +1,15 @@
+// PR c++/107085
+// { dg-do compile { target c++11 } }
+
+struct X {
+  X();
+  X(X&&);
+};
+struct Z : X {};
+X x1 = Z();
+X x2 = X(Z());
+
+struct B { };
+struct D : B { };
+B b1 = D();
+B b2 = B(D());
index 76de905a35dc03b01e0572931b7f99b11dc6037e..5354b1dc4e6b4c8ae998bb4d7f6a210f2d50af25 100644 (file)
@@ -201,7 +201,7 @@ SA(!__reference_constructs_from_temporary(const int&, H));
 SA(!__reference_constructs_from_temporary(int&&, G2));
 SA(!__reference_constructs_from_temporary(const int&, H2));
 
-SA(!__reference_constructs_from_temporary(const Base&, Der));
+SA(__reference_constructs_from_temporary(const Base&, Der));
 
 // This fails because std::is_constructible_v<int&&, id<int[3]>> is false.
 SA(!__reference_constructs_from_temporary(int&&, id<int[3]>));
index 90196c387428fabf55d811682b19c740f65a47f1..e6c159e9b0057dfb3802544bc0eaa805513d12db 100644 (file)
@@ -201,7 +201,7 @@ SA( __reference_converts_from_temporary(const int&, H));
 SA(!__reference_converts_from_temporary(int&&, G2));
 SA(!__reference_converts_from_temporary(const int&, H2));
 
-SA(!__reference_converts_from_temporary(const Base&, Der));
+SA(__reference_converts_from_temporary(const Base&, Der));
 
 // This fails because std::is_constructible_v<int&&, id<int[3]>> is false.
 SA(!__reference_converts_from_temporary(int&&, id<int[3]>));