authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-27 14:44:37-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-07-27 16:19:23-07:00
log6a4df2778e5f35fb112d87b12f94d4e3d06ee1d3
tree536361099545cc9f553ec4a29e4c7ade51375363
parent401abd793d9e1abd0e37a6db55d222060ae35107

AstGen: fix ref instruction injection for functions

For the expressions regarding return type, alignment, parameter type, etc.

2 files changed, 35 insertions(+), 20 deletions(-)

src/AstGen.zig+21-20
...@@ -10282,11 +10282,11 @@ const GenZir = struct {...@@ -10282,11 +10282,11 @@ const GenZir = struct {
10282 try astgen.extra.ensureUnusedCapacity(10282 try astgen.extra.ensureUnusedCapacity(
10283 gpa,10283 gpa,
10284 @typeInfo(Zir.Inst.FuncFancy).Struct.fields.len +10284 @typeInfo(Zir.Inst.FuncFancy).Struct.fields.len +
10285 fancyFnExprExtraLen(align_body, args.align_ref) +10285 fancyFnExprExtraLen(astgen, align_body, args.align_ref) +
10286 fancyFnExprExtraLen(addrspace_body, args.addrspace_ref) +10286 fancyFnExprExtraLen(astgen, addrspace_body, args.addrspace_ref) +
10287 fancyFnExprExtraLen(section_body, args.section_ref) +10287 fancyFnExprExtraLen(astgen, section_body, args.section_ref) +
10288 fancyFnExprExtraLen(cc_body, args.cc_ref) +10288 fancyFnExprExtraLen(astgen, cc_body, args.cc_ref) +
10289 fancyFnExprExtraLen(ret_body, ret_ref) +10289 fancyFnExprExtraLen(astgen, ret_body, ret_ref) +
10290 body_len + src_locs.len +10290 body_len + src_locs.len +
10291 @boolToInt(args.lib_name != 0) +10291 @boolToInt(args.lib_name != 0) +
10292 @boolToInt(args.noalias_bits != 0),10292 @boolToInt(args.noalias_bits != 0),
...@@ -10322,36 +10322,36 @@ const GenZir = struct {...@@ -10322,36 +10322,36 @@ const GenZir = struct {
1032210322
10323 const zir_datas = astgen.instructions.items(.data);10323 const zir_datas = astgen.instructions.items(.data);
10324 if (align_body.len != 0) {10324 if (align_body.len != 0) {
10325 astgen.extra.appendAssumeCapacity(@intCast(u32, align_body.len));10325 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, align_body));
10326 astgen.extra.appendSliceAssumeCapacity(align_body);10326 astgen.appendBodyWithFixups(align_body);
10327 zir_datas[align_body[align_body.len - 1]].@"break".block_inst = new_index;10327 zir_datas[align_body[align_body.len - 1]].@"break".block_inst = new_index;
10328 } else if (args.align_ref != .none) {10328 } else if (args.align_ref != .none) {
10329 astgen.extra.appendAssumeCapacity(@enumToInt(args.align_ref));10329 astgen.extra.appendAssumeCapacity(@enumToInt(args.align_ref));
10330 }10330 }
10331 if (addrspace_body.len != 0) {10331 if (addrspace_body.len != 0) {
10332 astgen.extra.appendAssumeCapacity(@intCast(u32, addrspace_body.len));10332 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, addrspace_body));
10333 astgen.extra.appendSliceAssumeCapacity(addrspace_body);10333 astgen.appendBodyWithFixups(addrspace_body);
10334 zir_datas[addrspace_body[addrspace_body.len - 1]].@"break".block_inst = new_index;10334 zir_datas[addrspace_body[addrspace_body.len - 1]].@"break".block_inst = new_index;
10335 } else if (args.addrspace_ref != .none) {10335 } else if (args.addrspace_ref != .none) {
10336 astgen.extra.appendAssumeCapacity(@enumToInt(args.addrspace_ref));10336 astgen.extra.appendAssumeCapacity(@enumToInt(args.addrspace_ref));
10337 }10337 }
10338 if (section_body.len != 0) {10338 if (section_body.len != 0) {
10339 astgen.extra.appendAssumeCapacity(@intCast(u32, section_body.len));10339 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, section_body));
10340 astgen.extra.appendSliceAssumeCapacity(section_body);10340 astgen.appendBodyWithFixups(section_body);
10341 zir_datas[section_body[section_body.len - 1]].@"break".block_inst = new_index;10341 zir_datas[section_body[section_body.len - 1]].@"break".block_inst = new_index;
10342 } else if (args.section_ref != .none) {10342 } else if (args.section_ref != .none) {
10343 astgen.extra.appendAssumeCapacity(@enumToInt(args.section_ref));10343 astgen.extra.appendAssumeCapacity(@enumToInt(args.section_ref));
10344 }10344 }
10345 if (cc_body.len != 0) {10345 if (cc_body.len != 0) {
10346 astgen.extra.appendAssumeCapacity(@intCast(u32, cc_body.len));10346 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, cc_body));
10347 astgen.extra.appendSliceAssumeCapacity(cc_body);10347 astgen.appendBodyWithFixups(cc_body);
10348 zir_datas[cc_body[cc_body.len - 1]].@"break".block_inst = new_index;10348 zir_datas[cc_body[cc_body.len - 1]].@"break".block_inst = new_index;
10349 } else if (args.cc_ref != .none) {10349 } else if (args.cc_ref != .none) {
10350 astgen.extra.appendAssumeCapacity(@enumToInt(args.cc_ref));10350 astgen.extra.appendAssumeCapacity(@enumToInt(args.cc_ref));
10351 }10351 }
10352 if (ret_body.len != 0) {10352 if (ret_body.len != 0) {
10353 astgen.extra.appendAssumeCapacity(@intCast(u32, ret_body.len));10353 astgen.extra.appendAssumeCapacity(countBodyLenAfterFixups(astgen, ret_body));
10354 astgen.extra.appendSliceAssumeCapacity(ret_body);10354 astgen.appendBodyWithFixups(ret_body);
10355 zir_datas[ret_body[ret_body.len - 1]].@"break".block_inst = new_index;10355 zir_datas[ret_body[ret_body.len - 1]].@"break".block_inst = new_index;
10356 } else if (ret_ref != .none) {10356 } else if (ret_ref != .none) {
10357 astgen.extra.appendAssumeCapacity(@enumToInt(ret_ref));10357 astgen.extra.appendAssumeCapacity(@enumToInt(ret_ref));
...@@ -10389,11 +10389,12 @@ const GenZir = struct {...@@ -10389,11 +10389,12 @@ const GenZir = struct {
10389 try astgen.extra.ensureUnusedCapacity(10389 try astgen.extra.ensureUnusedCapacity(
10390 gpa,10390 gpa,
10391 @typeInfo(Zir.Inst.Func).Struct.fields.len + 1 +10391 @typeInfo(Zir.Inst.Func).Struct.fields.len + 1 +
10392 @maximum(ret_body.len, @boolToInt(ret_ref != .none)) +10392 fancyFnExprExtraLen(astgen, ret_body, ret_ref) +
10393 body_len + src_locs.len,10393 body_len + src_locs.len,
10394 );10394 );
10395
10395 const ret_body_len = if (ret_body.len != 0)10396 const ret_body_len = if (ret_body.len != 0)
10396 @intCast(u32, ret_body.len)10397 countBodyLenAfterFixups(astgen, ret_body)
10397 else10398 else
10398 @boolToInt(ret_ref != .none);10399 @boolToInt(ret_ref != .none);
1039910400
...@@ -10404,7 +10405,7 @@ const GenZir = struct {...@@ -10404,7 +10405,7 @@ const GenZir = struct {
10404 });10405 });
10405 const zir_datas = astgen.instructions.items(.data);10406 const zir_datas = astgen.instructions.items(.data);
10406 if (ret_body.len != 0) {10407 if (ret_body.len != 0) {
10407 astgen.extra.appendSliceAssumeCapacity(ret_body);10408 astgen.appendBodyWithFixups(ret_body);
10408 zir_datas[ret_body[ret_body.len - 1]].@"break".block_inst = new_index;10409 zir_datas[ret_body[ret_body.len - 1]].@"break".block_inst = new_index;
10409 } else if (ret_ref != .none) {10410 } else if (ret_ref != .none) {
10410 astgen.extra.appendAssumeCapacity(@enumToInt(ret_ref));10411 astgen.extra.appendAssumeCapacity(@enumToInt(ret_ref));
...@@ -10435,10 +10436,10 @@ const GenZir = struct {...@@ -10435,10 +10436,10 @@ const GenZir = struct {
10435 }10436 }
10436 }10437 }
1043710438
10438 fn fancyFnExprExtraLen(body: []Zir.Inst.Index, ref: Zir.Inst.Ref) usize {10439 fn fancyFnExprExtraLen(astgen: *AstGen, body: []Zir.Inst.Index, ref: Zir.Inst.Ref) u32 {
10439 // In the case of non-empty body, there is one for the body length,10440 // In the case of non-empty body, there is one for the body length,
10440 // and then one for each instruction.10441 // and then one for each instruction.
10441 return body.len + @boolToInt(ref != .none);10442 return countBodyLenAfterFixups(astgen, body) + @boolToInt(ref != .none);
10442 }10443 }
1044310444
10444 fn addVar(gz: *GenZir, args: struct {10445 fn addVar(gz: *GenZir, args: struct {
test/behavior/fn.zig+14
...@@ -408,3 +408,17 @@ test "function with inferred error set but returning no error" {...@@ -408,3 +408,17 @@ test "function with inferred error set but returning no error" {
408 const return_ty = @typeInfo(@TypeOf(S.foo)).Fn.return_type.?;408 const return_ty = @typeInfo(@TypeOf(S.foo)).Fn.return_type.?;
409 try expectEqual(0, @typeInfo(@typeInfo(return_ty).ErrorUnion.error_set).ErrorSet.?.len);409 try expectEqual(0, @typeInfo(@typeInfo(return_ty).ErrorUnion.error_set).ErrorSet.?.len);
410}410}
411
412test "import passed byref to function in return type" {
413 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
414 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
415
416 const S = struct {
417 fn get() @import("std").ArrayListUnmanaged(i32) {
418 var x: @import("std").ArrayListUnmanaged(i32) = .{};
419 return x;
420 }
421 };
422 var list = S.get();
423 try expect(list.items.len == 0);
424}