Pointer Reform: proper slicing and indexing (#1053)

* enable slicing for single-item ptr to arrays
 * disable slicing for other single-item pointers
 * enable indexing for single-item ptr to arrays
 * disable indexing for other single-item pointers

see #770
closes #386
This commit is contained in:
Andrew Kelley
2018-06-04 22:11:14 -04:00
committed by GitHub
parent 32e0dfd4f0
commit e53b683bd3
21 changed files with 268 additions and 79 deletions

View File

@@ -115,3 +115,32 @@ test "array len property" {
var x: [5]i32 = undefined;
assert(@typeOf(x).len == 5);
}
test "single-item pointer to array indexing and slicing" {
testSingleItemPtrArrayIndexSlice();
comptime testSingleItemPtrArrayIndexSlice();
}
fn testSingleItemPtrArrayIndexSlice() void {
var array = "aaaa";
doSomeMangling(&array);
assert(mem.eql(u8, "azya", array));
}
fn doSomeMangling(array: *[4]u8) void {
array[1] = 'z';
array[2..3][0] = 'y';
}
test "implicit cast single-item pointer" {
testImplicitCastSingleItemPtr();
comptime testImplicitCastSingleItemPtr();
}
fn testImplicitCastSingleItemPtr() void {
var byte: u8 = 100;
const slice = (*[1]u8)(&byte)[0..];
slice[0] += 1;
assert(byte == 101);
}