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 {...@@ -289,10 +289,9 @@ fn refreshWithHeldLock(self: *Progress) void {
289 }289 }
290 }290 }
291291
292 _ = file.write(self.output_buffer[0..end]) catch blk: {292 _ = file.write(self.output_buffer[0..end]) catch {
293 // Stop trying to write to this file once it errors.293 // Stop trying to write to this file once it errors.
294 self.terminal = null;294 self.terminal = null;
295 break :blk 0; // TODO stage2 requires this
296 };295 };
297 if (self.timer) |*timer| {296 if (self.timer) |*timer| {
298 self.prev_refresh_timestamp = timer.read();297 self.prev_refresh_timestamp = timer.read();
src/AstGen.zig+8-1
...@@ -244,10 +244,14 @@ pub const ResultLoc = union(enum) {...@@ -244,10 +244,14 @@ pub const ResultLoc = union(enum) {
244 fn strategy(rl: ResultLoc, block_scope: *GenZir) Strategy {244 fn strategy(rl: ResultLoc, block_scope: *GenZir) Strategy {
245 switch (rl) {245 switch (rl) {
246 // In this branch there will not be any store_to_block_ptr instructions.246 // 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 .{
248 .tag = .break_operand,248 .tag = .break_operand,
249 .elide_store_to_block_ptr_instructions = false,249 .elide_store_to_block_ptr_instructions = false,
250 },250 },
251 .discard => return .{
252 .tag = .break_void,
253 .elide_store_to_block_ptr_instructions = false,
254 },
251 // The pointer got passed through to the sub-expressions, so we will use255 // The pointer got passed through to the sub-expressions, so we will use
252 // break_void here.256 // break_void here.
253 // In this branch there will not be any store_to_block_ptr instructions.257 // 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...@@ -1766,6 +1770,9 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
1766 // we assume the result location is written, and we break with void.1770 // we assume the result location is written, and we break with void.
1767 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);1771 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
1768 },1772 },
1773 .discard => {
1774 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
1775 },
1769 else => {1776 else => {
1770 _ = try parent_gz.addBreak(break_tag, block_inst, operand);1777 _ = try parent_gz.addBreak(break_tag, block_inst, operand);
1771 },1778 },
test/behavior/basic.zig+34
...@@ -819,3 +819,37 @@ test "if expression type coercion" {...@@ -819,3 +819,37 @@ test "if expression type coercion" {
819 const x: u16 = if (cond) 1 else 0;819 const x: u16 = if (cond) 1 else 0;
820 try expect(@as(u16, x) == 1);820 try expect(@as(u16, x) == 1);
821}821}
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}