From eba66f4a584bbbfed93ac4de890c8a23f245fb1f Mon Sep 17 00:00:00 2001 From: Veikka Tuominen Date: Wed, 25 May 2022 18:27:17 +0300 Subject: [PATCH] Sema: handle block.is_typeof in more places --- src/Sema.zig | 7 ++++++- test/behavior/sizeof_and_typeof.zig | 10 ++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/Sema.zig b/src/Sema.zig index 340b1f9db3..2d2e7f9030 100644 --- a/src/Sema.zig +++ b/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); diff --git a/test/behavior/sizeof_and_typeof.zig b/test/behavior/sizeof_and_typeof.zig index 293acda267..d1f26c8167 100644 --- a/test/behavior/sizeof_and_typeof.zig +++ b/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); +}