diff --git a/crates/cg-core/src/brand.rs b/crates/cg-core/src/brand.rs index d6cfb16..f9c1943 100644 --- a/crates/cg-core/src/brand.rs +++ b/crates/cg-core/src/brand.rs @@ -17,7 +17,7 @@ pub const UPSTREAM_CONFIG_FILE: &str = "codegraph.json"; pub const CONFIG_FILE: &str = "cgr.json"; /// Version of the resolution rules: changing resolver behaviour bumps this, which /// marks older indexes as "built by an older engine" (design doc 5.8). -pub const ENGINE_VERSION: u32 = 10; +pub const ENGINE_VERSION: u32 = 11; /// Version of the on-disk segment format; a mismatch forces a full rebuild. pub const FORMAT_VERSION: u32 = 2; /// The upstream commit whose kernel is vendored (see crates/cg-kernel/UPSTREAM_COMMIT). diff --git a/crates/cg-extract/src/facts/mod.rs b/crates/cg-extract/src/facts/mod.rs index 6d24285..145a237 100644 --- a/crates/cg-extract/src/facts/mod.rs +++ b/crates/cg-extract/src/facts/mod.rs @@ -157,6 +157,9 @@ pub enum BindKind { /// A Rust type parameter of the fn, impl or trait whose scope holds it. Only a type /// reference sees it: `T` in `fn new(value: T)` names no `struct T` elsewhere. TypeParam = 16, + /// A Rust method of an `impl Trait for X` block, keyed by its node: an inherent `X::m` + /// shadows it. Metadata only; not a lexical binding. + TraitImpl = 17, } #[derive(Clone, Debug, PartialEq, Eq)] diff --git a/crates/cg-extract/src/facts/rust.rs b/crates/cg-extract/src/facts/rust.rs index a0248d5..0e0ced3 100644 --- a/crates/cg-extract/src/facts/rust.rs +++ b/crates/cg-extract/src/facts/rust.rs @@ -112,6 +112,7 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) { bind_type_params(cx, sc); if n.kind() == "impl_item" { deref_impl(cx, n, scope); + trait_impl(cx, n, sc); } walk_children(cx, n, sc); cx.generics.pop(); @@ -325,6 +326,17 @@ fn deref_impl(cx: &mut Cx, n: Node, scope: u32) { } } +/// `impl Trait for X { fn m() }`: `m` is the trait's, which an inherent `X::m` shadows. +fn trait_impl(cx: &mut Cx, n: Node, scope: u32) { + let (Some(_), Some(body)) = (n.child_by_field_name("trait"), n.child_by_field_name("body")) else { return }; + for f in kids(body).filter(|c| c.kind() == "function_item") { + let (Some(name), Some(row)) = (f.child_by_field_name("name"), cx.row_at(cx.start(f))) else { continue }; + let name = cx.text(name).to_owned(); + let line = cx.line(f); + cx.facts.bindings.push(BindingFact { scope, name, kind: BindKind::TraitImpl, decl_type: String::new(), init_ctor: String::new(), line, node_row: row }); + } +} + fn rust_type_name(cx: &Cx, n: Node) -> String { match n.kind() { "type_identifier" | "scoped_type_identifier" => cx.text(n).to_owned(), diff --git a/crates/cg-extract/src/facts/util.rs b/crates/cg-extract/src/facts/util.rs index eebd0b6..d4d5691 100644 --- a/crates/cg-extract/src/facts/util.rs +++ b/crates/cg-extract/src/facts/util.rs @@ -131,6 +131,7 @@ impl<'a> Cx<'a> { | BindKind::Mutation | BindKind::CommonJsObject | BindKind::AssignedThisCall + | BindKind::TraitImpl ) }) { return Some((b.decl_type.clone(), b.init_ctor.clone())); diff --git a/crates/cg-resolve/src/driver.rs b/crates/cg-resolve/src/driver.rs index a0f1326..2178897 100644 --- a/crates/cg-resolve/src/driver.rs +++ b/crates/cg-resolve/src/driver.rs @@ -195,15 +195,13 @@ pub fn resolve_all(e: &mut Engine<'_>) -> ResolveOutput { let mut outcomes = vec![Outcome::PENDING; n_refs]; resolve_into(e, &files, Round::One, &mut outcomes); - let pairs = outcomes - .iter() - .enumerate() - .filter_map(|(id, o)| { - let r = view.reference(id as u32); - (o.status == status::RESOLVED && matches!(RefKind::from_wire(r.kind), Some(RefKind::Edge(EdgeKind::Extends | EdgeKind::Implements)))) - .then_some((r.from, NodeIdx(o.target))) - }) - .collect::>(); + let mut pairs = Vec::new(); + for (id, o) in outcomes.iter().enumerate() { + let r = view.reference(id as u32); + if o.status == status::RESOLVED && matches!(RefKind::from_wire(r.kind), Some(RefKind::Edge(EdgeKind::Extends | EdgeKind::Implements))) { + pairs.push((r.from, NodeIdx(o.target), (r.line, r.col))); + } + } e.set_hierarchy(pairs.into_iter()); resolve_into(e, &files, Round::Two, &mut outcomes); diff --git a/crates/cg-resolve/src/engine.rs b/crates/cg-resolve/src/engine.rs index 5728029..1e3c487 100644 --- a/crates/cg-resolve/src/engine.rs +++ b/crates/cg-resolve/src/engine.rs @@ -47,8 +47,12 @@ pub struct Engine<'v> { pub stars: Vec>, /// Per package: top-level definitions by name. pub pkg_defs: Vec>>, - /// Direct supertypes (`extends` / `implements` resolved in round 1). + /// Direct supertypes (`extends` / `implements` resolved in round 1), in the order written. pub supers: FxHashMap>, + /// Python classes' method resolution order (C3), past the class itself. + mro: FxHashMap>, + /// Rust methods of `impl Trait for X` blocks: an inherent `X::m` shadows them. + trait_methods: rustc_hash::FxHashSet, /// C++ namespaces opened by a `#define` anywhere in the project. pub macro_namespaces: rustc_hash::FxHashSet<&'v str>, /// JavaScript functions with syntax evidence that they implement an object member. @@ -126,6 +130,14 @@ impl<'v> Engine<'v> { stars: vec![Vec::new(); n_files], pkg_defs: Vec::new(), supers: FxHashMap::default(), + mro: FxHashMap::default(), + trait_methods: files + .iter() + .filter(|f| view.file(**f).lang() == Lang::Rust) + .flat_map(|&f| view.bindings_of(f)) + .filter(|b| b.kind == BindKind::TraitImpl as u8 && b.node.is_some()) + .map(|b| b.node) + .collect(), macro_namespaces: files .iter() .filter(|f| view.file(**f).lang() == Lang::Cpp) @@ -614,8 +626,12 @@ impl<'v> Engine<'v> { self.supers.get(&n).map(|v| v.as_slice()).unwrap_or(&[]) } - /// Breadth-first ancestors of a type (excluding itself), cycle-safe, depth-limited. + /// Breadth-first ancestors of a type (excluding itself), cycle-safe, depth-limited; a + /// Python class's in its method resolution order. pub fn ancestors(&self, n: NodeIdx) -> Vec { + if let Some(mro) = self.mro.get(&n) { + return mro.clone(); + } let mut out = Vec::new(); let mut frontier = vec![n]; let mut seen = rustc_hash::FxHashSet::default(); @@ -638,19 +654,61 @@ impl<'v> Engine<'v> { out } - /// Install the hierarchy from resolved `extends` / `implements` outcomes. - pub fn set_hierarchy(&mut self, edges: impl Iterator) { - for (sub, sup) in edges { + /// Install the hierarchy from resolved `extends` / `implements` outcomes, each with the + /// (line, column) it is written at: a class's bases are in the order written. + pub fn set_hierarchy(&mut self, edges: impl Iterator) { + let mut written: Vec<_> = edges.filter(|(sub, sup, _)| sub != sup).map(|(sub, sup, pos)| (sub, pos, sup)).collect(); + written.sort(); + for (sub, _, sup) in written { let v = self.supers.entry(sub).or_default(); - if !v.contains(&sup) && sub != sup { + if !v.contains(&sup) { v.push(sup); } } - for v in self.supers.values_mut() { - v.sort(); + let view = self.view; + let python: Vec = self.supers.keys().copied().filter(|n| view.file(view.hot(*n).file).lang() == Lang::Python).collect(); + let mut memo = FxHashMap::default(); + for t in python { + if let Some(mro) = self.c3(t, &mut memo, 0) { + self.mro.insert(t, mro[1..].to_vec()); + } } } + /// Python's C3 linearization of `t`, `t` first. `None` for a cycle or for bases no order + /// satisfies (Python refuses such a class); breadth-first lookup stands in then. + fn c3(&self, t: NodeIdx, memo: &mut FxHashMap>>, depth: u8) -> Option> { + if let Some(m) = memo.get(&t) { + return m.clone(); + } + if depth > 16 { + return None; + } + memo.insert(t, None); + let bases = self.direct_supers(t).to_vec(); + let mut seqs = Vec::with_capacity(bases.len() + 1); + for b in &bases { + seqs.push(self.c3(*b, memo, depth + 1)?); + } + seqs.push(bases); + let mut out = vec![t]; + loop { + seqs.retain(|s| !s.is_empty()); + if seqs.is_empty() { + break; + } + let head = seqs.iter().map(|s| s[0]).find(|h| !seqs.iter().any(|s| s[1..].contains(h)))?; + out.push(head); + for s in &mut seqs { + if s[0] == head { + s.remove(0); + } + } + } + memo.insert(t, Some(out.clone())); + Some(out) + } + /// Hierarchy from refs already resolved in the view (incremental runs). pub fn hierarchy_from_view(&mut self) { let mut pairs = Vec::new(); @@ -659,7 +717,7 @@ impl<'v> Engine<'v> { if r.status == status::RESOLVED && matches!(cg_core::RefKind::from_wire(r.kind), Some(cg_core::RefKind::Edge(EdgeKind::Extends | EdgeKind::Implements))) { - pairs.push((r.from, r.target)); + pairs.push((r.from, r.target, (r.line, r.col))); } } } @@ -671,29 +729,49 @@ impl<'v> Engine<'v> { /// qualified name for methods declared outside the type body: Go receivers, Rust impls). pub fn direct_member(&self, t: NodeIdx, name: &str) -> Option { let tn = self.view.node(t); + // Rust: an inherent method shadows a trait's, wherever either impl is written + let mut of_trait = None; for e in self.view.out_edges(t) { if e.kind == EdgeKind::Contains { let c = self.view.node(e.dst); if c.name() == name && c.kind() != NodeKind::File { - return Some(e.dst); + if !self.trait_methods.contains(&e.dst) { + return Some(e.dst); + } + of_trait.get_or_insert(e.dst); } } } let q = format!("{}::{}", tn.qname(), name); let tfile = tn.file(); let tpkg = self.modules.pkg_of(tfile); + let family = self.view.file(tfile).lang().family(); let mut best = None; let mut cross: SmallVec<[NodeIdx; 4]> = SmallVec::new(); + let mut owned: SmallVec<[NodeIdx; 2]> = SmallVec::new(); for cand in self.view.by_qname(&q) { let cf = self.view.hot(cand).file; // Go: same package; Rust: the owning module or a proved type import. if cf == tfile || (!tpkg.is_none() && self.modules.pkg_of(cf) == tpkg) || self.rust_type_owner(t, cf) { - return Some(cand); + if family != LangFamily::Rust { + return Some(cand); + } + owned.push(cand); + continue; } best.get_or_insert(cand); cross.push(cand); } - match self.view.file(tfile).lang().family() { + // Rust impls of one type in several files: inherent first, then by path and line, so + // a base/delta split cannot decide + let by_place = owned.into_iter().min_by_key(|c| { + let h = self.view.hot(*c); + (self.trait_methods.contains(c), self.view.file(h.file).path(), self.view.node(*c).start_line()) + }); + if let Some(m) = by_place.or(of_trait) { + return Some(m); + } + match family { // Java/Kotlin nested types (pre-existing behaviour), then a Kotlin extension function LangFamily::Jvm => best.filter(|_| tn.qname().contains("::")).or_else(|| self.kotlin_extension(t, name)), // Swift extensions (emitted as class nodes) add members in other files and other diff --git a/crates/cg-resolve/src/resolve.rs b/crates/cg-resolve/src/resolve.rs index 53ba48e..abab750 100644 --- a/crates/cg-resolve/src/resolve.rs +++ b/crates/cg-resolve/src/resolve.rs @@ -496,6 +496,7 @@ impl<'e, 'v> FileCx<'e, 'v> { || b.kind == BindKind::AssignedThisCall as u8 || b.kind == BindKind::MacroNamespace as u8 || b.kind == BindKind::TypeParam as u8 + || b.kind == BindKind::TraitImpl as u8 { continue; } @@ -647,6 +648,12 @@ impl<'e, 'v> FileCx<'e, 'v> { // ------------------------------------------------------------ type lookup /// Type (or namespace) denoted by `name` in this file's context. pub fn lookup_type(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str) -> TypeRes { + self.lookup_type_at(wcx, name, u32::MAX) + } + + /// [`Self::lookup_type`] as written on `line` (`u32::MAX`: anywhere), where a JS class + /// declared in the enclosing function shadows the file's. + fn lookup_type_at(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str, line: u32) -> TypeRes { let name = name.trim(); if name.is_empty() { return TypeRes::Unknown; @@ -657,7 +664,7 @@ impl<'e, 'v> FileCx<'e, 'v> { t => t, }; } - match self.resolve_name(wcx, name, Accept::Type, 0, u32::MAX, None, true) { + match self.resolve_name(wcx, name, Accept::Type, 0, line, None, true) { NameRes::Found(n, ..) => TypeRes::Node(n), NameRes::External => TypeRes::External, NameRes::Builtin => TypeRes::Builtin, @@ -832,7 +839,7 @@ impl<'e, 'v> FileCx<'e, 'v> { } let dt = self.view.text_str(b.decl_type); if !dt.is_empty() { - return self.type_value(wcx, dt, None, depth + 1); + return self.type_value(wcx, dt, None, b.line, depth + 1); } let init = self.view.text_str(b.init_ctor); if init.is_empty() && b.kind == BindKind::Param as u8 && self.family == LangFamily::Python { @@ -953,7 +960,7 @@ impl<'e, 'v> FileCx<'e, 'v> { return self.binding_value(wcx, other, depth + 1); } } - self.type_value(wcx, init, None, depth + 1) + self.type_value(wcx, init, None, line, depth + 1) } /// `let (tx, rx) = mpsc::channel()`: element `index` of the tuple a Rust function returns, @@ -983,7 +990,7 @@ impl<'e, 'v> FileCx<'e, 'v> { ty = *type_list(ty[open + 1..].strip_suffix('>')?).first()?; } let elems = type_list(ty.strip_prefix('(')?.strip_suffix(')')?); - cx.type_value(wcx, elems.get(index)?, None, depth + 1) + cx.type_value(wcx, elems.get(index)?, None, u32::MAX, depth + 1) } /// The function a Rust call path names: `helper`, `mpsc::channel`, `Pair::new`. @@ -1040,7 +1047,7 @@ impl<'e, 'v> FileCx<'e, 'v> { let cx = wcx.file_cx(self.e, f); let declared = self.view.text_str(b.decl_type); if !declared.is_empty() { - return cx.type_value(wcx, declared, None, depth + 1); + return cx.type_value(wcx, declared, None, u32::MAX, depth + 1); } cx.expression_value(wcx, self.view.text_str(b.init_ctor), b.scope.0.wrapping_sub(cx.scope_base), u32::MAX, depth + 1) } @@ -1113,7 +1120,7 @@ impl<'e, 'v> FileCx<'e, 'v> { standard.then_some(wrapper) } - fn type_value(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str, receiver: Option, depth: u8) -> Option { + fn type_value(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str, receiver: Option, line: u32, depth: u8) -> Option { if depth >= 16 || name.len() > 4096 { return None; } @@ -1161,12 +1168,12 @@ impl<'e, 'v> FileCx<'e, 'v> { _ => {} } } - let mut inner = self.type_value(wcx, &args[..end], receiver, depth + 1)?; + let mut inner = self.type_value(wcx, &args[..end], receiver, line, depth + 1)?; inner.wrappers.push(wrapper); return Some(inner); } } - match self.lookup_type(wcx, name) { + match self.lookup_type_at(wcx, name, line) { TypeRes::Node(t) => Some(ValueType::plain(t)), _ if self.family == LangFamily::Js => self.es5_constructor(wcx, name).map(ValueType::plain), _ => None, @@ -1196,11 +1203,11 @@ impl<'e, 'v> FileCx<'e, 'v> { return Some(cx.ts_return_value(n, rt).unwrap_or_else(ValueType::unknown)); } if !rt.is_empty() { - return cx.type_value(wcx, rt, receiver, depth + 1); + return cx.type_value(wcx, rt, receiver, u32::MAX, depth + 1); } return cx.expression_value(wcx, self.view.text_str(b.init_ctor), b.scope.0.wrapping_sub(cx.scope_base), u32::MAX, depth + 1); } - cx.type_value(wcx, node.return_type()?, receiver, depth + 1) + cx.type_value(wcx, node.return_type()?, receiver, u32::MAX, depth + 1) } /// TS return facts contain only nominal names. Look in the declaration's @@ -1287,7 +1294,7 @@ impl<'e, 'v> FileCx<'e, 'v> { } else if self.family == LangFamily::Rust && owner.contains('.') && !owner.contains("::") { self.path_value(wcx, owner, scope, line, depth + 1) } else { - self.type_value(wcx, owner, None, depth + 1) + self.type_value(wcx, owner, None, line, depth + 1) }; if let Some(t) = receiver { return self.member_return(wcx, t, method, depth + 1); @@ -1384,7 +1391,7 @@ impl<'e, 'v> FileCx<'e, 'v> { let next = match self.e.type_aliases.get(&cur) { Some(b) => { let cx = wcx.file_cx(self.e, self.view.hot(cur).file); - cx.type_value(wcx, self.view.text_str(b.decl_type), None, 0).and_then(|v| v.receiver()) + cx.type_value(wcx, self.view.text_str(b.decl_type), None, u32::MAX, 0).and_then(|v| v.receiver()) } None => self.deref_target(wcx, cur), }; @@ -1406,7 +1413,7 @@ impl<'e, 'v> FileCx<'e, 'v> { if cx.lookup_type(wcx, self.view.text_str(b.name)) != TypeRes::Node(t) { continue; } - let target = cx.type_value(wcx, self.view.text_str(b.decl_type), Some(t), 0)?.receiver()?; + let target = cx.type_value(wcx, self.view.text_str(b.decl_type), Some(t), u32::MAX, 0)?.receiver()?; if found.is_some_and(|x| x != target) { return None; } @@ -2117,6 +2124,18 @@ impl<'e, 'v> FileCx<'e, 'v> { if !same.is_empty() { // prefer top-level definitions, then the first declared; overloads by the arguments let mut pick = same.iter().copied().find(|n| view.node(*n).parent().is_some_and(|p| kind_of(view, p) == NodeKind::File)).unwrap_or(same[0]); + // JS: one declared in the function the code sits in shadows those outside it + // ponytail: a block's class counts as the whole function's + if self.family == LangFamily::Js && line != u32::MAX { + let around = |n: &NodeIdx| { + let p = view.node(*n).parent().filter(|p| kind_of(view, *p) != NodeKind::File)?; + let pn = view.node(p); + (pn.start_line() <= line && line <= pn.end_line()).then_some((pn.start_line(), pn.start_col())) + }; + if let Some(inner) = same.iter().filter(|n| around(n).is_some()).max_by_key(|n| around(n)) { + pick = *inner; + } + } if let Some((a, r)) = args.filter(|_| same.len() > 1 && matches!(self.family, LangFamily::Jvm | LangFamily::Js)) { let set = self.sibling_overloads(pick); if let Some(m) = self.pick_overload(wcx, &set, a, r) { @@ -2202,19 +2221,31 @@ impl<'e, 'v> FileCx<'e, 'v> { conf: u8, args: Option<(&CallArgs<'v>, &RefRec)>, ) -> Outcome { - for f in path { + for (i, f) in path.iter().enumerate() { t = self.cpp_unalias(wcx, t); - match self.field_type(wcx, t, f) { - Some(ft) => t = ft, + if let Some(ft) = self.field_type(wcx, t, f) { + t = ft; + continue; + } + // Rust `worker: Arc`: the value behind the smart pointer, though the pointer's + // own methods (`clone`) stay the pointer's + if self.family == LangFamily::Rust { + if let Some(v) = self.field_value(wcx, t, f, 0) { + match v.method_receiver(if i + 1 == path.len() { method } else { "" }) { + Some(ft) => t = ft, + None => return Outcome::st(status::NO_CANDIDATE), + } + continue; + } + } + match self.e.direct_member(t, f) { // `Outer.Inner.m()` / `Mod::Klass.m`: a nested type rather than a field - None => match self.e.direct_member(t, f) { - Some(inner) if self.holds_members(self.view.node(inner).kind()) => t = inner, - // `NotificationCenter.default.postNotification(..)`: a member of an outside type - // the project only extends, its type unknown - // ponytail: guessed to be that type (`.default`, `.shared`); a wrong guess finds no method - _ if self.is_swift_extension(t) && self.swift_declaration(t).is_none() => {} - _ => return Outcome::st(status::NO_CANDIDATE), - }, + Some(inner) if self.holds_members(self.view.node(inner).kind()) => t = inner, + // `NotificationCenter.default.postNotification(..)`: a member of an outside type + // the project only extends, its type unknown + // ponytail: guessed to be that type (`.default`, `.shared`); a wrong guess finds no method + _ if self.is_swift_extension(t) && self.swift_declaration(t).is_none() => {} + _ => return Outcome::st(status::NO_CANDIDATE), } } t = self.cpp_unalias(wcx, t); @@ -3248,7 +3279,7 @@ impl<'e, 'v> FileCx<'e, 'v> { Shape::This { kw, fields, method } if self.family == LangFamily::Js && kw != "this" => self.call_member(wcx, r, kw, &fields, method, args), Shape::This { kw, fields, method } => self.call_this(wcx, r, kw, &fields, method, args), Shape::Super { method } => self.call_super(wcx, r, method, args), - Shape::New { class, method } => match self.lookup_type(wcx, class) { + Shape::New { class, method } => match self.lookup_type_at(wcx, class, r.line) { TypeRes::Node(t) => self.member_call(wcx, t, &[], method, 90, args.map(|a| (a, r))), TypeRes::External => Outcome::st(status::EXTERNAL), _ => match self.es5_constructor(wcx, class).filter(|_| self.family == LangFamily::Js) { diff --git a/crates/cg-resolve/tests/resolve_e2e.rs b/crates/cg-resolve/tests/resolve_e2e.rs index 09caddc..8d2bdfb 100644 --- a/crates/cg-resolve/tests/resolve_e2e.rs +++ b/crates/cg-resolve/tests/resolve_e2e.rs @@ -1240,3 +1240,78 @@ fn cpp_receivers_constructors_and_overrides() { assert!(has(&edges, "lib.hpp::holder::len -calls-> lib.hpp::detail::buf::size"), "a field receiver: {edges:?}"); assert!(has(&edges, "lib.hpp::base::run -calls-> lib.hpp::impl::run"), "a virtual call's hop: {edges:?}"); } + +#[test] +fn javascript_local_class_shadows_a_same_named_outer_class() { + let src = "class Factory {\n run() { return \"outer\"; }\n}\n\nexport function top() {\n const value = new Factory();\n return value.run();\n}\n\nexport function nested() {\n class Factory {\n run() { return \"inner\"; }\n }\n const value = new Factory();\n new Factory().run();\n return value.run();\n}\n"; + for file in ["app.ts", "app.js"] { + let (_d, view) = project(&[(file, src)]); + let edges = edges_of(&view); + for want in [ + "top -calls-> {f}::Factory::run", + "top -instantiates-> {f}::Factory", + "nested -calls-> {f}::nested::Factory::run", + "nested -instantiates-> {f}::nested::Factory", + ] { + assert!(has(&edges, &format!("{file}::{}", want.replace("{f}", file))), "{want}: {edges:#?}"); + } + assert!(!has(&edges, &format!("{file}::nested -calls-> {file}::Factory::run")), "{edges:#?}"); + assert!(!has(&edges, &format!("{file}::nested -instantiates-> {file}::Factory")), "{edges:#?}"); + } +} + +#[test] +fn rust_field_calls_look_through_smart_pointers() { + for wrap in ["Arc", "Rc", "Box", ""] { + let field = if wrap.is_empty() { "Worker".to_owned() } else { format!("{wrap}") }; + let src = format!( + "use std::rc::Rc;\nuse std::sync::Arc;\npub struct Worker;\nimpl Worker {{ pub fn execute(&self) -> u8 {{ 7 }} pub fn clone(&self) -> u8 {{ 99 }} }}\npub struct Noise;\nimpl Noise {{ pub fn execute(&self) -> u8 {{ 0 }} }}\npub struct Owner {{ worker: {field} }}\nimpl Owner {{ pub fn run(&self) -> u8 {{ self.worker.execute() }} pub fn dup(&self) {{ self.worker.clone(); }} }}\n" + ); + let (_d, view) = project(&[("Cargo.toml", "[package]\nname = \"demo\"\n"), ("src/main.rs", &src)]); + let edges = edges_of(&view); + assert!(has(&edges, "src/main.rs::Owner::run -calls-> src/main.rs::Worker::execute"), "{wrap}: {edges:#?}"); + // `Arc::clone` / `Rc::clone` clone the pointer, not the value + let clones = has(&edges, "src/main.rs::Owner::dup -calls-> src/main.rs::Worker::clone"); + if wrap != "Box" { + assert!(clones == wrap.is_empty(), "{wrap}: {edges:#?}"); + } + } +} + +#[test] +fn python_members_follow_the_c3_method_resolution_order() { + let (_d, view) = project(&[ + ("swap.py", "class Left:\n def run(self):\n return 'left'\n\nclass Right:\n def run(self):\n return 'right'\n\nclass Derived(Right, Left):\n def invoke(self):\n return self.run()\n\ndef caller():\n value = Derived()\n return value.run()\n"), + ("deep.py", "class Root:\n def run(self):\n return 'root'\n\nclass Right:\n def run(self):\n return 'right'\n\nclass Left(Root):\n pass\n\nclass Derived(Left, Right):\n def invoke(self):\n return self.run()\n"), + // C3, not depth-first: D -> B -> C -> A + ("diamond.py", "class A:\n def run(self):\n return 'a'\n\nclass B(A):\n pass\n\nclass C(A):\n def run(self):\n return 'c'\n\nclass D(B, C):\n def invoke(self):\n return self.run()\n"), + ]); + let edges = edges_of(&view); + assert!(has(&edges, "swap.py::Derived::invoke -calls-> swap.py::Right::run"), "{edges:#?}"); + assert!(has(&edges, "swap.py::caller -calls-> swap.py::Right::run"), "{edges:#?}"); + assert!(has(&edges, "deep.py::Derived::invoke -calls-> deep.py::Root::run"), "{edges:#?}"); + assert!(has(&edges, "diamond.py::D::invoke -calls-> diamond.py::C::run"), "{edges:#?}"); +} + +#[test] +fn rust_inherent_method_shadows_a_trait_method() { + let (_d, view) = project(&[ + ("Cargo.toml", "[package]\nname = \"demo\"\n"), + ( + "src/main.rs", + "mod a_trait;\nmod z_inherent;\nmod same;\n\npub trait Action { fn run(&self) -> u8; }\npub struct Worker;\n\npub fn direct(w: &Worker) -> u8 { w.run() }\npub fn associated(w: &Worker) -> u8 { Worker::run(w) }\n", + ), + ("src/a_trait.rs", "use crate::{Action, Worker};\nimpl Action for Worker { fn run(&self) -> u8 { 1 } }\n"), + ("src/z_inherent.rs", "use crate::Worker;\nimpl Worker { pub fn run(&self) -> u8 { 2 } }\n"), + // both impls beside the type, the trait's first + ("src/same.rs", "use crate::Action;\npub struct Job;\nimpl Action for Job { fn run(&self) -> u8 { 1 } }\nimpl Job { pub fn run(&self) -> u8 { 2 } }\npub fn go(j: &Job) -> u8 { j.run() }\npub trait Speak { fn speak(&self); }\nimpl Speak for Job { fn speak(&self) {} }\npub fn talk(j: &Job) { j.speak() }\n"), + ]); + let edges = edges_of(&view); + assert!(has(&edges, "src/main.rs::direct -calls-> src/z_inherent.rs::Worker::run"), "{edges:#?}"); + assert!(has(&edges, "src/main.rs::associated -calls-> src/z_inherent.rs::Worker::run"), "{edges:#?}"); + assert!(!edges.iter().any(|e| e.ends_with("-calls-> src/a_trait.rs::Worker::run")), "{edges:#?}"); + let go: Vec<_> = keyed_edges(&view, &[cg_core::EdgeKind::Calls]).into_iter().filter(|e| e.starts_with("src/same.rs::go ")).collect(); + assert!(go.len() == 1 && go[0].contains("src/same.rs:4:"), "{go:#?}"); + // with no inherent method of the name, the trait's + assert!(has(&edges, "src/same.rs::talk -calls-> src/same.rs::Job::speak"), "{edges:#?}"); +} diff --git a/crates/cg-sync/src/sync.rs b/crates/cg-sync/src/sync.rs index 09d651a..0d8f7d8 100644 --- a/crates/cg-sync/src/sync.rs +++ b/crates/cg-sync/src/sync.rs @@ -303,7 +303,8 @@ fn resolver_fact_sig(view: &View, f: FileId) -> ResolverFactSig { || b.kind == BindKind::ReturnType as u8 || b.kind == BindKind::Fixture as u8 || b.kind == BindKind::TypeAlias as u8 - || b.kind == BindKind::FactoryMixin as u8) + || b.kind == BindKind::FactoryMixin as u8 + || b.kind == BindKind::TraitImpl as u8) && b.node.is_some()) || b.kind == BindKind::Deref as u8 || b.kind == BindKind::Mutation as u8 @@ -391,7 +392,8 @@ fn is_hierarchy_kind(kind: u8) -> bool { /// that share kind and name, so that inserting a line above a class does not look like a change /// of the class hierarchy (which would widen the re-resolution to every reference in the /// project), while a renamed class, a moved class or a changed parent still does. -fn hierarchy_keys(view: &View, pairs: impl Iterator) -> BTreeSet<(String, String)> { +/// Each supertype with its rank among the subtype's: reordering a class's bases changes lookup. +fn hierarchy_keys(view: &View, pairs: impl Iterator) -> BTreeSet<(String, usize, String)> { let mut ordinals: FxHashMap> = FxHashMap::default(); let mut key = |n: NodeIdx| -> String { let node = view.node(n); @@ -410,7 +412,15 @@ fn hierarchy_keys(view: &View, pairs: impl Iterator) let dead = if view.is_alive(n) { "" } else { "!dead" }; format!("{}#{}@{}~{}{dead}", view.file(file).path(), node.kind(), node.qname(), ord.get(&n.0).copied().unwrap_or(0)) }; - pairs.map(|(a, b)| (key(a), key(b))).collect() + let mut written: Vec<(String, (u32, u32), String)> = pairs.map(|(a, b, pos)| (key(a), pos, key(b))).collect(); + written.sort(); + let mut out = BTreeSet::new(); + let mut rank = 0; + for (i, (a, _, b)) in written.iter().enumerate() { + rank = if i > 0 && written[i - 1].0 == *a { rank + 1 } else { 0 }; + out.insert((a.clone(), rank, b.clone())); + } + out } pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOptions) -> Result { @@ -837,7 +847,7 @@ pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOp } } // hierarchy from all resolved extends / implements refs - let mut pairs: Vec<(NodeIdx, NodeIdx)> = Vec::new(); + let mut pairs: Vec<(NodeIdx, NodeIdx, (u32, u32))> = Vec::new(); for &f in &all_files { let range = view1.ref_range(f); for (i, r) in view1.refs_of_file(f).iter().enumerate() { @@ -850,7 +860,7 @@ pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOp None => (r.status, retarget.get(&id).copied().unwrap_or(r.target.0)), }; if st == status::RESOLVED { - pairs.push((r.from, NodeIdx(tg))); + pairs.push((r.from, NodeIdx(tg), (r.line, r.col))); } } } @@ -858,7 +868,7 @@ pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOp let mut v = Vec::new(); for r in base.p.refs.iter() { if is_hierarchy_kind(r.kind) && r.status == status::RESOLVED { - v.push((r.from, r.target)); + v.push((r.from, r.target, (r.line, r.col))); } } hierarchy_keys(&base_view, v.into_iter()) diff --git a/crates/cg-sync/tests/incremental_parity.rs b/crates/cg-sync/tests/incremental_parity.rs index 054b333..8bda8ab 100644 --- a/crates/cg-sync/tests/incremental_parity.rs +++ b/crates/cg-sync/tests/incremental_parity.rs @@ -128,6 +128,38 @@ const EDITS: &[(&str, &str, &str)] = &[ ("fwd/express.js", "exports.Router = createApplication;", "exports.Router = Router;"), ]; +/// Shadowing a sync must follow: each edit changes which declaration a call or `new` reaches. +const SHADOW_FILES: &[(&str, &str)] = &[ + ("Cargo.toml", "[package]\nname = \"shadow\"\n"), + ( + "src/main.rs", + "mod a_tr;\nmod z_imp;\npub trait Action { fn run(&self) -> u8; }\npub struct Worker;\npub struct Owner { worker: std::sync::Arc }\nimpl Owner { pub fn go(&self) -> u8 { self.worker.run() } }\npub fn direct(w: &Worker) -> u8 { w.run() }\n", + ), + ("src/a_tr.rs", "use crate::{Action, Worker};\nimpl Action for Worker { fn run(&self) -> u8 { 1 } }\n"), + ("src/z_imp.rs", "use crate::Worker;\nimpl Worker { pub fn run(&self) -> u8 { 2 } }\n"), + ("py/base.py", "class Left:\n def run(self):\n pass\n\nclass Right:\n def run(self):\n pass\n\nclass Derived(Right, Left):\n pass\n"), + ("py/use.py", "from .base import Derived\n\ndef caller():\n Derived().run()\n"), + ("js/app.ts", "class Factory {\n run() {}\n}\nexport function nested() {\n class Factory {\n run() {}\n }\n return new Factory().run();\n}\n"), +]; + +const SHADOW_EDITS: &[(&str, &str, &str)] = &[ + // an inherent `Worker::run` shadows the trait's + ("src/z_imp.rs", "pub fn run(&self)", "pub fn act(&self)"), + ("src/z_imp.rs", "pub fn act(&self)", "pub fn run(&self)"), + // the same method, at the same place, now inherent + ("src/a_tr.rs", "impl Action for Worker", "impl /* */Worker"), + ("src/a_tr.rs", "impl /* */Worker", "impl Action for Worker"), + // a field behind `Arc` and a plain one + ("src/main.rs", "worker: std::sync::Arc", "worker: Worker"), + ("src/main.rs", "worker: Worker", "worker: std::sync::Arc"), + // Python's method resolution order follows the bases as written + ("py/base.py", "class Derived(Right, Left):", "class Derived(Left, Right):"), + ("py/base.py", "class Derived(Left, Right):", "class Derived(Right, Left):"), + // a function's own class shadows the file's + ("js/app.ts", " class Factory {\n run() {}\n }\n", ""), + ("js/app.ts", "export function nested() {\n", "export function nested() {\n class Factory {\n run() {}\n }\n"), +]; + struct Rng(u64); impl Rng { fn next(&mut self) -> u64 { @@ -195,8 +227,12 @@ fn first_diff(a: &[String], b: &[String]) -> String { } fn setup() -> tempfile::TempDir { + setup_with(FILES) +} + +fn setup_with(files: &[(&str, &str)]) -> tempfile::TempDir { let dir = tempfile::tempdir().unwrap(); - for (i, (f, c)) in FILES.iter().enumerate() { + for (i, (f, c)) in files.iter().enumerate() { write(dir.path(), f, c, i as u64); } index_project(dir.path(), &opts()).unwrap(); @@ -229,9 +265,18 @@ fn fixture_draws_every_ported_link() { #[test] fn targeted_edits_match_full_rebuild() { - let dir = setup(); + edits_match_full_rebuild(FILES, EDITS); +} + +#[test] +fn shadowing_edits_match_full_rebuild() { + edits_match_full_rebuild(SHADOW_FILES, SHADOW_EDITS); +} + +fn edits_match_full_rebuild(files: &[(&str, &str)], edits: &[(&str, &str, &str)]) { + let dir = setup_with(files); let root = dir.path(); - for (step, (f, from, to)) in EDITS.iter().enumerate() { + for (step, (f, from, to)) in edits.iter().enumerate() { let cur = fs::read_to_string(root.join(f)).unwrap(); if !cur.contains(from) { continue;