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(...@@ -820,7 +820,7 @@ fn linuxLookupNameFromHosts(
820 // Skip to the delimiter in the stream, to fix parsing820 // Skip to the delimiter in the stream, to fix parsing
821 try stream.skipUntilDelimiterOrEof('\n');821 try stream.skipUntilDelimiterOrEof('\n');
822 // Use the truncated line. A truncated comment or hostname will be handled correctly.822 // 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 necessary823 break :blk &line_buf;
824 },824 },
825 else => |e| return e,825 else => |e| return e,
826 }) |line| {826 }) |line| {
...@@ -1017,7 +1017,7 @@ fn getResolvConf(allocator: *mem.Allocator, rc: *ResolvConf) !void {...@@ -1017,7 +1017,7 @@ fn getResolvConf(allocator: *mem.Allocator, rc: *ResolvConf) !void {
1017 // Skip to the delimiter in the stream, to fix parsing1017 // Skip to the delimiter in the stream, to fix parsing
1018 try stream.skipUntilDelimiterOrEof('\n');1018 try stream.skipUntilDelimiterOrEof('\n');
1019 // Give an empty line to the while loop, which will be skipped.1019 // 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 necessary1020 break :blk line_buf[0..0];
1021 },1021 },
1022 else => |e| return e,1022 else => |e| return e,
1023 }) |line| {1023 }) |line| {
src/ir.cpp+14-3
...@@ -12350,11 +12350,22 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT...@@ -12350,11 +12350,22 @@ static ZigType *ir_resolve_peer_types(IrAnalyze *ira, AstNode *source_node, ZigT
12350 prev_type->data.pointer.child_type->id == ZigTypeIdArray &&12350 prev_type->data.pointer.child_type->id == ZigTypeIdArray &&
12351 prev_type->data.pointer.ptr_len == PtrLenSingle &&12351 prev_type->data.pointer.ptr_len == PtrLenSingle &&
12352 ((cur_type->id == ZigTypeIdErrorUnion && is_slice(cur_type->data.error_union.payload_type)) ||12352 ((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)))
12354 {12355 {
12355 ZigType *array_type = prev_type->data.pointer.child_type;12356 ZigType *array_type = prev_type->data.pointer.child_type;
12356 ZigType *slice_type = (cur_type->id == ZigTypeIdErrorUnion) ?12357 ZigType *slice_type;
12357 cur_type->data.error_union.payload_type : cur_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 }
12358 ZigType *slice_ptr_type = slice_type->data.structure.fields[slice_ptr_index]->type_entry;12369 ZigType *slice_ptr_type = slice_type->data.structure.fields[slice_ptr_index]->type_entry;
12359 if ((slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0 ||12370 if ((slice_ptr_type->data.pointer.is_const || array_type->data.array.len == 0 ||
12360 !prev_type->data.pointer.is_const) &&12371 !prev_type->data.pointer.is_const) &&
test/stage1/behavior/slice.zig+8
...@@ -159,6 +159,7 @@ test "slice syntax resulting in pointer-to-array" {...@@ -159,6 +159,7 @@ test "slice syntax resulting in pointer-to-array" {
159 testSlice();159 testSlice();
160 testSliceZ();160 testSliceZ();
161 testSlice0();161 testSlice0();
162 testSliceOpt();
162 testSliceAlign();163 testSliceAlign();
163 }164 }
164165
...@@ -249,6 +250,13 @@ test "slice syntax resulting in pointer-to-array" {...@@ -249,6 +250,13 @@ test "slice syntax resulting in pointer-to-array" {
249 comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);250 comptime expect(@TypeOf(slice[1..3 :4]) == *[2:4]u8);
250 }251 }
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
252 fn testSlice0() void {260 fn testSlice0() void {
253 {261 {
254 var array = [0]u8{};262 var array = [0]u8{};