Bug 44158 - [C++0x] wrong overload resolution for copy-initialization from an rvalue
Summary: [C++0x] wrong overload resolution for copy-initialization from an rvalue
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: c++ (show other bugs)
Version: 4.5.0
: P3 normal
Target Milestone: 4.5.1
Assignee: Jason Merrill
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2010-05-16 13:55 UTC by Ai Azuma
Modified: 2010-05-19 16:10 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2010-05-16 23:21:43


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Ai Azuma 2010-05-16 13:55:47 UTC
In the following code, the X's move constructor (the constructor overloaded for
rvalue reference) should be called for the copy-initialization
`X y = static_cast<X&&>(x);', and two successive assertions should be passed.
However, it seems that the implicitly defined copy constructor is called, and
the second assertion fails.


cryolite@thinblue:~/work/gcc_bug/move_on_tribially_copy_constructible$ g++-4.5.0 -v -save-temps -std=c++0x main.cpp 
Using built-in specs.
COLLECT_GCC=g++-4.5.0
COLLECT_LTO_WRAPPER=/home/cryolite/local/libexec/gcc/i486-linux-gnu/4.5.0/lto-wrapper
Target: i486-linux-gnu
Configured with: ../gcc-4.5.0/configure --build=i486-linux-gnu --prefix=/home/cryolite/local --program-suffix=-4.5.0 --enable-version-specific-runtime-libs --enable-languages=c,c++
Thread model: posix
gcc version 4.5.0 (GCC) 
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++0x' '-shared-libgcc' '-mtune=i486' '-march=i486'
 /home/cryolite/local/libexec/gcc/i486-linux-gnu/4.5.0/cc1plus -E -quiet -v -D_GNU_SOURCE main.cpp -mtune=i486 -march=i486 -std=c++0x -fpch-preprocess -o main.ii
ignoring duplicate directory "/home/cryolite/local/include"
ignoring nonexistent directory "/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/../../../../i486-linux-gnu/include"
#include "..." search starts here:
#include <...> search starts here:
 /home/cryolite/local/include
 /home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include/c++
 /home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include/c++/i486-linux-gnu
 /home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include/c++/backward
 /usr/local/include
 /home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include
 /home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include-fixed
 /usr/include
End of search list.
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++0x' '-shared-libgcc' '-mtune=i486' '-march=i486'
 /home/cryolite/local/libexec/gcc/i486-linux-gnu/4.5.0/cc1plus -fpreprocessed main.ii -quiet -dumpbase main.cpp -mtune=i486 -march=i486 -auxbase main -std=c++0x -version -o main.s
GNU C++ (GCC) version 4.5.0 (i486-linux-gnu)
	compiled by GNU C version 4.5.0, GMP version 4.3.1, MPFR version 2.4.1-p2, MPC version 0.8.1
GGC heuristics: --param ggc-min-expand=81 --param ggc-min-heapsize=95967
GNU C++ (GCC) version 4.5.0 (i486-linux-gnu)
	compiled by GNU C version 4.5.0, GMP version 4.3.1, MPFR version 2.4.1-p2, MPC version 0.8.1
GGC heuristics: --param ggc-min-expand=81 --param ggc-min-heapsize=95967
Compiler executable checksum: f3cfa96cf9dcd630b91f4110d3b8b2b3
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++0x' '-shared-libgcc' '-mtune=i486' '-march=i486'
 as -V -Qy --32 -o main.o main.s
GNU assembler version 2.20 (i486-linux-gnu) using BFD version (GNU Binutils for Ubuntu) 2.20
COMPILER_PATH=/home/cryolite/local/libexec/gcc/i486-linux-gnu/4.5.0/:/home/cryolite/local/libexec/gcc/i486-linux-gnu/4.5.0/:/home/cryolite/local/libexec/gcc/i486-linux-gnu/:/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/:/home/cryolite/local/lib/gcc/i486-linux-gnu/
LIBRARY_PATH=/home/cryolite/local/lib/:/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/:/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/../../../:/lib/:/usr/lib/
COLLECT_GCC_OPTIONS='-v' '-save-temps' '-std=c++0x' '-shared-libgcc' '-mtune=i486' '-march=i486'
 /home/cryolite/local/libexec/gcc/i486-linux-gnu/4.5.0/collect2 --eh-frame-hdr -m elf_i386 -dynamic-linker /lib/ld-linux.so.2 /usr/lib/crt1.o /usr/lib/crti.o /home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/crtbegin.o -L/home/cryolite/local/lib -L/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0 -L/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/../../.. main.o -lstdc++ -lm -lgcc_s -lgcc -lc -lgcc_s -lgcc /home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/crtend.o /usr/lib/crtn.o

cryolite@thinblue:~/work/gcc_bug/move_on_tribially_copy_constructible$ cat main.ii 
# 1 "main.cpp"
# 1 "<built-in>"
# 1 "<command-line>"
# 1 "main.cpp"
# 1 "/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include/c++/cassert" 1 3
# 43 "/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include/c++/cassert" 3
       
# 44 "/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include/c++/cassert" 3

# 1 "/usr/include/assert.h" 1 3 4
# 37 "/usr/include/assert.h" 3 4
# 1 "/usr/include/features.h" 1 3 4
# 313 "/usr/include/features.h" 3 4
# 1 "/usr/include/bits/predefs.h" 1 3 4
# 314 "/usr/include/features.h" 2 3 4
# 346 "/usr/include/features.h" 3 4
# 1 "/usr/include/sys/cdefs.h" 1 3 4
# 353 "/usr/include/sys/cdefs.h" 3 4
# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 354 "/usr/include/sys/cdefs.h" 2 3 4
# 347 "/usr/include/features.h" 2 3 4
# 378 "/usr/include/features.h" 3 4
# 1 "/usr/include/gnu/stubs.h" 1 3 4



# 1 "/usr/include/bits/wordsize.h" 1 3 4
# 5 "/usr/include/gnu/stubs.h" 2 3 4


# 1 "/usr/include/gnu/stubs-32.h" 1 3 4
# 8 "/usr/include/gnu/stubs.h" 2 3 4
# 379 "/usr/include/features.h" 2 3 4
# 38 "/usr/include/assert.h" 2 3 4
# 68 "/usr/include/assert.h" 3 4
extern "C" {


extern void __assert_fail (__const char *__assertion, __const char *__file,
      unsigned int __line, __const char *__function)
     throw () __attribute__ ((__noreturn__));


extern void __assert_perror_fail (int __errnum, __const char *__file,
      unsigned int __line,
      __const char *__function)
     throw () __attribute__ ((__noreturn__));




extern void __assert (const char *__assertion, const char *__file, int __line)
     throw () __attribute__ ((__noreturn__));


}
# 45 "/home/cryolite/local/lib/gcc/i486-linux-gnu/4.5.0/include/c++/cassert" 2 3
# 2 "main.cpp" 2

struct X
{
  X(int i) : i_(i) {}
  X(X &&x) : i_(x.i_) { x.i_ = 0; }
  int i_;
};

int main()
{
  X x(42);
  X y = static_cast<X&&>(x);
  ((y.i_ == 42) ? static_cast<void> (0) : __assert_fail ("y.i_ == 42", "main.cpp", 14, __PRETTY_FUNCTION__));
  ((x.i_ == 0) ? static_cast<void> (0) : __assert_fail ("x.i_ == 0", "main.cpp", 15, __PRETTY_FUNCTION__));
}

cryolite@thinblue:~/work/gcc_bug/move_on_tribially_copy_constructible$ ./a.out 
a.out: main.cpp:15: int main(): Assertion `x.i_ == 0' failed.
Aborted
Comment 1 Jonathan Wakely 2010-05-16 17:07:07 UTC
The rules say that for copy-initialization where the source type (X&&) is not the same as the destination type (X) a temporary is created. That temporary is copy-constructed from x, then y is move-constructed from the temporary.

To avoid the temporary use direct-initialization;

Y y( static_cast<X&&>(x) );
Comment 2 Jonathan Wakely 2010-05-16 17:08:08 UTC
(In reply to comment #1)
> Y y( static_cast<X&&>(x) );
  ^
Oops. I meant X not Y


Comment 3 Paolo Carlini 2010-05-16 18:29:58 UTC
This is invalid then.
Comment 4 Jonathan Wakely 2010-05-16 19:34:45 UTC
it might be a valid enhancement request, as I think the temporary is eligible for copy-elision (Jason?) but the compiler isn't required to elide it, so it is wrong to assert that a temporary won't be created
Comment 5 Paolo Carlini 2010-05-16 21:25:29 UTC
To be safe, let's reopen the bug. For the record, this works:

#include <cassert>

struct X
{
  X(int i) : i_(i) {}
  X(X&& x) : i_(x.i_) { x.i_ = 0; }
  int i_;

  X(const X&) = delete;
};

int main()
{
  X x(42);
  X y = static_cast<X&&>(x);
  assert( y.i_ == 42 );
  assert( x.i_ == 0 );
}
Comment 6 Jason Merrill 2010-05-16 22:32:43 UTC
(In reply to comment #1)
> The rules say that for copy-initialization where the source type (X&&) is not
> the same as the destination type (X) a temporary is created.

But the source type is X; there are no expressions of reference type.

The problem is not the overload resolution, but that build_over_call looks at TYPE_HAS_COMPLEX_INIT_REF even though we're dealing with the move ctor, not the copy ctor.
Comment 7 Jason Merrill 2010-05-17 19:54:01 UTC
Subject: Bug 44158

Author: jason
Date: Mon May 17 19:53:45 2010
New Revision: 159508

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=159508
Log:
	PR c++/44158
	* call.c (build_over_call): Don't do bitwise copy for move ctor.

Modified:
    trunk/gcc/cp/ChangeLog
    trunk/gcc/cp/call.c
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/testsuite/g++.dg/cpp0x/rv-trivial-bug.C

Comment 8 Jason Merrill 2010-05-19 15:49:19 UTC
Subject: Bug 44158

Author: jason
Date: Wed May 19 15:49:01 2010
New Revision: 159577

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=159577
Log:
	PR c++/44158
	* call.c (build_over_call): Don't do bitwise copy for move ctor.

Modified:
    branches/gcc-4_5-branch/gcc/cp/ChangeLog
    branches/gcc-4_5-branch/gcc/cp/call.c
    branches/gcc-4_5-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_5-branch/gcc/testsuite/g++.dg/cpp0x/rv-trivial-bug.C

Comment 9 Jason Merrill 2010-05-19 16:10:37 UTC
Fixed for 4.5.1.