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.
This commit is contained in:
Andrew Kelley 2021-10-07 16:01:13 -07:00
parent 76335bc7ba
commit 3df19b765d
3 changed files with 30 additions and 10 deletions

View File

@ -1282,10 +1282,10 @@ fn arrayInitExpr(
}
},
.ptr, .inferred_ptr => |ptr_inst| {
return arrayInitExprRlPtr(gz, scope, node, array_init.ast.elements, ptr_inst);
return arrayInitExprRlPtr(gz, scope, rl, node, ptr_inst, array_init.ast.elements, types.array);
},
.block_ptr => |block_gz| {
return arrayInitExprRlPtr(gz, scope, node, array_init.ast.elements, block_gz.rl_ptr);
return arrayInitExprRlPtr(gz, scope, rl, node, block_gz.rl_ptr, array_init.ast.elements, types.array);
},
}
}
@ -1341,9 +1341,29 @@ fn arrayInitExprRlTy(
fn arrayInitExprRlPtr(
gz: *GenZir,
scope: *Scope,
rl: ResultLoc,
node: Ast.Node.Index,
elements: []const Ast.Node.Index,
result_ptr: Zir.Inst.Ref,
elements: []const Ast.Node.Index,
array_ty: Zir.Inst.Ref,
) InnerError!Zir.Inst.Ref {
if (array_ty == .none) {
return arrayInitExprRlPtrInner(gz, scope, node, result_ptr, elements);
}
var as_scope = try gz.makeCoercionScope(scope, array_ty, result_ptr);
defer as_scope.instructions.deinit(gz.astgen.gpa);
const result = try arrayInitExprRlPtrInner(&as_scope, scope, node, as_scope.rl_ptr, elements);
return as_scope.finishCoercion(gz, rl, node, result, array_ty);
}
fn arrayInitExprRlPtrInner(
gz: *GenZir,
scope: *Scope,
node: Ast.Node.Index,
result_ptr: Zir.Inst.Ref,
elements: []const Ast.Node.Index,
) InnerError!Zir.Inst.Ref {
const astgen = gz.astgen;
const gpa = astgen.gpa;

View File

@ -43,3 +43,10 @@ test "array literal with explicit type" {
try expect(hex_mult.len == 4);
try expect(hex_mult[1] == 256);
}
test "array literal with inferred length" {
const hex_mult = [_]u16{ 4096, 256, 16, 1 };
try expect(hex_mult.len == 4);
try expect(hex_mult[1] == 256);
}

View File

@ -4,13 +4,6 @@ const mem = std.mem;
const expect = testing.expect;
const expectEqual = testing.expectEqual;
test "array literal with inferred length" {
const hex_mult = [_]u16{ 4096, 256, 16, 1 };
try expect(hex_mult.len == 4);
try expect(hex_mult[1] == 256);
}
test "array with sentinels" {
const S = struct {
fn doTheTest(is_ct: bool) !void {