Skip to content

Commit

Permalink
more cimport fixes (#1580)
Browse files Browse the repository at this point in the history
* fix potential memory when zig exe or zig lib path config option change

* handle clobber when resolving cimports

* acquire lock for accessing store cimports when collecting diagnostics
  • Loading branch information
Techatrix authored Nov 4, 2023
1 parent b6561cb commit 7430bb6
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 2 deletions.
9 changes: 8 additions & 1 deletion src/DocumentStore.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1354,7 +1354,14 @@ pub fn resolveCImport(self: *DocumentStore, handle: Handle, node: Ast.Node.Index
{
self.lock.lock();
defer self.lock.unlock();
self.cimports.putNoClobber(self.allocator, hash, result) catch result.deinit(self.allocator);
const gop = self.cimports.getOrPutValue(self.allocator, hash, result) catch |err| {
result.deinit(self.allocator);
return err;
};
if (gop.found_existing) {
result.deinit(self.allocator);
result = gop.value_ptr.*;
}
}

switch (result) {
Expand Down
3 changes: 3 additions & 0 deletions src/Server.zig
Original file line number Diff line number Diff line change
Expand Up @@ -878,6 +878,9 @@ pub fn updateConfiguration(server: *Server, new_config: configuration.Configurat
}

if (new_zig_exe_path or new_zig_lib_path) {
for (server.document_store.cimports.values()) |*result| {
result.deinit(server.document_store.allocator);
}
server.document_store.cimports.clearAndFree(server.document_store.allocator);
}

Expand Down
7 changes: 6 additions & 1 deletion src/features/diagnostics.zig
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,12 @@ pub fn generateDiagnostics(server: *Server, arena: std.mem.Allocator, handle: *D
}

for (handle.cimports.items(.hash), handle.cimports.items(.node)) |hash, node| {
const error_bundle: std.zig.ErrorBundle = switch (server.document_store.cimports.get(hash) orelse continue) {
const result = blk: {
server.document_store.lock.lock();
defer server.document_store.lock.unlock();
break :blk server.document_store.cimports.get(hash) orelse continue;
};
const error_bundle: std.zig.ErrorBundle = switch (result) {
.success => continue,
.failure => |bundle| bundle,
};
Expand Down

0 comments on commit 7430bb6

Please sign in to comment.