diff --git a/crates/cg-core/src/brand.rs b/crates/cg-core/src/brand.rs index f9c1943..056dea2 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 = 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). diff --git a/crates/cg-extract/src/facts/java.rs b/crates/cg-extract/src/facts/java.rs index 4b3279c..a367b72 100644 --- a/crates/cg-extract/src/facts/java.rs +++ b/crates/cg-extract/src/facts/java.rs @@ -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" => { @@ -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}"), + }); +} + +/// `` 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) { @@ -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() { diff --git a/crates/cg-extract/src/facts/mod.rs b/crates/cg-extract/src/facts/mod.rs index 145a237..c439f1f 100644 --- a/crates/cg-extract/src/facts/mod.rs +++ b/crates/cg-extract/src/facts/mod.rs @@ -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. diff --git a/crates/cg-resolve/src/modules.rs b/crates/cg-resolve/src/modules.rs index 57f647f..494db03 100644 --- a/crates/cg-resolve/src/modules.rs +++ b/crates/cg-resolve/src/modules.rs @@ -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; @@ -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), @@ -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); } } @@ -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(); @@ -773,6 +794,8 @@ impl<'v> Modules<'v> { let mut full: Vec = 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); } diff --git a/crates/cg-resolve/src/project.rs b/crates/cg-resolve/src/project.rs index 3d5c8b5..e1cfc05 100644 --- a/crates/cg-resolve/src/project.rs +++ b/crates/cg-resolve/src/project.rs @@ -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("") } diff --git a/crates/cg-resolve/src/resolve.rs b/crates/cg-resolve/src/resolve.rs index b3f275a..ad9e0ef 100644 --- a/crates/cg-resolve/src/resolve.rs +++ b/crates/cg-resolve/src/resolve.rs @@ -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; @@ -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 { + 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. @@ -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) } @@ -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); } @@ -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); } } } @@ -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); @@ -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 { + 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), @@ -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) diff --git a/crates/cg-resolve/tests/resolve_e2e.rs b/crates/cg-resolve/tests/resolve_e2e.rs index 0c85210..c8b6521 100644 --- a/crates/cg-resolve/tests/resolve_e2e.rs +++ b/crates/cg-resolve/tests/resolve_e2e.rs @@ -1377,3 +1377,139 @@ fn rust_inherent_method_shadows_a_trait_method() { // with no inherent method of the name, the trait's assert!(has(&edges, "src/same.rs::talk -calls-> src/same.rs::Job::speak"), "{edges:#?}"); } + +/// `dump_edges` with the target's start line, which tells overloads apart. +fn edges_with_target_lines(view: &View) -> Vec { + let mut out = Vec::new(); + for f in view.files() { + for n in view.nodes_in_file(f) { + let n = cg_core::NodeIdx(n); + for e in view.out_edges(n) { + if e.kind != cg_core::EdgeKind::Contains { + out.push(format!("{} -{}-> {}:{}", dump::node_label(view, n), e.kind, dump::node_label(view, e.dst), view.node(e.dst).start_line())); + } + } + } + } + out.sort(); + out.dedup(); + out +} + +#[test] +fn java_overloads_fields_super_and_type_parameters() { + let (_d, view) = project(&[ + ( + "src/p/Box.java", + r#"package p; +public class Box { + private int size; + public int size() { return size; } + public void put(Object a) {} + public void put(Object a, Object b) {} + public void log() {} + public void log(String fmt, Object... args) {} + public T pick(T value) { return value; } + public int run() { put(1, 2); log("a", 1, 2); return size(); } +} +"#, + ), + ("src/p/E.java", "package p;\npublic enum E { T, U }\n"), + ("src/p/Base.java", "package p;\npublic class Base { public void close() {} }\n"), + ("src/p/Child.java", "package p;\npublic class Child extends Base {\n @Override public void close() { super.close(); }\n}\n"), + ("src/p/Builder.java", "package p;\npublic interface Builder { R build(); }\n"), + ( + "src/p/Maker.java", + "package p;\npublic class Maker implements Builder {\n public Integer build() { return 1; }\n public static class Builder {}\n}\n", + ), + ]); + let edges = edges_with_target_lines(&view); + for e in &edges { + eprintln!("{e}"); + } + let run = "src/p/Box.java::p::Box::run"; + assert!(has(&edges, &format!("{run} -calls-> src/p/Box.java::p::Box::put:6")), "two arguments pick the two-parameter overload"); + assert!(!has(&edges, &format!("{run} -calls-> src/p/Box.java::p::Box::put:5"))); + assert!(has(&edges, &format!("{run} -calls-> src/p/Box.java::p::Box::log:8")), "varargs overload"); + assert!(has(&edges, &format!("{run} -calls-> src/p/Box.java::p::Box::size:4")), "a call names the method, not the field"); + assert!(!has(&edges, &format!("{run} -calls-> src/p/Box.java::p::Box::size:3"))); + let close = "src/p/Child.java::p::Child::close"; + assert!(has(&edges, &format!("{close} -calls-> src/p/Base.java::p::Base::close:2")), "super.close() is the parent's"); + assert!(!has(&edges, &format!("{close} -calls-> {close}:3")), "super.close() is not recursion"); + assert!(!edges.iter().any(|e| e.contains("-references-> src/p/E.java::p::E::T")), "a type parameter is not the enum constant T"); + assert!( + has(&edges, "src/p/Maker.java::p::Maker -implements-> src/p/Builder.java::p::Builder:2"), + "a supertype clause cannot name the class's own member type" + ); +} + +#[test] +fn java_duplicate_source_roots_resolve_within_their_own_root() { + let closer = "package q;\npublic final class Closer {\n public static Closer create() { return new Closer(); }\n public void close() {}\n}\n"; + let user = "package q;\nclass User {\n void run() {\n Closer closer = Closer.create();\n closer.close();\n }\n}\n"; + let (_d, view) = project(&[ + ("lib/src/q/Closer.java", closer), + ("lib/src/q/User.java", user), + ("android/lib/src/q/Closer.java", closer), + ("android/lib/src/q/User.java", user), + ("lib-tests/test/q/UserTest.java", "package q;\nclass UserTest {\n void t() { Closer.create().close(); }\n}\n"), + ]); + let edges = dump::dump_edges(&view); + for e in &edges { + eprintln!("{e}"); + } + for root in ["lib", "android/lib"] { + let run = format!("{root}/src/q/User.java::q::User::run"); + assert!(has(&edges, &format!("{run} -calls-> {root}/src/q/Closer.java::q::Closer::create")), "{root}: same-package type in its own root"); + assert!(has(&edges, &format!("{run} -calls-> {root}/src/q/Closer.java::q::Closer::close")), "{root}: typed local"); + } + assert!( + has(&edges, "lib-tests/test/q/UserTest.java::q::UserTest::t -calls-> lib/src/q/Closer.java::q::Closer::create"), + "the nearest root by directory distance" + ); +} + +#[test] +fn rust_inline_test_modules_self_init_calls_and_guesses() { + let (_d, view) = project(&[ + ("Cargo.toml", "[package]\nname = \"demo\"\n"), + ("src/lib.rs", "macro_rules! cfg_net { ($($i:item)*) => { $($i)* } }\npub mod net;\npub mod app;\n"), + ( + "src/net.rs", + r#"cfg_net! { + pub struct Sock { fd: i32 } +} +impl Sock { + pub fn bind() -> Sock { Sock { fd: 0 } } + pub fn send(&self) { self.flush(); } + fn flush(&self) {} + pub fn sync_all(&self) {} +} +"#, + ), + ( + "src/app.rs", + r#"use crate::net::Sock; +fn rt() -> u32 { 1 } +pub fn run() { + let s = Sock::bind(); + let rt = rt(); +} +pub struct File { std: u32 } +impl File { + pub fn sync_all(&self) { let std = self.std; std.sync_all(); } +} +"#, + ), + ("tests/it.rs", "mod support {\n pub mod helper;\n}\nuse support::helper::assist;\n#[test]\nfn t() { assist(); }\n"), + ("tests/support/helper.rs", "pub fn assist() {}\n"), + ]); + let mut edges = dump::dump_edges(&view); + edges.dedup(); + for e in &edges { + eprintln!("{e}"); + } + assert!(has(&edges, "src/app.rs::run -calls-> src/app.rs::rt"), "`let rt = rt()` calls the function"); + assert!(!has(&edges, "src/app.rs::File::sync_all -calls-> src/app.rs::File::sync_all"), "a name-only guess is never the caller"); + assert!(has(&edges, "tests/it.rs::t -calls-> tests/support/helper.rs::assist"), "inline `mod support {{ pub mod helper; }}` in a test crate"); +}