stage2: fix global variables with inferred type

Also, when a global variable does have a type, perform coercion on it.
This commit is contained in:
Andrew Kelley 2021-09-16 21:43:01 -07:00
parent dbe9a5114e
commit 091a98f524
3 changed files with 24 additions and 11 deletions

View File

@ -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);

View File

@ -111,3 +111,10 @@ fn test_u128_cmpxchg() !void {
try expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
try expect(x == 42);
}
var a_global_variable = @as(u32, 1234);
test "cmpxchg on a global variable" {
_ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic);
try expect(a_global_variable == 42);
}

View File

@ -3,13 +3,6 @@ const expect = std.testing.expect;
const expectEqual = std.testing.expectEqual;
const builtin = @import("builtin");
var a_global_variable = @as(u32, 1234);
test "cmpxchg on a global variable" {
_ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic);
try expectEqual(@as(u32, 42), a_global_variable);
}
test "atomic load and rmw with enum" {
const Value = enum(u8) {
a,