Skip to content
Draft
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 = 11;
pub const ENGINE_VERSION: u32 = 12;
/// 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
33 changes: 33 additions & 0 deletions crates/cg-extract/src/facts/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) {
"lambda_expression" => lambda(cx, n, scope),
"method_invocation" => {
super::args::java_call(cx, n);
super_receiver(cx, n);
walk_children(cx, n, scope);
}
"local_variable_declaration" => {
Expand Down Expand Up @@ -148,8 +149,39 @@ fn import(cx: &mut Cx, n: Node, scope: u32) {
cx.add_import(fact);
}

/// The kernel names `super.m()` as a bare `m`, which would bind to the caller's own
/// override; restore the `super.` receiver so the call resolves in the ancestors.
fn super_receiver(cx: &mut Cx, n: Node) {
let Some(name) = n.child_by_field_name("name") else { return };
if !n.child_by_field_name("object").is_some_and(|o| o.kind() == "super") {
return;
}
let pos = cx.start(n);
let method = cx.text(name).to_owned();
cx.facts.call_names.push(CallNameFact {
line: (pos >> 32) as u32,
col: pos as u32,
tail: method.clone(),
kernel_name: method.clone(),
name: format!("super.{method}"),
});
}

/// `<T extends Foo, U>` of a class or method: `T` names no project declaration.
fn type_params(cx: &mut Cx, n: Node, scope: u32) {
let Some(tp) = n.child_by_field_name("type_parameters") else { return };
for p in kids(tp).filter(|p| p.kind() == "type_parameter") {
if let Some(nm) = kids(p).find(|c| matches!(c.kind(), "type_identifier" | "identifier")) {
let t = cx.text(nm).to_owned();
let line = cx.line(nm);
cx.add_binding(scope, &t, BindKind::TypeParam, "", "", line);
}
}
}

fn class(cx: &mut Cx, n: Node, scope: u32) {
let sc = cx.push_scope(ScopeKind::Class, scope, n);
type_params(cx, n, sc);
// record components `record R(Foo a)`
if let Some(params) = n.child_by_field_name("parameters") {
for p in kids(params) {
Expand Down Expand Up @@ -178,6 +210,7 @@ fn class(cx: &mut Cx, n: Node, scope: u32) {

fn function(cx: &mut Cx, n: Node, scope: u32) {
let sc = cx.push_scope(ScopeKind::Function, scope, n);
type_params(cx, n, sc);
if let Some(params) = n.child_by_field_name("parameters") {
for p in kids(params) {
match p.kind() {
Expand Down
5 changes: 3 additions & 2 deletions crates/cg-extract/src/facts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,8 +154,9 @@ pub enum BindKind {
/// C++ `#define BEGIN namespace fmt {`: `name` is a namespace opened only by a macro, which
/// no node of the index stands for. Metadata only; not a lexical binding.
MacroNamespace = 15,
/// 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.
/// A type parameter of the Rust fn, impl or trait, or the Java class or method, whose
/// scope holds it. Only a type reference sees it: `T` in `fn new(value: T)` names no
/// `struct T` (or Java enum constant `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.
Expand Down
31 changes: 27 additions & 4 deletions crates/cg-resolve/src/modules.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Module and package indexes (design doc 6.2 `ModuleIndex`) and import-specifier resolution
//! for every MVP language. Built once per run from the file list and the project manifests.

use crate::project::{normalize, parent_dir, ProjectInfo};
use crate::project::{normalize, parent_dir, path_affinity, ProjectInfo};
use cg_core::{FileId, Lang, LangFamily, NodeIdx, NONE};
use cg_extract::BindKind;
use cg_store::View;
Expand Down Expand Up @@ -323,7 +323,7 @@ impl<'v> Modules<'v> {
LangFamily::Js => self.resolve_js(spec, from_path, lang),
LangFamily::Python => self.resolve_python(spec, from_path),
LangFamily::Go => self.resolve_go(spec, from_path),
LangFamily::Jvm => self.resolve_jvm(spec),
LangFamily::Jvm => self.resolve_jvm(spec, from_path),
LangFamily::Rust => self.resolve_rust(spec, from),
LangFamily::C => self.resolve_c(spec, from_path),
LangFamily::CSharp => self.resolve_csharp(spec),
Expand Down Expand Up @@ -710,11 +710,13 @@ impl<'v> Modules<'v> {
}

// ---- Java / Kotlin
fn resolve_jvm(&self, spec: &str) -> ImportTarget {
fn resolve_jvm(&self, spec: &str, from_path: &str) -> ImportTarget {
let segs: Vec<&str> = spec.split('.').collect();
for i in (1..segs.len()).rev() {
let qname = format!("{}::{}", segs[..i].join("."), segs[i..].join("::"));
if let Some(n) = self.view.by_qname(&qname).next() {
// one class copied into several source roots: the importer's own root
let affinity = |n: &NodeIdx| (path_affinity(self.view.file(self.view.hot(*n).file).path(), from_path), std::cmp::Reverse(n.0));
if let Some(n) = self.view.by_qname(&qname).max_by_key(affinity) {
return ImportTarget::Node(n);
}
}
Expand Down Expand Up @@ -744,6 +746,25 @@ impl<'v> Modules<'v> {
/// The longest module prefix and the number of trailing declaration/member
/// segments. Returning the tail prevents `crate::api::Type` being mistaken
/// for the module itself when resolving a qualified type or return value.
/// `mod support { pub mod panic; }` in a crate root such as `tests/sync_panic.rs`:
/// the inline module's out-of-line children live under the root's own directory
/// (`tests/support/panic.rs`). Only existing files count, longest path first.
fn inline_mod_file(&self, from: FileId, modpath: &str, segs: &[&str]) -> Option<(ImportTarget, usize)> {
if !modpath.is_empty() {
return None;
}
let dir = dir_of(self.path_of(from));
for k in (1..=segs.len()).rev() {
let base = if dir.is_empty() { segs[..k].join("/") } else { format!("{dir}/{}", segs[..k].join("/")) };
for path in [format!("{base}.rs"), format!("{base}/mod.rs")] {
if let Some(f) = self.path_to_file.get(path.as_str()) {
return Some((ImportTarget::File(*f), segs.len() - k));
}
}
}
None
}

pub fn resolve_rust_path(&self, spec: &str, from: FileId) -> (ImportTarget, usize) {
let Some((crate_idx, modpath)) = self.rs_file_mod.get(&from) else { return (ImportTarget::External, 0) };
let segs: Vec<&str> = spec.split("::").filter(|s| !s.is_empty()).collect();
Expand Down Expand Up @@ -773,6 +794,8 @@ impl<'v> Modules<'v> {
let mut full: Vec<String> = modpath.split("::").filter(|s| !s.is_empty()).map(str::to_owned).collect();
full.extend(segs.iter().map(|s| s.to_string()));
(*crate_idx, full)
} else if let Some(found) = self.inline_mod_file(from, modpath, &segs) {
return found;
} else {
return (ImportTarget::External, 0);
}
Expand Down
10 changes: 10 additions & 0 deletions crates/cg-resolve/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,16 @@ pub fn normalize(p: &str) -> String {
out.join("/")
}

/// How close two project paths are: minus the number of directory steps between their
/// folders. Copies of one declaration in several source roots (`guava/src/...`,
/// `android/guava/src/...`) are told apart by it; `guava-tests/test` is nearer the first.
pub fn path_affinity(a: &str, b: &str) -> i64 {
let (a, b) = (parent_dir(a), parent_dir(b));
let depth = |p: &str| if p.is_empty() { 0 } else { p.split('/').count() };
let common = a.split('/').zip(b.split('/')).take_while(|(x, y)| x == y && !x.is_empty()).count();
-((depth(a) - common + depth(b) - common) as i64)
}

pub fn parent_dir(p: &str) -> &str {
p.rfind('/').map(|i| &p[..i]).unwrap_or("")
}
Expand Down
90 changes: 86 additions & 4 deletions crates/cg-resolve/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1766,6 +1766,14 @@ impl<'e, 'v> FileCx<'e, 'v> {
let pick = args.and_then(|(a, r)| self.pick_overload(wcx, &best, a, r)).unwrap_or(best[0]);
return NameRes::Found(pick, by::PACKAGE, 80);
}
// one declaration copied into several source roots; only JVM qualified
// names carry the package, so equal names elsewhere are distinct symbols
let q0 = self.view.node(best[0]).qname();
if self.family == LangFamily::Jvm && best.iter().all(|c| self.view.node(*c).qname() == q0) {
if let Some(n) = self.nearest_copy(&best) {
return NameRes::Found(n, by::PACKAGE, 80);
}
}
if self.lang == Lang::Cpp {
// a name written alone is no class's member while something else answers it
let view = self.view;
Expand All @@ -1784,6 +1792,16 @@ impl<'e, 'v> FileCx<'e, 'v> {
}
}

/// The candidate in this file's own source root: the only one nearest it by directory
/// distance.
fn nearest_copy(&self, cands: &[NodeIdx]) -> Option<NodeIdx> {
let affinity = |n: NodeIdx| crate::project::path_affinity(self.view.file(self.view.hot(n).file).path(), self.path);
let best = cands.iter().map(|n| affinity(*n)).max()?;
let mut top = cands.iter().copied().filter(|n| affinity(*n) == best);
let first = top.next()?;
top.next().is_none().then_some(first)
}

/// C / C++: a macro invocation is not a call (upstream keeps macros out of the call graph).
/// Ruby: a variable is never reached by a bare call from another scope.
/// A C++ `using X = ..` is also bound as a typed local: `X(1)` constructs, it calls no variable.
Expand Down Expand Up @@ -1980,6 +1998,14 @@ impl<'e, 'v> FileCx<'e, 'v> {

/// Swift `guard let request = request(for: task)`: the name a declaration binds is not in
/// scope in its own initializer, so the call there is to the method of that name.
/// `let rt = rt();` / `copy = self.copy()`: an initializer cannot see the local it
/// declares, so a call of that name on its line is not a call of the local. Only a local
/// initialised by a call of this very name, so `[f() for f in fs]` keeps its `f`.
fn self_init_call(&self, b: &BindingRec, name: &str, line: u32) -> bool {
let callee = self.view.text_str(b.init_ctor).split('(').next().unwrap_or("");
b.kind == BindKind::Local as u8 && b.line == line && callee.rsplit(['.', ':']).next() == Some(name)
}

fn swift_self_init(&self, b: &BindingRec, name: &str, line: u32) -> bool {
self.family == LangFamily::Swift && b.line == line && self.view.text_str(b.init_ctor).strip_suffix("()") == Some(name)
}
Expand Down Expand Up @@ -2045,7 +2071,13 @@ impl<'e, 'v> FileCx<'e, 'v> {
let e = self.e;
// 2. lexical scope
if accept != Accept::Type {
if let Some(b) = self.lookup_binding(name, scope, line, false).filter(|b| !self.is_cpp_alias(b, name) && !self.swift_self_init(b, name, line)) {
let mut found = self.lookup_binding(name, scope, line, false);
// the local this line declares is not yet in scope: an earlier binding (a callable
// parameter in `default = default()`) still is
if accept == Accept::Callable && found.is_some_and(|b| self.self_init_call(b, name, line)) {
found = self.lookup_binding(name, scope, line.saturating_sub(1), false);
}
if let Some(b) = found.filter(|b| !self.is_cpp_alias(b, name) && !self.swift_self_init(b, name, line)) {
if b.kind == BindKind::Alias as u8 {
return self.follow_alias(wcx, b, accept, from, allow_global, args);
}
Expand Down Expand Up @@ -2148,8 +2180,8 @@ impl<'e, 'v> FileCx<'e, 'v> {
if self.pkg.0 != NONE {
if let Some(list) = e.pkg_defs.get(self.pkg.0 as usize).and_then(|m| m.get(name)) {
let hits: SmallVec<[NodeIdx; 2]> = list.iter().copied().filter(|n| accept.ok(kind_of(view, *n)) && view.hot(*n).file != self.file).collect();
if let Some(n) = hits.first() {
return NameRes::Found(*n, by::PACKAGE, 85);
if let Some(n) = self.nearest_copy(&hits).or_else(|| hits.first().copied()) {
return NameRes::Found(n, by::PACKAGE, 85);
}
}
}
Expand Down Expand Up @@ -3620,6 +3652,13 @@ impl<'e, 'v> FileCx<'e, 'v> {
let name = self.view.text_str(r.name);
let Some(kind) = RefKind::from_wire(r.kind) else { return Outcome::st(status::NO_CANDIDATE) };
let o = self.resolve_kind(wcx, r, name, kind);
// a name-only guess for an untyped receiver is never the caller itself:
// `std.sync_all()` inside `File::sync_all` calls the std file's method (dynamic
// languages do recurse through untyped children: `child.relabel(..)`)
let typed = matches!(self.family, LangFamily::Rust | LangFamily::Jvm | LangFamily::Go);
if typed && kind == RefKind::Edge(EdgeKind::Calls) && o.status == status::RESOLVED && o.by == by::GLOBAL && o.target == r.from.0 {
return Outcome::st(status::NO_CANDIDATE);
}
if o.status == status::LOCAL_BINDING && matches!(kind, RefKind::Edge(EdgeKind::Calls | EdgeKind::Instantiates)) {
if let Some(n) = self.js_declared_value(r, name) {
return Outcome::found(n, by::LEXICAL, 90);
Expand All @@ -3637,6 +3676,37 @@ impl<'e, 'v> FileCx<'e, 'v> {
o
}

/// Whether `n` is declared inside `outer`.
fn within(&self, n: NodeIdx, outer: NodeIdx) -> bool {
let mut cur = self.view.node(n).parent();
while let Some(p) = cur {
if p == outer {
return true;
}
cur = self.view.node(p).parent();
}
false
}

/// A supertype named `name` that is not one of `decl`'s own members: an import, the
/// package, then another type of this file.
fn supertype_outside(&self, name: &str, decl: NodeIdx) -> Option<NodeIdx> {
let view = self.view;
let is_type = |n: NodeIdx| kind_of(view, n).is_type() && !self.within(n, decl) && n != decl;
if let Some(i) = self.visible_import(name, 0) {
if let Bound::Node(n) = self.e.bind_import(self.file, i as usize) {
return is_type(n).then_some(n);
}
}
if let Some(list) = self.e.pkg_defs.get(self.pkg.0 as usize).and_then(|m| m.get(name)) {
let hits: SmallVec<[NodeIdx; 2]> = list.iter().copied().filter(|n| is_type(*n)).collect();
if let Some(n) = self.nearest_copy(&hits).or_else(|| hits.first().copied()) {
return Some(n);
}
}
view.by_name(name).find(|n| view.hot(*n).file == self.file && is_type(*n))
}

fn resolve_kind(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str, kind: RefKind) -> Outcome {
match kind {
RefKind::Edge(EdgeKind::Imports) => self.resolve_import_ref(name, r),
Expand All @@ -3653,7 +3723,19 @@ impl<'e, 'v> FileCx<'e, 'v> {
o
}
}
RefKind::Edge(EdgeKind::Extends | EdgeKind::Implements | EdgeKind::TypeOf | EdgeKind::Returns) => self.resolve_type_ref(wcx, r, name, Accept::Type),
RefKind::Edge(EdgeKind::Extends | EdgeKind::Implements) => {
let o = self.resolve_type_ref(wcx, r, name, Accept::Type);
// `class C implements Builder` cannot mean C's own nested `Builder`:
// a supertype clause is outside the body that declares C's members
if o.status == status::RESOLVED && self.within(NodeIdx(o.target), r.from) {
return match self.supertype_outside(name, r.from) {
Some(n) => Outcome::found(n, by::PACKAGE, 85),
None => Outcome::st(status::NO_CANDIDATE),
};
}
o
}
RefKind::Edge(EdgeKind::TypeOf | EdgeKind::Returns) => self.resolve_type_ref(wcx, r, name, Accept::Type),
RefKind::Edge(EdgeKind::Instantiates) if self.swift_extended_builtin(name, r) => Outcome::st(status::BUILTIN),
// JS `new Class(..)` where `Class` is a parameter or a local: the value it holds, never a
// same-named class elsewhere (type lookup does not see lexical bindings)
Expand Down
Loading