| ... | ... | @@ -378,12 +378,16 @@ test "slice syntax resulting in pointer-to-array" { |
| 378 | 378 | try testPointer0(); |
| 379 | 379 | try testPointerAlign(); |
| 380 | 380 | try testSlice(); |
| 381 | try testSliceZ(); |
| 381 | 382 | try testSliceOpt(); |
| 382 | 383 | try testSliceAlign(); |
| 384 | try testConcatStrLiterals(); |
| 383 | 385 | try testSliceLength(); |
| 384 | 386 | try testSliceLengthZ(); |
| 385 | 387 | try testArrayLength(); |
| 386 | 388 | try testArrayLengthZ(); |
| 389 | try testMultiPointer(); |
| 390 | try testMultiPointerLengthZ(); |
| 387 | 391 | } |
| 388 | 392 | |
| 389 | 393 | fn testArray() !void { |
| ... | ... | @@ -469,8 +473,12 @@ test "slice syntax resulting in pointer-to-array" { |
| 469 | 473 | var array = [5:0]u8{ 1, 2, 3, 4, 5 }; |
| 470 | 474 | var slice: [:0]u8 = &array; |
| 471 | 475 | try comptime expect(@TypeOf(slice[1..3]) == *[2]u8); |
| 472 | | try comptime expect(@TypeOf(slice[1..]) == [:0]u8); |
| 473 | 476 | try comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8); |
| 477 | if (@inComptime()) { |
| 478 | try comptime expect(@TypeOf(slice[1..]) == *[4:0]u8); |
| 479 | } else { |
| 480 | try comptime expect(@TypeOf(slice[1..]) == [:0]u8); |
| 481 | } |
| 474 | 482 | } |
| 475 | 483 | |
| 476 | 484 | fn testSliceOpt() !void { |
| ... | ... | @@ -491,8 +499,8 @@ test "slice syntax resulting in pointer-to-array" { |
| 491 | 499 | } |
| 492 | 500 | |
| 493 | 501 | fn testConcatStrLiterals() !void { |
| 494 | | try expectEqualSlices("a"[0..] ++ "b"[0..], "ab"); |
| 495 | | try expectEqualSlices("a"[0.. :0] ++ "b"[0.. :0], "ab"); |
| 502 | try expectEqualSlices(u8, "ab", "a"[0..] ++ "b"[0..]); |
| 503 | try expectEqualSlices(u8, "ab", "a"[0.. :0] ++ "b"[0.. :0]); |
| 496 | 504 | } |
| 497 | 505 | |
| 498 | 506 | fn testSliceLength() !void { |
| ... | ... | @@ -541,18 +549,18 @@ test "slice syntax resulting in pointer-to-array" { |
| 541 | 549 | var array = [5:0]u8{ 1, 2, 3, 4, 5 }; |
| 542 | 550 | var ptr: [*]u8 = &array; |
| 543 | 551 | try comptime expect(@TypeOf(ptr[1..][0..2]) == *[2]u8); |
| 544 | | try comptime expect(@TypeOf(ptr[1..][0..4]) == *[4:0]u8); |
| 552 | try comptime expect(@TypeOf(ptr[1..][0..4]) == *[4]u8); |
| 545 | 553 | try comptime expect(@TypeOf(ptr[1..][0..2 :4]) == *[2:4]u8); |
| 546 | 554 | try comptime expect(@TypeOf(ptr[1.. :0][0..2]) == *[2]u8); |
| 547 | | try comptime expect(@TypeOf(ptr[1.. :0][0..4]) == *[4:0]u8); |
| 555 | try comptime expect(@TypeOf(ptr[1.. :0][0..4]) == *[4]u8); |
| 548 | 556 | try comptime expect(@TypeOf(ptr[1.. :0][0..2 :4]) == *[2:4]u8); |
| 549 | 557 | |
| 550 | 558 | var ptr_z: [*:0]u8 = &array; |
| 551 | 559 | try comptime expect(@TypeOf(ptr_z[1..][0..2]) == *[2]u8); |
| 552 | | try comptime expect(@TypeOf(ptr_z[1..][0..4]) == *[4:0]u8); |
| 560 | try comptime expect(@TypeOf(ptr_z[1..][0..4]) == *[4]u8); |
| 553 | 561 | try comptime expect(@TypeOf(ptr_z[1..][0..2 :4]) == *[2:4]u8); |
| 554 | 562 | try comptime expect(@TypeOf(ptr_z[1.. :0][0..2]) == *[2]u8); |
| 555 | | try comptime expect(@TypeOf(ptr_z[1.. :0][0..4]) == *[4:0]u8); |
| 563 | try comptime expect(@TypeOf(ptr_z[1.. :0][0..4]) == *[4]u8); |
| 556 | 564 | try comptime expect(@TypeOf(ptr_z[1.. :0][0..2 :4]) == *[2:4]u8); |
| 557 | 565 | } |
| 558 | 566 | }; |
| ... | ... | @@ -869,3 +877,20 @@ test "write through pointer to optional slice arg" { |
| 869 | 877 | try S.bar(&foo); |
| 870 | 878 | try expectEqualStrings(foo.?, "ok"); |
| 871 | 879 | } |
| 880 | |
| 881 | test "modify slice length at comptime" { |
| 882 | if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; |
| 883 | if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; |
| 884 | if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; |
| 885 | if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest; |
| 886 | |
| 887 | const arr: [2]u8 = .{ 10, 20 }; |
| 888 | comptime var s: []const u8 = arr[0..0]; |
| 889 | s.len += 1; |
| 890 | const a = s; |
| 891 | s.len += 1; |
| 892 | const b = s; |
| 893 | |
| 894 | try expectEqualSlices(u8, &.{10}, a); |
| 895 | try expectEqualSlices(u8, &.{ 10, 20 }, b); |
| 896 | } |