Fixed crash when completing import with dot access

This commit is contained in:
Alexandros Naskos 2020-05-14 12:12:04 +03:00
parent 76e9a079a3
commit 7a8a4e1ec5
3 changed files with 58 additions and 52 deletions

View File

@ -178,23 +178,21 @@ pub fn getChild(tree: *ast.Tree, node: *ast.Node, name: []const u8) ?*ast.Node {
} }
/// Resolves the type of a node /// Resolves the type of a node
pub fn resolveTypeOfNode(tree: *ast.Tree, node: *ast.Node, import_ctx: *ImportContext) ?*ast.Node { pub fn resolveTypeOfNode(import_ctx: *ImportContext, node: *ast.Node) ?*ast.Node {
var latest_tree = import_ctx.lastTree() orelse tree;
switch (node.id) { switch (node.id) {
.VarDecl => { .VarDecl => {
const vari = node.cast(ast.Node.VarDecl).?; const vari = node.cast(ast.Node.VarDecl).?;
return resolveTypeOfNode(latest_tree, vari.type_node orelse vari.init_node.?, import_ctx) orelse null; return resolveTypeOfNode(import_ctx, vari.type_node orelse vari.init_node.?) orelse null;
}, },
.FnProto => { .FnProto => {
const func = node.cast(ast.Node.FnProto).?; const func = node.cast(ast.Node.FnProto).?;
switch (func.return_type) { switch (func.return_type) {
.Explicit, .InferErrorSet => |return_type| {return resolveTypeOfNode(latest_tree, return_type, import_ctx);} .Explicit, .InferErrorSet => |return_type| return resolveTypeOfNode(import_ctx, return_type),
} }
}, },
.Identifier => { .Identifier => {
if (getChild(latest_tree, &latest_tree.root_node.base, latest_tree.getNodeSource(node))) |child| { if (getChild(import_ctx.tree, &import_ctx.tree.root_node.base, import_ctx.tree.getNodeSource(node))) |child| {
return resolveTypeOfNode(latest_tree, child, import_ctx); return resolveTypeOfNode(import_ctx, child);
} else return null; } else return null;
}, },
.ContainerDecl => { .ContainerDecl => {
@ -202,13 +200,13 @@ pub fn resolveTypeOfNode(tree: *ast.Tree, node: *ast.Node, import_ctx: *ImportCo
}, },
.ContainerField => { .ContainerField => {
const field = node.cast(ast.Node.ContainerField).?; const field = node.cast(ast.Node.ContainerField).?;
return resolveTypeOfNode(latest_tree, field.type_expr.?, import_ctx); return resolveTypeOfNode(import_ctx, field.type_expr.?);
}, },
.SuffixOp => { .SuffixOp => {
const suffix_op = node.cast(ast.Node.SuffixOp).?; const suffix_op = node.cast(ast.Node.SuffixOp).?;
switch (suffix_op.op) { switch (suffix_op.op) {
.Call => { .Call => {
return resolveTypeOfNode(latest_tree, suffix_op.lhs.node, import_ctx); return resolveTypeOfNode(import_ctx, suffix_op.lhs.node);
}, },
else => {} else => {}
} }
@ -217,10 +215,14 @@ pub fn resolveTypeOfNode(tree: *ast.Tree, node: *ast.Node, import_ctx: *ImportCo
const infix_op = node.cast(ast.Node.InfixOp).?; const infix_op = node.cast(ast.Node.InfixOp).?;
switch (infix_op.op) { switch (infix_op.op) {
.Period => { .Period => {
var left = resolveTypeOfNode(latest_tree, infix_op.lhs, import_ctx) orelse return null; // Save the child string from this tree since the tree may switch when processing
if (nodeToString(latest_tree, infix_op.rhs)) |string| { // an import lhs.
return getChild(latest_tree, left, string); var rhs_str = nodeToString(import_ctx.tree, infix_op.rhs) orelse return null;
} else return null; // @TODO: This is hackish, pass an explicit allocator or smth
rhs_str = std.mem.dupe(import_ctx.store.allocator, u8, rhs_str) catch return null;
defer import_ctx.store.allocator.free(rhs_str);
const left = resolveTypeOfNode(import_ctx, infix_op.lhs) orelse return null;
return getChild(import_ctx.tree, left, rhs_str);
}, },
else => {} else => {}
} }
@ -229,22 +231,24 @@ pub fn resolveTypeOfNode(tree: *ast.Tree, node: *ast.Node, import_ctx: *ImportCo
const prefix_op = node.cast(ast.Node.PrefixOp).?; const prefix_op = node.cast(ast.Node.PrefixOp).?;
switch (prefix_op.op) { switch (prefix_op.op) {
.PtrType => { .PtrType => {
return resolveTypeOfNode(latest_tree, prefix_op.rhs, import_ctx); return resolveTypeOfNode(import_ctx, prefix_op.rhs);
}, },
else => {} else => {}
} }
}, },
.BuiltinCall => { .BuiltinCall => {
const builtin_call = node.cast(ast.Node.BuiltinCall).?; const builtin_call = node.cast(ast.Node.BuiltinCall).?;
if (!std.mem.eql(u8, latest_tree.tokenSlice(builtin_call.builtin_token), "@import")) return null; if (!std.mem.eql(u8, import_ctx.tree.tokenSlice(builtin_call.builtin_token), "@import")) return null;
if (builtin_call.params.len > 1) return null; if (builtin_call.params.len > 1) return null;
const import_param = builtin_call.params.at(0).*; const import_param = builtin_call.params.at(0).*;
if (import_param.id != .StringLiteral) return null; if (import_param.id != .StringLiteral) return null;
const import_str = latest_tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token); const import_str = import_ctx.tree.tokenSlice(import_param.cast(ast.Node.StringLiteral).?.token);
// @TODO: Handle this error better. return import_ctx.onImport(import_str[1 .. import_str.len - 1]) catch |err| block: {
return (import_ctx.onImport(import_str[1 .. import_str.len - 1]) catch unreachable); std.debug.warn("Error {} while proessing import {}\n", .{err, import_str});
break :block null;
};
}, },
else => { else => {
std.debug.warn("Type resolution case not implemented; {}\n", .{node.id}); std.debug.warn("Type resolution case not implemented; {}\n", .{node.id});
@ -253,11 +257,10 @@ pub fn resolveTypeOfNode(tree: *ast.Tree, node: *ast.Node, import_ctx: *ImportCo
return null; return null;
} }
pub fn getFieldAccessTypeNode(tree: *ast.Tree, tokenizer: *std.zig.Tokenizer, import_ctx: *ImportContext) ?*ast.Node { pub fn getFieldAccessTypeNode(import_ctx: *ImportContext, tokenizer: *std.zig.Tokenizer) ?*ast.Node {
var current_node = &tree.root_node.base; var current_node = &import_ctx.tree.root_node.base;
while (true) { while (true) {
var latest_tree = import_ctx.lastTree() orelse tree;
var next = tokenizer.next(); var next = tokenizer.next();
switch (next.id) { switch (next.id) {
.Eof => { .Eof => {
@ -266,8 +269,8 @@ pub fn getFieldAccessTypeNode(tree: *ast.Tree, tokenizer: *std.zig.Tokenizer, im
.Identifier => { .Identifier => {
// var root = current_node.cast(ast.Node.Root).?; // var root = current_node.cast(ast.Node.Root).?;
// current_node. // current_node.
if (getChild(latest_tree, current_node, tokenizer.buffer[next.start..next.end])) |child| { if (getChild(import_ctx.tree, current_node, tokenizer.buffer[next.start..next.end])) |child| {
if (resolveTypeOfNode(latest_tree, child, import_ctx)) |node_type| { if (resolveTypeOfNode(import_ctx, child)) |node_type| {
current_node = node_type; current_node = node_type;
} else return null; } else return null;
} else return null; } else return null;
@ -277,8 +280,8 @@ pub fn getFieldAccessTypeNode(tree: *ast.Tree, tokenizer: *std.zig.Tokenizer, im
if (after_period.id == .Eof) { if (after_period.id == .Eof) {
return current_node; return current_node;
} else if (after_period.id == .Identifier) { } else if (after_period.id == .Identifier) {
if (getChild(latest_tree, current_node, tokenizer.buffer[after_period.start..after_period.end])) |child| { if (getChild(import_ctx.tree, current_node, tokenizer.buffer[after_period.start..after_period.end])) |child| {
if (resolveTypeOfNode(latest_tree, child, import_ctx)) |child_type| { if (resolveTypeOfNode(import_ctx, child)) |child_type| {
current_node = child_type; current_node = child_type;
} else return null; } else return null;
} else return null; } else return null;

View File

@ -220,12 +220,13 @@ pub fn applyChanges(self: *DocumentStore, handle: *Handle, content_changes: std.
pub const ImportContext = struct { pub const ImportContext = struct {
store: *DocumentStore, store: *DocumentStore,
handle: *Handle, handle: *Handle,
trees: std.ArrayList(*std.zig.ast.Tree), tree: *std.zig.ast.Tree,
pub fn lastTree(self: *ImportContext) ?*std.zig.ast.Tree { // @TODO RemoveMe
if (self.trees.items.len == 0) return null; // pub fn lastTree(self: *ImportContext) ?*std.zig.ast.Tree {
return self.trees.items[self.trees.items.len - 1]; // if (self.trees.items.len == 0) return null;
} // return self.trees.items[self.trees.items.len - 1];
// }
pub fn onImport(self: *ImportContext, import_str: []const u8) !?*std.zig.ast.Node { pub fn onImport(self: *ImportContext, import_str: []const u8) !?*std.zig.ast.Node {
const allocator = self.store.allocator; const allocator = self.store.allocator;
@ -260,9 +261,11 @@ pub const ImportContext = struct {
// If we did, set our new handle and return the parsed tree root node. // If we did, set our new handle and return the parsed tree root node.
if (std.mem.eql(u8, uri, final_uri)) { if (std.mem.eql(u8, uri, final_uri)) {
self.handle = self.store.getHandle(final_uri) orelse return null; self.handle = self.store.getHandle(final_uri) orelse return null;
self.tree.deinit();
if (try self.handle.saneTree(allocator)) |tree| { if (try self.handle.saneTree(allocator)) |tree| {
try self.trees.append(tree); self.tree = tree;
return &tree.root_node.base; return &self.tree.root_node.base;
} }
return null; return null;
} }
@ -274,9 +277,11 @@ pub const ImportContext = struct {
// If it is, increment the count, set our new handle and return the parsed tree root node. // If it is, increment the count, set our new handle and return the parsed tree root node.
new_handle.count += 1; new_handle.count += 1;
self.handle = new_handle; self.handle = new_handle;
self.tree.deinit();
if (try self.handle.saneTree(allocator)) |tree| { if (try self.handle.saneTree(allocator)) |tree| {
try self.trees.append(tree); self.tree = tree;
return &tree.root_node.base; return &self.tree.root_node.base;
} }
return null; return null;
} }
@ -311,27 +316,28 @@ pub const ImportContext = struct {
self.handle = try newDocument(self.store, try std.mem.dupe(allocator, u8, final_uri), file_contents); self.handle = try newDocument(self.store, try std.mem.dupe(allocator, u8, final_uri), file_contents);
} }
// Free old tree, add new one if it exists.
// If we return null, no one should access the tree.
self.tree.deinit();
if (try self.handle.saneTree(allocator)) |tree| { if (try self.handle.saneTree(allocator)) |tree| {
try self.trees.append(tree); self.tree = tree;
return &tree.root_node.base; return &self.tree.root_node.base;
} }
return null; return null;
} }
pub fn deinit(self: *ImportContext) void { pub fn deinit(self: *ImportContext) void {
for (self.trees.items) |tree| { self.tree.deinit();
tree.deinit();
}
self.trees.deinit();
} }
}; };
pub fn importContext(self: *DocumentStore, handle: *Handle) ImportContext { pub fn importContext(self: *DocumentStore, handle: *Handle) !?ImportContext {
return .{ const tree = (try handle.saneTree(self.allocator)) orelse return null;
return ImportContext{
.store = self, .store = self,
.handle = handle, .handle = handle,
.trees = std.ArrayList(*std.zig.ast.Tree).init(self.allocator), .tree = tree,
}; };
} }

View File

@ -254,8 +254,8 @@ fn completeGlobal(id: i64, handle: DocumentStore.Handle, config: Config) !void {
} }
fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.Position, config: Config) !void { fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.Position, config: Config) !void {
const tree = (try handle.saneTree(allocator)) orelse { var import_ctx = (try document_store.importContext(handle)) orelse {
return try send(types.Response{ return send(types.Response{
.id = .{.Integer = id}, .id = .{.Integer = id},
.result = .{ .result = .{
.CompletionList = .{ .CompletionList = .{
@ -265,7 +265,7 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
}, },
}); });
}; };
defer tree.deinit(); defer import_ctx.deinit();
// We use a local arena allocator to deallocate all temporary data without iterating // We use a local arena allocator to deallocate all temporary data without iterating
var arena = std.heap.ArenaAllocator.init(allocator); var arena = std.heap.ArenaAllocator.init(allocator);
@ -276,13 +276,10 @@ fn completeFieldAccess(id: i64, handle: *DocumentStore.Handle, position: types.P
var line = try handle.document.getLine(@intCast(usize, position.line)); var line = try handle.document.getLine(@intCast(usize, position.line));
var tokenizer = std.zig.Tokenizer.init(line); var tokenizer = std.zig.Tokenizer.init(line);
// @TODO Pass import ctx. if (analysis.getFieldAccessTypeNode(&import_ctx, &tokenizer)) |node| {
var import_ctx = document_store.importContext(handle);
defer import_ctx.deinit();
if (analysis.getFieldAccessTypeNode(tree, &tokenizer, &import_ctx)) |node| {
var index: usize = 0; var index: usize = 0;
while (node.iterate(index)) |child_node| { while (node.iterate(index)) |child_node| {
if (try nodeToCompletion(&arena.allocator, import_ctx.lastTree() orelse tree, child_node, config)) |completion| { if (try nodeToCompletion(&arena.allocator, import_ctx.tree, child_node, config)) |completion| {
try completions.append(completion); try completions.append(completion);
} }
index += 1; index += 1;