authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-28 14:16:15-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-02 13:26:50-07:00
log7f5560689aeb0e2ab84c5e3dc48f84247bd5bdc3
treeeafe7ba69a4ebffc6c54067f6bf615daf6d89bdb
parentcffa22a658d23bdedbdd7e23853b80d856e43627

AstGen: introduce 'reachableExpr' function

This function can be swapped out for calls to expr() to report a compile error when the expression results in control flow that does not return.

2 files changed, 36 insertions(+), 39 deletions(-)

src/AstGen.zig+25-34
......@@ -261,6 +261,23 @@ fn typeExpr(gz: *GenZir, scope: *Scope, type_node: ast.Node.Index) InnerError!Zi
261261 return expr(gz, scope, .{ .ty = .type_type }, type_node);
262262}
263263
264/// Same as `expr` but fails with a compile error if the result type is `noreturn`.
265fn reachableExpr(
266 gz: *GenZir,
267 scope: *Scope,
268 rl: ResultLoc,
269 node: ast.Node.Index,
270 src_node: ast.Node.Index,
271) InnerError!Zir.Inst.Ref {
272 const result_inst = try expr(gz, scope, rl, node);
273 if (gz.refIsNoReturn(result_inst)) {
274 return gz.astgen.failNodeNotes(src_node, "unreachable code", .{}, &[_]u32{
275 try gz.astgen.errNoteNode(node, "control flow is diverted here", .{}),
276 });
277 }
278 return result_inst;
279}
280
264281fn lvalExpr(gz: *GenZir, scope: *Scope, node: ast.Node.Index) InnerError!Zir.Inst.Ref {
265282 const astgen = gz.astgen;
266283 const tree = astgen.tree;
......@@ -2331,8 +2348,7 @@ fn varDecl(
23312348 const result_loc: ResultLoc = if (var_decl.ast.type_node != 0) .{
23322349 .ty = try typeExpr(gz, scope, var_decl.ast.type_node),
23332350 } else .none;
2334 const init_inst = try expr(gz, scope, result_loc, var_decl.ast.init_node);
2335 try astgen.checkVarInitExpr(gz.*, node, var_decl.ast.init_node, init_inst, "local constant");
2351 const init_inst = try reachableExpr(gz, scope, result_loc, var_decl.ast.init_node, node);
23362352
23372353 const sub_scope = try block_arena.create(Scope.LocalVal);
23382354 sub_scope.* = .{
......@@ -2383,8 +2399,7 @@ fn varDecl(
23832399 init_scope.rl_ptr = alloc;
23842400 }
23852401 const init_result_loc: ResultLoc = .{ .block_ptr = &init_scope };
2386 const init_inst = try expr(&init_scope, &init_scope.base, init_result_loc, var_decl.ast.init_node);
2387 try astgen.checkVarInitExpr(init_scope, node, var_decl.ast.init_node, init_inst, "local constant");
2402 const init_inst = try reachableExpr(&init_scope, &init_scope.base, init_result_loc, var_decl.ast.init_node, node);
23882403
23892404 const zir_tags = astgen.instructions.items(.tag);
23902405 const zir_datas = astgen.instructions.items(.data);
......@@ -2486,8 +2501,7 @@ fn varDecl(
24862501 resolve_inferred_alloc = alloc;
24872502 break :a .{ .alloc = alloc, .result_loc = .{ .inferred_ptr = alloc } };
24882503 };
2489 const init_inst = try expr(gz, scope, var_data.result_loc, var_decl.ast.init_node);
2490 try astgen.checkVarInitExpr(gz.*, node, var_decl.ast.init_node, init_inst, "local variable");
2504 _ = try reachableExpr(gz, scope, var_data.result_loc, var_decl.ast.init_node, node);
24912505 if (resolve_inferred_alloc != .none) {
24922506 _ = try gz.addUnNode(.resolve_inferred_alloc, resolve_inferred_alloc, node);
24932507 }
......@@ -6618,14 +6632,14 @@ fn as(
66186632 const dest_type = try typeExpr(gz, scope, lhs);
66196633 switch (rl) {
66206634 .none, .none_or_ref, .discard, .ref, .ty => {
6621 const result = try expr(gz, scope, .{ .ty = dest_type }, rhs);
6635 const result = try reachableExpr(gz, scope, .{ .ty = dest_type }, rhs, node);
66226636 return rvalue(gz, rl, result, node);
66236637 },
66246638 .ptr, .inferred_ptr => |result_ptr| {
6625 return asRlPtr(gz, scope, rl, result_ptr, rhs, dest_type);
6639 return asRlPtr(gz, scope, rl, node, result_ptr, rhs, dest_type);
66266640 },
66276641 .block_ptr => |block_scope| {
6628 return asRlPtr(gz, scope, rl, block_scope.rl_ptr, rhs, dest_type);
6642 return asRlPtr(gz, scope, rl, node, block_scope.rl_ptr, rhs, dest_type);
66296643 },
66306644 }
66316645}
......@@ -6679,6 +6693,7 @@ fn asRlPtr(
66796693 parent_gz: *GenZir,
66806694 scope: *Scope,
66816695 rl: ResultLoc,
6696 src_node: ast.Node.Index,
66826697 result_ptr: Zir.Inst.Ref,
66836698 operand_node: ast.Node.Index,
66846699 dest_type: Zir.Inst.Ref,
......@@ -6692,7 +6707,7 @@ fn asRlPtr(
66926707 defer as_scope.instructions.deinit(astgen.gpa);
66936708
66946709 as_scope.rl_ptr = try as_scope.addBin(.coerce_result_ptr, dest_type, result_ptr);
6695 const result = try expr(&as_scope, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node);
6710 const result = try reachableExpr(&as_scope, &as_scope.base, .{ .block_ptr = &as_scope }, operand_node, src_node);
66966711 const parent_zir = &parent_gz.instructions;
66976712 if (as_scope.rvalue_rl_count == 1) {
66986713 // Busted! This expression didn't actually need a pointer.
......@@ -9607,27 +9622,3 @@ fn advanceSourceCursor(astgen: *AstGen, source: []const u8, end: usize) void {
96079622 astgen.source_line = line;
96089623 astgen.source_column = column;
96099624}
9610
9611fn checkVarInitExpr(
9612 astgen: *AstGen,
9613 gz: GenZir,
9614 var_node: ast.Node.Index,
9615 init_node: ast.Node.Index,
9616 init_inst: Zir.Inst.Ref,
9617 var_name_text: []const u8,
9618) !void {
9619 if (gz.refIsNoReturn(init_inst)) {
9620 return astgen.failNodeNotes(
9621 var_node,
9622 "useless {s}",
9623 .{var_name_text},
9624 &[_]u32{
9625 try astgen.errNoteNode(
9626 init_node,
9627 "control flow is diverted here",
9628 .{},
9629 ),
9630 },
9631 );
9632 }
9633}
test/compile_errors.zig+11-5
......@@ -4831,7 +4831,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
48314831 \\ const a = return;
48324832 \\}
48334833 , &[_][]const u8{
4834 "tmp.zig:2:5: error: useless local constant",
4834 "tmp.zig:2:5: error: unreachable code",
48354835 "tmp.zig:2:15: note: control flow is diverted here",
48364836 });
48374837
......@@ -5058,6 +5058,7 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
50585058 \\export fn entry() void { _ = f(); }
50595059 , &[_][]const u8{
50605060 "tmp.zig:2:12: error: unreachable code",
5061 "tmp.zig:2:21: note: control flow is diverted here",
50615062 });
50625063
50635064 cases.add("invalid builtin fn",
......@@ -7143,9 +7144,6 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
71437144 \\export fn entry8() void {
71447145 \\ var h = (Foo {}).bar;
71457146 \\}
7146 \\export fn entry9() void {
7147 \\ var z: noreturn = return;
7148 \\}
71497147 \\const Opaque = opaque {};
71507148 \\const Foo = struct {
71517149 \\ fn bar(self: *const Foo) void {}
......@@ -7161,7 +7159,15 @@ pub fn addCases(cases: *tests.CompileErrorContext) void {
71617159 "tmp.zig:17:4: error: variable of type 'Opaque' not allowed",
71627160 "tmp.zig:20:4: error: variable of type 'type' must be const or comptime",
71637161 "tmp.zig:23:4: error: variable of type '(bound fn(*const Foo) void)' must be const or comptime",
7164 "tmp.zig:26:22: error: unreachable code",
7162 });
7163
7164 cases.add("variable with type 'noreturn'",
7165 \\export fn entry9() void {
7166 \\ var z: noreturn = return;
7167 \\}
7168 , &[_][]const u8{
7169 "tmp.zig:2:5: error: unreachable code",
7170 "tmp.zig:2:23: note: control flow is diverted here",
71657171 });
71667172
71677173 cases.add("wrong types given to atomic order args in cmpxchg",