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(...@@ -1513,6 +1513,7 @@ fn resolveDefinedValue(
1513) CompileError!?Value {1513) CompileError!?Value {
1514 if (try sema.resolveMaybeUndefVal(block, src, air_ref)) |val| {1514 if (try sema.resolveMaybeUndefVal(block, src, air_ref)) |val| {
1515 if (val.isUndef()) {1515 if (val.isUndef()) {
1516 if (block.is_typeof) return null;
1516 return sema.failWithUseOfUndef(block, src);1517 return sema.failWithUseOfUndef(block, src);
1517 }1518 }
1518 return val;1519 return val;
...@@ -12268,6 +12269,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr...@@ -12268,6 +12269,7 @@ fn zirTypeofBuiltin(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErr
12268 .inlining = block.inlining,12269 .inlining = block.inlining,
12269 .is_comptime = false,12270 .is_comptime = false,
12270 .is_typeof = true,12271 .is_typeof = true,
12272 .want_safety = false,
12271 };12273 };
12272 defer child_block.instructions.deinit(sema.gpa);12274 defer child_block.instructions.deinit(sema.gpa);
1227312275
...@@ -20832,7 +20834,7 @@ fn analyzeDeclVal(...@@ -20832,7 +20834,7 @@ fn analyzeDeclVal(
20832 const decl_ref = try sema.analyzeDeclRef(decl_index);20834 const decl_ref = try sema.analyzeDeclRef(decl_index);
20833 const result = try sema.analyzeLoad(block, src, decl_ref, src);20835 const result = try sema.analyzeLoad(block, src, decl_ref, src);
20834 if (Air.refToIndex(result)) |index| {20836 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) {
20836 try sema.decl_val_table.put(sema.gpa, decl_index, result);20838 try sema.decl_val_table.put(sema.gpa, decl_index, result);
20837 }20839 }
20838 }20840 }
...@@ -20963,6 +20965,9 @@ fn analyzeLoad(...@@ -20963,6 +20965,9 @@ fn analyzeLoad(
20963 if (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) |elem_val| {20965 if (try sema.pointerDeref(block, ptr_src, ptr_val, ptr_ty)) |elem_val| {
20964 return sema.addConstant(elem_ty, elem_val);20966 return sema.addConstant(elem_ty, elem_val);
20965 }20967 }
20968 if (block.is_typeof) {
20969 return sema.addConstUndef(elem_ty);
20970 }
20966 }20971 }
2096720972
20968 const valid_rt = try sema.validateRunTimeType(block, src, elem_ty, false);20973 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" {...@@ -280,3 +280,13 @@ test "@sizeOf comparison against zero" {
280 try S.doTheTest(S1, true);280 try S.doTheTest(S1, true);
281 try S.doTheTest(U1, true);281 try S.doTheTest(U1, true);
282}282}
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}