authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-07-11 15:11:28-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-07-11 15:11:28-04:00
log7ef85468265ecbc53efa18f67a5bd5ef46b8c7fb
treee4d188bc4388a33ce6402cf561e003e002e4ee75
parentaa2a31612fa677ddb747f8fa730f6a5732b3afa9
parentb0b9c3c2dc4e55609db44dc0564795b7e2ed1b34
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9352 from g-w1/fix-9346

stage2 astgen: error for return outside of function scope

3 files changed, 10 insertions(+), 2 deletions(-)

src/AstGen.zig+5-1
...@@ -4604,7 +4604,7 @@ fn tryExpr(...@@ -4604,7 +4604,7 @@ fn tryExpr(
4604 const astgen = parent_gz.astgen;4604 const astgen = parent_gz.astgen;
46054605
4606 const fn_block = astgen.fn_block orelse {4606 const fn_block = astgen.fn_block orelse {
4607 return astgen.failNode(node, "invalid 'try' outside function scope", .{});4607 return astgen.failNode(node, "'try' outside function scope", .{});
4608 };4608 };
46094609
4610 if (parent_gz.in_defer) return astgen.failNode(node, "'try' not allowed inside defer expression", .{});4610 if (parent_gz.in_defer) return astgen.failNode(node, "'try' not allowed inside defer expression", .{});
...@@ -6167,6 +6167,10 @@ fn ret(gz: *GenZir, scope: *Scope, node: ast.Node.Index) InnerError!Zir.Inst.Ref...@@ -6167,6 +6167,10 @@ fn ret(gz: *GenZir, scope: *Scope, node: ast.Node.Index) InnerError!Zir.Inst.Ref
6167 const node_datas = tree.nodes.items(.data);6167 const node_datas = tree.nodes.items(.data);
6168 const node_tags = tree.nodes.items(.tag);6168 const node_tags = tree.nodes.items(.tag);
61696169
6170 if (astgen.fn_block == null) {
6171 return astgen.failNode(node, "'return' outside function scope", .{});
6172 }
6173
6170 if (gz.in_defer) return astgen.failNode(node, "cannot return from defer expression", .{});6174 if (gz.in_defer) return astgen.failNode(node, "cannot return from defer expression", .{});
61716175
6172 const defer_outer = &astgen.fn_block.?.base;6176 const defer_outer = &astgen.fn_block.?.base;
test/cases.zig+1-1
...@@ -904,7 +904,7 @@ pub fn addCases(ctx: *TestContext) !void {...@@ -904,7 +904,7 @@ pub fn addCases(ctx: *TestContext) !void {
904 \\ _ = S;904 \\ _ = S;
905 \\}905 \\}
906 ,906 ,
907 &.{":4:13: error: invalid 'try' outside function scope"},907 &.{":4:13: error: 'try' outside function scope"},
908 );908 );
909 }909 }
910 {910 {
test/compile_errors.zig+4
...@@ -2,6 +2,10 @@ const std = @import("std");...@@ -2,6 +2,10 @@ const std = @import("std");
2const TestContext = @import("../src/test.zig").TestContext;2const TestContext = @import("../src/test.zig").TestContext;
33
4pub fn addCases(ctx: *TestContext) !void {4pub fn addCases(ctx: *TestContext) !void {
5 ctx.objErrStage1("issue #9346: return outside of function scope",
6 \\pub const empty = return 1;
7 , &.{"tmp.zig:1:19: error: 'return' outside function scope"});
8
5 ctx.exeErrStage1("std.fmt error for unused arguments",9 ctx.exeErrStage1("std.fmt error for unused arguments",
6 \\pub fn main() !void {10 \\pub fn main() !void {
7 \\ @import("std").debug.print("{d} {d} {d} {d} {d}", .{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15});11 \\ @import("std").debug.print("{d} {d} {d} {d} {d}", .{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15});