Skip to content

Commit

Permalink
Sema: make @returnAddress return 0 at comptime
Browse files Browse the repository at this point in the history
See also ziglang#14938.

Resolves: ziglang#14931
  • Loading branch information
mlugg authored and truemedian committed Mar 30, 2023
1 parent c44363a commit 0237100
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -15454,9 +15454,13 @@ fn zirRetAddr(
block: *Block,
extended: Zir.Inst.Extended.InstData,
) CompileError!Air.Inst.Ref {
const src = LazySrcLoc.nodeOffset(@bitCast(i32, extended.operand));
try sema.requireRuntimeBlock(block, src, null);
return try block.addNoOp(.ret_addr);
_ = extended;
if (block.is_comptime) {
// TODO: we could give a meaningful lazy value here. #14938
return sema.addIntUnsigned(Type.usize, 0);
} else {
return block.addNoOp(.ret_addr);
}
}

fn zirFrameAddress(
Expand Down
1 change: 1 addition & 0 deletions test/behavior.zig
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ test {
_ = @import("behavior/pub_enum.zig");
_ = @import("behavior/ref_var_in_if_after_if_2nd_switch_prong.zig");
_ = @import("behavior/reflection.zig");
_ = @import("behavior/return_address.zig");
_ = @import("behavior/saturating_arithmetic.zig");
_ = @import("behavior/select.zig");
_ = @import("behavior/shuffle.zig");
Expand Down
17 changes: 17 additions & 0 deletions test/behavior/return_address.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const builtin = @import("builtin");
const testing = @import("std").testing;

fn retAddr() usize {
return @returnAddress();
}

test "return address" {
if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
_ = retAddr();
// TODO: #14938
try testing.expectEqual(0, comptime retAddr());
}

0 comments on commit 0237100

Please sign in to comment.