diff options
| author | Mitchell Hashimoto <mitchell.hashimoto@gmail.com> | 2022-03-06 17:31:43 -0800 |
|---|---|---|
| committer | Mitchell Hashimoto <mitchell.hashimoto@gmail.com> | 2022-03-07 07:30:30 -0800 |
| commit | c9fac41368c872d424681ea3cf93a9d97157143e (patch) | |
| tree | 887f6058136580aad6110a329d82038ba71ad663 /src/Sema.zig | |
| parent | c7e4c711fc5795e66f974316611922a0b962eb99 (diff) | |
| download | zig-c9fac41368c872d424681ea3cf93a9d97157143e.tar.gz zig-c9fac41368c872d424681ea3cf93a9d97157143e.zip | |
stage2: resolve array type for typed array init expressions
Array types with sentinels were not being typed correctly in the
translation from ZIR to Sema (comptime). This modifies the `array_init`
ZIR to also retain the type of the init expression (note: untyped array
initialization is done via the `array_init_anon` ZIR and so is unchanged
in this commit).
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 31 |
1 files changed, 21 insertions, 10 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index fb7209ae0e..6c1c3d57b0 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -11796,23 +11796,34 @@ fn zirArrayInit( for (args) |arg, i| resolved_args[i] = sema.resolveInst(arg); - const elem_ty = sema.typeOf(resolved_args[0]); + const elem_ty = sema.typeOf(resolved_args[1]); + const array_ty = switch (resolved_args[0]) { + .none => try Type.Tag.array.create(sema.arena, .{ + .len = resolved_args.len, + .elem_type = elem_ty, + }), - const array_ty = try Type.Tag.array.create(sema.arena, .{ - .len = resolved_args.len, - .elem_type = elem_ty, - }); + else => |ref| blk: { + assert(sema.typeOf(ref).zigTypeTag() == .Type); + var buffer: Value.ToTypeBuffer = undefined; + const val = try sema.resolveConstValue(block, src, ref); + const ty = val.toType(&buffer); + break :blk try ty.copy(sema.arena); + }, + }; + + const elems = resolved_args[1..]; - const opt_runtime_src: ?LazySrcLoc = for (resolved_args) |arg| { + const opt_runtime_src: ?LazySrcLoc = for (elems) |arg| { const arg_src = src; // TODO better source location const comptime_known = try sema.isComptimeKnown(block, arg_src, arg); if (!comptime_known) break arg_src; } else null; const runtime_src = opt_runtime_src orelse { - const elem_vals = try sema.arena.alloc(Value, resolved_args.len); + const elem_vals = try sema.arena.alloc(Value, elems.len); - for (resolved_args) |arg, i| { + for (elems) |arg, i| { // We checked that all args are comptime above. elem_vals[i] = (sema.resolveMaybeUndefVal(block, src, arg) catch unreachable).?; } @@ -11839,7 +11850,7 @@ fn zirArrayInit( }); const elem_ptr_ty_ref = try sema.addType(elem_ptr_ty); - for (resolved_args) |arg, i| { + for (elems) |arg, i| { const index = try sema.addIntUnsigned(Type.usize, i); const elem_ptr = try block.addPtrElemPtrTypeRef(alloc, index, elem_ptr_ty_ref); _ = try block.addBinOp(.store, elem_ptr, arg); @@ -11847,7 +11858,7 @@ fn zirArrayInit( return alloc; } - return block.addAggregateInit(array_ty, resolved_args); + return block.addAggregateInit(array_ty, elems); } fn zirArrayInitAnon( |
