| 1 |
/* |
| 2 |
RUN_OUTPUT: |
| 3 |
--- |
| 4 |
Success |
| 5 |
--- |
| 6 |
*/ |
| 7 |
// REQUIRED_ARGS: |
| 8 |
|
| 9 |
// Test array bounds checking |
| 10 |
|
| 11 |
import core.exception; |
| 12 |
extern(C) int printf(const char*, ...); |
| 13 |
|
| 14 |
template TypeTuple(T...) { alias T TypeTuple; } |
| 15 |
|
| 16 |
/******************************************/ |
| 17 |
|
| 18 |
const int[10] foos = [1,2,3,4,5,6,7,8,9,10]; |
| 19 |
const int[] food = [21,22,23,24,25,26,27,28,29,30]; |
| 20 |
const int *foop = cast(int*) foos; |
| 21 |
|
| 22 |
static int x = 2; |
| 23 |
|
| 24 |
int index() |
| 25 |
{ |
| 26 |
return x++; |
| 27 |
} |
| 28 |
|
| 29 |
int tests(int i) |
| 30 |
{ |
| 31 |
return foos[index()]; |
| 32 |
} |
| 33 |
|
| 34 |
int testd(int i) |
| 35 |
{ |
| 36 |
return food[index()]; |
| 37 |
} |
| 38 |
|
| 39 |
int testp(int i) |
| 40 |
{ |
| 41 |
return foop[i]; |
| 42 |
} |
| 43 |
|
| 44 |
const(int)[] slices(int lwr, int upr) |
| 45 |
{ |
| 46 |
return foos[lwr .. upr]; |
| 47 |
} |
| 48 |
|
| 49 |
const(int)[] sliced(int lwr, int upr) |
| 50 |
{ |
| 51 |
return food[lwr .. upr]; |
| 52 |
} |
| 53 |
|
| 54 |
const(int)[] slicep(int lwr, int upr) |
| 55 |
{ |
| 56 |
return foop[lwr .. upr]; |
| 57 |
} |
| 58 |
|
| 59 |
void test1() |
| 60 |
{ |
| 61 |
int i; |
| 62 |
|
| 63 |
i = tests(0); |
| 64 |
assert(i == 3); |
| 65 |
|
| 66 |
i = testd(0); |
| 67 |
assert(i == 24); |
| 68 |
|
| 69 |
i = testp(1); |
| 70 |
assert(i == 2); |
| 71 |
|
| 72 |
x = 10; |
| 73 |
try |
| 74 |
{ |
| 75 |
i = tests(0); |
| 76 |
} |
| 77 |
catch (RangeError a) |
| 78 |
{ |
| 79 |
i = 73; |
| 80 |
} |
| 81 |
assert(i == 73); |
| 82 |
|
| 83 |
x = -1; |
| 84 |
try |
| 85 |
{ |
| 86 |
i = testd(0); |
| 87 |
} |
| 88 |
catch (RangeError a) |
| 89 |
{ |
| 90 |
i = 37; |
| 91 |
} |
| 92 |
assert(i == 37); |
| 93 |
|
| 94 |
const(int)[] r; |
| 95 |
|
| 96 |
r = slices(3,5); |
| 97 |
assert(r[0] == foos[3]); |
| 98 |
assert(r[1] == foos[4]); |
| 99 |
|
| 100 |
r = sliced(3,5); |
| 101 |
assert(r[0] == food[3]); |
| 102 |
assert(r[1] == food[4]); |
| 103 |
|
| 104 |
r = slicep(3,5); |
| 105 |
assert(r[0] == foos[3]); |
| 106 |
assert(r[1] == foos[4]); |
| 107 |
|
| 108 |
try |
| 109 |
{ |
| 110 |
i = 7; |
| 111 |
r = slices(5,3); |
| 112 |
} |
| 113 |
catch (RangeError a) |
| 114 |
{ |
| 115 |
i = 53; |
| 116 |
} |
| 117 |
assert(i == 53); |
| 118 |
|
| 119 |
try |
| 120 |
{ |
| 121 |
i = 7; |
| 122 |
r = slices(5,11); |
| 123 |
} |
| 124 |
catch (RangeError a) |
| 125 |
{ |
| 126 |
i = 53; |
| 127 |
} |
| 128 |
assert(i == 53); |
| 129 |
|
| 130 |
try |
| 131 |
{ |
| 132 |
i = 7; |
| 133 |
r = sliced(5,11); |
| 134 |
} |
| 135 |
catch (RangeError a) |
| 136 |
{ |
| 137 |
i = 53; |
| 138 |
} |
| 139 |
assert(i == 53); |
| 140 |
|
| 141 |
try |
| 142 |
{ |
| 143 |
i = 7; |
| 144 |
r = slicep(5,3); |
| 145 |
} |
| 146 |
catch (RangeError a) |
| 147 |
{ |
| 148 |
i = 53; |
| 149 |
} |
| 150 |
assert(i == 53); |
| 151 |
|
| 152 |
// Take side effects into account |
| 153 |
x = 1; |
| 154 |
r = foos[index() .. 3]; |
| 155 |
assert(x == 2); |
| 156 |
assert(r[0] == foos[1]); |
| 157 |
assert(r[1] == foos[2]); |
| 158 |
|
| 159 |
r = foos[1 .. index()]; |
| 160 |
assert(r.length == 1); |
| 161 |
assert(x == 3); |
| 162 |
assert(r[0] == foos[1]); |
| 163 |
|
| 164 |
x = 1; |
| 165 |
r = food[index() .. 3]; |
| 166 |
assert(x == 2); |
| 167 |
assert(r[0] == food[1]); |
| 168 |
assert(r[1] == food[2]); |
| 169 |
|
| 170 |
r = food[1 .. index()]; |
| 171 |
assert(r.length == 1); |
| 172 |
assert(x == 3); |
| 173 |
assert(r[0] == food[1]); |
| 174 |
|
| 175 |
x = 1; |
| 176 |
r = foop[index() .. 3]; |
| 177 |
assert(x == 2); |
| 178 |
assert(r[0] == foop[1]); |
| 179 |
assert(r[1] == foop[2]); |
| 180 |
|
| 181 |
r = foop[1 .. index()]; |
| 182 |
assert(r.length == 1); |
| 183 |
assert(x == 3); |
| 184 |
assert(r[0] == foop[1]); |
| 185 |
} |
| 186 |
|
| 187 |
/******************************************/ |
| 188 |
// https://issues.dlang.org/show_bug.cgi?id=13976 |
| 189 |
|
| 190 |
void test13976() |
| 191 |
{ |
| 192 |
int[] da = new int[](10); |
| 193 |
int[10] sa; |
| 194 |
enum size_t two = 2; |
| 195 |
enum size_t five = 5; |
| 196 |
size_t lb = 0; // upperInRange |
| 197 |
size_t ub = 9; // | lowerLessThan |
| 198 |
// | | check code |
| 199 |
{ auto s = da[lb .. ub]; } // 0 0 (ub <= $ && lb <= ub ) |
| 200 |
{ auto s = da[1 .. ub]; } // 0 0 (ub <= $ && 1 <= ub ) |
| 201 |
{ auto s = da[lb .. 10]; } // 0 0 (10 <= $ && lb <= 10 ) |
| 202 |
{ auto s = da[1 .. ub%5]; } // 0 0 (ub%5 <= $ && 1 <= ub%5) |
| 203 |
|
| 204 |
{ auto s = da[lb .. ub]; } // 0 0 (ub <= $ && lb <= ub ) |
| 205 |
{ auto s = da[0 .. ub]; } // 0 1 (ub <= $ ) |
| 206 |
{ auto s = da[lb .. 10]; } // 0 0 (10 <= $ && lb <= 10 ) |
| 207 |
{ auto s = da[0 .. ub%5]; } // 0 1 (ub%5 <= $ ) |
| 208 |
|
| 209 |
{ auto s = da[0 .. 0]; } // 1 1 NULL |
| 210 |
{ auto s = da[0 .. $]; } // 1 1 NULL |
| 211 |
{ auto s = da[1 .. $]; } // 1 0 ( 1 <= $ ) |
| 212 |
{ auto s = da[$ .. $]; } // 1 0 ( $ <= $ ) |
| 213 |
{ auto s = da[0 .. $/two]; } // 0 1 ($/2 <= $ ) |
| 214 |
{ auto s = da[$/two .. $]; } // 1 0 ( $/2 <= $ ) |
| 215 |
{ auto s = da[$/five .. $/two]; } // 0 0 ($/2 <= $ && $/5 <= $/2) |
| 216 |
|
| 217 |
{ auto s = sa[lb .. ub]; } // 0 0 (ub <= 10 && lb <= ub ) |
| 218 |
{ auto s = sa[1 .. ub]; } // 0 0 (ub <= 10 && 1 <= ub ) |
| 219 |
{ auto s = sa[lb .. 10]; } // 1 0 ( lb <= 10 ) |
| 220 |
{ auto s = sa[1 .. ub%5]; } // 1 0 ( 1 <= ub%5) |
| 221 |
|
| 222 |
{ auto s = sa[lb .. ub]; } // 0 0 (ub <= 10 && lb <= ub ) |
| 223 |
{ auto s = sa[0 .. ub]; } // 0 1 (ub <= 10 ) |
| 224 |
{ auto s = sa[lb .. 10]; } // 1 0 ( lb <= 10 ) |
| 225 |
{ auto s = sa[0 .. ub%5]; } // 1 1 NULL |
| 226 |
|
| 227 |
{ auto s = sa[0 .. 0]; } // 1 1 NULL |
| 228 |
{ auto s = sa[0 .. $]; } // 1 1 NULL |
| 229 |
{ auto s = sa[1 .. $]; } // 1 1 NULL |
| 230 |
{ auto s = sa[$ .. $]; } // 1 1 NULL |
| 231 |
{ auto s = sa[0 .. $/two]; } // 1 1 NULL |
| 232 |
{ auto s = sa[$/two .. $]; } // 1 1 NULL |
| 233 |
{ auto s = sa[$/five .. $/two]; } // 1 1 NULL |
| 234 |
|
| 235 |
int* p = new int[](10).ptr; |
| 236 |
{ auto s = p[0 .. ub]; } // 1 1 NULL |
| 237 |
{ auto s = p[lb .. ub]; } // 1 0 (lb <= ub ) |
| 238 |
{ auto s = p[0 .. ub%5]; } // 1 1 NULL |
| 239 |
{ auto s = p[1 .. ub%5]; } // 1 0 (1 <= ub%5) |
| 240 |
{ auto s = p[0 .. 0]; } // 1 1 NULL |
| 241 |
} |
| 242 |
|
| 243 |
/******************************************/ |
| 244 |
// https://issues.dlang.org/show_bug.cgi?id=3652 |
| 245 |
|
| 246 |
void test3652() |
| 247 |
{ |
| 248 |
int foo(int[4] x) |
| 249 |
{ |
| 250 |
return x[0] + x[1] * x[2] - x[3]; |
| 251 |
} |
| 252 |
|
| 253 |
int[] xs = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]; |
| 254 |
|
| 255 |
// simple case |
| 256 |
foo(xs[0 .. 4]); |
| 257 |
|
| 258 |
version(none) |
| 259 |
{ |
| 260 |
// Need deformation of formula and detection of base point |
| 261 |
int x = 0; |
| 262 |
int y = 0; |
| 263 |
foreach (i; 0 .. 4) |
| 264 |
{ |
| 265 |
x += foo(xs[i .. i + 4]); |
| 266 |
y += foo(xs[(i*4+10)/2 .. (i*8>>1)/2+9]); |
| 267 |
// lwr = (i*4 + 10)/2 = i*4/2 + 10/2 = (i*2+5) |
| 268 |
// upr = (i*8>>1)/2 + 5 = (i*4/2) + 5 = i*2 + 9 = (i*2+5) + 4 |
| 269 |
} |
| 270 |
assert(x == (0,1,2,3) + (1,2,3, 4) + (2, 3, 4, 5) + ( 3, 4, 5, 6)); |
| 271 |
assert(y == (5,6,7,8) + (7,8,9,10) + (9,10,11,12) + (11,12,13,14)); |
| 272 |
} |
| 273 |
} |
| 274 |
|
| 275 |
void test3652a() @safe |
| 276 |
{ |
| 277 |
string str = "aaaabbbbccccdddd"; |
| 278 |
//printf("str.ptr = %p\n", str.ptr); |
| 279 |
|
| 280 |
void foo(ref const(char)[16] buf) |
| 281 |
{ |
| 282 |
//printf("buf.ptr = %p\n", buf.ptr); |
| 283 |
assert(buf.ptr is str.ptr); |
| 284 |
} |
| 285 |
|
| 286 |
// can check length at runtime |
| 287 |
assert(str.length == 16); |
| 288 |
|
| 289 |
// compiler can check the length of string literal, so |
| 290 |
// conversion from immutable(char)[] to ref const(char)[16] is allowed; |
| 291 |
static assert(__traits(compiles, foo("aaaabbbbccccdddd"))); |
| 292 |
|
| 293 |
// OK, correctly rejected by the compiler. |
| 294 |
static assert(!__traits(compiles, foo(str[]))); |
| 295 |
|
| 296 |
// Ugly, furthermore does not work in safe code! |
| 297 |
//foo(*cast(const(char)[16]*)(str[0..16].ptr)); |
| 298 |
|
| 299 |
// New: compiler can check the length of slice, but currently it is not allowed. |
| 300 |
enum size_t m = 0; |
| 301 |
size_t calc(){ return 0; } |
| 302 |
foo(str[0 .. 16]); |
| 303 |
foo(str[m .. 16]); |
| 304 |
//foo(str[calc() .. 16]); // with CTFE |
| 305 |
|
| 306 |
// If boundaries cannot be calculated in compile time, it's rejected. |
| 307 |
size_t n; |
| 308 |
size_t calc2(){ return n; } |
| 309 |
static assert(!__traits(compiles, foo(str[n .. 16]))); |
| 310 |
static assert(!__traits(compiles, foo(str[calc2() .. 16]))); |
| 311 |
|
| 312 |
void hoo1(size_t dim)(char[dim]) { static assert(dim == 2); } |
| 313 |
void hoo2(char[2]) {} |
| 314 |
void hoo3(size_t dim)(ref char[dim]) {} |
| 315 |
void hoo4(ref char[2]) {} |
| 316 |
hoo1(str[0 .. 2]); |
| 317 |
hoo2(str[0 .. 2]); |
| 318 |
static assert(!__traits(compiles, hoo3(str[0 .. 2]))); |
| 319 |
static assert(!__traits(compiles, hoo4(str[0 .. 2]))); |
| 320 |
} |
| 321 |
void test3652b() @safe |
| 322 |
{ |
| 323 |
int[] da = [1,2,3,4,5]; |
| 324 |
|
| 325 |
void bar(int[3] sa1, ref int[3] sa2) |
| 326 |
{ |
| 327 |
assert(sa1 == [1,2,3] && sa1.ptr !is da.ptr); |
| 328 |
assert(sa2 == [1,2,3] && sa2.ptr is da.ptr); |
| 329 |
} |
| 330 |
bar(da[0..3], da[0..3]); |
| 331 |
static assert(!__traits(compiles, bar(da[0..4], da[0..4]))); |
| 332 |
|
| 333 |
void baz1(T)(T[3] sa1, ref T[3] sa2) |
| 334 |
{ |
| 335 |
assert(sa1 == [1,2,3] && sa1.ptr !is da.ptr); |
| 336 |
assert(sa2 == [1,2,3] && sa2.ptr is da.ptr); |
| 337 |
} |
| 338 |
void baz2(T, size_t dim)(T[dim] sa1, ref T[dim] sa2, size_t result) |
| 339 |
{ |
| 340 |
assert(dim == result); |
| 341 |
static if (dim == 3) |
| 342 |
{ |
| 343 |
assert(sa1 == [1,2,3] && sa1.ptr !is da.ptr); |
| 344 |
assert(sa2 == [1,2,3] && sa2.ptr is da.ptr); |
| 345 |
} |
| 346 |
else |
| 347 |
{ |
| 348 |
assert(sa1 == [1,2,3,4] && sa1.ptr !is da.ptr); |
| 349 |
assert(sa2 == [1,2,3,4] && sa2.ptr is da.ptr); |
| 350 |
} |
| 351 |
} |
| 352 |
baz1(da[0..3], da[0..3]); |
| 353 |
static assert(!__traits(compiles, baz1(da[0..4], da[0..4]))); |
| 354 |
baz2(da[0..3], da[0..3], 3); |
| 355 |
baz2(da[0..4], da[0..4], 4); |
| 356 |
|
| 357 |
void hoo1(size_t dim)(int[dim]) { static assert(dim == 2); } |
| 358 |
void hoo2(int[2]) {} |
| 359 |
void hoo3(size_t dim)(ref int[dim]) {} |
| 360 |
void hoo4(ref int[2]) {} |
| 361 |
hoo1(da.idup[0 .. 2]); |
| 362 |
hoo2(da.idup[0 .. 2]); |
| 363 |
static assert(!__traits(compiles, hoo3(da.idup[0 .. 2]))); |
| 364 |
static assert(!__traits(compiles, hoo4(da.idup[0 .. 2]))); |
| 365 |
} |
| 366 |
|
| 367 |
/**********************************/ |
| 368 |
// https://issues.dlang.org/show_bug.cgi?id=9654 |
| 369 |
|
| 370 |
auto foo9654a(ref char[8] str) { return str; } |
| 371 |
auto foo9654b(ref const char[8] str) { return str; } |
| 372 |
auto foo9654c(ref immutable char[8] str) { return str; } |
| 373 |
static assert(!is(typeof(foo9654a("testinfo")))); |
| 374 |
static assert( is(typeof(foo9654b("testinfo")) == const char[8])); |
| 375 |
static assert( is(typeof(foo9654c("testinfo")) == immutable char[8])); |
| 376 |
|
| 377 |
auto bar9654a(T)(ref T[8] str) { return str; static assert(is(T == immutable char)); } |
| 378 |
auto bar9654b(T)(ref const T[8] str) { return str; static assert(is(T == char)); } |
| 379 |
auto bar9654c(T)(ref immutable T[8] str) { return str; static assert(is(T == char)); } |
| 380 |
static assert( is(typeof(bar9654a("testinfo")) == immutable char[8])); |
| 381 |
static assert( is(typeof(bar9654b("testinfo")) == const char[8])); |
| 382 |
static assert( is(typeof(bar9654c("testinfo")) == immutable char[8])); |
| 383 |
|
| 384 |
auto baz9654a(T, size_t dim)(ref T[dim] str) { return str; static assert(is(T == immutable char)); } |
| 385 |
auto baz9654b(T, size_t dim)(ref const T[dim] str) { return str; static assert(is(T == char)); } |
| 386 |
auto baz9654c(T, size_t dim)(ref immutable T[dim] str) { return str; static assert(is(T == char)); } |
| 387 |
static assert( is(typeof(baz9654a("testinfo")) == immutable char[8])); |
| 388 |
static assert( is(typeof(baz9654b("testinfo")) == const char[8])); |
| 389 |
static assert( is(typeof(baz9654c("testinfo")) == immutable char[8])); |
| 390 |
|
| 391 |
/******************************************/ |
| 392 |
// https://issues.dlang.org/show_bug.cgi?id=9712 |
| 393 |
|
| 394 |
auto func9712(T)(T[2] arg) { return arg; } |
| 395 |
static assert(is(typeof(func9712([1,2])) == int[2])); |
| 396 |
|
| 397 |
auto deduceLength9712(T,size_t n)(T[n] a) { return a; } |
| 398 |
static assert(is(typeof(deduceLength9712([1,2,3])) == int[3])); |
| 399 |
|
| 400 |
/******************************************/ |
| 401 |
// https://issues.dlang.org/show_bug.cgi?id=9743 |
| 402 |
|
| 403 |
void test9743() |
| 404 |
{ |
| 405 |
// +-Char |
| 406 |
// |+-Immutable or Const or Mutable |
| 407 |
// ||+-Value or Ref |
| 408 |
// |||+-Function or +-Template |
| 409 |
void fCIVF( immutable char[4]) {} void fCIVT()( immutable char[4]) {} |
| 410 |
void fCCVF( const char[4]) {} void fCCVT()( const char[4]) {} |
| 411 |
void fCMVF( char[4]) {} void fCMVT()( char[4]) {} |
| 412 |
void fCIRF(ref immutable char[4]) {} void fCIRT()(ref immutable char[4]) {} |
| 413 |
void fCCRF(ref const char[4]) {} void fCCRT()(ref const char[4]) {} |
| 414 |
void fCMRF(ref char[4]) {} void fCMRT()(ref char[4]) {} |
| 415 |
alias fcOK = TypeTuple!(fCIVF, fCIVT, fCCVF, fCCVT, fCMVF, fCMVT, fCIRF, fCIRT, fCCRF, fCCRT); |
| 416 |
foreach (f; fcOK) f("1234" ) ; |
| 417 |
foreach (f; fcOK) f("1234"c) ; |
| 418 |
foreach (f; fcOK) static assert(!__traits(compiles, f("1234"w) )); |
| 419 |
foreach (f; fcOK) static assert(!__traits(compiles, f("1234"d) )); |
| 420 |
alias fcNG = TypeTuple!(fCMRF, fCMRT); // cannot hold immutable data by mutable ref |
| 421 |
foreach (f; fcNG) static assert(!__traits(compiles, f("1234" ) )); |
| 422 |
foreach (f; fcNG) static assert(!__traits(compiles, f("1234"c) )); |
| 423 |
foreach (f; fcNG) static assert(!__traits(compiles, f("1234"w) )); |
| 424 |
foreach (f; fcNG) static assert(!__traits(compiles, f("1234"d) )); |
| 425 |
|
| 426 |
// +-Wchar |
| 427 |
void fWIVF( immutable wchar[4]) {} void fWIVT()( immutable wchar[4]) {} |
| 428 |
void fWCVF( const wchar[4]) {} void fWCVT()( const wchar[4]) {} |
| 429 |
void fWMVF( wchar[4]) {} void fWMVT()( wchar[4]) {} |
| 430 |
void fWIRF(ref immutable wchar[4]) {} void fWIRT()(ref immutable wchar[4]) {} |
| 431 |
void fWCRF(ref const wchar[4]) {} void fWCRT()(ref const wchar[4]) {} |
| 432 |
void fWMRF(ref wchar[4]) {} void fWMRT()(ref wchar[4]) {} |
| 433 |
alias fwOK = TypeTuple!(fWIVF, fWIVT, fWCVF, fWCVT, fWMVF, fWMVT, fWIRF, fWIRT, fWCRF, fWCRT); |
| 434 |
foreach (f; fwOK) f("1234" ) ; |
| 435 |
foreach (f; fwOK) static assert(!__traits(compiles, f("1234"c) )); |
| 436 |
foreach (f; fwOK) f("1234"w) ; |
| 437 |
foreach (f; fwOK) static assert(!__traits(compiles, f("1234"d) )); |
| 438 |
alias fwNG = TypeTuple!(fWMRF, fWMRT); // cannot hold immutable data by mutable ref |
| 439 |
foreach (f; fwNG) static assert(!__traits(compiles, f("1234" ) )); |
| 440 |
foreach (f; fwNG) static assert(!__traits(compiles, f("1234"c) )); |
| 441 |
foreach (f; fwNG) static assert(!__traits(compiles, f("1234"w) )); |
| 442 |
foreach (f; fwNG) static assert(!__traits(compiles, f("1234"d) )); |
| 443 |
|
| 444 |
// +-Dchar |
| 445 |
void fDIVF( immutable dchar[4]) {} void fDIVT()( immutable dchar[4]) {} |
| 446 |
void fDCVF( const dchar[4]) {} void fDCVT()( const dchar[4]) {} |
| 447 |
void fDMVF( dchar[4]) {} void fDMVT()( dchar[4]) {} |
| 448 |
void fDIRF(ref immutable dchar[4]) {} void fDIRT()(ref immutable dchar[4]) {} |
| 449 |
void fDCRF(ref const dchar[4]) {} void fDCRT()(ref const dchar[4]) {} |
| 450 |
void fDMRF(ref dchar[4]) {} void fDMRT()(ref dchar[4]) {} |
| 451 |
alias fdOK = TypeTuple!(fDIVF, fDIVT, fDCVF, fDCVT, fDMVF, fDMVT, fDIRF, fDIRT, fDCRF, fDCRT); |
| 452 |
foreach (f; fdOK) f("1234" ) ; |
| 453 |
foreach (f; fdOK) static assert(!__traits(compiles, f("1234"c) )); |
| 454 |
foreach (f; fdOK) static assert(!__traits(compiles, f("1234"w) )); |
| 455 |
foreach (f; fdOK) f("1234"d) ; |
| 456 |
alias fdNG = TypeTuple!(fDMRF, fDMRT); // cannot hold immutable data by mutable ref |
| 457 |
foreach (f; fdNG) static assert(!__traits(compiles, f("1234" ) )); |
| 458 |
foreach (f; fdNG) static assert(!__traits(compiles, f("1234"c) )); |
| 459 |
foreach (f; fdNG) static assert(!__traits(compiles, f("1234"w) )); |
| 460 |
foreach (f; fdNG) static assert(!__traits(compiles, f("1234"d) )); |
| 461 |
} |
| 462 |
|
| 463 |
/******************************************/ |
| 464 |
// https://issues.dlang.org/show_bug.cgi?id=9747 |
| 465 |
|
| 466 |
void foo9747A(T)(T[4]) {} |
| 467 |
void foo9747C(size_t dim)(char[dim]) {} |
| 468 |
void foo9747W(size_t dim)(wchar[dim]) {} |
| 469 |
void foo9747D(size_t dim)(dchar[dim]) {} |
| 470 |
|
| 471 |
void test9747() |
| 472 |
{ |
| 473 |
foo9747A("abcd"c); |
| 474 |
foo9747A("abcd"w); |
| 475 |
foo9747A("abcd"d); |
| 476 |
foo9747C("abcd"c); |
| 477 |
foo9747W("abcd"w); |
| 478 |
foo9747D("abcd"d); |
| 479 |
} |
| 480 |
|
| 481 |
/******************************************/ |
| 482 |
// https://issues.dlang.org/show_bug.cgi?id=12876 |
| 483 |
|
| 484 |
void test12876() |
| 485 |
{ |
| 486 |
void foo(int[4] b) {} |
| 487 |
void bar(size_t n)(int[n] c) { static assert(n == 4); } |
| 488 |
|
| 489 |
int[5] a; |
| 490 |
foo(a[1 .. $]); // OK |
| 491 |
bar(a[1 .. $]); // OK <- Error |
| 492 |
} |
| 493 |
|
| 494 |
/******************************************/ |
| 495 |
// https://issues.dlang.org/show_bug.cgi?id=13775 |
| 496 |
|
| 497 |
void test13775() |
| 498 |
{ |
| 499 |
ubyte[4] ubytes = [1,2,3,4]; |
| 500 |
|
| 501 |
// CT-known slicing (https://issues.dlang.org/show_bug.cgi?id=3652) |
| 502 |
auto ok1 = cast(ubyte[2]) ubytes[0 .. 2]; |
| 503 |
assert(ok1 == [1, 2]); |
| 504 |
|
| 505 |
// CT-known slicing with implicit conversion of SliceExp::e1 (https://issues.dlang.org/show_bug.cgi?id=13154) |
| 506 |
enum double[] arr = [1.0, 2.0, 3.0]; |
| 507 |
auto ok2 = cast(float[2]) [1.0, 2.0, 3.0][0..2]; |
| 508 |
auto ok3 = cast(float[2]) arr[1..3]; // currently this is accepted |
| 509 |
assert(ok2 == [1f, 2f]); |
| 510 |
assert(ok3 == [2f, 3f]); |
| 511 |
|
| 512 |
// CT-known slicing with type coercing (https://issues.dlang.org/show_bug.cgi?id=13775) |
| 513 |
auto ok4 = cast( byte[2]) ubytes[0 .. 2]; // CT-known slicing + type coercing |
| 514 |
auto ok5 = cast(short[1]) ubytes[0 .. 2]; // CT-known slicing + type coercing |
| 515 |
assert(ok4 == [1, 2]); |
| 516 |
version(LittleEndian) assert(ok5 == [0x0201]); |
| 517 |
version( BigEndian) assert(ok5 == [0x0102]); |
| 518 |
} |
| 519 |
|
| 520 |
/******************************************/ |
| 521 |
// https://issues.dlang.org/show_bug.cgi?id=15889 |
| 522 |
// Array bounds check should report index and length |
| 523 |
void test15889() |
| 524 |
{ |
| 525 |
int[] a = new int[2]; |
| 526 |
|
| 527 |
try { |
| 528 |
a[3] = 40; |
| 529 |
} catch (ArrayIndexError e) { |
| 530 |
assert(e.index == 3); |
| 531 |
assert(e.length == 2); |
| 532 |
} |
| 533 |
|
| 534 |
try { |
| 535 |
a[1 .. 4] = 50; |
| 536 |
} catch (ArraySliceError e) { |
| 537 |
assert(e.lower == 1); |
| 538 |
assert(e.upper == 4); |
| 539 |
assert(e.length == 2); |
| 540 |
} |
| 541 |
} |
| 542 |
|
| 543 |
/******************************************/ |
| 544 |
|
| 545 |
int main() |
| 546 |
{ |
| 547 |
test1(); |
| 548 |
test13976(); |
| 549 |
test3652(); |
| 550 |
test3652a(); |
| 551 |
test3652b(); |
| 552 |
test9743(); |
| 553 |
test9747(); |
| 554 |
test13775(); |
| 555 |
test15889(); |
| 556 |
|
| 557 |
printf("Success\n"); |
| 558 |
return 0; |
| 559 |
} |