| 1 |
module test18545; |
| 2 |
|
| 3 |
enum Constness |
| 4 |
{ |
| 5 |
Mutable, |
| 6 |
Const, |
| 7 |
Immutable, |
| 8 |
} |
| 9 |
|
| 10 |
enum Cases = [Constness.Mutable, Constness.Const, Constness.Immutable]; |
| 11 |
|
| 12 |
void main() |
| 13 |
{ |
| 14 |
static foreach (from; Cases) |
| 15 |
{ |
| 16 |
static foreach (to; Cases) |
| 17 |
{ |
| 18 |
test!(from, to)(); |
| 19 |
} |
| 20 |
} |
| 21 |
} |
| 22 |
|
| 23 |
void test(Constness from, Constness to)() |
| 24 |
{ |
| 25 |
struct S { |
| 26 |
int i; |
| 27 |
|
| 28 |
@property int get() const { return 0; } |
| 29 |
|
| 30 |
alias get this; |
| 31 |
} |
| 32 |
|
| 33 |
static if (from == Constness.Mutable) |
| 34 |
{ |
| 35 |
alias ConstS = S; |
| 36 |
} |
| 37 |
else static if (from == Constness.Const) |
| 38 |
{ |
| 39 |
alias ConstS = const(S); |
| 40 |
} |
| 41 |
else |
| 42 |
{ |
| 43 |
alias ConstS = immutable(S); |
| 44 |
} |
| 45 |
|
| 46 |
ConstS s1 = S(42); |
| 47 |
|
| 48 |
// this should reinterpret-cast, NOT call the implicit constructor with .get! |
| 49 |
static if (to == Constness.Mutable) |
| 50 |
{ |
| 51 |
auto s2 = cast() s1; |
| 52 |
} |
| 53 |
else static if (to == Constness.Const) |
| 54 |
{ |
| 55 |
const s2 = cast(const) s1; |
| 56 |
} |
| 57 |
else static if (to == Constness.Immutable) |
| 58 |
{ |
| 59 |
immutable s2 = cast(immutable) s1; |
| 60 |
} |
| 61 |
|
| 62 |
assert(s2.i == s1.i, "Bug 18545 occurred casting from "~from.stringof~" to "~to.stringof); |
| 63 |
} |