authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-03-20 12:59:55+01:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-20 12:23:05-04:00
log541e763010403d8233a6420756a1e8393f1dd4c2
treeb45a795d35451ab672fd563757ca4a087ba94355
parent28a6c136e9dc9bcf3e04ab0aa38edc21918c78b9

ir: Peer type resolution between ?[]T and *[N]T

Closes #4767

3 files changed, 24 insertions(+), 5 deletions(-)

lib/std/net.zig+2-2
......@@ -820,7 +820,7 @@ fn linuxLookupNameFromHosts(
820820 // Skip to the delimiter in the stream, to fix parsing
821821 try stream.skipUntilDelimiterOrEof('\n');
822822 // Use the truncated line. A truncated comment or hostname will be handled correctly.
823 break :blk @as([]u8, &line_buf); // TODO the cast should not be necessary
823 break :blk &line_buf;
824824 },
825825 else => |e| return e,
826826 }) |line| {
......@@ -1017,7 +1017,7 @@ fn getResolvConf(allocator: *mem.Allocator, rc: *ResolvConf) !void {
10171017 // Skip to the delimiter in the stream, to fix parsing
10181018 try stream.skipUntilDelimiterOrEof('\n');
10191019 // Give an empty line to the while loop, which will be skipped.
1020 break :blk @as([]u8, line_buf[0..0]); // TODO the cast should not be necessary
1020 break :blk line_buf[0..0];
10211021 },
10221022 else => |e| return e,
10231023 }) |line| {
src/ir.cpp+14-3
......@@ -12350,11 +12350,22 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
1235012350 prev_type->data.pointer.child_type->id == ZigTypeIdArray &&
1235112351 prev_type->data.pointer.ptr_len == PtrLenSingle &&
1235212352 ((cur_type->id == ZigTypeIdErrorUnion && is_slice(cur_type->data.error_union.payload_type)) ||
12353 is_slice(cur_type)))
12353 (cur_type->id == ZigTypeIdOptional && is_slice(cur_type->data.maybe.child_type)) ||
12354 is_slice(cur_type)))
1235412355 {
1235512356 ZigType *array_type = prev_type->data.pointer.child_type;
12356 ZigType *slice_type = (cur_type->id == ZigTypeIdErrorUnion) ?
12357 cur_type->data.error_union.payload_type : cur_type;
12357 ZigType *slice_type;
12358 switch (cur_type->id) {
12359 case ZigTypeIdErrorUnion:
12360 slice_type = cur_type->data.error_union.payload_type;
12361 break;
12362 case ZigTypeIdOptional:
12363 slice_type = cur_type->data.maybe.child_type;
12364 break;
12365 default:
12366 slice_type = cur_type;
12367 break;
12368 }
1235812369 ZigType *slice_ptr_type = slice_type->data.structure.fields[slice_ptr_index]->type_entry;
1235912370 if ((slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0 ||
1236012371 !prev_type->data.pointer.is_const) &&
test/stage1/behavior/slice.zig+8
......@@ -159,6 +159,7 @@ test "slice syntax resulting in pointer-to-array" {
159159 testSlice();
160160 testSliceZ();
161161 testSlice0();
162 testSliceOpt();
162163 testSliceAlign();
163164 }
164165
......@@ -249,6 +250,13 @@ test "slice syntax resulting in pointer-to-array" {
249250 comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
250251 }
251252
253 fn testSliceOpt() void {
254 var array: [2]u8 = [2]u8{ 1, 2 };
255 var slice: ?[]u8 = &array;
256 comptime expect(@TypeOf(&array, slice) == ?[]u8);
257 comptime expect(@TypeOf(slice.?[0..2]) == *[2]u8);
258 }
259
252260 fn testSlice0() void {
253261 {
254262 var array = [0]u8{};