| 1 |
// RUN: %clang_cc1 -verify=expected,both -std=c++20 %s -fexperimental-new-constant-interpreter |
| 2 |
// RUN: %clang_cc1 -verify=ref,both -std=c++20 %s |
| 3 |
|
| 4 |
/// FIXME: Slight difference in diagnostic output here. |
| 5 |
|
| 6 |
struct Foo { |
| 7 |
int a; |
| 8 |
}; |
| 9 |
|
| 10 |
constexpr int dead1() { |
| 11 |
|
| 12 |
Foo *F2 = nullptr; |
| 13 |
{ |
| 14 |
Foo F{12}; // expected-note {{declared here}} |
| 15 |
F2 = &F; |
| 16 |
} // Ends lifetime of F. |
| 17 |
|
| 18 |
return F2->a; // expected-note {{read of variable whose lifetime has ended}} \ |
| 19 |
// ref-note {{read of object outside its lifetime is not allowed in a constant expression}} |
| 20 |
} |
| 21 |
static_assert(dead1() == 1, ""); // both-error {{not an integral constant expression}} \ |
| 22 |
// both-note {{in call to}} |
| 23 |
|
| 24 |
|
| 25 |
struct S { |
| 26 |
int &&r; // both-note {{reference member declared here}} |
| 27 |
int t; |
| 28 |
constexpr S() : r(0), t(r) {} // both-error {{reference member 'r' binds to a temporary object whose lifetime would be shorter than the lifetime of the constructed object}} \ |
| 29 |
// ref-note {{read of object outside its lifetime is not allowed in a constant expression}} \ |
| 30 |
// expected-note {{temporary created here}} \ |
| 31 |
// expected-note {{read of temporary whose lifetime has ended}} |
| 32 |
}; |
| 33 |
constexpr int k1 = S().t; // both-error {{must be initialized by a constant expression}} \ |
| 34 |
// both-note {{in call to}} |
| 35 |
|
| 36 |
|
| 37 |
namespace MoveFnWorks { |
| 38 |
template<typename T> constexpr T &&ref(T &&t) { return (T&&)t; } |
| 39 |
|
| 40 |
struct Buf {}; |
| 41 |
|
| 42 |
struct A { |
| 43 |
constexpr A(Buf &buf) : buf(buf) { } |
| 44 |
Buf &buf; |
| 45 |
}; |
| 46 |
|
| 47 |
constexpr bool dtor_calls_dtor() { |
| 48 |
struct B { |
| 49 |
A &&d; |
| 50 |
constexpr B(Buf &buf) : d(ref(A(buf))) {} |
| 51 |
}; |
| 52 |
|
| 53 |
Buf buf; |
| 54 |
{ |
| 55 |
B b(buf); |
| 56 |
} |
| 57 |
|
| 58 |
return true; |
| 59 |
} |
| 60 |
static_assert(dtor_calls_dtor(), ""); |
| 61 |
} |
| 62 |
|
| 63 |
namespace PrimitiveMoveFn { |
| 64 |
/// This used to crash. |
| 65 |
void test() { |
| 66 |
const float y = 100; |
| 67 |
const float &x = y; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
/// FIXME: |
| 72 |
/// 1) This doesn't work for parameters |
| 73 |
/// 2) We need to do this for all fields in composite scenarios |
| 74 |
namespace PseudoDtor { |
| 75 |
typedef int I; |
| 76 |
constexpr bool foo() { // both-error {{never produces a constant expression}} |
| 77 |
{ |
| 78 |
int a; // both-note {{destroying object 'a' whose lifetime has already ended}} |
| 79 |
a.~I(); |
| 80 |
} |
| 81 |
return true; |
| 82 |
} |
| 83 |
|
| 84 |
int k; |
| 85 |
struct T { |
| 86 |
int n : (k.~I(), 1); // both-error {{constant expression}} \ |
| 87 |
// both-note {{visible outside that expression}} |
| 88 |
}; |
| 89 |
} |
| 90 |
|
| 91 |
/// Diagnostic differences |
| 92 |
namespace CallScope { |
| 93 |
struct Q { |
| 94 |
int n = 0; |
| 95 |
constexpr int f() const { return 0; } |
| 96 |
}; |
| 97 |
constexpr Q *out_of_lifetime(Q q) { return &q; } // both-warning {{address of stack}} \ |
| 98 |
// expected-note 2{{declared here}} |
| 99 |
constexpr int k3 = out_of_lifetime({})->n; // both-error {{must be initialized by a constant expression}} \ |
| 100 |
// expected-note {{read of variable whose lifetime has ended}} \ |
| 101 |
// ref-note {{read of object outside its lifetime}} |
| 102 |
|
| 103 |
constexpr int k4 = out_of_lifetime({})->f(); // both-error {{must be initialized by a constant expression}} \ |
| 104 |
// expected-note {{member call on variable whose lifetime has ended}} \ |
| 105 |
// ref-note {{member call on object outside its lifetime}} |
| 106 |
} |
| 107 |
|
| 108 |
namespace ExprDoubleDestroy { |
| 109 |
template <typename T> |
| 110 |
constexpr bool test() { |
| 111 |
T{}.~T(); // both-note {{lifetime has already ended}} |
| 112 |
return true; |
| 113 |
} |
| 114 |
|
| 115 |
struct S { int x; }; |
| 116 |
constexpr bool t = test<S>(); // both-error {{must be initialized by a constant expression}} \ |
| 117 |
// both-note {{in call to}} |
| 118 |
} |