authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-31 22:08:59-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-31 23:47:34-07:00
log26253acf1ddcc2007804a3fbf538f3120e048547
treef77837e5ed537f69eae4df0fc81f8eb54304d894
parentecd756834bb79b198bd560cfc11c8b63c66437ec

AstGen: use `block_inline` and `break_inline` consistently

These are more efficiently semantically analyzed. More importantly, if they don't match, we get a crash in Sema. Missing places prior to this commit: * labeled blocks * `break` and `continue` on comptime (not inline) loops * `if`, `try`, `orelse`, and `catch` inside comptime scopes

1 files changed, 18 insertions(+), 7 deletions(-)

src/AstGen.zig+18-7
......@@ -1801,7 +1801,10 @@ fn breakExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index) Inn
18011801 continue;
18021802 };
18031803
1804 const break_tag: Zir.Inst.Tag = if (block_gz.is_inline) .break_inline else .@"break";
1804 const break_tag: Zir.Inst.Tag = if (block_gz.is_inline or block_gz.force_comptime)
1805 .break_inline
1806 else
1807 .@"break";
18051808
18061809 if (rhs == 0) {
18071810 _ = try parent_gz.addBreak(break_tag, block_inst, .void_value);
......@@ -1887,7 +1890,10 @@ fn continueExpr(parent_gz: *GenZir, parent_scope: *Scope, node: Ast.Node.Index)
18871890 continue;
18881891 }
18891892
1890 const break_tag: Zir.Inst.Tag = if (gen_zir.is_inline) .break_inline else .@"break";
1893 const break_tag: Zir.Inst.Tag = if (gen_zir.is_inline or gen_zir.force_comptime)
1894 .break_inline
1895 else
1896 .@"break";
18911897 _ = try parent_gz.addBreak(break_tag, continue_block, .void_value);
18921898 return Zir.Inst.Ref.unreachable_value;
18931899 },
......@@ -1993,7 +1999,8 @@ fn labeledBlockExpr(
19931999
19942000 // Reserve the Block ZIR instruction index so that we can put it into the GenZir struct
19952001 // so that break statements can reference it.
1996 const block_inst = try gz.makeBlockInst(.block, block_node);
2002 const block_tag: Zir.Inst.Tag = if (gz.force_comptime) .block_inline else .block;
2003 const block_inst = try gz.makeBlockInst(block_tag, block_node);
19972004 try gz.instructions.append(astgen.gpa, block_inst);
19982005
19992006 var block_scope = gz.makeSubBlock(parent_scope);
......@@ -2007,7 +2014,8 @@ fn labeledBlockExpr(
20072014
20082015 try blockExprStmts(&block_scope, &block_scope.base, statements);
20092016 if (!block_scope.endsWithNoReturn()) {
2010 _ = try block_scope.addBreak(.@"break", block_inst, .void_value);
2017 const break_tag: Zir.Inst.Tag = if (block_scope.force_comptime) .break_inline else .@"break";
2018 _ = try block_scope.addBreak(break_tag, block_inst, .void_value);
20112019 }
20122020
20132021 if (!block_scope.label.?.used) {
......@@ -4833,6 +4841,7 @@ fn tryExpr(
48334841 try genDefers(&else_scope, &fn_block.base, scope, .{ .both = err_code });
48344842 const else_result = try else_scope.addUnNode(.ret_node, err_code, node);
48354843
4844 const break_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .break_inline else .@"break";
48364845 return finishThenElseBlock(
48374846 parent_gz,
48384847 rl,
......@@ -4846,7 +4855,7 @@ fn tryExpr(
48464855 else_result,
48474856 block,
48484857 block,
4849 .@"break",
4858 break_tag,
48504859 );
48514860}
48524861
......@@ -4928,6 +4937,7 @@ fn orelseCatchExpr(
49284937 // instructions into place until we know whether to keep store_to_block_ptr
49294938 // instructions or not.
49304939
4940 const break_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .break_inline else .@"break";
49314941 return finishThenElseBlock(
49324942 parent_gz,
49334943 rl,
......@@ -4941,7 +4951,7 @@ fn orelseCatchExpr(
49414951 else_result,
49424952 block,
49434953 block,
4944 .@"break",
4954 break_tag,
49454955 );
49464956}
49474957
......@@ -5306,6 +5316,7 @@ fn ifExpr(
53065316 .result = .none,
53075317 };
53085318
5319 const break_tag: Zir.Inst.Tag = if (parent_gz.force_comptime) .break_inline else .@"break";
53095320 return finishThenElseBlock(
53105321 parent_gz,
53115322 rl,
......@@ -5319,7 +5330,7 @@ fn ifExpr(
53195330 else_info.result,
53205331 block,
53215332 block,
5322 .@"break",
5333 break_tag,
53235334 );
53245335}
53255336