Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

Extra temporaries with should-be-xvalue conditional expressions with operands requiring conversion #19915

Open Quuxplusone opened 10 years ago

Quuxplusone commented 10 years ago
Bugzilla Link PR19916
Status NEW
Importance P normal
Reported by hstong@ca.ibm.com
Reported on 2014-06-01 21:32:06 -0700
Last modified on 2015-03-25 12:35:21 -0700
Version trunk
Hardware All All
CC dgregor@apple.com, llvm-bugs@lists.llvm.org
Fixed by commit(s)
Attachments
Blocks
Blocked by
See also
The conditional operator as implemented by Clang introduces extra temporaries
when given a class that can be implicitly converted to an xvalue of the "other
type" through a conversion function.

N3290 subclause 5.16 [expr.cond] paragraph 3 applies. Note that the third
bullet contains an instance of "rvalue" which was replaced with "prvalue" as an
editorial fix.

The source below demonstrates that implicit conversion to int && from the
expression "aObj" is possible.
The conditions for the second bullet of [expr.cond]/3 is met.
The conversion in the other direction is not possible.
The end result from the last sentence of [expr.cond]/3 is that both the second
and third operand should be considered xvalues of type int for [expr.cond]/4.

### SOURCE:> cat condExprUDCtoXval_Xval.cc
int glob;

struct A {
   operator int &&() { return static_cast<int &&>(glob); }
} aObj;

int &&implicitconvcheck = aObj;
int &&condExprResult = false ? aObj : aObj.operator int &&();

extern "C" int printf(const char *, ...);

int main() {
   printf("address of implicitconvcheck %s consistent with direct binding\n", &implicitconvcheck == &glob ? "is" : "is NOT");
   printf("address of condExprResult %s consistent with direct binding\n", &condExprResult == &glob ? "is" : "is NOT");
   return 0;
}
Return:  0x00:0

### COMPILER INVOCATION AND OUTPUT:> clang++ -std=c++11
condExprUDCtoXval_Xval.cc && ./a.out
address of implicitconvcheck is consistent with direct binding
address of condExprResult is NOT consistent with direct binding
Return:  0x00:0

### EXPECTED OUTPUT:> ./a.out
address of implicitconvcheck is consistent with direct binding
address of condExprResult is consistent with direct binding
Return:  0x00:0

### VERSION INFO:> clang++ -v
clang version 3.4 (tags/RELEASE_34/final)
Target: powerpc64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/lib/gcc/ppc64-redhat-linux/4.4.4
Found candidate GCC installation: /usr/lib/gcc/ppc64-redhat-linux/4.4.6
Selected GCC installation: /usr/lib/gcc/ppc64-redhat-linux/4.4.6
Return:  0x00:0
Quuxplusone commented 9 years ago
Much simpler case; Clang errors out when trying match cv-qualification because
it tries to create a temporary but could not find an appropriate constructor:

### SOURCE (<stdin>):
struct A { };

A &&f1();
volatile A &&f2();

void g(bool cond) {
   cond ? f1() : f2();
}

### COMPILER INVOCATION:
clang++ -cc1 -std=c++11 -x c++ -

### OUTPUT:
<stdin>:7:11: error: no matching constructor for initialization of 'volatile A'
   cond ? f1() : f2();
          ^~~~
<stdin>:1:8: note: candidate constructor (the implicit copy constructor) not
viable: 1st argument ('volatile A') would lose volatile qualifier
struct A { };
       ^
<stdin>:1:8: note: candidate constructor (the implicit move constructor) not
viable: 1st argument ('volatile A') would lose volatile qualifier
struct A { };
       ^
<stdin>:1:8: note: candidate constructor (the implicit default constructor) not
viable: requires 0 arguments, but 1 was provided
1 error generated.

### COMPILER VERSION INFO:
clang version 3.7.0 (trunk 233189)
Target: x86_64-unknown-linux-gnu
Thread model: posix
Found candidate GCC installation: /usr/local/gcc-4.8.2/lib/gcc/x86_64-unknown-
linux-gnu/4.8.2
Selected GCC installation: /usr/local/gcc-4.8.2/lib/gcc/x86_64-unknown-linux-
gnu/4.8.2
Candidate multilib: .;@m64
Selected multilib: .;@m64