From b756ed4da59cb0ec419a4010ab6f870c1924924f Mon Sep 17 00:00:00 2001 From: Alexandros Naskos Date: Fri, 4 Jun 2021 14:06:11 +0300 Subject: [PATCH] Fixed HashMap usage for latest master stdlib --- src/analysis.zig | 71 ++++++++++++++++++++++-------------------- src/document_store.zig | 67 +++++++++++++++++++-------------------- src/references.zig | 8 ++--- src/types.zig | 4 +-- 4 files changed, 77 insertions(+), 73 deletions(-) diff --git a/src/analysis.zig b/src/analysis.zig index e366015..4d5fda2 100644 --- a/src/analysis.zig +++ b/src/analysis.zig @@ -1980,7 +1980,7 @@ pub const DeclWithHandle = struct { if (isMetaType(self.handle.tree, param_decl.type_expr)) { var bound_param_it = bound_type_params.iterator(); while (bound_param_it.next()) |entry| { - if (std.meta.eql(entry.key, param_decl)) return entry.value; + if (std.meta.eql(entry.key_ptr.*, param_decl)) return entry.value_ptr.*; } return null; } else if (node_tags[param_decl.type_expr] == .identifier) { @@ -2033,7 +2033,7 @@ pub const DeclWithHandle = struct { if (node_tags[pay.items[0]] == .enum_literal) { const scope = findContainerScope(.{ .node = switch_expr_type.type.data.other, .handle = switch_expr_type.handle }) orelse return null; if (scope.decls.getEntry(tree.tokenSlice(main_tokens[pay.items[0]]))) |candidate| { - switch (candidate.value) { + switch (candidate.value_ptr.*) { .ast_node => |node| { if (containerField(switch_expr_type.handle.tree, node)) |container_field| { if (container_field.ast.type_expr != 0) { @@ -2098,7 +2098,7 @@ fn iterateSymbolsContainerInternal( var decl_it = container_scope.decls.iterator(); while (decl_it.next()) |entry| { - switch (entry.value) { + switch (entry.value_ptr.*) { .ast_node => |node| { if (node_tags[node].isContainerField()) { if (!instance_access and !is_enum) continue; @@ -2106,15 +2106,16 @@ fn iterateSymbolsContainerInternal( } else if (node_tags[node] == .global_var_decl or node_tags[node] == .local_var_decl or node_tags[node] == .simple_var_decl or - node_tags[node] == .aligned_var_decl) { - if (instance_access) continue; + node_tags[node] == .aligned_var_decl) + { + if (instance_access) continue; } }, .label_decl => continue, else => {}, } - const decl = DeclWithHandle{ .decl = &entry.value, .handle = handle }; + const decl = DeclWithHandle{ .decl = entry.value_ptr, .handle = handle }; if (handle != orig_handle and !decl.isPublic()) continue; try callback(context, decl); } @@ -2172,11 +2173,11 @@ pub fn iterateLabels( if (source_index >= scope.range.start and source_index < scope.range.end) { var decl_it = scope.decls.iterator(); while (decl_it.next()) |entry| { - switch (entry.value) { + switch (entry.value_ptr.*) { .label_decl => {}, else => continue, } - try callback(context, DeclWithHandle{ .decl = &entry.value, .handle = handle }); + try callback(context, DeclWithHandle{ .decl = entry.value_ptr, .handle = handle }); } } if (scope.range.start >= source_index) return; @@ -2196,9 +2197,10 @@ fn iterateSymbolsGlobalInternal( if (source_index >= scope.range.start and source_index <= scope.range.end) { var decl_it = scope.decls.iterator(); while (decl_it.next()) |entry| { - if (entry.value == .ast_node and handle.tree.nodes.items(.tag)[entry.value.ast_node].isContainerField()) continue; - if (entry.value == .label_decl) continue; - try callback(context, DeclWithHandle{ .decl = &entry.value, .handle = handle }); + if (entry.value_ptr.* == .ast_node and + handle.tree.nodes.items(.tag)[entry.value_ptr.*.ast_node].isContainerField()) continue; + if (entry.value_ptr.* == .label_decl) continue; + try callback(context, DeclWithHandle{ .decl = entry.value_ptr, .handle = handle }); } for (scope.uses) |use| { @@ -2323,12 +2325,12 @@ pub fn lookupLabel( for (handle.document_scope.scopes) |scope| { if (source_index >= scope.range.start and source_index < scope.range.end) { if (scope.decls.getEntry(symbol)) |candidate| { - switch (candidate.value) { + switch (candidate.value_ptr.*) { .label_decl => {}, else => continue, } return DeclWithHandle{ - .decl = &candidate.value, + .decl = candidate.value_ptr, .handle = handle, }; } @@ -2352,7 +2354,7 @@ pub fn lookupSymbolGlobal( const scope = &handle.document_scope.scopes[curr]; if (source_index >= scope.range.start and source_index <= scope.range.end) blk: { if (scope.decls.getEntry(symbol)) |candidate| { - switch (candidate.value) { + switch (candidate.value_ptr.*) { .ast_node => |node| { if (handle.tree.nodes.items(.tag)[node].isContainerField()) break :blk; }, @@ -2360,7 +2362,7 @@ pub fn lookupSymbolGlobal( else => {}, } return DeclWithHandle{ - .decl = &candidate.value, + .decl = candidate.value_ptr, .handle = handle, }; } @@ -2391,7 +2393,7 @@ pub fn lookupSymbolContainer( if (findContainerScope(container_handle)) |container_scope| { if (container_scope.decls.getEntry(symbol)) |candidate| { - switch (candidate.value) { + switch (candidate.value_ptr.*) { .ast_node => |node| { if (node_tags[node].isContainerField()) { if (!instance_access and !is_enum) return null; @@ -2401,7 +2403,7 @@ pub fn lookupSymbolContainer( .label_decl => unreachable, else => {}, } - return DeclWithHandle{ .decl = &candidate.value, .handle = handle }; + return DeclWithHandle{ .decl = candidate.value_ptr, .handle = handle }; } if (try resolveUse(store, arena, container_scope.uses, symbol, handle)) |result| return result; @@ -2411,23 +2413,24 @@ pub fn lookupSymbolContainer( return null; } -fn eqlCompletionItem(a: types.CompletionItem, b: types.CompletionItem) bool { - return std.mem.eql(u8, a.label, b.label); -} +const CompletionContext = struct { + pub fn hash(self: @This(), item: types.CompletionItem) u32 { + return @truncate(u32, std.hash.Wyhash.hash(0, item.label)); + } -fn hashCompletionItem(completion_item: types.CompletionItem) u32 { - return @truncate(u32, std.hash.Wyhash.hash(0, completion_item.label)); -} + pub fn eql(self: @This(), a: types.CompletionItem, b: types.CompletionItem) bool { + return std.mem.eql(u8, a.label, b.label); + } +}; pub const CompletionSet = std.ArrayHashMapUnmanaged( types.CompletionItem, void, - hashCompletionItem, - eqlCompletionItem, + CompletionContext, false, ); comptime { - std.debug.assert(@sizeOf(types.CompletionItem) == @sizeOf(CompletionSet.Entry)); + std.debug.assert(@sizeOf(types.CompletionItem) == @sizeOf(CompletionSet.Data)); } pub const DocumentScope = struct { @@ -2466,12 +2469,12 @@ pub const DocumentScope = struct { allocator.free(scope.tests); } allocator.free(self.scopes); - for (self.error_completions.entries.items) |entry| { - if (entry.key.documentation) |doc| allocator.free(doc.value); + for (self.error_completions.entries.items(.key)) |item| { + if (item.documentation) |doc| allocator.free(doc.value); } self.error_completions.deinit(allocator); - for (self.enum_completions.entries.items) |entry| { - if (entry.key.documentation) |doc| allocator.free(doc.value); + for (self.enum_completions.entries.items(.key)) |item| { + if (item.documentation) |doc| allocator.free(doc.value); } self.enum_completions.deinit(allocator); } @@ -2507,12 +2510,12 @@ pub fn makeDocumentScope(allocator: *std.mem.Allocator, tree: ast.Tree) !Documen errdefer { scopes.deinit(allocator); - for (error_completions.entries.items) |entry| { - if (entry.key.documentation) |doc| allocator.free(doc.value); + for (error_completions.entries.items(.key)) |completion| { + if (completion.documentation) |doc| allocator.free(doc.value); } error_completions.deinit(allocator); - for (enum_completions.entries.items) |entry| { - if (entry.key.documentation) |doc| allocator.free(doc.value); + for (enum_completions.entries.items(.key)) |completion| { + if (completion.documentation) |doc| allocator.free(doc.value); } enum_completions.deinit(allocator); } diff --git a/src/document_store.zig b/src/document_store.zig index de278d4..392270e 100644 --- a/src/document_store.zig +++ b/src/document_store.zig @@ -283,12 +283,12 @@ fn newDocument(self: *DocumentStore, uri: []const u8, text: []u8) anyerror!*Hand pub fn openDocument(self: *DocumentStore, uri: []const u8, text: []const u8) !*Handle { if (self.handles.getEntry(uri)) |entry| { log.debug("Document already open: {s}, incrementing count", .{uri}); - entry.value.count += 1; - if (entry.value.is_build_file) |build_file| { + entry.value_ptr.*.count += 1; + if (entry.value_ptr.*.is_build_file) |build_file| { build_file.refs += 1; } - log.debug("New count: {}", .{entry.value.count}); - return entry.value; + log.debug("New count: {}", .{entry.value_ptr.*.count}); + return entry.value_ptr.*; } const duped_text = try std.mem.dupe(self.allocator, u8, text); @@ -322,39 +322,40 @@ fn decrementBuildFileRefs(self: *DocumentStore, build_file: *BuildFile) void { fn decrementCount(self: *DocumentStore, uri: []const u8) void { if (self.handles.getEntry(uri)) |entry| { - if (entry.value.count == 0) return; - entry.value.count -= 1; + const handle = entry.value_ptr.*; + if (handle.count == 0) return; + handle.count -= 1; - if (entry.value.count > 0) + if (handle.count > 0) return; log.debug("Freeing document: {s}", .{uri}); - if (entry.value.associated_build_file) |build_file| { + if (handle.associated_build_file) |build_file| { self.decrementBuildFileRefs(build_file); } - if (entry.value.is_build_file) |build_file| { + if (handle.is_build_file) |build_file| { self.decrementBuildFileRefs(build_file); } - entry.value.tree.deinit(self.allocator); - self.allocator.free(entry.value.document.mem); + handle.tree.deinit(self.allocator); + self.allocator.free(handle.document.mem); - for (entry.value.imports_used.items) |import_uri| { + for (handle.imports_used.items) |import_uri| { self.decrementCount(import_uri); } - for (entry.value.import_uris) |import_uri| { + for (handle.import_uris) |import_uri| { self.allocator.free(import_uri); } - entry.value.document_scope.deinit(self.allocator); - entry.value.imports_used.deinit(self.allocator); - self.allocator.free(entry.value.import_uris); - self.allocator.destroy(entry.value); - const uri_key = entry.key; - self.handles.removeAssertDiscard(uri); + handle.document_scope.deinit(self.allocator); + handle.imports_used.deinit(self.allocator); + self.allocator.free(handle.import_uris); + self.allocator.destroy(handle); + const uri_key = entry.key_ptr.*; + std.debug.assert(self.handles.remove(uri)); self.allocator.free(uri_key); } } @@ -646,16 +647,16 @@ fn stdUriFromLibPath(allocator: *std.mem.Allocator, zig_lib_path: ?[]const u8) ! pub fn deinit(self: *DocumentStore) void { var entry_iterator = self.handles.iterator(); while (entry_iterator.next()) |entry| { - entry.value.document_scope.deinit(self.allocator); - entry.value.tree.deinit(self.allocator); - self.allocator.free(entry.value.document.mem); - for (entry.value.import_uris) |uri| { + entry.value_ptr.*.document_scope.deinit(self.allocator); + entry.value_ptr.*.tree.deinit(self.allocator); + self.allocator.free(entry.value_ptr.*.document.mem); + for (entry.value_ptr.*.import_uris) |uri| { self.allocator.free(uri); } - self.allocator.free(entry.value.import_uris); - entry.value.imports_used.deinit(self.allocator); - self.allocator.free(entry.key); - self.allocator.destroy(entry.value); + self.allocator.free(entry.value_ptr.*.import_uris); + entry.value_ptr.*.imports_used.deinit(self.allocator); + self.allocator.free(entry.key_ptr.*); + self.allocator.destroy(entry.value_ptr.*); } self.handles.deinit(); @@ -690,17 +691,17 @@ fn tagStoreCompletionItems( var result_set = analysis.CompletionSet{}; try result_set.ensureCapacity(&arena.allocator, max_len); - result_set.entries.appendSliceAssumeCapacity(@field(base.document_scope, name).entries.items); - try result_set.reIndex(&arena.allocator); + for (@field(base.document_scope, name).entries.items(.key)) |completion| { + result_set.putAssumeCapacityNoClobber(completion, {}); + } for (base.imports_used.items) |uri| { const curr_set = &@field(self.handles.get(uri).?.document_scope, name); - for (curr_set.entries.items) |entry| { - result_set.putAssumeCapacity(entry.key, {}); + for (curr_set.entries.items(.key)) |completion| { + result_set.putAssumeCapacity(completion, {}); } } - // This is safe to do because CompletionSet.Entry == struct { value: types.CompletionItem } - return std.mem.bytesAsSlice(types.CompletionItem, std.mem.sliceAsBytes(result_set.entries.items)); + return result_set.entries.items(.key); } pub fn errorCompletionItems( diff --git a/src/references.zig b/src/references.zig index c3df550..92285c0 100644 --- a/src/references.zig +++ b/src/references.zig @@ -536,13 +536,13 @@ pub fn symbolReferences( var handle_it = store.handles.iterator(); while (handle_it.next()) |entry| { - if (skip_std_references and std.mem.indexOf(u8, entry.key, "std") != null) { - if (!include_decl or entry.value != curr_handle) + if (skip_std_references and std.mem.indexOf(u8, entry.key_ptr.*, "std") != null) { + if (!include_decl or entry.value_ptr.* != curr_handle) continue; } // Check entry's transitive imports - try imports.append(entry.value); + try imports.append(entry.value_ptr.*); var i: usize = 0; blk: while (i < imports.items.len) : (i += 1) { const import = imports.items[i]; @@ -551,7 +551,7 @@ pub fn symbolReferences( if (h == curr_handle) { // entry does import curr_handle - try symbolReferencesInternal(arena, store, .{ .node = 0, .handle = entry.value }, decl_handle, encoding, context, handler); + try symbolReferencesInternal(arena, store, .{ .node = 0, .handle = entry.value_ptr.* }, decl_handle, encoding, context, handler); break :blk; } diff --git a/src/types.zig b/src/types.zig index c010ec4..bd82d61 100644 --- a/src/types.zig +++ b/src/types.zig @@ -136,9 +136,9 @@ pub const WorkspaceEdit = struct { if (idx != 0) try writer.writeAll(", "); try writer.writeByte('"'); - try writer.writeAll(entry.key); + try writer.writeAll(entry.key_ptr.*); try writer.writeAll("\":"); - try std.json.stringify(entry.value, options, writer); + try std.json.stringify(entry.value_ptr.*, options, writer); } try writer.writeByte('}'); }