test4090a()1 void test4090a()
2 {
3     // for the mutable elements
4     {
5         int[] arr = [1,2,3];
6 
7         // inference + qualifier
8         foreach (          x; arr) static assert(is(typeof(x) == int));
9         foreach (    const x; arr) static assert(is(typeof(x) == const int));
10         foreach (immutable x; arr) static assert(is(typeof(x) == immutable int));
11 
12         // inference + qualifier + ref
13         foreach (          ref x; arr) static assert(is(typeof(x) == int));
14         foreach (    const ref x; arr) static assert(is(typeof(x) == const int));
15       static assert(!__traits(compiles, {
16         foreach (immutable ref x; arr) {}
17       }));
18 
19         // with exact type + qualifier
20         foreach (          int x; arr) static assert(is(typeof(x) == int));
21         foreach (    const int x; arr) static assert(is(typeof(x) == const int));
22         foreach (immutable int x; arr) static assert(is(typeof(x) == immutable int));
23 
24         // with exact type + qualifier + ref
25         foreach (          ref int x; arr) static assert(is(typeof(x) == int));
26         foreach (    const ref int x; arr) static assert(is(typeof(x) == const int));
27       static assert(!__traits(compiles, {
28         foreach (immutable ref int x; arr) {}
29       }));
30 
31         // convertible type + qualifier
32         foreach (          double x; arr) static assert(is(typeof(x) == double));
33         foreach (    const double x; arr) static assert(is(typeof(x) == const double));
34         foreach (immutable double x; arr) static assert(is(typeof(x) == immutable double));
35 
36         // convertible type + qualifier + ref
37       static assert(!__traits(compiles, {
38         foreach (          ref double x; arr) {}
39       }));
40       static assert(!__traits(compiles, {
41         foreach (    const ref double x; arr) {}
42       }));
43       static assert(!__traits(compiles, {
44         foreach (immutable ref double x; arr) {}
45       }));
46     }
47     // for the immutable elements
48     {
49         immutable(int)[] iarr = [1,2,3];
50 
51         // inference + qualifier
52         foreach (          x; iarr) static assert(is(typeof(x) == immutable int));  // same as variable declaration
53         foreach (    const x; iarr) static assert(is(typeof(x) == immutable int));  // same as variable declaration
54         foreach (immutable x; iarr) static assert(is(typeof(x) == immutable int));
55 
56         // inference + qualifier + ref
57         foreach (          ref x; iarr) static assert(is(typeof(x) == immutable int));  // same as variable declaration
58         foreach (    const ref x; iarr) static assert(is(typeof(x) == immutable int));  // same as variable declaration
59         foreach (immutable ref x; iarr) static assert(is(typeof(x) == immutable int));
60 
61         // with exact type + qualifier
62         foreach (          int x; iarr) static assert(is(typeof(x) == int));
63         foreach (    const int x; iarr) static assert(is(typeof(x) == const int));
64         foreach (immutable int x; iarr) static assert(is(typeof(x) == immutable int));
65 
66         // with exact type + qualifier + ref
67       static assert(!__traits(compiles, {
68         foreach (          ref int x; iarr) {}
69       }));
70         foreach (    const ref int x; iarr) static assert(is(typeof(x) == const int));
71         foreach (immutable ref int x; iarr) static assert(is(typeof(x) == immutable int));
72 
73         // convertible type + qualifier
74         foreach (          double x; iarr) static assert(is(typeof(x) == double));
75         foreach (    const double x; iarr) static assert(is(typeof(x) == const double));
76         foreach (immutable double x; iarr) static assert(is(typeof(x) == immutable double));
77 
78         // convertible type + qualifier + ref
79       static assert(!__traits(compiles, {
80         foreach (ref double x; iarr) {}
81       }));
82       static assert(!__traits(compiles, {
83         foreach (const ref double x; iarr) {}
84       }));
85       static assert(!__traits(compiles, {
86         foreach (immutable ref double x; iarr) {}
87       }));
88     }
89 }
90 
test4090b()91 void test4090b()
92 {
93     // for the key
94     {
95         int[] arr = [1,2,3];
96 
97         // inference + qualifier
98         foreach (          i, x; arr) static assert(is(typeof(i) == size_t));
99         foreach (    const i, x; arr) static assert(is(typeof(i) == const size_t));
100         foreach (immutable i, x; arr) static assert(is(typeof(i) == immutable size_t));
101 
102         // inference + qualifier + ref
103         foreach (          ref i, x; arr) static assert(is(typeof(i) == size_t));
104         foreach (    const ref i, x; arr) static assert(is(typeof(i) == const size_t));
105       static assert(!__traits(compiles, {
106         foreach (immutable ref i, x; arr) {}
107       }));
108 
109         // with exact type + qualifier
110         foreach (          size_t i, x; arr) static assert(is(typeof(i) == size_t));
111         foreach (    const size_t i, x; arr) static assert(is(typeof(i) == const size_t));
112         foreach (immutable size_t i, x; arr) static assert(is(typeof(i) == immutable size_t));
113 
114         // with exact type + qualifier + ref
115         foreach (          ref size_t i, x; arr) static assert(is(typeof(i) == size_t));
116         foreach (    const ref size_t i, x; arr) static assert(is(typeof(i) == const size_t));
117       static assert(!__traits(compiles, {
118         foreach (immutable ref size_t i, x; arr) {}
119       }));
120     }
121 
122     // for the mutable elements
123     {
124         int[] arr = [1,2,3];
125 
126         // inference + qualifier
127         foreach (i,           x; arr) static assert(is(typeof(x) == int));
128         foreach (i,     const x; arr) static assert(is(typeof(x) == const int));
129         foreach (i, immutable x; arr) static assert(is(typeof(x) == immutable int));
130 
131         // inference + qualifier + ref
132         foreach (i,           ref x; arr) static assert(is(typeof(x) == int));
133         foreach (i,     const ref x; arr) static assert(is(typeof(x) == const int));
134       static assert(!__traits(compiles, {
135         foreach (i, immutable ref x; arr) {}
136       }));
137 
138         // with exact type + qualifier
139         foreach (i,           int x; arr) static assert(is(typeof(x) == int));
140         foreach (i,     const int x; arr) static assert(is(typeof(x) == const int));
141         foreach (i, immutable int x; arr) static assert(is(typeof(x) == immutable int));
142 
143         // with exact type + qualifier + ref
144         foreach (i,           ref int x; arr) static assert(is(typeof(x) == int));
145         foreach (i,     const ref int x; arr) static assert(is(typeof(x) == const int));
146       static assert(!__traits(compiles, {
147         foreach (i, immutable ref int x; arr) {}
148       }));
149 
150         // convertible type + qualifier
151         foreach (i,           double x; arr) static assert(is(typeof(x) == double));
152         foreach (i,     const double x; arr) static assert(is(typeof(x) == const double));
153         foreach (i, immutable double x; arr) static assert(is(typeof(x) == immutable double));
154 
155         // convertible type + qualifier + ref
156       static assert(!__traits(compiles, {
157         foreach (i,           ref double x; arr) {}
158       }));
159       static assert(!__traits(compiles, {
160         foreach (i,     const ref double x; arr) {}
161       }));
162       static assert(!__traits(compiles, {
163         foreach (i, immutable ref double x; arr) {}
164       }));
165     }
166     // for the immutable elements
167     {
168         immutable(int)[] iarr = [1,2,3];
169 
170         // inference + qualifier
171         foreach (i,           x; iarr) static assert(is(typeof(x) == immutable int));  // same as variable declaration
172         foreach (i,     const x; iarr) static assert(is(typeof(x) == immutable int));  // same as variable declaration
173         foreach (i, immutable x; iarr) static assert(is(typeof(x) == immutable int));
174 
175         // inference + qualifier + ref
176         foreach (i,           ref x; iarr) static assert(is(typeof(x) == immutable int));  // same as variable declaration
177         foreach (i,     const ref x; iarr) static assert(is(typeof(x) == immutable int));  // same as variable declaration
178         foreach (i, immutable ref x; iarr) static assert(is(typeof(x) == immutable int));
179 
180         // with exact type + qualifier
181         foreach (i,           int x; iarr) static assert(is(typeof(x) == int));
182         foreach (i,     const int x; iarr) static assert(is(typeof(x) == const int));
183         foreach (i, immutable int x; iarr) static assert(is(typeof(x) == immutable int));
184 
185         // with exact type + qualifier + ref
186       static assert(!__traits(compiles, {
187         foreach (i,           ref int x; iarr) {}
188       }));
189         foreach (i,     const ref int x; iarr) static assert(is(typeof(x) == const int));
190         foreach (i, immutable ref int x; iarr) static assert(is(typeof(x) == immutable int));
191 
192         // convertible type + qualifier
193         foreach (i      ,     double x; iarr) static assert(is(typeof(x) == double));
194         foreach (i,     const double x; iarr) static assert(is(typeof(x) == const double));
195         foreach (i, immutable double x; iarr) static assert(is(typeof(x) == immutable double));
196 
197         // convertible type + qualifier + ref
198       static assert(!__traits(compiles, {
199         foreach (i, ref double x; iarr) {}
200       }));
201       static assert(!__traits(compiles, {
202         foreach (i, const ref double x; iarr) {}
203       }));
204       static assert(!__traits(compiles, {
205         foreach (i, immutable ref double x; iarr) {}
206       }));
207     }
208 }
209 
test4090c()210 void test4090c()
211 {
212     foreach (          x; 1..11) static assert(is(typeof(x) == int));
213     foreach (    const x; 1..11) static assert(is(typeof(x) == const int));
214     foreach (immutable x; 1..11) static assert(is(typeof(x) == immutable int));
215 
216     foreach (          int x; 1..11) static assert(is(typeof(x) == int));
217     foreach (    const int x; 1..11) static assert(is(typeof(x) == const int));
218     foreach (immutable int x; 1..11) static assert(is(typeof(x) == immutable int));
219 
220     foreach (          ref x; 1..11) static assert(is(typeof(x) == int));
221     foreach (    const ref x; 1..11) static assert(is(typeof(x) == const int));
222   static assert(!__traits(compiles, {
223     foreach (immutable ref x; 1..11) {}
224   }));
225 
226     foreach (          double x; 1..11) static assert(is(typeof(x) == double));
227     foreach (    const double x; 1..11) static assert(is(typeof(x) == const double));
228     foreach (immutable double x; 1..11) static assert(is(typeof(x) == immutable double));
229 
230     foreach (          ref double x; 1..11) static assert(is(typeof(x) == double));
231     foreach (    const ref double x; 1..11) static assert(is(typeof(x) == const double));
232   static assert(!__traits(compiles, {
233     foreach (immutable ref double x; 1..11) {}
234   }));
235 }
236