summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gdc.test/fail_compilation/test19097.d
blob: 9c025a83ff052210c1015a84adb923eed4c3aee5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/* REQUIRED_ARGS: -preview=dip1000
 * TEST_OUTPUT:
---
fail_compilation/test19097.d(44): Error: scope variable `s` may not be returned
fail_compilation/test19097.d(48): Error: scope variable `s1` may not be returned
fail_compilation/test19097.d(77): Error: scope variable `z` assigned to `refPtr` with longer lifetime
fail_compilation/test19097.d(108): Error: scope variable `s4` may not be returned
fail_compilation/test19097.d(126): Error: scope variable `s5c` may not be returned
fail_compilation/test19097.d(130): Error: scope variable `s5m` may not be returned
fail_compilation/test19097.d(147): Error: scope variable `s6c` may not be returned
fail_compilation/test19097.d(151): Error: scope variable `s6m` may not be returned
---
 */

// Test extended return-scope / return-ref semantics, e.g. assigning to `this` or the first parameter

// https://issues.dlang.org/show_bug.cgi?id=19097

@safe:

void betty(ref scope int* r, return scope int* p)
{
    r = p;
}

void freddy(out scope int* r, return scope int* p)
{
    r = p;
}

struct S
{
    int* a;
    this(return scope int* b) scope { a = b; }

    int* c;
    void mem(return scope int* d) scope { c = d; }
}

S thorin()
{
    int i;
    S s = S(&i); // should infer scope for s
    return s;    // so this should error

    S s1;
    s1.mem(&i);
    return s1;
}

/************************/

struct S2(T)
{
    int* p;

    void silent(lazy void dg);

    void foo()
    {
        char[] name;
        silent(name = parseType());
    }

    char[] parseType(char[] name = null);
}

S2!int s2;

/************************/
struct S3
{
    int* ptr;
    void assign(ref int* refPtr, return scope int* z) scope @safe
    {
        this.ptr = z; // allowed, first ref
        refPtr = z; // should not be allowed
    }
}

int* escape() @safe
{
    int local;

    S3 escapeThis;
    int* escapeRef;

    escapeThis.assign(escapeRef, &local);

    return escapeRef;
}

/************************/
// https://issues.dlang.org/show_bug.cgi?id=22837
struct S4
{
    int* p;
    this(int dummy, return scope int* p) @safe
    {
        this.p = p;
    }
}

int* escape2()
{
    int x;
    auto s4 = S4(0, &x);
    return s4.p;
}

/************************/
// https://issues.dlang.org/show_bug.cgi?id=22801
struct S5
{
    int* a;
    this(return ref int b) { a = &b; }

    int* c;
    void mem(return ref int d) scope { c = &d; }
}

S5 frerin()
{
    int i;
    S5 s5c = S5(i); // should infer scope for s
    return s5c;    // so this should error

    S5 s5m;
    s5m.mem(i);
    return s5m;
}


struct S6
{
    int** a;
    this(return ref int* b) { a = &b; }

    int** c;
    void mem(return ref int* d) scope { c = &d; }
}

S6 dis()
{
    int* i = null;
    S6 s6c = S6(i); // should infer scope for s
    return s6c;    // so this should error

    S6 s6m;
    s6m.mem(i);
    return s6m;
}