Skip to content

Commit af247f5

Browse files
committed
fix(pipeline): from_id fallback picks the enclosing same-name symbol
- `CollectorCaches::name_in_file` keys to a candidate list per (name, file_id) instead of overwriting on insert, retaining every same-name symbol in a file - `lookup_by_name_in_file` selects the candidate whose declared range encloses the call site, innermost on nesting, falling back to the last-declared candidate when none encloses - `create_unresolved_relationship` passes the call site range into the from_id fallback lookup to enable enclosure matching
1 parent 2c0dbe2 commit af247f5

1 file changed

Lines changed: 144 additions & 6 deletions

File tree

‎src/indexing/pipeline/stages/collect.rs‎

Lines changed: 144 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ pub struct CollectStage {
3131
start_symbol_counter: u32,
3232
}
3333

34+
type NameInFileCandidates = HashMap<(Arc<str>, FileId), Vec<(Range, SymbolId)>>;
35+
3436
/// Ephemeral caches for relationship reconnection.
3537
///
3638
/// Design from decisions.md:
@@ -42,8 +44,9 @@ pub struct CollectStage {
4244
struct CollectorCaches {
4345
/// Primary lookup: (name, file_id, range) -> SymbolId
4446
symbol_lookup: HashMap<(Arc<str>, FileId, Range), SymbolId>,
45-
/// Fallback: (name, file_id) -> SymbolId for when range doesn't match exactly
46-
name_in_file: HashMap<(Arc<str>, FileId), SymbolId>,
47+
/// Fallback candidates per (name, file_id), with declared ranges so
48+
/// the call site can pick the enclosing symbol among same-name ones
49+
name_in_file: NameInFileCandidates,
4750
/// Path to FileId mapping for Phase 2 resolution
4851
file_ids: HashMap<PathBuf, FileId>,
4952
}
@@ -66,7 +69,10 @@ impl CollectorCaches {
6669
fn insert(&mut self, name: Arc<str>, file_id: FileId, range: Range, symbol_id: SymbolId) {
6770
self.symbol_lookup
6871
.insert((Arc::clone(&name), file_id, range), symbol_id);
69-
self.name_in_file.insert((name, file_id), symbol_id);
72+
self.name_in_file
73+
.entry((name, file_id))
74+
.or_default()
75+
.push((range, symbol_id));
7076
}
7177

7278
/// Lookup by exact (name, file_id, range) match.
@@ -77,8 +83,26 @@ impl CollectorCaches {
7783
}
7884

7985
/// Fallback lookup by (name, file_id) when range doesn't match.
80-
fn lookup_by_name_in_file(&self, name: &Arc<str>, file_id: FileId) -> Option<SymbolId> {
81-
self.name_in_file.get(&(Arc::clone(name), file_id)).copied()
86+
///
87+
/// Among same-name symbols the caller is the one whose declared range
88+
/// encloses the call site, innermost on nesting. When none encloses
89+
/// (e.g. a parser emits name-node-only symbol ranges), keep the
90+
/// previous last-declared pick rather than dropping the attribution.
91+
fn lookup_by_name_in_file(
92+
&self,
93+
name: &Arc<str>,
94+
file_id: FileId,
95+
call_site: Range,
96+
) -> Option<SymbolId> {
97+
let candidates = self.name_in_file.get(&(Arc::clone(name), file_id))?;
98+
candidates
99+
.iter()
100+
.filter(|(range, _)| {
101+
range.start_line <= call_site.start_line && call_site.start_line <= range.end_line
102+
})
103+
.max_by_key(|(range, _)| range.start_line)
104+
.or_else(|| candidates.last())
105+
.map(|&(_, id)| id)
82106
}
83107
}
84108

@@ -401,7 +425,7 @@ fn create_unresolved_relationship(
401425
// Try to resolve from_id using the cache (zero-copy: pass Arc reference)
402426
let from_id = caches
403427
.lookup(&raw.from_name, file_id, raw.from_range)
404-
.or_else(|| caches.lookup_by_name_in_file(&raw.from_name, file_id));
428+
.or_else(|| caches.lookup_by_name_in_file(&raw.from_name, file_id, raw.to_range));
405429

406430
UnresolvedRelationship {
407431
from_id,
@@ -581,6 +605,120 @@ mod tests {
581605
assert_eq!(rel.to_name.as_ref(), "callee");
582606
}
583607

608+
#[test]
609+
fn from_id_fallback_attributes_to_enclosing_same_name_symbol() {
610+
// Two same-name symbols in one file (production method + test-mod
611+
// stub). When the exact (name, file, range) lookup misses, the
612+
// caller is the symbol whose range encloses the call site — not
613+
// whichever same-name symbol the cache saw last.
614+
let (parsed_tx, parsed_rx) = bounded(100);
615+
let (batch_tx, batch_rx) = bounded(100);
616+
617+
let mut parsed = make_parsed_file(
618+
"test.rs",
619+
vec![
620+
RawSymbol::new("resolve", SymbolKind::Method, Range::new(5, 4, 30, 5)),
621+
RawSymbol::new("resolve", SymbolKind::Method, Range::new(50, 4, 52, 5)),
622+
],
623+
);
624+
parsed.raw_relationships.push(RawRelationship::new(
625+
"resolve",
626+
Range::new(12, 8, 12, 20), // from_range: not a declared range -> fallback
627+
"resolve_one",
628+
Range::new(12, 8, 12, 30), // call site inside the FIRST symbol's body
629+
RelationKind::Calls,
630+
));
631+
632+
parsed_tx.send(parsed).unwrap();
633+
drop(parsed_tx);
634+
635+
let stage = CollectStage::new(100);
636+
let _ = stage.run(parsed_rx, batch_tx, None, None);
637+
638+
let batches: Vec<_> = batch_rx.iter().collect();
639+
let rel = &batches[0].unresolved_relationships[0];
640+
assert_eq!(
641+
rel.from_id.map(|id| id.value()),
642+
Some(1),
643+
"caller must be the enclosing symbol, not the later same-name stub"
644+
);
645+
}
646+
647+
fn collect_single_rel(parsed: ParsedFile) -> Option<u32> {
648+
let (parsed_tx, parsed_rx) = bounded(100);
649+
let (batch_tx, batch_rx) = bounded(100);
650+
parsed_tx.send(parsed).unwrap();
651+
drop(parsed_tx);
652+
let stage = CollectStage::new(100);
653+
let _ = stage.run(parsed_rx, batch_tx, None, None);
654+
let batches: Vec<_> = batch_rx.iter().collect();
655+
batches[0].unresolved_relationships[0]
656+
.from_id
657+
.map(|id| id.value())
658+
}
659+
660+
#[test]
661+
fn from_id_fallback_encloses_by_range_not_declaration_order() {
662+
// Call site inside the SECOND same-name symbol attributes there.
663+
let mut parsed = make_parsed_file(
664+
"test.rs",
665+
vec![
666+
RawSymbol::new("resolve", SymbolKind::Method, Range::new(5, 4, 30, 5)),
667+
RawSymbol::new("resolve", SymbolKind::Method, Range::new(50, 4, 60, 5)),
668+
],
669+
);
670+
parsed.raw_relationships.push(RawRelationship::new(
671+
"resolve",
672+
Range::new(55, 8, 55, 20),
673+
"helper",
674+
Range::new(55, 8, 55, 30),
675+
RelationKind::Calls,
676+
));
677+
assert_eq!(collect_single_rel(parsed), Some(2));
678+
}
679+
680+
#[test]
681+
fn from_id_fallback_picks_innermost_enclosing_on_nesting() {
682+
// Nested same-name symbols (outer 5-40, inner 10-20): a call at
683+
// line 15 belongs to the inner one.
684+
let mut parsed = make_parsed_file(
685+
"test.rs",
686+
vec![
687+
RawSymbol::new("run", SymbolKind::Function, Range::new(5, 0, 40, 1)),
688+
RawSymbol::new("run", SymbolKind::Function, Range::new(10, 4, 20, 5)),
689+
],
690+
);
691+
parsed.raw_relationships.push(RawRelationship::new(
692+
"run",
693+
Range::new(15, 8, 15, 20),
694+
"helper",
695+
Range::new(15, 8, 15, 30),
696+
RelationKind::Calls,
697+
));
698+
assert_eq!(collect_single_rel(parsed), Some(2));
699+
}
700+
701+
#[test]
702+
fn from_id_fallback_without_enclosing_range_still_attributes() {
703+
// Name-node-only symbol ranges (single line) never enclose a call
704+
// site on another line; attribution must not drop to None.
705+
let mut parsed = make_parsed_file(
706+
"test.py",
707+
vec![
708+
make_raw_symbol("handler", SymbolKind::Function, 1),
709+
make_raw_symbol("handler", SymbolKind::Function, 20),
710+
],
711+
);
712+
parsed.raw_relationships.push(RawRelationship::new(
713+
"handler",
714+
Range::new(3, 4, 3, 12),
715+
"helper",
716+
Range::new(3, 4, 3, 20),
717+
RelationKind::Calls,
718+
));
719+
assert!(collect_single_rel(parsed).is_some());
720+
}
721+
584722
#[test]
585723
fn test_collect_converts_imports() {
586724
let (parsed_tx, parsed_rx) = bounded(100);

0 commit comments

Comments
 (0)