`tb.dut.g_sw[0].buf_q` written in a file that does not declare `tb` now resolves like an instance path: the binder marks a dotted path's head (Target::PathHead), resolve/root.rs answers a plain lexical miss with a hierarchical external candidate, head_site roots the spine there, and fold_path walks it — every hop a module/interface surface. find_references, LSP references and rename see every segment; $root.tb works; a local `tb` shadows. FACTS_FORMAT 22. Rooted only when the root is provable (owner's ruling: conservative): not when the head has an import candidate (so `import uvm_pkg::*` with uvm_pkg outside the project stays warned — opentitan's DV force paths behave as before), not from inside a class, not beside a macro that stands where a declaration could (item, statement, port list), not when the root name is also a package or ambiguous. The LSP rename set adds the files of the path's root and middle hops (sources.rs, WorkspaceIndex::path_units_to). An unresolved path is warned once. QA (Opus) x2: F1-F4 and R1 silent-wrong-rename shapes fixed with tests; QA's attack cases 1-6 each warned or correctly renamed; lucisv check hw byte-identical to master. Completion after `tb.` deferred (TODO §65.8). tools/gates.sh: PASS, 15 steps, 5 180 tests, corpus 814/820. docs/features/top-rooted-paths. Co-Authored-By: Claude Opus 5.5 <[email protected]>
73 lines
2.9 KiB
Rust
73 lines
2.9 KiB
Rust
//! The design units a hierarchical path to a declaration walks through —
|
|
//! what a caller handing the refactoring engine a *subset* of the workspace
|
|
//! must include for that path to resolve inside the subset as it does in the
|
|
//! whole (top-rooted-paths QA F3).
|
|
//!
|
|
//! `tb.dut.u_core.sig` in a DV file names `sig` through `tb` (the top module
|
|
//! it is rooted at), `dut_a` (the type of `tb.dut`) and `core` (the type of
|
|
//! `u_core`). The index resolved it, so the use is in `sig`'s postings; but a
|
|
//! rename handed only `sig`'s declaring file and that DV file cannot resolve
|
|
//! the root, and would leave the path behind. The answer is read off the
|
|
//! records the index already holds: no file is read.
|
|
|
|
use std::collections::{BTreeSet, HashMap, HashSet};
|
|
|
|
use crate::model::Surface;
|
|
|
|
use super::record::ReferenceFact;
|
|
use super::{Def, FileId, WorkspaceIndex};
|
|
|
|
impl WorkspaceIndex {
|
|
/// Every unit an indexed use of `def` written as a hierarchical path
|
|
/// passes through before its last segment: the path's first unit (an
|
|
/// instance's type, a `bind` target, a top module) and the unit of each
|
|
/// hop after it. Empty when no use of `def` is such a path.
|
|
pub fn path_units_to(&self, def: &Def) -> BTreeSet<String> {
|
|
let mut by_file: HashMap<FileId, HashSet<(u32, u32)>> = HashMap::new();
|
|
for site in self.references_to(def) {
|
|
let span = (site.name_span.start, site.name_span.end);
|
|
by_file.entry(site.file).or_default().insert(span);
|
|
}
|
|
let mut out = BTreeSet::new();
|
|
for (file, spans) in by_file {
|
|
let Some(facts) = self.facts.get(file.0 as usize) else { continue };
|
|
for fact in &facts.references {
|
|
let ReferenceFact::External {
|
|
paths,
|
|
surface: Surface::Hierarchical,
|
|
occurrence,
|
|
..
|
|
} = fact
|
|
else {
|
|
continue;
|
|
};
|
|
if spans.contains(&(occurrence.start, occurrence.end)) {
|
|
for path in paths {
|
|
self.collect_hops(path, &mut out);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
out
|
|
}
|
|
|
|
/// The units `path`'s segments before its last stand in, folded as
|
|
/// `external::fold_path` folds them, as far as the fold goes.
|
|
fn collect_hops(&self, path: &str, out: &mut BTreeSet<String>) {
|
|
let mut segments: Vec<&str> = path.split("::").collect();
|
|
segments.pop();
|
|
let Some((first, hops)) = segments.split_first() else { return };
|
|
let mut unit = first.to_string();
|
|
out.insert(unit.clone());
|
|
for hop in hops {
|
|
match self.member_unit_in(&unit, hop, Surface::Hierarchical) {
|
|
Some(next) => {
|
|
unit = next;
|
|
out.insert(unit.clone());
|
|
}
|
|
None => return,
|
|
}
|
|
}
|
|
}
|
|
}
|