Commit 2ff707be78

Bogdan Romanyuk <65823030+wrongnull@users.noreply.github.com>
2023-11-26 08:21:58
AstGen: check allowed non-function builtins with declarative field (#18120)
1 parent 2252dcc
Changed files (2)
lib
src
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,
             },
         },
     });
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 => {