Commit 98b3734b67

Veikka Tuominen <git@vexu.eu>
2022-11-17 22:00:01
Sema: prioritize Value.variable over OPV when resolving const value
Closes #12275
1 parent 9877a7d
Changed files (2)
src
test
behavior
src/Sema.zig
@@ -1897,10 +1897,15 @@ fn resolveMaybeUndefValAllowVariablesMaybeRuntime(
     }
     i -= Air.Inst.Ref.typed_value_map.len;
 
+    const air_tags = sema.air_instructions.items(.tag);
     if (try sema.typeHasOnePossibleValue(sema.typeOf(inst))) |opv| {
+        if (air_tags[i] == .constant) {
+            const ty_pl = sema.air_instructions.items(.data)[i].ty_pl;
+            const val = sema.air_values.items[ty_pl.payload];
+            if (val.tag() == .variable) return val;
+        }
         return opv;
     }
-    const air_tags = sema.air_instructions.items(.tag);
     switch (air_tags[i]) {
         .constant => {
             const ty_pl = sema.air_instructions.items(.data)[i].ty_pl;
test/behavior/basic.zig
@@ -1118,3 +1118,12 @@ test "ambiguous reference error ignores current declaration" {
     };
     try expect(S.b.foo == 666);
 }
+
+test "pointer to zero sized global is mutable" {
+    const S = struct {
+        const Thing = struct {};
+
+        var thing: Thing = undefined;
+    };
+    try expect(@TypeOf(&S.thing) == *S.Thing);
+}