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,
1534115341 ZigType *array_type = actual_type->data.pointer.child_type;
1534215342 bool const_ok = (slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0
1534315343 || !actual_type->data.pointer.is_const);
15344
1534415345 if (const_ok && types_match_const_cast_only(ira, slice_ptr_type->data.pointer.child_type,
1534515346 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))))
1534715352 {
1534815353 // If the pointers both have ABI align, it works.
1534915354 // 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");
22const std = @import("std");
33
44pub 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
513 cases.add("@Type with undefined",
614 \\comptime {
715 \\ _ = @Type(.{ .Array = .{ .len = 0, .child = u8, .sentinel = undefined } });