stage2: able to slice to sentinel index at comptime

The runtime behavior allowed this in both stage1 and stage2, but stage1
fails with index out of bounds during comptime. This behavior makes
sense to support, and comptime behavior should match runtime behavior. I
implement this fix only in stage2.
This commit is contained in:
Mitchell Hashimoto
2022-03-23 09:40:29 -07:00
committed by Andrew Kelley
parent f27d3409bd
commit a36f4ee290
3 changed files with 87 additions and 9 deletions

View File

@@ -26,11 +26,16 @@ pub fn addCases(ctx: *TestContext) !void {
\\comptime {
\\ var array = [_:0]u8{ 1, 2, 3, 4 };
\\ var src_slice: [:0]u8 = &array;
\\ var slice = src_slice[2..5];
\\ var slice = src_slice[2..6];
\\ _ = slice;
\\}
\\comptime {
\\ var array = [_:0]u8{ 1, 2, 3, 4 };
\\ var slice = array[2..6];
\\ _ = slice;
\\}
\\comptime {
\\ var array = [_]u8{ 1, 2, 3, 4 };
\\ var slice = array[2..5];
\\ _ = slice;
\\}
@@ -40,9 +45,10 @@ pub fn addCases(ctx: *TestContext) !void {
\\ _ = slice;
\\}
, &[_][]const u8{
":4:26: error: end index 5 out of bounds for slice of length 4",
":9:22: error: end index 5 out of bounds for array of length 4",
":14:22: error: start index 3 is larger than end index 2",
":4:26: error: end index 6 out of bounds for slice of length 4 +1 (sentinel)",
":9:22: error: end index 6 out of bounds for array of length 4 +1 (sentinel)",
":14:22: error: end index 5 out of bounds for array of length 4",
":19:22: error: start index 3 is larger than end index 2",
});
}