Skip to content

Commit 091a98f

Browse files
committed
stage2: fix global variables with inferred type
Also, when a global variable does have a type, perform coercion on it.
1 parent dbe9a51 commit 091a98f

File tree

3 files changed

+24
-11
lines changed

3 files changed

+24
-11
lines changed

src/Sema.zig

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7920,7 +7920,6 @@ fn zirVarExtended(
79207920
const mut_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at mut token
79217921
const init_src: LazySrcLoc = src; // TODO add a LazySrcLoc that points at init expr
79227922
const small = @bitCast(Zir.Inst.ExtendedVar.Small, extended.small);
7923-
const var_ty = try sema.resolveType(block, ty_src, extra.data.var_type);
79247923

79257924
var extra_index: usize = extra.end;
79267925

@@ -7940,11 +7939,25 @@ fn zirVarExtended(
79407939
// break :blk align_tv.val;
79417940
//} else Value.initTag(.null_value);
79427941

7943-
const init_val: Value = if (small.has_init) blk: {
7942+
const uncasted_init: Air.Inst.Ref = if (small.has_init) blk: {
79447943
const init_ref = @intToEnum(Zir.Inst.Ref, sema.code.extra[extra_index]);
79457944
extra_index += 1;
7946-
const init_air_inst = sema.resolveInst(init_ref);
7947-
break :blk (try sema.resolveMaybeUndefVal(block, init_src, init_air_inst)) orelse
7945+
break :blk sema.resolveInst(init_ref);
7946+
} else .none;
7947+
7948+
const have_ty = extra.data.var_type != .none;
7949+
const var_ty = if (have_ty)
7950+
try sema.resolveType(block, ty_src, extra.data.var_type)
7951+
else
7952+
sema.typeOf(uncasted_init);
7953+
7954+
const init_val = if (uncasted_init != .none) blk: {
7955+
const init = if (have_ty)
7956+
try sema.coerce(block, var_ty, uncasted_init, init_src)
7957+
else
7958+
uncasted_init;
7959+
7960+
break :blk (try sema.resolveMaybeUndefVal(block, init_src, init)) orelse
79487961
return sema.failWithNeededComptime(block, init_src);
79497962
} else Value.initTag(.unreachable_value);
79507963

test/behavior/atomics.zig

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,3 +111,10 @@ fn test_u128_cmpxchg() !void {
111111
try expect(@cmpxchgStrong(u128, &x, 5678, 42, .SeqCst, .SeqCst) == null);
112112
try expect(x == 42);
113113
}
114+
115+
var a_global_variable = @as(u32, 1234);
116+
117+
test "cmpxchg on a global variable" {
118+
_ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic);
119+
try expect(a_global_variable == 42);
120+
}

test/behavior/atomics_stage1.zig

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,6 @@ const expect = std.testing.expect;
33
const expectEqual = std.testing.expectEqual;
44
const builtin = @import("builtin");
55

6-
var a_global_variable = @as(u32, 1234);
7-
8-
test "cmpxchg on a global variable" {
9-
_ = @cmpxchgWeak(u32, &a_global_variable, 1234, 42, .Acquire, .Monotonic);
10-
try expectEqual(@as(u32, 42), a_global_variable);
11-
}
12-
136
test "atomic load and rmw with enum" {
147
const Value = enum(u8) {
158
a,

0 commit comments

Comments
 (0)