Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cg-core/src/brand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
3 changes: 3 additions & 0 deletions crates/cg-extract/src/facts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
12 changes: 12 additions & 0 deletions crates/cg-extract/src/facts/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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(),
Expand Down
1 change: 1 addition & 0 deletions crates/cg-extract/src/facts/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down
16 changes: 7 additions & 9 deletions crates/cg-resolve/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Vec<_>>();
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);

Expand Down
102 changes: 90 additions & 12 deletions crates/cg-resolve/src/engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,12 @@ pub struct Engine<'v> {
pub stars: Vec<Vec<FileId>>,
/// Per package: top-level definitions by name.
pub pkg_defs: Vec<FxHashMap<&'v str, SmallVec<[NodeIdx; 2]>>>,
/// Direct supertypes (`extends` / `implements` resolved in round 1).
/// Direct supertypes (`extends` / `implements` resolved in round 1), in the order written.
pub supers: FxHashMap<NodeIdx, SmallVec<[NodeIdx; 3]>>,
/// Python classes' method resolution order (C3), past the class itself.
mro: FxHashMap<NodeIdx, Vec<NodeIdx>>,
/// Rust methods of `impl Trait for X` blocks: an inherent `X::m` shadows them.
trait_methods: rustc_hash::FxHashSet<NodeIdx>,
/// 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.
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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<NodeIdx> {
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();
Expand All @@ -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<Item = (NodeIdx, NodeIdx)>) {
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<Item = (NodeIdx, NodeIdx, (u32, u32))>) {
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<NodeIdx> = 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<NodeIdx, Option<Vec<NodeIdx>>>, depth: u8) -> Option<Vec<NodeIdx>> {
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();
Expand All @@ -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)));
}
}
}
Expand All @@ -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<NodeIdx> {
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
Expand Down
Loading
Loading