From 2ff707be789046a733d0d23c6fe42dd2b7210396 Mon Sep 17 00:00:00 2001 From: Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com> Date: Sun, 26 Nov 2023 10:21:58 +0300 Subject: [PATCH] AstGen: check allowed non-function builtins with declarative field (#18120) --- lib/std/zig/BuiltinFn.zig | 15 +++++++++++++++ src/AstGen.zig | 21 ++------------------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/lib/std/zig/BuiltinFn.zig b/lib/std/zig/BuiltinFn.zig index 3296114ef9..decb3cf7fd 100644 --- a/lib/std/zig/BuiltinFn.zig +++ b/lib/std/zig/BuiltinFn.zig @@ -152,6 +152,8 @@ needs_mem_loc: MemLocRequirement = .never, eval_to_error: EvalToError = .never, /// `true` if the builtin call can be the left-hand side of an expression (assigned to). allows_lvalue: bool = false, +/// `true` if builtin call is not available outside function scope +illegal_outside_function: bool = false, /// The number of parameters to this builtin function. `null` means variable number /// of parameters. param_count: ?u8, @@ -258,6 +260,7 @@ pub const list = list: { .{ .tag = .breakpoint, .param_count = 0, + .illegal_outside_function = true, }, }, .{ @@ -378,24 +381,28 @@ pub const list = list: { "@cVaArg", .{ .tag = .c_va_arg, .param_count = 2, + .illegal_outside_function = true, }, }, .{ "@cVaCopy", .{ .tag = .c_va_copy, .param_count = 1, + .illegal_outside_function = true, }, }, .{ "@cVaEnd", .{ .tag = .c_va_end, .param_count = 1, + .illegal_outside_function = true, }, }, .{ "@cVaStart", .{ .tag = .c_va_start, .param_count = 0, + .illegal_outside_function = true, }, }, .{ @@ -533,6 +540,7 @@ pub const list = list: { .{ .tag = .frame_address, .param_count = 0, + .illegal_outside_function = true, }, }, .{ @@ -709,6 +717,7 @@ pub const list = list: { .{ .tag = .return_address, .param_count = 0, + .illegal_outside_function = true, }, }, .{ @@ -723,6 +732,7 @@ pub const list = list: { .{ .tag = .set_align_stack, .param_count = 1, + .illegal_outside_function = true, }, }, .{ @@ -730,6 +740,7 @@ pub const list = list: { .{ .tag = .set_cold, .param_count = 1, + .illegal_outside_function = true, }, }, .{ @@ -808,6 +819,7 @@ pub const list = list: { .tag = .src, .needs_mem_loc = .always, .param_count = 0, + .illegal_outside_function = true, }, }, .{ @@ -997,6 +1009,7 @@ pub const list = list: { "@workItemId", .{ .tag = .work_item_id, .param_count = 1, + .illegal_outside_function = true, }, }, .{ @@ -1004,6 +1017,7 @@ pub const list = list: { .{ .tag = .work_group_size, .param_count = 1, + .illegal_outside_function = true, }, }, .{ @@ -1011,6 +1025,7 @@ pub const list = list: { .{ .tag = .work_group_id, .param_count = 1, + .illegal_outside_function = true, }, }, }); diff --git a/src/AstGen.zig b/src/AstGen.zig index 8a6a6a7292..060e2af62b 100644 --- a/src/AstGen.zig +++ b/src/AstGen.zig @@ -8268,25 +8268,8 @@ fn builtinCall( // Check function scope-only builtins - if (astgen.fn_block == null) { - switch (info.tag) { - .c_va_arg, - .c_va_copy, - .c_va_end, - .c_va_start, - .work_item_id, - .work_group_size, - .work_group_id, - .set_align_stack, - .set_cold, - .return_address, - .frame_address, - .breakpoint, - .src, - => return astgen.failNode(node, "'{s}' outside function scope", .{builtin_name}), - else => {}, - } - } + if (astgen.fn_block == null and info.illegal_outside_function) + return astgen.failNode(node, "'{s}' outside function scope", .{builtin_name}); switch (info.tag) { .import => {