summaryrefslogtreecommitdiff
path: root/gcc/testsuite/gdc.test/fail_compilation/fail_scope.d
blob: 41a8c2d85e3bdfc91b605ed8dcb5b28d2328eda0 (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
/*
REQUIRED_ARGS:
TEST_OUTPUT:
---
fail_compilation/fail_scope.d(44): Error: returning `cast(char[])string` escapes a reference to local variable `string`
fail_compilation/fail_scope.d(62): Error: returning `s.bar()` escapes a reference to local variable `s`
fail_compilation/fail_scope.d(73): Error: `fail_scope.foo8` called with argument types `(int)` matches both:
fail_compilation/fail_scope.d(67):     `fail_scope.foo8(ref int x)`
and:
fail_compilation/fail_scope.d(68):     `fail_scope.foo8(return ref int x)`
fail_compilation/fail_scope.d(81): Error: returning `& string` escapes a reference to local variable `string`
fail_compilation/fail_scope.d(91): Error: returning `cast(int[])a` escapes a reference to local variable `a`
fail_compilation/fail_scope.d(99): Error: returning `cast(int[])a` escapes a reference to local variable `a`
fail_compilation/fail_scope.d(107): Deprecation: escaping reference to outer local variable `x`
fail_compilation/fail_scope.d(126): Error: returning `s.bar()` escapes a reference to local variable `s`
fail_compilation/fail_scope.d(136): Error: returning `foo16226(i)` escapes a reference to local variable `i`
---
//fail_compilation/fail_scope.d(30): Error: scope variable `da` may not be returned
//fail_compilation/fail_scope.d(32): Error: scope variable `o` may not be returned
//fail_compilation/fail_scope.d(33): Error: scope variable `dg` may not be returned
//fail_compilation/fail_scope.d(35): Error: scope variable `da` may not be returned
//fail_compilation/fail_scope.d(37): Error: scope variable `o` may not be returned
//fail_compilation/fail_scope.d(38): Error: scope variable `dg` may not be returned
//fail_compilation/fail_scope.d(40): Error: scope variable `p` may not be returned
*/

alias int delegate() dg_t;

int[]  checkEscapeScope1(scope int[]  da) { return da; }
int[3] checkEscapeScope2(scope int[3] sa) { return sa; }
Object checkEscapeScope3(scope Object o)  { return o;  }
dg_t   checkEscapeScope4(scope dg_t   dg) { return dg; }

int[]  checkEscapeScope1() { scope int[]  da = [];           return da; }
int[3] checkEscapeScope2() { scope int[3] sa = [1,2,3];      return sa; }
Object checkEscapeScope3() { scope Object  o = new Object;   return o;  }   // same with fail7294.d
dg_t   checkEscapeScope4() { scope dg_t   dg = () => 1;      return dg; }

int* test(scope int* p) @safe { return p; }

char[] foo140()
{
    char[4] string = "abcd";
    return string;
}

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

struct S
{
    int x;

    ref int bar() return
    {
        return x;
    }
}

ref int test()
{
    S s;
    return s.bar();
}

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

ref int foo8(ref int x);
ref int foo8(return ref int x);

void testover()
{
    int x;
    foo8(x);
}

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

char* fail141()
{
    char[4] string = "abcd";
    return string.ptr;
}

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

int[] test1313b()
out{}
do
{
    int[2] a;
    return a;
}

int[] test1313a()
//out{}
do
{
    int[2] a;
    return a;
}

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

ref int fun15192(ref int x) @safe
{
    ref int bar(){ return x; }
    return bar();
}

ref int fun15192_2(return ref int x) @safe
{
    ref int bar(){ return x; }
    return bar();
}

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

ref int foo15193()@safe{
    struct S{
        int x;
        ref int bar() { return x; }
    }
    S s;
    return s.bar();
}


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

ref int test16226() @safe
{
    int i;
    return foo16226(i);
}


ref foo16226(ref int bar) @safe
{
    return bar;
}