Skip to content

Commit

Permalink
.zon: If tree has errors then we don't have a root decl..
Browse files Browse the repository at this point in the history
Any error in a .zon file prevents the creation of the root decl which leads to accessing an undefined value, ie results in a crash.

Also -- any tree.helperFns that call rootDecls aren't ready for .zon

Thank you to NIHKLAS on the Zig Programming Language Discord Server for bringing this to my attention 👋
  • Loading branch information
llogick committed Nov 14, 2024
1 parent e3e1e47 commit e84d1f4
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/DocumentScope.zig
Original file line number Diff line number Diff line change
Expand Up @@ -815,6 +815,8 @@ noinline fn walkContainerDecl(
const tracy_zone = tracy.trace(@src());
defer tracy_zone.end();

// This is not ready for .zon -- tree.fullContainerDecl on 0 calls tree.rootDecls
if (tree.mode == .zon) return; // and node_idx == 0 and tree.errors.len != 0) return;
const allocator = context.allocator;
const tags = tree.nodes.items(.tag);
const token_tags = tree.tokens.items(.tag);
Expand Down
1 change: 1 addition & 0 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1428,6 +1428,7 @@ fn semanticTokensFullHandler(server: *Server, arena: std.mem.Allocator, request:
if (server.config.semantic_tokens == .none) return null;

const handle = server.document_store.getHandle(request.textDocument.uri) orelse return null;
if (handle.tree.mode == .zon and handle.tree.errors.len != 0) return null;

var analyser = server.initAnalyser(handle);
defer analyser.deinit();
Expand Down
1 change: 1 addition & 0 deletions src/ast.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1698,6 +1698,7 @@ fn iterateChildrenTypeErased(
},

.root => {
if (tree.mode == .zon and tree.errors.len != 0) return;
for (rootDecls(tree)) |child| {
try callback(context, tree, child);
}
Expand Down
1 change: 1 addition & 0 deletions src/features/references.zig
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,7 @@ pub fn referencesHandler(server: *Server, arena: std.mem.Allocator, request: Gen
defer tracy_zone.end();

const handle = server.document_store.getHandle(request.uri()) orelse return null;
if (handle.tree.mode == .zon) return null;

if (request.position().character <= 0) return null;

Expand Down

0 comments on commit e84d1f4

Please sign in to comment.