authorgravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-27 13:32:55+02:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2022-02-27 13:32:55+02:00
log7a92b89a9d19f3e6d258c848f1be13a6e31fec97
tree24c8172ece3a13981189b7bef2d4665c118cd0e6
parent813f368a3c44133106584b0d7e30748cff79a776

stage2: forward discard result loc to more expressions


3 files changed, 43 insertions(+), 3 deletions(-)

lib/std/Progress.zig+1-2
......@@ -289,10 +289,9 @@ fn refreshWithHeldLock(self: *Progress) void {
289289 }
290290 }
291291
292 _ = file.write(self.output_buffer[0..end]) catch blk: {
292 _ = file.write(self.output_buffer[0..end]) catch {
293293 // Stop trying to write to this file once it errors.
294294 self.terminal = null;
295 break :blk 0; // TODO stage2 requires this
296295 };
297296 if (self.timer) |*timer| {
298297 self.prev_refresh_timestamp = timer.read();
src/AstGen.zig+8-1
......@@ -244,10 +244,14 @@ pub const ResultLoc = union(enum) {
244244 fn strategy(rl: ResultLoc, block_scope: *GenZir) Strategy {
245245 switch (rl) {
246246 // In this branch there will not be any store_to_block_ptr instructions.
247 .discard, .none, .ty, .coerced_ty, .ref => return .{
247 .none, .ty, .coerced_ty, .ref => return .{
248248 .tag = .break_operand,
249249 .elide_store_to_block_ptr_instructions = false,
250250 },
251 .discard => return .{
252 .tag = .break_void,
253 .elide_store_to_block_ptr_instructions = false,
254 },
251255 // The pointer got passed through to the sub-expressions, so we will use
252256 // break_void here.
253257 // In this branch there will not be any store_to_block_ptr instructions.
......@@ -1766,6 +1770,9 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
17661770 // we assume the result location is written, and we break with void.
17671771 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
17681772 },
1773 .discard => {
1774 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
1775 },
17691776 else => {
17701777 _ = try parent_gz.addBreak(break_tag, block_inst, operand);
17711778 },
test/behavior/basic.zig+34
......@@ -819,3 +819,37 @@ test "if expression type coercion" {
819819 const x: u16 = if (cond) 1 else 0;
820820 try expect(@as(u16, x) == 1);
821821}
822
823test "discarding the result of various expressions" {
824 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
825 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
826 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
827
828 const S = struct {
829 fn foo() !u32 {
830 return 1;
831 }
832 fn bar() ?u32 {
833 return 1;
834 }
835 };
836 _ = S.bar() orelse {
837 // do nothing
838 };
839 _ = S.foo() catch {
840 // do nothing
841 };
842 _ = switch (1) {
843 1 => 1,
844 2 => {},
845 else => return,
846 };
847 _ = try S.foo();
848 _ = if (S.bar()) |some| some else {};
849 _ = blk: {
850 if (S.bar()) |some| break :blk some;
851 break :blk;
852 };
853 _ = while (S.bar()) |some| break some else {};
854 _ = for ("foo") |char| break char else {};
855}