diff options
| author | mlugg <mlugg@mlugg.co.uk> | 2025-02-05 20:33:39 +0000 |
|---|---|---|
| committer | mlugg <mlugg@mlugg.co.uk> | 2025-02-05 21:17:40 +0000 |
| commit | fbbf34e563a376ea1654dce827b9194ba7211b3a (patch) | |
| tree | dc95e30f87d14c2ff3b3784df76ed10327f4425a /src/Sema.zig | |
| parent | cac814cf58ca65ffd8081dca6f2f5b26d822ef5d (diff) | |
| download | zig-fbbf34e563a376ea1654dce827b9194ba7211b3a.tar.gz zig-fbbf34e563a376ea1654dce827b9194ba7211b3a.zip | |
Sema: disable runtime safety checks in comptime blocks
Sometimes we emit runtime instructions in comptime scopes. These
instructions will be discarded, but they allow comptime blocks to
contain intermediate runtime-known values, which is necessary for
expressions like `runtime_array.len` to work.
Since we will always throw away these runtime instructions, including
safety checks is a time waste at best and trips an assertion at worst!
Resolves: #20064
Diffstat (limited to 'src/Sema.zig')
| -rw-r--r-- | src/Sema.zig | 16 |
1 files changed, 13 insertions, 3 deletions
diff --git a/src/Sema.zig b/src/Sema.zig index b04cae9a8f..d66587abb1 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -504,7 +504,17 @@ pub const Block = struct { }; } - pub fn wantSafety(block: *const Block) bool { + fn wantSafeTypes(block: *const Block) bool { + return block.want_safety orelse switch (block.sema.pt.zcu.optimizeMode()) { + .Debug => true, + .ReleaseSafe => true, + .ReleaseFast => false, + .ReleaseSmall => false, + }; + } + + fn wantSafety(block: *const Block) bool { + if (block.isComptime()) return false; // runtime safety checks are pointless in comptime blocks return block.want_safety orelse switch (block.sema.pt.zcu.optimizeMode()) { .Debug => true, .ReleaseSafe => true, @@ -3294,7 +3304,7 @@ fn zirUnionDecl( .tagged else if (small.layout != .auto) .none - else switch (block.wantSafety()) { + else switch (block.wantSafeTypes()) { true => .safety, false => .none, }, @@ -22219,7 +22229,7 @@ fn reifyUnion( .tagged else if (layout != .auto) .none - else switch (block.wantSafety()) { + else switch (block.wantSafeTypes()) { true => .safety, false => .none, }, |
