authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-16 21:43:01-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-09-16 21:43:01-07:00
log091a98f524b41cac1fb299cdfdf911ae3b31c6ae
tree15ee8668a3e29e1ba4a978bebb4fb46583b3ac08
parentdbe9a5114e2d56f847b674539ffa0d28fc57ea78

stage2: fix global variables with inferred type

Also, when a global variable does have a type, perform coercion on it.

3 files changed, 24 insertions(+), 11 deletions(-)

src/Sema.zig+17-4
......@@ -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+7
......@@ -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
115var a_global_variable = @as(u32, 1234);
116
117test "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-7
......@@ -3,13 +3,6 @@ const expect = std.testing.expect;
33const expectEqual = std.testing.expectEqual;
44const builtin = @import("builtin");
55
6var a_global_variable = @as(u32, 1234);
7
8test "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
136test "atomic load and rmw with enum" {
147 const Value = enum(u8) {
158 a,