Skip to content

Commit 4f3cf63

Browse files
committed
references: only lookup symbols with matching names
1 parent bcdeab5 commit 4f3cf63

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/features/references.zig

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ const Builder = struct {
6565
locations: std.ArrayListUnmanaged(types.Location) = .empty,
6666
/// this is the declaration we are searching for
6767
decl_handle: Analyser.DeclWithHandle,
68+
/// the decl is local to a function, block, etc
69+
local_only_decl: bool,
6870
/// Whether the `decl_handle` has been added
6971
did_add_decl_handle: bool = false,
7072
analyser: *Analyser,
@@ -104,6 +106,10 @@ const Builder = struct {
104106
fn referenceNode(self: *const Context, tree: Ast, node: Ast.Node.Index) error{OutOfMemory}!void {
105107
const builder = self.builder;
106108
const handle = self.handle;
109+
const decl_name = offsets.identifierTokenToNameSlice(
110+
builder.decl_handle.handle.tree,
111+
builder.decl_handle.nameToken(),
112+
);
107113

108114
switch (tree.nodeTag(node)) {
109115
.identifier,
@@ -119,6 +125,7 @@ const Builder = struct {
119125
else => unreachable,
120126
};
121127
const name = offsets.identifierTokenToNameSlice(tree, name_token);
128+
if (!std.mem.eql(u8, name, decl_name)) return;
122129

123130
const child = try builder.analyser.lookupSymbolGlobal(
124131
handle,
@@ -131,15 +138,18 @@ const Builder = struct {
131138
}
132139
},
133140
.field_access => {
134-
const lhs_node, const field_name = tree.nodeData(node).node_and_token;
141+
if (builder.local_only_decl) return;
142+
const lhs_node, const field_token = tree.nodeData(node).node_and_token;
143+
const name = offsets.identifierTokenToNameSlice(tree, field_token);
144+
if (!std.mem.eql(u8, name, decl_name)) return;
145+
135146
const lhs = try builder.analyser.resolveTypeOfNode(.of(lhs_node, handle)) orelse return;
136147
const deref_lhs = try builder.analyser.resolveDerefType(lhs) orelse lhs;
137148

138-
const symbol = offsets.identifierTokenToNameSlice(tree, field_name);
139-
const child = try deref_lhs.lookupSymbol(builder.analyser, symbol) orelse return;
149+
const child = try deref_lhs.lookupSymbol(builder.analyser, name) orelse return;
140150

141151
if (builder.decl_handle.eql(child)) {
142-
try builder.add(handle, field_name);
152+
try builder.add(handle, field_token);
143153
}
144154
},
145155
.struct_init_one,
@@ -151,23 +161,28 @@ const Builder = struct {
151161
.struct_init_dot_two,
152162
.struct_init_dot_two_comma,
153163
=> {
164+
if (builder.local_only_decl) return;
154165
var buffer: [2]Ast.Node.Index = undefined;
155166
const struct_init = tree.fullStructInit(&buffer, node).?;
156167
for (struct_init.ast.fields) |value_node| { // the node of `value` in `.name = value`
157168
const name_token = tree.firstToken(value_node) - 2; // math our way two token indexes back to get the `name`
158169
const name_loc = offsets.tokenToLoc(tree, name_token);
159170
const name = offsets.locToSlice(tree.source, name_loc);
171+
if (!std.mem.eql(u8, name, decl_name)) continue;
160172

161173
const lookup = try builder.analyser.lookupSymbolFieldInit(handle, name, node, &.{}) orelse continue;
162174

163175
if (builder.decl_handle.eql(lookup)) {
164176
try builder.add(handle, name_token);
177+
return;
165178
}
166179
}
167180
},
168181
.enum_literal => {
182+
if (builder.local_only_decl) return;
169183
const name_token = tree.nodeMainToken(node);
170184
const name = offsets.identifierTokenToNameSlice(handle.tree, name_token);
185+
if (!std.mem.eql(u8, name, decl_name)) return;
171186
const lookup = try builder.analyser.getSymbolEnumLiteral(handle, tree.tokenStart(name_token), name) orelse return;
172187

173188
if (builder.decl_handle.eql(lookup)) {
@@ -277,6 +292,7 @@ fn symbolReferences(
277292
.allocator = allocator,
278293
.analyser = analyser,
279294
.decl_handle = decl_handle,
295+
.local_only_decl = local_node != null,
280296
.encoding = encoding,
281297
};
282298
errdefer builder.deinit();

0 commit comments

Comments
 (0)