Fixed HashMap usage for latest master stdlib

This commit is contained in:
Alexandros Naskos 2021-06-04 14:06:11 +03:00
parent affbd2de6b
commit b756ed4da5
No known key found for this signature in database
GPG Key ID: 02BF2E72B0EA32D2
4 changed files with 77 additions and 73 deletions

View File

@ -1980,7 +1980,7 @@ pub const DeclWithHandle = struct {
if (isMetaType(self.handle.tree, param_decl.type_expr)) { if (isMetaType(self.handle.tree, param_decl.type_expr)) {
var bound_param_it = bound_type_params.iterator(); var bound_param_it = bound_type_params.iterator();
while (bound_param_it.next()) |entry| { 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; return null;
} else if (node_tags[param_decl.type_expr] == .identifier) { } 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) { 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; 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| { if (scope.decls.getEntry(tree.tokenSlice(main_tokens[pay.items[0]]))) |candidate| {
switch (candidate.value) { switch (candidate.value_ptr.*) {
.ast_node => |node| { .ast_node => |node| {
if (containerField(switch_expr_type.handle.tree, node)) |container_field| { if (containerField(switch_expr_type.handle.tree, node)) |container_field| {
if (container_field.ast.type_expr != 0) { if (container_field.ast.type_expr != 0) {
@ -2098,7 +2098,7 @@ fn iterateSymbolsContainerInternal(
var decl_it = container_scope.decls.iterator(); var decl_it = container_scope.decls.iterator();
while (decl_it.next()) |entry| { while (decl_it.next()) |entry| {
switch (entry.value) { switch (entry.value_ptr.*) {
.ast_node => |node| { .ast_node => |node| {
if (node_tags[node].isContainerField()) { if (node_tags[node].isContainerField()) {
if (!instance_access and !is_enum) continue; if (!instance_access and !is_enum) continue;
@ -2106,7 +2106,8 @@ fn iterateSymbolsContainerInternal(
} else if (node_tags[node] == .global_var_decl or } else if (node_tags[node] == .global_var_decl or
node_tags[node] == .local_var_decl or node_tags[node] == .local_var_decl or
node_tags[node] == .simple_var_decl or node_tags[node] == .simple_var_decl or
node_tags[node] == .aligned_var_decl) { node_tags[node] == .aligned_var_decl)
{
if (instance_access) continue; if (instance_access) continue;
} }
}, },
@ -2114,7 +2115,7 @@ fn iterateSymbolsContainerInternal(
else => {}, 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; if (handle != orig_handle and !decl.isPublic()) continue;
try callback(context, decl); try callback(context, decl);
} }
@ -2172,11 +2173,11 @@ pub fn iterateLabels(
if (source_index >= scope.range.start and source_index < scope.range.end) { if (source_index >= scope.range.start and source_index < scope.range.end) {
var decl_it = scope.decls.iterator(); var decl_it = scope.decls.iterator();
while (decl_it.next()) |entry| { while (decl_it.next()) |entry| {
switch (entry.value) { switch (entry.value_ptr.*) {
.label_decl => {}, .label_decl => {},
else => continue, 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; 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) { if (source_index >= scope.range.start and source_index <= scope.range.end) {
var decl_it = scope.decls.iterator(); var decl_it = scope.decls.iterator();
while (decl_it.next()) |entry| { 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_ptr.* == .ast_node and
if (entry.value == .label_decl) continue; handle.tree.nodes.items(.tag)[entry.value_ptr.*.ast_node].isContainerField()) continue;
try callback(context, DeclWithHandle{ .decl = &entry.value, .handle = handle }); if (entry.value_ptr.* == .label_decl) continue;
try callback(context, DeclWithHandle{ .decl = entry.value_ptr, .handle = handle });
} }
for (scope.uses) |use| { for (scope.uses) |use| {
@ -2323,12 +2325,12 @@ pub fn lookupLabel(
for (handle.document_scope.scopes) |scope| { for (handle.document_scope.scopes) |scope| {
if (source_index >= scope.range.start and source_index < scope.range.end) { if (source_index >= scope.range.start and source_index < scope.range.end) {
if (scope.decls.getEntry(symbol)) |candidate| { if (scope.decls.getEntry(symbol)) |candidate| {
switch (candidate.value) { switch (candidate.value_ptr.*) {
.label_decl => {}, .label_decl => {},
else => continue, else => continue,
} }
return DeclWithHandle{ return DeclWithHandle{
.decl = &candidate.value, .decl = candidate.value_ptr,
.handle = handle, .handle = handle,
}; };
} }
@ -2352,7 +2354,7 @@ pub fn lookupSymbolGlobal(
const scope = &handle.document_scope.scopes[curr]; const scope = &handle.document_scope.scopes[curr];
if (source_index >= scope.range.start and source_index <= scope.range.end) blk: { if (source_index >= scope.range.start and source_index <= scope.range.end) blk: {
if (scope.decls.getEntry(symbol)) |candidate| { if (scope.decls.getEntry(symbol)) |candidate| {
switch (candidate.value) { switch (candidate.value_ptr.*) {
.ast_node => |node| { .ast_node => |node| {
if (handle.tree.nodes.items(.tag)[node].isContainerField()) break :blk; if (handle.tree.nodes.items(.tag)[node].isContainerField()) break :blk;
}, },
@ -2360,7 +2362,7 @@ pub fn lookupSymbolGlobal(
else => {}, else => {},
} }
return DeclWithHandle{ return DeclWithHandle{
.decl = &candidate.value, .decl = candidate.value_ptr,
.handle = handle, .handle = handle,
}; };
} }
@ -2391,7 +2393,7 @@ pub fn lookupSymbolContainer(
if (findContainerScope(container_handle)) |container_scope| { if (findContainerScope(container_handle)) |container_scope| {
if (container_scope.decls.getEntry(symbol)) |candidate| { if (container_scope.decls.getEntry(symbol)) |candidate| {
switch (candidate.value) { switch (candidate.value_ptr.*) {
.ast_node => |node| { .ast_node => |node| {
if (node_tags[node].isContainerField()) { if (node_tags[node].isContainerField()) {
if (!instance_access and !is_enum) return null; if (!instance_access and !is_enum) return null;
@ -2401,7 +2403,7 @@ pub fn lookupSymbolContainer(
.label_decl => unreachable, .label_decl => unreachable,
else => {}, 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; if (try resolveUse(store, arena, container_scope.uses, symbol, handle)) |result| return result;
@ -2411,23 +2413,24 @@ pub fn lookupSymbolContainer(
return null; return null;
} }
fn eqlCompletionItem(a: types.CompletionItem, b: types.CompletionItem) bool { const CompletionContext = struct {
return std.mem.eql(u8, a.label, b.label); 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 { pub fn eql(self: @This(), a: types.CompletionItem, b: types.CompletionItem) bool {
return @truncate(u32, std.hash.Wyhash.hash(0, completion_item.label)); return std.mem.eql(u8, a.label, b.label);
} }
};
pub const CompletionSet = std.ArrayHashMapUnmanaged( pub const CompletionSet = std.ArrayHashMapUnmanaged(
types.CompletionItem, types.CompletionItem,
void, void,
hashCompletionItem, CompletionContext,
eqlCompletionItem,
false, false,
); );
comptime { comptime {
std.debug.assert(@sizeOf(types.CompletionItem) == @sizeOf(CompletionSet.Entry)); std.debug.assert(@sizeOf(types.CompletionItem) == @sizeOf(CompletionSet.Data));
} }
pub const DocumentScope = struct { pub const DocumentScope = struct {
@ -2466,12 +2469,12 @@ pub const DocumentScope = struct {
allocator.free(scope.tests); allocator.free(scope.tests);
} }
allocator.free(self.scopes); allocator.free(self.scopes);
for (self.error_completions.entries.items) |entry| { for (self.error_completions.entries.items(.key)) |item| {
if (entry.key.documentation) |doc| allocator.free(doc.value); if (item.documentation) |doc| allocator.free(doc.value);
} }
self.error_completions.deinit(allocator); self.error_completions.deinit(allocator);
for (self.enum_completions.entries.items) |entry| { for (self.enum_completions.entries.items(.key)) |item| {
if (entry.key.documentation) |doc| allocator.free(doc.value); if (item.documentation) |doc| allocator.free(doc.value);
} }
self.enum_completions.deinit(allocator); self.enum_completions.deinit(allocator);
} }
@ -2507,12 +2510,12 @@ pub fn makeDocumentScope(allocator: *std.mem.Allocator, tree: ast.Tree) !Documen
errdefer { errdefer {
scopes.deinit(allocator); scopes.deinit(allocator);
for (error_completions.entries.items) |entry| { for (error_completions.entries.items(.key)) |completion| {
if (entry.key.documentation) |doc| allocator.free(doc.value); if (completion.documentation) |doc| allocator.free(doc.value);
} }
error_completions.deinit(allocator); error_completions.deinit(allocator);
for (enum_completions.entries.items) |entry| { for (enum_completions.entries.items(.key)) |completion| {
if (entry.key.documentation) |doc| allocator.free(doc.value); if (completion.documentation) |doc| allocator.free(doc.value);
} }
enum_completions.deinit(allocator); enum_completions.deinit(allocator);
} }

View File

@ -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 { pub fn openDocument(self: *DocumentStore, uri: []const u8, text: []const u8) !*Handle {
if (self.handles.getEntry(uri)) |entry| { if (self.handles.getEntry(uri)) |entry| {
log.debug("Document already open: {s}, incrementing count", .{uri}); log.debug("Document already open: {s}, incrementing count", .{uri});
entry.value.count += 1; entry.value_ptr.*.count += 1;
if (entry.value.is_build_file) |build_file| { if (entry.value_ptr.*.is_build_file) |build_file| {
build_file.refs += 1; build_file.refs += 1;
} }
log.debug("New count: {}", .{entry.value.count}); log.debug("New count: {}", .{entry.value_ptr.*.count});
return entry.value; return entry.value_ptr.*;
} }
const duped_text = try std.mem.dupe(self.allocator, u8, text); 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 { fn decrementCount(self: *DocumentStore, uri: []const u8) void {
if (self.handles.getEntry(uri)) |entry| { if (self.handles.getEntry(uri)) |entry| {
if (entry.value.count == 0) return; const handle = entry.value_ptr.*;
entry.value.count -= 1; if (handle.count == 0) return;
handle.count -= 1;
if (entry.value.count > 0) if (handle.count > 0)
return; return;
log.debug("Freeing document: {s}", .{uri}); 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); self.decrementBuildFileRefs(build_file);
} }
if (entry.value.is_build_file) |build_file| { if (handle.is_build_file) |build_file| {
self.decrementBuildFileRefs(build_file); self.decrementBuildFileRefs(build_file);
} }
entry.value.tree.deinit(self.allocator); handle.tree.deinit(self.allocator);
self.allocator.free(entry.value.document.mem); 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); self.decrementCount(import_uri);
} }
for (entry.value.import_uris) |import_uri| { for (handle.import_uris) |import_uri| {
self.allocator.free(import_uri); self.allocator.free(import_uri);
} }
entry.value.document_scope.deinit(self.allocator); handle.document_scope.deinit(self.allocator);
entry.value.imports_used.deinit(self.allocator); handle.imports_used.deinit(self.allocator);
self.allocator.free(entry.value.import_uris); self.allocator.free(handle.import_uris);
self.allocator.destroy(entry.value); self.allocator.destroy(handle);
const uri_key = entry.key; const uri_key = entry.key_ptr.*;
self.handles.removeAssertDiscard(uri); std.debug.assert(self.handles.remove(uri));
self.allocator.free(uri_key); 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 { pub fn deinit(self: *DocumentStore) void {
var entry_iterator = self.handles.iterator(); var entry_iterator = self.handles.iterator();
while (entry_iterator.next()) |entry| { while (entry_iterator.next()) |entry| {
entry.value.document_scope.deinit(self.allocator); entry.value_ptr.*.document_scope.deinit(self.allocator);
entry.value.tree.deinit(self.allocator); entry.value_ptr.*.tree.deinit(self.allocator);
self.allocator.free(entry.value.document.mem); self.allocator.free(entry.value_ptr.*.document.mem);
for (entry.value.import_uris) |uri| { for (entry.value_ptr.*.import_uris) |uri| {
self.allocator.free(uri); self.allocator.free(uri);
} }
self.allocator.free(entry.value.import_uris); self.allocator.free(entry.value_ptr.*.import_uris);
entry.value.imports_used.deinit(self.allocator); entry.value_ptr.*.imports_used.deinit(self.allocator);
self.allocator.free(entry.key); self.allocator.free(entry.key_ptr.*);
self.allocator.destroy(entry.value); self.allocator.destroy(entry.value_ptr.*);
} }
self.handles.deinit(); self.handles.deinit();
@ -690,17 +691,17 @@ fn tagStoreCompletionItems(
var result_set = analysis.CompletionSet{}; var result_set = analysis.CompletionSet{};
try result_set.ensureCapacity(&arena.allocator, max_len); try result_set.ensureCapacity(&arena.allocator, max_len);
result_set.entries.appendSliceAssumeCapacity(@field(base.document_scope, name).entries.items); for (@field(base.document_scope, name).entries.items(.key)) |completion| {
try result_set.reIndex(&arena.allocator); result_set.putAssumeCapacityNoClobber(completion, {});
}
for (base.imports_used.items) |uri| { for (base.imports_used.items) |uri| {
const curr_set = &@field(self.handles.get(uri).?.document_scope, name); const curr_set = &@field(self.handles.get(uri).?.document_scope, name);
for (curr_set.entries.items) |entry| { for (curr_set.entries.items(.key)) |completion| {
result_set.putAssumeCapacity(entry.key, {}); result_set.putAssumeCapacity(completion, {});
} }
} }
// This is safe to do because CompletionSet.Entry == struct { value: types.CompletionItem } return result_set.entries.items(.key);
return std.mem.bytesAsSlice(types.CompletionItem, std.mem.sliceAsBytes(result_set.entries.items));
} }
pub fn errorCompletionItems( pub fn errorCompletionItems(

View File

@ -536,13 +536,13 @@ pub fn symbolReferences(
var handle_it = store.handles.iterator(); var handle_it = store.handles.iterator();
while (handle_it.next()) |entry| { while (handle_it.next()) |entry| {
if (skip_std_references and std.mem.indexOf(u8, entry.key, "std") != null) { if (skip_std_references and std.mem.indexOf(u8, entry.key_ptr.*, "std") != null) {
if (!include_decl or entry.value != curr_handle) if (!include_decl or entry.value_ptr.* != curr_handle)
continue; continue;
} }
// Check entry's transitive imports // Check entry's transitive imports
try imports.append(entry.value); try imports.append(entry.value_ptr.*);
var i: usize = 0; var i: usize = 0;
blk: while (i < imports.items.len) : (i += 1) { blk: while (i < imports.items.len) : (i += 1) {
const import = imports.items[i]; const import = imports.items[i];
@ -551,7 +551,7 @@ pub fn symbolReferences(
if (h == curr_handle) { if (h == curr_handle) {
// entry does import 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; break :blk;
} }

View File

@ -136,9 +136,9 @@ pub const WorkspaceEdit = struct {
if (idx != 0) try writer.writeAll(", "); if (idx != 0) try writer.writeAll(", ");
try writer.writeByte('"'); try writer.writeByte('"');
try writer.writeAll(entry.key); try writer.writeAll(entry.key_ptr.*);
try writer.writeAll("\":"); try writer.writeAll("\":");
try std.json.stringify(entry.value, options, writer); try std.json.stringify(entry.value_ptr.*, options, writer);
} }
try writer.writeByte('}'); try writer.writeByte('}');
} }