Skip to content

Commit eba66f4

Browse files
committed
Sema: handle block.is_typeof in more places
1 parent d214b6b commit eba66f4

File tree

2 files changed

+16
-1
lines changed

2 files changed

+16
-1
lines changed

src/Sema.zig

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1513,6 +1513,7 @@ fn resolveDefinedValue(
15131513
) CompileError!?Value {
15141514
if (try sema.resolveMaybeUndefVal(block, src, air_ref)) |val| {
15151515
if (val.isUndef()) {
1516+
if (block.is_typeof) return null;
15161517
return sema.failWithUseOfUndef(block, src);
15171518
}
15181519
return val;
@@ -12268,6 +12269,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
1226812269
.inlining = block.inlining,
1226912270
.is_comptime = false,
1227012271
.is_typeof = true,
12272+
.want_safety = false,
1227112273
};
1227212274
defer child_block.instructions.deinit(sema.gpa);
1227312275

@@ -20832,7 +20834,7 @@ fn analyzeDeclVal(
2083220834
const decl_ref = try sema.analyzeDeclRef(decl_index);
2083320835
const result = try sema.analyzeLoad(block, src, decl_ref, src);
2083420836
if (Air.refToIndex(result)) |index| {
20835-
if (sema.air_instructions.items(.tag)[index] == .constant) {
20837+
if (sema.air_instructions.items(.tag)[index] == .constant and !block.is_typeof) {
2083620838
try sema.decl_val_table.put(sema.gpa, decl_index, result);
2083720839
}
2083820840
}
@@ -20963,6 +20965,9 @@ fn analyzeLoad(
2096320965
if (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) |elem_val| {
2096420966
return sema.addConstant(elem_ty, elem_val);
2096520967
}
20968+
if (block.is_typeof) {
20969+
return sema.addConstUndef(elem_ty);
20970+
}
2096620971
}
2096720972

2096820973
const valid_rt = try sema.validateRunTimeType(block, src, elem_ty, false);

test/behavior/sizeof_and_typeof.zig

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -280,3 +280,13 @@ test "@sizeOf comparison against zero" {
280280
try S.doTheTest(S1, true);
281281
try S.doTheTest(U1, true);
282282
}
283+
284+
test "hardcoded address in typeof expression" {
285+
const S = struct {
286+
fn func() @TypeOf(@intToPtr(*[]u8, 0x10).*[0]) {
287+
return 0;
288+
}
289+
};
290+
try expect(S.func() == 0);
291+
comptime try expect(S.func() == 0);
292+
}

0 commit comments

Comments
 (0)