c++: fixes for derived-to-base reference binding [PR107085]
commit895dd027d5dda51a95d242aec8a49a6dfa5db58d
authorMarek Polacek <polacek@redhat.com>
Wed, 5 Oct 2022 19:51:30 +0000 (5 15:51 -0400)
committerMarek Polacek <polacek@redhat.com>
Fri, 7 Oct 2022 21:18:16 +0000 (7 17:18 -0400)
tree8dd6b92061555471a8c532e2f62a2d03bd17acf6
parentf30e9fd33e56a5a721346ea6140722e1b193db42
c++: fixes for derived-to-base reference binding [PR107085]

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