authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-08-20 17:44:03-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-08-20 17:44:03-07:00
logf0176eec4a744c17fc49cbd63c67179887005935
treeb7efae8b8cfe53a9496cbd4252886a644e4c3bba
parent6c55d854cffbe7a518e8ec62f746585ddc68ebbf

stage2: support comptime fn call returning type

...when the field type expressions reference locals as well as comptime function parameters.

2 files changed, 30 insertions(+), 1 deletions(-)

src/Sema.zig+12-1
......@@ -2623,7 +2623,18 @@ fn analyzeCall(
26232623 defer sema.fn_ret_ty = parent_fn_ret_ty;
26242624
26252625 _ = try sema.analyzeBody(&child_block, fn_info.body);
2626 break :res try sema.analyzeBlockBody(block, call_src, &child_block, merges);
2626 const result = try sema.analyzeBlockBody(block, call_src, &child_block, merges);
2627
2628 // Much like in `Module.semaDecl`, if the result is a struct or union type,
2629 // we need to resolve the field type expressions right here, right now, while
2630 // the child `Sema` is still available, with the AIR instruction map intact,
2631 // because the field type expressions may reference into it.
2632 if (sema.typeOf(result).zigTypeTag() == .Type) {
2633 const ty = try sema.analyzeAsType(&child_block, call_src, result);
2634 try sema.resolveDeclFields(&child_block, call_src, ty);
2635 }
2636
2637 break :res result;
26272638 } else if (func_ty_info.is_generic) res: {
26282639 const func_val = try sema.resolveConstValue(block, func_src, func);
26292640 const module_fn = func_val.castTag(.function).?.data;
test/behavior/generics.zig+18
......@@ -78,3 +78,21 @@ fn max_i32(a: i32, b: i32) i32 {
7878fn max_f64(a: f64, b: f64) f64 {
7979 return max_anytype(a, b);
8080}
81
82test "type constructed by comptime function call" {
83 var l: List(10) = undefined;
84 l.array[0] = 10;
85 l.array[1] = 11;
86 l.array[2] = 12;
87 const ptr = @ptrCast([*]u8, &l.array);
88 try expect(ptr[0] == 10);
89 try expect(ptr[1] == 11);
90 try expect(ptr[2] == 12);
91}
92
93fn List(comptime L: usize) type {
94 var T = u8;
95 return struct {
96 array: [L]T,
97 };
98}