authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-07 16:01:13-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-07 16:01:13-07:00
log3df19b765d3b33cfda43b875adb714487bfb8c6b
treeb8325170ca693b5c94a569a256449a4dde0e6064
parent76335bc7badd41af0ebb7dd196e1550d7e99d8e7

AstGen: make array literals work like struct literals

Now, array literals will emit a coerce_result_ptr ZIR instruction just like struct literals do. This makes for another passing behavior test case.

3 files changed, 29 insertions(+), 9 deletions(-)

src/AstGen.zig+22-2
...@@ -1282,10 +1282,10 @@ fn arrayInitExpr(...@@ -1282,10 +1282,10 @@ fn arrayInitExpr(
1282 }1282 }
1283 },1283 },
1284 .ptr, .inferred_ptr => |ptr_inst| {1284 .ptr, .inferred_ptr => |ptr_inst| {
1285 return arrayInitExprRlPtr(gz, scope, node, array_init.ast.elements, ptr_inst);1285 return arrayInitExprRlPtr(gz, scope, rl, node, ptr_inst, array_init.ast.elements, types.array);
1286 },1286 },
1287 .block_ptr => |block_gz| {1287 .block_ptr => |block_gz| {
1288 return arrayInitExprRlPtr(gz, scope, node, array_init.ast.elements, block_gz.rl_ptr);1288 return arrayInitExprRlPtr(gz, scope, rl, node, block_gz.rl_ptr, array_init.ast.elements, types.array);
1289 },1289 },
1290 }1290 }
1291}1291}
...@@ -1341,9 +1341,29 @@ fn arrayInitExprRlTy(...@@ -1341,9 +1341,29 @@ fn arrayInitExprRlTy(
1341fn arrayInitExprRlPtr(1341fn arrayInitExprRlPtr(
1342 gz: *GenZir,1342 gz: *GenZir,
1343 scope: *Scope,1343 scope: *Scope,
1344 rl: ResultLoc,
1344 node: Ast.Node.Index,1345 node: Ast.Node.Index,
1346 result_ptr: Zir.Inst.Ref,
1345 elements: []const Ast.Node.Index,1347 elements: []const Ast.Node.Index,
1348 array_ty: Zir.Inst.Ref,
1349) InnerError!Zir.Inst.Ref {
1350 if (array_ty == .none) {
1351 return arrayInitExprRlPtrInner(gz, scope, node, result_ptr, elements);
1352 }
1353
1354 var as_scope = try gz.makeCoercionScope(scope, array_ty, result_ptr);
1355 defer as_scope.instructions.deinit(gz.astgen.gpa);
1356
1357 const result = try arrayInitExprRlPtrInner(&as_scope, scope, node, as_scope.rl_ptr, elements);
1358 return as_scope.finishCoercion(gz, rl, node, result, array_ty);
1359}
1360
1361fn arrayInitExprRlPtrInner(
1362 gz: *GenZir,
1363 scope: *Scope,
1364 node: Ast.Node.Index,
1346 result_ptr: Zir.Inst.Ref,1365 result_ptr: Zir.Inst.Ref,
1366 elements: []const Ast.Node.Index,
1347) InnerError!Zir.Inst.Ref {1367) InnerError!Zir.Inst.Ref {
1348 const astgen = gz.astgen;1368 const astgen = gz.astgen;
1349 const gpa = astgen.gpa;1369 const gpa = astgen.gpa;
test/behavior/array.zig+7
...@@ -43,3 +43,10 @@ test "array literal with explicit type" {...@@ -43,3 +43,10 @@ test "array literal with explicit type" {
43 try expect(hex_mult.len == 4);43 try expect(hex_mult.len == 4);
44 try expect(hex_mult[1] == 256);44 try expect(hex_mult[1] == 256);
45}45}
46
47test "array literal with inferred length" {
48 const hex_mult = [_]u16{ 4096, 256, 16, 1 };
49
50 try expect(hex_mult.len == 4);
51 try expect(hex_mult[1] == 256);
52}
test/behavior/array_stage1.zig-7
...@@ -4,13 +4,6 @@ const mem = std.mem;...@@ -4,13 +4,6 @@ const mem = std.mem;
4const expect = testing.expect;4const expect = testing.expect;
5const expectEqual = testing.expectEqual;5const expectEqual = testing.expectEqual;
66
7test "array literal with inferred length" {
8 const hex_mult = [_]u16{ 4096, 256, 16, 1 };
9
10 try expect(hex_mult.len == 4);
11 try expect(hex_mult[1] == 256);
12}
13
14test "array with sentinels" {7test "array with sentinels" {
15 const S = struct {8 const S = struct {
16 fn doTheTest(is_ct: bool) !void {9 fn doTheTest(is_ct: bool) !void {