Debian Codegraph @

gcc/testsuite/gdc.test/fail_compilation/fail20183.d

raw
1 /* REQUIRED_ARGS: -preview=dip1000
2 TEST_OUTPUT:
3 ---
4 fail_compilation/fail20183.d(1016): Error: function `addr` is not callable using argument types `(int)`
5 fail_compilation/fail20183.d(1016): cannot pass rvalue argument `S(0).i` of type `int` to parameter `return ref int b`
6 fail_compilation/fail20183.d(1004): `fail20183.addr(return ref int b)` declared here
7 ---
8 */
9
10 #line 1000
11
12 // https://issues.dlang.org/show_bug.cgi?id=20183
13 @safe:
14
15 int* addr(return ref int b) { return &b; }
16
17 struct S
18 {
19 int i;
20 S* addrOf() return => &this;
21 }
22
23 S s() { return S(); }
24
25 void test()
26 {
27 scope int* p = addr(S().i); // struct literal
28 scope int* q = addr(s().i); // struct temporary
29 scope S* r = S().addrOf(); // struct literal
30 }
31
32 /*
33 TEST_OUTPUT:
34 ---
35 fail_compilation/fail20183.d(1017): Error: assigning address of expression temporary returned by `s()` to `q` with longer lifetime is not allowed in a `@safe` function
36 fail_compilation/fail20183.d(1018): Error: assigning address of struct literal `S(0)` to `r` with longer lifetime is not allowed in a `@safe` function
37 fail_compilation/fail20183.d(1107): Error: assigning address of expression temporary returned by `s()` to `this.ptr` with longer lifetime is not allowed in a `@safe` function
38 ---
39 */
40 #line 1100
41
42 class Foo
43 {
44 int* ptr;
45
46 this() @safe
47 {
48 ptr = addr(s().i); // struct literal
49 }
50 }