Skip to content

Commit

Permalink
make offsets.zig identifierIndexToLoc and `identifierIndexToNameLoc…
Browse files Browse the repository at this point in the history
…` return an error
  • Loading branch information
llogick committed Dec 9, 2023
1 parent 44b55a8 commit 6e6d754
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 67 deletions.
39 changes: 17 additions & 22 deletions src/DocumentScope.zig
Original file line number Diff line number Diff line change
Expand Up @@ -665,11 +665,7 @@ noinline fn walkContainerDecl(
continue;
}

if (token_tags[main_tokens[decl]] != .identifier) {
// TODO this code path should not be reachable
continue;
}
const name = offsets.identifierTokenToNameSlice(tree, main_tokens[decl]);
const name = offsets.identifierTokenToNameSlice(tree, main_tokens[decl]) catch continue; // "TODO this code path should not be reachable"
try scope.pushDeclaration(name, .{ .ast_node = decl }, .field);

if (is_enum_or_tagged_union) {
Expand Down Expand Up @@ -698,7 +694,7 @@ noinline fn walkContainerDecl(
=> {
var buffer: [1]Ast.Node.Index = undefined;
const name_token = tree.fullFnProto(&buffer, decl).?.name_token orelse continue;
const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch continue;
try scope.pushDeclaration(name, .{ .ast_node = decl }, .other);
},
.local_var_decl,
Expand All @@ -709,7 +705,7 @@ noinline fn walkContainerDecl(
const name_token = tree.fullVarDecl(decl).?.ast.mut_token + 1;
if (name_token >= tree.tokens.len) continue;

const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch continue;
try scope.pushDeclaration(name, .{ .ast_node = decl }, .other);
},

Expand Down Expand Up @@ -752,7 +748,7 @@ noinline fn walkErrorSetNode(
switch (token_tags[tok_i]) {
.doc_comment, .comma => {},
.identifier => {
const name = offsets.identifierTokenToNameSlice(tree, tok_i);
const name = offsets.identifierTokenToNameSlice(tree, tok_i) catch unreachable;
try scope.pushDeclaration(name, .{ .error_token = tok_i }, .other);
const gop = try context.doc_scope.error_completions.getOrPut(context.allocator, .{
.label = name,
Expand Down Expand Up @@ -795,7 +791,7 @@ noinline fn walkFuncNode(
while (ast.nextFnParam(&it)) |param| : (param_index += 1) {
if (param.name_token) |name_token| {
try scope.pushDeclaration(
offsets.identifierTokenToNameSlice(tree, name_token),
offsets.identifierTokenToNameSlice(tree, name_token) catch break,
.{ .param_payload = .{ .param_index = param_index, .func = node_idx } },
.other,
);
Expand Down Expand Up @@ -844,7 +840,7 @@ fn walkBlockNodeKeepOpen(
// if labeled block
if (token_tags[first_token] == .identifier) {
try scope.pushDeclaration(
offsets.identifierTokenToNameSlice(tree, first_token),
offsets.identifierTokenToNameSlice(tree, first_token) catch unreachable,
.{ .label_decl = .{ .label = first_token, .block = node_idx } },
.other,
);
Expand All @@ -862,7 +858,7 @@ fn walkBlockNodeKeepOpen(
.simple_var_decl,
=> {
const var_decl = tree.fullVarDecl(idx).?;
const name = offsets.identifierTokenToNameSlice(tree, var_decl.ast.mut_token + 1);
const name = offsets.identifierTokenToNameSlice(tree, var_decl.ast.mut_token + 1) catch continue;
try scope.pushDeclaration(name, .{ .ast_node = idx }, .other);
},
.assign_destructure => {
Expand All @@ -871,7 +867,7 @@ fn walkBlockNodeKeepOpen(

for (lhs_exprs, 0..) |lhs_node, i| {
const var_decl = tree.fullVarDecl(lhs_node) orelse continue;
const name = offsets.identifierTokenToNameSlice(tree, var_decl.ast.mut_token + 1);
const name = offsets.identifierTokenToNameSlice(tree, var_decl.ast.mut_token + 1) catch continue;
try scope.pushDeclaration(
name,
.{ .assign_destructure = .{ .node = idx, .index = @intCast(i) } },
Expand All @@ -897,7 +893,7 @@ noinline fn walkIfNode(

if (if_node.payload_token) |payload_token| {
const name_token = payload_token + @intFromBool(token_tags[payload_token] == .asterisk);
const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch unreachable;

const decl: Declaration = if (if_node.error_token != null)
.{ .error_union_payload = .{ .name = name_token, .condition = if_node.ast.cond_expr } }
Expand All @@ -913,7 +909,7 @@ noinline fn walkIfNode(

if (if_node.ast.else_expr != 0) {
if (if_node.error_token) |error_token| {
const name = offsets.identifierTokenToNameSlice(tree, error_token);
const name = offsets.identifierTokenToNameSlice(tree, error_token) catch unreachable;

const else_scope = try walkNodeEnsureScope(context, tree, if_node.ast.else_expr, error_token);
try else_scope.pushDeclaration(
Expand Down Expand Up @@ -944,7 +940,7 @@ noinline fn walkCatchNode(
token_tags[catch_token - 1] == .pipe and
token_tags[catch_token] == .identifier)
{
const name = offsets.identifierTokenToNameSlice(tree, catch_token);
const name = offsets.identifierTokenToNameSlice(tree, catch_token) catch unreachable;

const expr_scope = try walkNodeEnsureScope(context, tree, data[node_idx].rhs, catch_token);
try expr_scope.pushDeclaration(
Expand Down Expand Up @@ -978,7 +974,7 @@ noinline fn walkWhileNode(

const payload_declaration, const payload_name = if (while_node.payload_token) |payload_token| blk: {
const name_token = payload_token + @intFromBool(token_tags[payload_token] == .asterisk);
const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch unreachable;

const decl: Declaration = if (while_node.error_token != null)
.{ .error_union_payload = .{ .name = name_token, .condition = while_node.ast.cond_expr } }
Expand Down Expand Up @@ -1031,7 +1027,7 @@ noinline fn walkWhileNode(
}

if (while_node.error_token) |error_token| {
const name = offsets.identifierTokenToNameSlice(tree, error_token);
const name = offsets.identifierTokenToNameSlice(tree, error_token) catch unreachable;

try else_scope.pushDeclaration(
name,
Expand Down Expand Up @@ -1070,16 +1066,15 @@ noinline fn walkForNode(
const name_token = capture_token + @intFromBool(capture_is_ref);
capture_token = name_token + 2;

if (tree.tokens.items(.tag)[name_token] != .identifier) break;
try then_scope.pushDeclaration(
offsets.identifierTokenToNameSlice(tree, name_token),
offsets.identifierTokenToNameSlice(tree, name_token) catch break,
.{ .array_payload = .{ .identifier = name_token, .array_expr = input } },
.other,
);
}

const label_name = if (for_node.label_token) |label_token|
offsets.identifierTokenToNameSlice(context.tree, label_token)
offsets.identifierTokenToNameSlice(context.tree, label_token) catch null
else
null;

Expand Down Expand Up @@ -1124,7 +1119,7 @@ noinline fn walkSwitchNode(

if (switch_case.payload_token) |payload_token| {
const name_token = payload_token + @intFromBool(token_tags[payload_token] == .asterisk);
const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch unreachable;

const expr_scope = try walkNodeEnsureScope(context, tree, switch_case.ast.target_expr, name_token);
try expr_scope.pushDeclaration(
Expand All @@ -1148,7 +1143,7 @@ noinline fn walkErrdeferNode(
const payload_token = data[node_idx].lhs;

if (payload_token != 0) {
const name = offsets.identifierTokenToNameSlice(tree, payload_token);
const name = offsets.identifierTokenToNameSlice(tree, payload_token) catch unreachable;

const expr_scope = try walkNodeEnsureScope(context, tree, data[node_idx].rhs, payload_token);
try expr_scope.pushDeclaration(
Expand Down
45 changes: 23 additions & 22 deletions src/analysis.zig
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ pub fn declNameTokenToSlice(tree: Ast, name_token: Ast.TokenIndex) ?[]const u8 {
const name = offsets.tokenToSlice(tree, name_token);
return name[1 .. name.len - 1];
},
.identifier => return offsets.identifierTokenToNameSlice(tree, name_token),
.identifier => return offsets.identifierTokenToNameSlice(tree, name_token) catch return null,
else => return null,
}
}
Expand Down Expand Up @@ -469,8 +469,7 @@ fn resolveVarDeclAliasInternal(analyser: *Analyser, node_handle: NodeWithHandle,
const resolved = switch (node_tags[node_handle.node]) {
.identifier => blk: {
const name_token = main_tokens[node_handle.node];
if (token_tags[name_token] != .identifier) return null;
const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch return null;
break :blk try analyser.lookupSymbolGlobal(
handle,
name,
Expand All @@ -488,7 +487,7 @@ fn resolveVarDeclAliasInternal(analyser: *Analyser, node_handle: NodeWithHandle,
else => return null,
};

const symbol_name = offsets.identifierTokenToNameSlice(tree, datas[node_handle.node].rhs);
const symbol_name = offsets.identifierTokenToNameSlice(tree, datas[node_handle.node].rhs) catch return null;

break :blk try analyser.lookupSymbolContainer(
.{ .node = resolved_node, .handle = resolved.handle },
Expand Down Expand Up @@ -1071,7 +1070,6 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
const main_tokens = tree.nodes.items(.main_token);
const node_tags = tree.nodes.items(.tag);
const datas = tree.nodes.items(.data);
const token_tags = tree.tokens.items(.tag);
const starts = tree.tokens.items(.start);

switch (node_tags[node]) {
Expand Down Expand Up @@ -1102,10 +1100,9 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
},
.identifier => {
const name_token = main_tokens[node];
if (tree.tokens.items(.tag)[name_token] != .identifier) return null;
const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch return null;

const is_escaped_identifier = tree.source[tree.tokens.items(.start)[name_token]] == '@';
const is_escaped_identifier = tree.source[starts[name_token]] == '@';
if (!is_escaped_identifier) {
if (std.mem.eql(u8, name, "_")) return null;
if (resolvePrimitiveType(name)) |primitive| {
Expand Down Expand Up @@ -1541,10 +1538,17 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
// HACK: resolve std.ArrayList(T).Slice
if (std.mem.endsWith(u8, node_handle.handle.uri, "array_list.zig") and
if_node.payload_token != null and
std.mem.eql(u8, offsets.identifierTokenToNameSlice(tree, if_node.payload_token.?), "a") and
std.mem.eql(
u8,
offsets.identifierTokenToNameSlice(tree, if_node.payload_token.?) catch return null,
"a",
) and
node_tags[if_node.ast.cond_expr] == .identifier and
std.mem.eql(u8, offsets.identifierTokenToNameSlice(tree, main_tokens[if_node.ast.cond_expr]), "alignment"))
blk: {
std.mem.eql(
u8,
offsets.identifierTokenToNameSlice(tree, main_tokens[if_node.ast.cond_expr]) catch return null,
"alignment",
)) blk: {
return (try analyser.resolveTypeOfNodeInternal(.{ .handle = handle, .node = if_node.ast.then_expr })) orelse break :blk;
}

Expand Down Expand Up @@ -1632,9 +1636,7 @@ fn resolveTypeOfNodeUncached(analyser: *Analyser, node_handle: NodeWithHandle) e
.block_two_semicolon,
=> {
const first_token = tree.firstToken(node);
if (token_tags[first_token] != .identifier) return null;

const block_label = offsets.identifierTokenToNameSlice(tree, first_token);
const block_label = offsets.identifierTokenToNameSlice(tree, first_token) catch return null;

// TODO: peer type resolution based on all `break` statements
var context = FindBreaks{
Expand Down Expand Up @@ -2304,7 +2306,7 @@ pub fn getFieldAccessType(
.eof => return current_type,
.identifier => {
const ct_handle = if (current_type) |c| c.handle else handle;
const symbol_name = offsets.identifierIndexToNameSlice(tokenizer.buffer, tok.loc.start);
const symbol_name = offsets.identifierIndexToNameSlice(tokenizer.buffer, tok.loc.start) catch return null;
if (try analyser.lookupSymbolGlobal(
ct_handle,
symbol_name,
Expand Down Expand Up @@ -2335,7 +2337,7 @@ pub fn getFieldAccessType(
else
return null;

const symbol = offsets.identifierIndexToNameSlice(tokenizer.buffer, after_period.loc.start);
const symbol = offsets.identifierIndexToNameSlice(tokenizer.buffer, after_period.loc.start) catch return null;
const current_type_nodes = try deref_type.getAllTypesWithHandles(analyser.arena.allocator());

// TODO: Return all options instead of first valid one
Expand Down Expand Up @@ -2473,7 +2475,7 @@ pub fn nodeToString(tree: Ast, node: Ast.Node.Index) ?[]const u8 {
return if (field.tuple_like) null else tree.tokenSlice(field.main_token);
},
.error_value => tree.tokenSlice(data[node].rhs),
.identifier => offsets.identifierTokenToNameSlice(tree, main_token),
.identifier => offsets.identifierTokenToNameSlice(tree, main_token) catch null,

Check warning on line 2478 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L2478

Added line #L2478 was not covered by tests
.fn_proto,
.fn_proto_multi,
.fn_proto_one,
Expand Down Expand Up @@ -3104,10 +3106,9 @@ pub const DeclWithHandle = struct {
}
return try analyser.resolveTypeOfNodeInternal(.{ .node = param.type_expr, .handle = self.handle });
} else if (node_tags[param.type_expr] == .identifier) {
if (tree.tokens.items(.tag)[main_tokens[param.type_expr]] != .identifier) return null;
const param_type_name = offsets.identifierTokenToNameSlice(tree, main_tokens[param.type_expr]);
const param_type_name = offsets.identifierTokenToNameSlice(tree, main_tokens[param.type_expr]) catch return null;
if (param.name_token) |name_tok| {
const name = offsets.identifierTokenToNameSlice(tree, name_tok);
const name = offsets.identifierTokenToNameSlice(tree, name_tok) catch return null;
if (std.mem.eql(u8, param_type_name, name))
return null;
}
Expand Down Expand Up @@ -4313,13 +4314,13 @@ fn addReferencedTypes(
.enum_literal => return "@TypeOf(.enum_literal)",

.error_value => {
const identifier = offsets.identifierTokenToNameSlice(tree, datas[p].rhs);
const identifier = offsets.identifierTokenToNameSlice(tree, datas[p].rhs) catch return null;

Check warning on line 4317 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L4317

Added line #L4317 was not covered by tests
return try std.fmt.allocPrint(allocator, "error{{{}}}", .{std.zig.fmtId(identifier)});
},

.identifier => {
const name_token = main_tokens[p];
const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch return null;

Check warning on line 4323 in src/analysis.zig

View check run for this annotation

Codecov / codecov/patch

src/analysis.zig#L4323

Added line #L4323 was not covered by tests
const is_escaped_identifier = tree.source[tree.tokens.items(.start)[name_token]] == '@';
if (is_escaped_identifier) return null;
const primitive = Analyser.resolvePrimitiveType(name) orelse return null;
Expand Down
4 changes: 2 additions & 2 deletions src/features/inlay_hints.zig
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ fn writeCallNodeHint(builder: *Builder, call: Ast.full.Call) !void {
switch (node_tags[call.ast.fn_expr]) {
.identifier => {
const name_token = main_tokens[call.ast.fn_expr];
const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch return;
const source_index = offsets.tokenToIndex(tree, name_token);

if (try builder.analyser.lookupSymbolGlobal(handle, name, source_index)) |decl_handle| {
Expand All @@ -359,7 +359,7 @@ fn writeCallNodeHint(builder: *Builder, call: Ast.full.Call) !void {
.end = rhs_loc.end,
})) |type_handle| {
const container_handle = try builder.analyser.resolveDerefType(type_handle) orelse type_handle;
const symbol = offsets.identifierTokenToNameSlice(tree, rhsToken);
const symbol = offsets.identifierTokenToNameSlice(tree, rhsToken) catch return;
if (try container_handle.lookupSymbol(builder.analyser, symbol)) |decl_handle| {
try writeCallHint(builder, call, decl_handle);
}
Expand Down
2 changes: 1 addition & 1 deletion src/features/semantic_tokens.zig
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ fn writeIdentifier(builder: *Builder, name_token: Ast.Node.Index) error{OutOfMem
const handle = builder.handle;
const tree = handle.tree;

const name = offsets.identifierTokenToNameSlice(tree, name_token);
const name = offsets.identifierTokenToNameSlice(tree, name_token) catch return;
const is_escaped_identifier = tree.source[tree.tokens.items(.start)[name_token]] == '@';

if (!is_escaped_identifier) {
Expand Down
Loading

0 comments on commit 6e6d754

Please sign in to comment.