motiejus/zig

fork of https://codeberg.org/ziglang/zig
git clone https://git.jakstys.lt/motiejus/zig.git
Log | Tree | Refs | README | LICENSE

commit 727b371bbc53b1fcabb6c6899043da3a84195b3c (tree)
parent 0153f3a8f9b93ebef7b5cd70db8560fcac658ce7
Author: Andrew Kelley <andrew@ziglang.org>
Date:   Tue, 18 Jul 2023 17:50:41 -0700

Sema: fix source location crash for function prototypes

Diffstat:
Msrc/Module.zig | 18+++++++++++++++---
1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/src/Module.zig b/src/Module.zig @@ -2146,9 +2146,17 @@ pub const SrcLoc = struct { const tree = try src_loc.file_scope.getTree(gpa); const node = src_loc.declRelativeToNodeIndex(fn_proto_param.fn_proto_node_offset); var buf: [1]Ast.Node.Index = undefined; - const fn_proto_full = tree.fullFnProto(&buf, node).?; - const src_node = fn_proto_full.ast.params[fn_proto_param.param_index]; - return nodeToSpan(tree, src_node); + const full = tree.fullFnProto(&buf, node).?; + var it = full.iterate(tree); + var i: usize = 0; + while (it.next()) |param| : (i += 1) { + if (i == fn_proto_param.param_index) { + if (param.anytype_ellipsis3) |token| return tokenToSpan(tree, token); + if (param.name_token) |token| return tokenToSpan(tree, token); + return nodeToSpan(tree, param.type_expr); + } + } + unreachable; }, .node_offset_bin_lhs => |node_off| { const tree = try src_loc.file_scope.getTree(gpa); @@ -2502,6 +2510,10 @@ pub const SrcLoc = struct { ); } + fn tokenToSpan(tree: *const Ast, token: Ast.TokenIndex) Span { + return tokensToSpan(tree, token, token, token); + } + fn tokensToSpan(tree: *const Ast, start: Ast.TokenIndex, end: Ast.TokenIndex, main: Ast.TokenIndex) Span { const token_starts = tree.tokens.items(.start); var start_tok = start;