Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

constexpr variable initialisation error after forwarding reference #42056

Open Quuxplusone opened 5 years ago

Quuxplusone commented 5 years ago
Bugzilla Link PR43086
Status CONFIRMED
Importance P normal
Reported by Paul Keir (pkeir@outlook.com)
Reported on 2019-08-22 02:43:16 -0700
Last modified on 2019-08-22 10:40:45 -0700
Version trunk
Hardware PC Linux
CC blitzrakete@gmail.com, dgregor@apple.com, erik.pilkington@gmail.com, llvm-bugs@lists.llvm.org, pkeir@outlook.com, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments constexpr_rvalue.cpp (152 bytes, text/x-c++src)
Blocks
Blocked by
See also
Created attachment 22413
Code as described.

The C++11 code below (also attached) will fail to compile:

struct Bar {};
struct Foo { Bar t; };

template <typename T>
constexpr T jam(T &&a) { return a; }

void test()
{
  Foo y;
  constexpr Foo x = jam(y);
}

Changing the types of x and y from Foo to Bar will remove the error. GCC
snapshot 17th August 2019 produces no error. The error message follows:

constexpr_rvalue.cpp:10:17: error: constexpr variable 'x' must be initialized
by a constant expression
  constexpr Foo x = jam(y);
                ^   ~~~~~~
constexpr_rvalue.cpp:10:21: note: read of non-constexpr variable 'y' is not
allowed in a constant expression
  constexpr Foo x = jam(y);
                    ^
constexpr_rvalue.cpp:10:21: note: in call to 'Foo(y)'
constexpr_rvalue.cpp:9:7: note: declared here
  Foo y;
      ^
1 error generated.
Quuxplusone commented 5 years ago

Attached constexpr_rvalue.cpp (152 bytes, text/x-c++src): Code as described.

Quuxplusone commented 5 years ago

Confirmed; this code doesn't actually read from the local variable 'y'. Looks like a bug in our trivial copy optimization.