authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-05-25 18:27:17+03:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-05-25 19:57:44+03:00
logeba66f4a584bbbfed93ac4de890c8a23f245fb1f
tree5420fa7e54db0f494155049291c3e5b934a25878
parentd214b6bdf05204377d897afe3eacc0cac33c59a3

Sema: handle block.is_typeof in more places


2 files changed, 16 insertions(+), 1 deletions(-)

src/Sema.zig+6-1
......@@ -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+10
......@@ -280,3 +280,13 @@ test "@sizeOf comparison against zero" {
280280 try S.doTheTest(S1, true);
281281 try S.doTheTest(U1, true);
282282}
283
284test "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}