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