diff options
| author | Andrew Kelley <andrew@ziglang.org> | 2021-09-16 21:43:01 -0700 |
|---|---|---|
| committer | Andrew Kelley <andrew@ziglang.org> | 2021-09-16 21:43:01 -0700 |
| commit | 091a98f524b41cac1fb299cdfdf911ae3b31c6ae (patch) | |
| tree | 15ee8668a3e29e1ba4a978bebb4fb46583b3ac08 /src/Sema.zig | |
| parent | dbe9a5114e2d56f847b674539ffa0d28fc57ea78 (diff) | |
| download | zig-091a98f524b41cac1fb299cdfdf911ae3b31c6ae.tar.gz zig-091a98f524b41cac1fb299cdfdf911ae3b31c6ae.zip | |
stage2: fix global variables with inferred type
Also, when a global variable does have a type, perform coercion on it.
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index 58aa7ca1c0..645e68c6ef 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -7920,7 +7920,6 @@ fn zirVarExtended( const mut_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at mut token const init_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at init expr const small = @bitCast(Zir.Inst.ExtendedVar.Small, extended.small); - const var_ty = try sema.resolveType(block, ty_src, extra.data.var_type); var extra_index: usize = extra.end; @@ -7940,11 +7939,25 @@ fn zirVarExtended( // break :blk align_tv.val; //} else Value.initTag(.null_value); - const init_val: Value = if (small.has_init) blk: { + const uncasted_init: Air.Inst.Ref = if (small.has_init) blk: { const init_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]); extra_index += 1; - const init_air_inst = sema.resolveInst(init_ref); - break :blk (try sema.resolveMaybeUndefVal(block, init_src, init_air_inst)) orelse + break :blk sema.resolveInst(init_ref); + } else .none; + + const have_ty = extra.data.var_type != .none; + const var_ty = if (have_ty) + try sema.resolveType(block, ty_src, extra.data.var_type) + else + sema.typeOf(uncasted_init); + + const init_val = if (uncasted_init != .none) blk: { + const init = if (have_ty) + try sema.coerce(block, var_ty, uncasted_init, init_src) + else + uncasted_init; + + break :blk (try sema.resolveMaybeUndefVal(block, init_src, init)) orelse return sema.failWithNeededComptime(block, init_src); } else Value.initTag(.unreachable_value); |
