mirror of
https://github.com/ziglang/zig.git
synced 2024-11-15 08:33:06 +00:00
Merge pull request #9352 from g-w1/fix-9346
stage2 astgen: error for return outside of function scope
This commit is contained in:
commit
7ef8546826
@ -4604,7 +4604,7 @@ fn tryExpr(
|
||||
const astgen = parent_gz.astgen;
|
||||
|
||||
const fn_block = astgen.fn_block orelse {
|
||||
return astgen.failNode(node, "invalid 'try' outside function scope", .{});
|
||||
return astgen.failNode(node, "'try' outside function scope", .{});
|
||||
};
|
||||
|
||||
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
|
||||
const node_datas = tree.nodes.items(.data);
|
||||
const node_tags = tree.nodes.items(.tag);
|
||||
|
||||
if (astgen.fn_block == null) {
|
||||
return astgen.failNode(node, "'return' outside function scope", .{});
|
||||
}
|
||||
|
||||
if (gz.in_defer) return astgen.failNode(node, "cannot return from defer expression", .{});
|
||||
|
||||
const defer_outer = &astgen.fn_block.?.base;
|
||||
|
@ -904,7 +904,7 @@ pub fn addCases(ctx: *TestContext) !void {
|
||||
\\ _ = S;
|
||||
\\}
|
||||
,
|
||||
&.{":4:13: error: invalid 'try' outside function scope"},
|
||||
&.{":4:13: error: 'try' outside function scope"},
|
||||
);
|
||||
}
|
||||
{
|
||||
|
@ -2,6 +2,10 @@ const std = @import("std");
|
||||
const TestContext = @import("../src/test.zig").TestContext;
|
||||
|
||||
pub fn addCases(ctx: *TestContext) !void {
|
||||
ctx.objErrStage1("issue #9346: return outside of function scope",
|
||||
\\pub const empty = return 1;
|
||||
, &.{"tmp.zig:1:19: error: 'return' outside function scope"});
|
||||
|
||||
ctx.exeErrStage1("std.fmt error for unused arguments",
|
||||
\\pub fn main() !void {
|
||||
\\ @import("std").debug.print("{d} {d} {d} {d} {d}", .{1,2,3,4,5,6,7,8,9,10,11,12,13,14,15});
|
||||
|
Loading…
Reference in New Issue
Block a user