From 57470e833e4ac545a75ad7b3dd6830fa666cd325 Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Wed, 9 Aug 2023 10:07:55 -0400 Subject: [PATCH] Module: implement `span` for `.call_arg` of a `@call` Closes #16750 --- src/Module.zig | 56 +++++++++++++------ .../builtin_call_with_invalid_param.zig | 12 ++++ 2 files changed, 52 insertions(+), 16 deletions(-) create mode 100644 test/cases/compile_errors/builtin_call_with_invalid_param.zig diff --git a/src/Module.zig b/src/Module.zig index f5438ebb9f..67d0494a56 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -2133,10 +2133,40 @@ pub const SrcLoc = struct { .call_arg => |call_arg| { const tree = try src_loc.file_scope.getTree(gpa); const node = src_loc.declRelativeToNodeIndex(call_arg.call_node_offset); - var buf: [1]Ast.Node.Index = undefined; - const call_full = tree.fullCall(&buf, node).?; - const src_node = call_full.ast.params[call_arg.arg_index]; - return nodeToSpan(tree, src_node); + var buf: [2]Ast.Node.Index = undefined; + const call_full = tree.fullCall(buf[0..1], node) orelse { + const node_tags = tree.nodes.items(.tag); + assert(node_tags[node] == .builtin_call); + const call_args_node = tree.extra_data[tree.nodes.items(.data)[node].rhs - 1]; + switch (node_tags[call_args_node]) { + .array_init_one, + .array_init_one_comma, + .array_init_dot_two, + .array_init_dot_two_comma, + .array_init_dot, + .array_init_dot_comma, + .array_init, + .array_init_comma, + => { + const full = tree.fullArrayInit(&buf, call_args_node).?.ast.elements; + return nodeToSpan(tree, full[call_arg.arg_index]); + }, + .struct_init_one, + .struct_init_one_comma, + .struct_init_dot_two, + .struct_init_dot_two_comma, + .struct_init_dot, + .struct_init_dot_comma, + .struct_init, + .struct_init_comma, + => { + const full = tree.fullStructInit(&buf, call_args_node).?.ast.fields; + return nodeToSpan(tree, full[call_arg.arg_index]); + }, + else => return nodeToSpan(tree, call_args_node), + } + }; + return nodeToSpan(tree, call_full.ast.params[call_arg.arg_index]); }, .fn_proto_param => |fn_proto_param| { const tree = try src_loc.file_scope.getTree(gpa); @@ -5926,21 +5956,15 @@ pub fn argSrc( }); return LazySrcLoc.nodeOffset(0); }; - const node_tags = tree.nodes.items(.tag); const node = decl.relativeToNodeIndex(call_node_offset); var args: [1]Ast.Node.Index = undefined; - const full = switch (node_tags[node]) { - .call_one, .call_one_comma, .async_call_one, .async_call_one_comma => tree.callOne(&args, node), - .call, .call_comma, .async_call, .async_call_comma => tree.callFull(node), - .builtin_call => { - const node_datas = tree.nodes.items(.data); - const call_args_node = tree.extra_data[node_datas[node].rhs - 1]; - const call_args_offset = decl.nodeIndexToRelative(call_args_node); - return mod.initSrc(call_args_offset, decl, arg_i); - }, - else => unreachable, + const call_full = tree.fullCall(&args, node) orelse { + assert(tree.nodes.items(.tag)[node] == .builtin_call); + const call_args_node = tree.extra_data[tree.nodes.items(.data)[node].rhs - 1]; + const call_args_offset = decl.nodeIndexToRelative(call_args_node); + return mod.initSrc(call_args_offset, decl, arg_i); }; - return LazySrcLoc.nodeOffset(decl.nodeIndexToRelative(full.ast.params[arg_i])); + return LazySrcLoc.nodeOffset(decl.nodeIndexToRelative(call_full.ast.params[arg_i])); } pub fn initSrc( diff --git a/test/cases/compile_errors/builtin_call_with_invalid_param.zig b/test/cases/compile_errors/builtin_call_with_invalid_param.zig new file mode 100644 index 0000000000..b0ce2c13b2 --- /dev/null +++ b/test/cases/compile_errors/builtin_call_with_invalid_param.zig @@ -0,0 +1,12 @@ +export fn builtinCallBoolFunctionInlineWithVoid() void { + @call(.always_inline, boolFunction, .{{}}); +} + +fn boolFunction(_: bool) void {} + +// error +// backend=stage2 +// target=native +// +// :2:43: error: expected type 'bool', found 'void' +// :5:20: note: parameter type declared here