Commit eba66f4a58

Veikka Tuominen <git@vexu.eu>
2022-05-25 17:27:17
Sema: handle block.is_typeof in more places
1 parent d214b6b
Changed files (2)
src/Sema.zig
@@ -1513,6 +1513,7 @@ fn resolveDefinedValue(
 ) CompileError!?Value {
     if (try sema.resolveMaybeUndefVal(block, src, air_ref)) |val| {
         if (val.isUndef()) {
+            if (block.is_typeof) return null;
             return sema.failWithUseOfUndef(block, src);
         }
         return val;
@@ -12268,6 +12269,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
         .inlining = block.inlining,
         .is_comptime = false,
         .is_typeof = true,
+        .want_safety = false,
     };
     defer child_block.instructions.deinit(sema.gpa);
 
@@ -20832,7 +20834,7 @@ fn analyzeDeclVal(
     const decl_ref = try sema.analyzeDeclRef(decl_index);
     const result = try sema.analyzeLoad(block, src, decl_ref, src);
     if (Air.refToIndex(result)) |index| {
-        if (sema.air_instructions.items(.tag)[index] == .constant) {
+        if (sema.air_instructions.items(.tag)[index] == .constant and !block.is_typeof) {
             try sema.decl_val_table.put(sema.gpa, decl_index, result);
         }
     }
@@ -20963,6 +20965,9 @@ fn analyzeLoad(
         if (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) |elem_val| {
             return sema.addConstant(elem_ty, elem_val);
         }
+        if (block.is_typeof) {
+            return sema.addConstUndef(elem_ty);
+        }
     }
 
     const valid_rt = try sema.validateRunTimeType(block, src, elem_ty, false);
test/behavior/sizeof_and_typeof.zig
@@ -280,3 +280,13 @@ test "@sizeOf comparison against zero" {
     try S.doTheTest(S1, true);
     try S.doTheTest(U1, true);
 }
+
+test "hardcoded address in typeof expression" {
+    const S = struct {
+        fn func() @TypeOf(@intToPtr(*[]u8, 0x10).*[0]) {
+            return 0;
+        }
+    };
+    try expect(S.func() == 0);
+    comptime try expect(S.func() == 0);
+}