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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
|
/* REQUIRED_ARGS: -preview=dip1021
*/
@safe:
/* TEST_OUTPUT:
---
fail_compilation/test1021.d(1009): Error: more than one mutable reference of `p` in arguments to `test1021.fooa()`
fail_compilation/test1021.d(1010): Error: mutable and const references of `p` in arguments to `test1021.foob()`
fail_compilation/test1021.d(1011): Error: mutable and const references of `p` in arguments to `test1021.fooc()`
fail_compilation/test1021.d(1013): Error: more than one mutable reference of `p` in arguments to `test1021.fooe()`
---
*/
#line 1000
void fooa(int*, int*);
void foob(const(int)*, int*);
void fooc(int*, const(int)*);
void food(const(int)*, const(int)*);
void fooe(int*, ...);
void test1(int* p)
{
fooa(p, p); // error
foob(p, p); // error
fooc(p, p); // error
food(p, p); // ok
fooe(p, p); // error
}
/***********************************/
/* TEST_OUTPUT:
---
fail_compilation/test1021.d(2010): Error: more than one mutable reference to `i` in arguments to `test1021.fopa()`
fail_compilation/test1021.d(2011): Error: mutable and const references to `i` in arguments to `test1021.fopb()`
fail_compilation/test1021.d(2012): Error: mutable and const references to `i` in arguments to `test1021.fopc()`
fail_compilation/test1021.d(2014): Error: more than one mutable reference to `i` in arguments to `test1021.fope()`
---
*/
#line 2000
void fopa(ref int, scope int*);
void fopb(ref int, scope const int*);
void fopc(ref const int, scope int*);
void fopd(ref const int, scope const int*);
inout(int) fope(ref inout int, scope int*);
void test2()
{
int i;
@trusted int* toPtr(ref int i) { return &i; }
fopa(i, toPtr(i)); // error
fopb(i, toPtr(i)); // error
fopc(i, toPtr(i)); // error
fopd(i, toPtr(i)); // ok
fope(i, toPtr(i)); // error
}
/***********************************/
/* TEST_OUTPUT:
---
fail_compilation/test1021.d(3015): Error: more than one mutable reference to `s` in arguments to `test1021.S.method()`
fail_compilation/test1021.d(3019): Error: more than one mutable reference of `c` in arguments to `test1021.C.method()`
---
*/
#line 3000
struct S
{
void method(ref S s);
}
class C
{
void method(C c);
}
void test3()
{
S s;
S* ps;
s.method(s); // error
ps.method(s); // ok
C c;
c.method(c); // error
}
/***********************************/
/* TEST_OUTPUT:
---
fail_compilation/test1021.d(4008): Error: more than one mutable reference to `i` in arguments to `test1021.test4.nested()`
---
*/
#line 4000
void test4()
{
int i, k;
int nested(ref int j)
{
return i + j;
}
nested(i); // error
nested(k); // ok
}
/***********************************/
/* TEST_OUTPUT:
---
fail_compilation/test1021.d(5012): Error: more than one mutable reference of `s` in arguments to `test1021.foo5()`
---
*/
#line 5000
struct S5
{
int i;
int* p;
}
void foo5(S5, S5);
void test5()
{
S5 s;
foo5(s, s);
}
alias A5 = void delegate() const;
void foo5(A5, A5);
void test5a()
{
A5 a;
foo5(a, a);
}
alias B5 = void function();
void foo5(B5, B5);
void test5b()
{
B5 b;
foo5(b, b);
}
struct S5c
{
void function() fp;
}
void foo5(S5c, S5c);
void test5c()
{
S5c s;
foo5(s, s);
}
|