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(
12821282 }
12831283 },
12841284 .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);
12861286 },
12871287 .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);
12891289 },
12901290 }
12911291}
......@@ -1341,9 +1341,29 @@ fn arrayInitExprRlTy(
13411341fn arrayInitExprRlPtr(
13421342 gz: *GenZir,
13431343 scope: *Scope,
1344 rl: ResultLoc,
13441345 node: Ast.Node.Index,
1346 result_ptr: Zir.Inst.Ref,
13451347 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,
13461365 result_ptr: Zir.Inst.Ref,
1366 elements: []const Ast.Node.Index,
13471367) InnerError!Zir.Inst.Ref {
13481368 const astgen = gz.astgen;
13491369 const gpa = astgen.gpa;
test/behavior/array.zig+7
......@@ -43,3 +43,10 @@ test "array literal with explicit type" {
4343 try expect(hex_mult.len == 4);
4444 try expect(hex_mult[1] == 256);
4545}
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;
44const expect = testing.expect;
55const 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
147test "array with sentinels" {
158 const S = struct {
169 fn doTheTest(is_ct: bool) !void {