authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-09-02 19:13:21-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2020-09-02 19:13:21-04:00
logc86108dd63349cbd99a8821fb4d628f31958e0ed
treee68a54f384a8908b37e560cadb11c0a91d6af603
parent405c7215a8054680a789cf94afe41fd66c7d61c7
parentdd4994a4e4379454f6b58779276f1b6aa9ed6e1b
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #6229 from LemonBoy/fix-6054

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

3 files changed, 15 insertions(+), 2 deletions(-)

lib/std/net.zig+1-1
...@@ -1164,7 +1164,7 @@ fn linuxLookupNameFromDnsSearch(...@@ -1164,7 +1164,7 @@ fn linuxLookupNameFromDnsSearch(
1164 }1164 }
11651165
1166 const search = if (rc.search.isNull() or dots >= rc.ndots or mem.endsWith(u8, name, "."))1166 const search = if (rc.search.isNull() or dots >= rc.ndots or mem.endsWith(u8, name, "."))
1167 &[_]u8{}1167 ""
1168 else1168 else
1169 rc.search.span();1169 rc.search.span();
11701170
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 } });