authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-01 17:29:10+02:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-09-01 17:29:10+02:00
logc51b871c4516003e8d2c84e7e1c36124c3797f5c
tree5cf10e332b277b2ed374296b5259ecd497219c76
parent26140678a5c72604f2baac3cb9d1e5f7b37b6b8d

ir: Typecheck the sentinel value in *[N:S1]T to [S2]T casts

Closes #6054

2 files changed, 14 insertions(+), 1 deletions(-)

src/ir.cpp+6-1
...@@ -15341,9 +15341,14 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,...@@ -15341,9 +15341,14 @@ static IrInstGen *ir_analyze_cast(IrAnalyze *ira, IrInst *source_instr,
15341 ZigType *array_type = actual_type->data.pointer.child_type;15341 ZigType *array_type = actual_type->data.pointer.child_type;
15342 bool const_ok = (slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 015342 bool const_ok = (slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0
15343 || !actual_type->data.pointer.is_const);15343 || !actual_type->data.pointer.is_const);
15344
15344 if (const_ok && types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type,15345 if (const_ok && types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type,
15345 array_type->data.array.child_type, source_node,15346 array_type->data.array.child_type, source_node,
15346 !slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk)15347 !slice_ptr_type->data.pointer.is_const).id == ConstCastResultIdOk &&
15348 (slice_ptr_type->data.pointer.sentinel == nullptr ||
15349 (array_type->data.array.sentinel != nullptr &&
15350 const_values_equal(ira->codegen, array_type->data.array.sentinel,
15351 slice_ptr_type->data.pointer.sentinel))))
15347 {15352 {
15348 // If the pointers both have ABI align, it works.15353 // If the pointers both have ABI align, it works.
15349 // Or if the array length is 0, alignment doesn't matter.15354 // Or if the array length is 0, alignment doesn't matter.
test/compile_errors.zig+8
...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");...@@ -2,6 +2,14 @@ const tests = @import("tests.zig");
2const std = @import("std");2const std = @import("std");
33
4pub fn addCases(cases: *tests.CompileErrorContext) void {4pub fn addCases(cases: *tests.CompileErrorContext) void {
5 cases.add("slice sentinel mismatch",
6 \\export fn entry() void {
7 \\ const y: [:1]const u8 = &[_:2]u8{ 1, 2 };
8 \\}
9 , &[_][]const u8{
10 "tmp.zig:2:37: error: expected type '[:1]const u8', found '*const [2:2]u8'",
11 });
12
5 cases.add("@Type with undefined",13 cases.add("@Type with undefined",
6 \\comptime {14 \\comptime {
7 \\ _ = @Type(.{ .Array = .{ .len = 0, .child = u8, .sentinel = undefined } });15 \\ _ = @Type(.{ .Array = .{ .len = 0, .child = u8, .sentinel = undefined } });