From 4a0271e461c5cca85c7cc75ce39cee8a5ccb275a Mon Sep 17 00:00:00 2001 From: NianJiuZst <3235467914@qq.com> Date: Sun, 1 Nov 2026 11:19:29 -0600 Subject: [PATCH] feat: close resolution gaps and port missing dynamic-dispatch passes Resolution, for the supported languages: - Go: embedded standard-library types join interface satisfaction (method table ported from upstream) and other outside embeds are opaque; a project embed the kernel writes without its package binds through the qualifier. - TS/JS: calls through `const x = y`, `export const alias = impl` and object-literal members reach the real function; ES5 constructors type their instances through prototype members and util.inherits / Object.create; `declare module 'x'` blocks in .d.ts files bind imports. - Rust: generic parameters resolve through trait bounds (an unbounded one never matches a same-named project type); impl Deref targets, type aliases and Mutex/RwLock guards are looked through; self.field chains are evaluated. - Python: branched factories are typed by their common base, self.x = self.make() types fields across methods, pytest fixture parameters take the fixture's return type. Synthesis passes: go-func-field, go-grpc-stub-impl, gin-middleware-chain, const-alias, redux-thunk, rtk-query, test-request (Spring MockMvc, WebTestClient, TestRestTemplate, RestAssured), kotlin-expect-actual, tauri-command and rn-native-module. All are recomputed whole on sync. Incremental sync re-resolves on Deref, type-alias and fixture changes (a changed conftest re-resolves the Python files below it). Engine rule version 7; segment format unchanged. Co-Authored-By: Claude Opus 5.5 --- README.md | 29 ++ README.zh-CN.md | 21 + crates/cg-core/src/brand.rs | 2 +- crates/cg-extract/src/facts/go.rs | 166 +++++++- crates/cg-extract/src/facts/js.rs | 102 ++++- crates/cg-extract/src/facts/mod.rs | 42 +- crates/cg-extract/src/facts/python.rs | 40 +- crates/cg-extract/src/facts/rust.rs | 163 +++++++- crates/cg-extract/src/facts/util.rs | 16 +- crates/cg-resolve/src/engine.rs | 82 +++- crates/cg-resolve/src/frameworks/mod.rs | 5 + crates/cg-resolve/src/modules.rs | 24 ++ crates/cg-resolve/src/resolve.rs | 413 +++++++++++++++++++- crates/cg-resolve/src/synth/bridges.rs | 212 ++++++++++ crates/cg-resolve/src/synth/dynamic.rs | 10 +- crates/cg-resolve/src/synth/gin.rs | 184 +++++++++ crates/cg-resolve/src/synth/go.rs | 244 +++++++++++- crates/cg-resolve/src/synth/go_fields.rs | 284 ++++++++++++++ crates/cg-resolve/src/synth/go_std.rs | 173 +++++++++ crates/cg-resolve/src/synth/js_alias.rs | 229 +++++++++++ crates/cg-resolve/src/synth/js_proto.rs | 60 +++ crates/cg-resolve/src/synth/jvm.rs | 269 +++++++++++++ crates/cg-resolve/src/synth/mod.rs | 37 ++ crates/cg-resolve/src/synth/redux.rs | 186 +++++++++ crates/cg-resolve/src/synth/sites.rs | 74 +++- crates/cg-resolve/tests/gap_closure.rs | 471 +++++++++++++++++++++++ crates/cg-sync/src/sync.rs | 33 +- crates/cg-sync/tests/gap_closure.rs | 119 ++++++ 28 files changed, 3597 insertions(+), 93 deletions(-) create mode 100644 crates/cg-resolve/src/synth/bridges.rs create mode 100644 crates/cg-resolve/src/synth/gin.rs create mode 100644 crates/cg-resolve/src/synth/go_fields.rs create mode 100644 crates/cg-resolve/src/synth/go_std.rs create mode 100644 crates/cg-resolve/src/synth/js_alias.rs create mode 100644 crates/cg-resolve/src/synth/js_proto.rs create mode 100644 crates/cg-resolve/src/synth/jvm.rs create mode 100644 crates/cg-resolve/src/synth/redux.rs create mode 100644 crates/cg-resolve/tests/gap_closure.rs create mode 100644 crates/cg-sync/tests/gap_closure.rs diff --git a/README.md b/README.md index 1d83d22..b0919ba 100644 --- a/README.md +++ b/README.md @@ -251,6 +251,35 @@ only arrays reached from a recognized router call participate in navigation matc route configuration remains outside this cross-file rule. Go interface dispatch includes unambiguous promoted methods; method-set inference remains a bounded static approximation. +Engine rule version 7 adds these resolution rules and synthesis passes, all bounded static rules +that link nothing when a name is ambiguous: + +- **Go**: embedded standard-library types take part in interface satisfaction (the `io.Reader`, + `*os.File`, `error`, ... method table is ported from upstream) and other outside embeds are + opaque; a project embedding the kernel writes without its package binds through the written + qualifier; func-field dispatch (`d.refreshf()` reaches the functions put in the field, through one + field-to-field copy); protoc `Unimplemented*Server` stubs reach hand-written servers; Gin's + `Context.Next` reaches registered middleware. +- **TS/JS**: calls through `const local = other` and `export const alias = realImpl` reach the + real function, as do object-literal members (`api.run()`); conditional / `??` / `||` arms of + module-level values (`const-alias`); ES5 constructors with `prototype` methods and + `util.inherits` / `Object.create` inheritance; ambient modules declared with `declare module 'x'` + in `.d.ts` files; Redux thunk `dispatch` chains and RTK Query generated hooks. +- **Rust**: generic parameters resolve through their trait bounds (an unbounded parameter no longer + matches a project type of the same name); `impl Deref` targets, `type X = Y` aliases and + `Mutex` / `RwLock` lock guards are looked through; `self.field` call chains are evaluated. +- **Python**: factories returning different classes in different branches are typed by the nearest + common base; `self.x = self.make()` types a field across methods; pytest fixture parameters (same + file or a `conftest.py` above) take the fixture's return type. +- **Cross-language / JVM**: Spring test requests (MockMvc, WebTestClient, TestRestTemplate, + RestAssured) reach routes; Kotlin Multiplatform `expect` reaches each platform's `actual`; a Tauri + frontend's `invoke('cmd')` reaches the registered `#[tauri::command]`; React Native + `NativeModules.X.m()` reaches the Java / Kotlin module's `@ReactMethod`. + +Not covered: MyBatis XML, Angular templates, Vue / Svelte ecosystem stores and routers, React Native +events and the Fabric / Expo native side, Tauri events, GoFrame, and Python parameter-type flow +(upstream implements it for Ruby only). + C/C++, C#, PHP, Ruby, Swift and the other upstream language families are outside this version's supported extraction set. Vue/Svelte-specific extraction is also outside the current scope. diff --git a/README.zh-CN.md b/README.zh-CN.md index c6c9c90..ac6ef40 100644 --- a/README.zh-CN.md +++ b/README.zh-CN.md @@ -222,6 +222,27 @@ React Router 支持通过导入和转导出使用其他文件中的导出路由 动态路由配置不在此跨文件规则的覆盖范围内。 Go 接口分派包含无歧义的提升方法;方法集推断仍是有界的静态近似。 +引擎规则版本 7 补充了以下解析规则与合成边(均为有界的静态规则,歧义时不连): + +- **Go**:嵌入的标准库类型参与接口判定(`io.Reader`、`*os.File`、`error` 等方法表移植自上游), + 其他外部嵌入类型按"不透明"处理;按书写的包限定绑定内核丢掉限定的项目内嵌入; + func 字段分派(`d.refreshf()` 连到写入该字段的函数,含一层字段间复制); + protoc 生成的 `Unimplemented*Server` 连到手写实现;Gin `Context.Next` 连到注册的中间件。 +- **TS/JS**:`const local = other` 与 `export const alias = realImpl` 的调用转发到真实函数, + 对象字面量成员(`api.run()`)同样转发;模块级常量的条件 / `??` / `||` 各分支(`const-alias`); + ES5 构造函数及其 `prototype` 方法、`util.inherits` / `Object.create` 继承; + `.d.ts` 中 `declare module 'x'` 声明的环境模块;Redux thunk 的 `dispatch` 链与 RTK Query 生成的 hook。 +- **Rust**:泛型参数按 trait 约束解析(无约束的类型参数不再误连同名项目类型); + `impl Deref` 目标类型、`type X = Y` 别名、`Mutex` / `RwLock` 的锁 guard 自动解引用;`self.字段` 调用链求值。 +- **Python**:分支返回不同类的工厂函数按最近公共基类类型化;`self.x = self.make()` 跨方法字段流; + pytest fixture 参数(同文件或上级目录 `conftest.py`)取 fixture 的返回类型。 +- **跨语言 / JVM**:Spring 测试请求(MockMvc、WebTestClient、TestRestTemplate、RestAssured)连到路由; + Kotlin Multiplatform `expect` 连到各平台 `actual`;Tauri 前端 `invoke('cmd')` 连到已注册的 `#[tauri::command]`; + React Native `NativeModules.X.m()` 连到 Java / Kotlin 模块的 `@ReactMethod`。 + +尚未覆盖:MyBatis XML、Angular 模板、Vue / Svelte 生态的 store 与路由、React Native 事件与 Fabric/Expo 原生侧、 +Tauri 事件、GoFrame,以及 Python 参数类型流(上游仅对 Ruby 实现)。 + 当前版本暂不支持 C/C++、C#、PHP、Ruby、Swift 等上游语言,也不包含 Vue/Svelte 专用提取逻辑。 ## 命令行参考 diff --git a/crates/cg-core/src/brand.rs b/crates/cg-core/src/brand.rs index ea99859..e6690aa 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 = 6; +pub const ENGINE_VERSION: u32 = 7; /// 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/go.rs b/crates/cg-extract/src/facts/go.rs index 853fd9a..e3b1330 100644 --- a/crates/cg-extract/src/facts/go.rs +++ b/crates/cg-extract/src/facts/go.rs @@ -48,9 +48,17 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) { // `type A = B` names another type; a defined `type A B` is a type of its own if let Some(name) = n.child_by_field_name("name") { let name = cx.text(name).to_owned(); - cx.facts.go_types.push(GoTypeFact { name, alias: true, type_params: Vec::new() }); + cx.facts.go_types.push(GoTypeFact { name, alias: true, type_params: Vec::new(), func_type: false }); } } + "composite_literal" => { + field_literal(cx, n); + walk_children(cx, n, scope); + } + "assignment_statement" => { + field_assignment(cx, n); + walk_children(cx, n, scope); + } "short_var_declaration" => { let (l, r) = (n.child_by_field_name("left"), n.child_by_field_name("right")); if let (Some(l), Some(r)) = (l, r) { @@ -193,15 +201,35 @@ fn function(cx: &mut Cx, n: Node, scope: u32) { } fn type_spec(cx: &mut Cx, n: Node, scope: u32) { - if let (Some(name), Some(tp)) = (n.child_by_field_name("name"), n.child_by_field_name("type_parameters")) { - let params: Vec = kids(tp) - .filter(|d| d.kind() == "type_parameter_declaration") - .flat_map(|d| kids(d).filter(|c| c.kind() == "identifier").map(|c| cx.text(c).to_owned()).collect::>()) - .collect(); - let name = cx.text(name).to_owned(); - cx.facts.go_types.push(GoTypeFact { name, alias: false, type_params: params }); + let ty = n.child_by_field_name("type"); + let func_type = ty.is_some_and(|t| t.kind() == "function_type"); + if let Some(name) = n.child_by_field_name("name") { + let params: Vec = n + .child_by_field_name("type_parameters") + .map(|tp| { + kids(tp) + .filter(|d| d.kind() == "type_parameter_declaration") + .flat_map(|d| kids(d).filter(|c| c.kind() == "identifier").map(|c| cx.text(c).to_owned()).collect::>()) + .collect() + }) + .unwrap_or_default(); + if !params.is_empty() || func_type { + let name = cx.text(name).to_owned(); + cx.facts.go_types.push(GoTypeFact { name, alias: false, type_params: params, func_type }); + } + } + let Some(ty) = ty else { return }; + let row = cx.row_at(cx.start(n)); + if ty.kind() == "interface_type" { + // embedded interfaces (`io.Reader`, `error`); a union or `~T` constrains, it embeds nothing + for elem in kids(ty).filter(|c| c.kind() == "type_elem") { + let types: Vec = kids(elem).filter(|c| c.is_named()).collect(); + if let ([t], Some(row)) = (types.as_slice(), row) { + embed(cx, row, *t); + } + } + return; } - let Some(ty) = n.child_by_field_name("type") else { return }; if ty.kind() != "struct_type" { return; } @@ -219,11 +247,131 @@ fn type_spec(cx: &mut Cx, n: Node, scope: u32) { // embedded field: promoted fields / methods; named after its type let simple = tname.rsplit('.').next().unwrap_or("").to_owned(); cx.add_binding(sc, &simple, BindKind::Field, &tname, "embedded", line); + if let (Some(t), Some(row)) = (ftype, row) { + embed(cx, row, t); + } } else { + let func = ftype.is_some_and(|t| t.kind() == "function_type"); for nm in names { let t = cx.text(nm).to_owned(); cx.add_binding(sc, &t, BindKind::Field, &tname, "", line); + if let (true, Some(row)) = (func, row) { + cx.facts.go_fields.push(GoFieldFact::FuncField { row, field: t }); + } + } + } + } +} + +/// Record an embedded type: `io.Reader`, `*os.File`, `Base`, `error`. +fn embed(cx: &mut Cx, row: u32, t: Node) { + let t = match t.kind() { + "pointer_type" | "parenthesized_type" => match t.named_child(0) { + Some(inner) => inner, + None => return, + }, + "generic_type" => match t.child_by_field_name("type") { + Some(inner) => inner, + None => return, + }, + _ => t, + }; + let (qualifier, name) = match t.kind() { + "type_identifier" => (String::new(), cx.text(t).to_owned()), + "qualified_type" => match (t.child_by_field_name("package"), t.child_by_field_name("name")) { + (Some(p), Some(nm)) => (cx.text(p).to_owned(), cx.text(nm).to_owned()), + _ => return, + }, + _ => return, + }; + cx.facts.go_embeds.push(GoEmbedFact { row, qualifier, name }); +} + +/// A value a struct field can be given: a name (`h`, `d.refresh`, `pkg.Handle`, `o.opts.F`) or +/// a conversion of one (`HandlerFunc(h.serve)`, kept as written for the linker to check). +fn field_value(cx: &Cx, v: Node) -> Option { + let v = if v.kind() == "literal_element" { v.named_child(0)? } else { v }; + match v.kind() { + "identifier" => Some(cx.text(v).to_owned()), + "selector_expression" => { + let text = cx.text(v); + let plain = text.split('.').all(|s| { + let s = s.trim(); + s.chars().next().is_some_and(|c| c.is_alphabetic() || c == '_') && s.chars().all(|c| c.is_alphanumeric() || c == '_') + }); + (plain && text.split('.').count() <= 3).then(|| text.split_whitespace().collect()) + } + "parenthesized_expression" => field_value(cx, v.named_child(0)?), + "call_expression" => { + let callee = v.child_by_field_name("function")?; + let args: Vec = kids(v.child_by_field_name("arguments")?).filter(|a| a.is_named()).collect(); + if !matches!(callee.kind(), "identifier" | "selector_expression") || args.len() != 1 { + return None; + } + let inner = field_value(cx, args[0])?; + Some(format!("{}({inner})", cx.text(callee))) + } + _ => None, + } +} + +/// `T{F: v}` / `&T{a, v}`: the values a composite literal puts in the fields of `T`. +fn field_literal(cx: &mut Cx, n: Node) { + let Some(ty) = n.child_by_field_name("type") else { return }; + let ty_text = match ty.kind() { + "type_identifier" | "qualified_type" => cx.text(ty).to_owned(), + "generic_type" => match ty.child_by_field_name("type") { + Some(t) => cx.text(t).to_owned(), + None => return, + }, + _ => return, + }; + let Some(body) = n.child_by_field_name("body") else { return }; + for (i, el) in kids(body).filter(|c| c.is_named() && c.kind() != "comment").enumerate() { + let (field, value) = match el.kind() { + "keyed_element" => { + let key = el.child_by_field_name("key").and_then(|k| k.named_child(0)); + let Some(key) = key.filter(|k| matches!(k.kind(), "identifier" | "field_identifier")) else { continue }; + let Some(value) = el.child_by_field_name("value") else { continue }; + (cx.text(key).to_owned(), value) } + "literal_element" => (format!("#{i}"), el), + _ => continue, + }; + let pos = cx.start(value); + if let Some(value) = field_value(cx, value) { + cx.facts.go_fields.push(GoFieldFact::Literal { ty: ty_text.clone(), field, value, line: (pos >> 32) as u32, col: pos as u32 }); + } + } +} + +/// `x.F = v` / `x.y.F = v`: a value put in field `F` of what `x` (or `x.y`) holds. +fn field_assignment(cx: &mut Cx, n: Node) { + if n.child_by_field_name("operator").map(|o| cx.text(o)) != Some("=") { + return; + } + let (Some(left), Some(right)) = (n.child_by_field_name("left"), n.child_by_field_name("right")) else { return }; + let lefts: Vec = kids(left).filter(|c| c.is_named()).collect(); + let rights: Vec = kids(right).filter(|c| c.is_named()).collect(); + if lefts.len() != rights.len() { + return; + } + for (l, r) in lefts.into_iter().zip(rights) { + if l.kind() != "selector_expression" { + continue; + } + let (Some(operand), Some(field)) = (l.child_by_field_name("operand"), l.child_by_field_name("field")) else { continue }; + let receiver = match operand.kind() { + "identifier" => cx.text(operand).to_owned(), + "selector_expression" => match (operand.child_by_field_name("operand"), operand.child_by_field_name("field")) { + (Some(o), Some(f)) if o.kind() == "identifier" => format!("{}.{}", cx.text(o), cx.text(f)), + _ => continue, + }, + _ => continue, + }; + if let Some(value) = field_value(cx, r) { + let pos = cx.start(r); + cx.facts.go_fields.push(GoFieldFact::Assign { receiver, field: cx.text(field).to_owned(), value, line: (pos >> 32) as u32, col: pos as u32 }); } } } diff --git a/crates/cg-extract/src/facts/js.rs b/crates/cg-extract/src/facts/js.rs index b84f524..f020b33 100644 --- a/crates/cg-extract/src/facts/js.rs +++ b/crates/cg-extract/src/facts/js.rs @@ -4,8 +4,32 @@ use super::util::*; use super::*; +/// Export-fact name marking a `declare module 'name' { ... }` block (`from_spec` is the module's +/// name, `from_name` `script` when the file has no top-level import or export, so that the block +/// declares the module rather than augmenting one). The leading NUL keeps it out of real names. +pub const AMBIENT_MODULE: &str = "\0ambient"; + pub fn extract(cx: &mut Cx, root: Node) { walk(cx, root, 0); + let script = !kids(root).any(|c| matches!(c.kind(), "import_statement" | "export_statement" | "import_alias")); + for decl in kids(root).filter(|c| c.kind() == "ambient_declaration") { + let Some(module) = kids(decl).find(|c| c.kind() == "module") else { continue }; + let (Some(name), Some(_body)) = (module.child_by_field_name("name"), module.child_by_field_name("body")) else { continue }; + if name.kind() != "string" { + continue; + } + let spec = unquote(cx.text(name)).to_owned(); + if spec.is_empty() || spec.contains('*') { + continue; // a wildcard pattern names no specifier exactly + } + cx.facts.exports.push(ExportFact { + name: AMBIENT_MODULE.into(), + node_row: NONE, + from_spec: spec, + from_name: if script { "script" } else { "module" }.into(), + star: false, + }); + } } fn walk_children(cx: &mut Cx, n: Node, scope: u32) { @@ -370,19 +394,71 @@ fn commonjs_value(cx: &mut Cx, name: &str, value: Node) { cx.facts.exports.push(ExportFact { name: name.to_owned(), node_row, from_spec, from_name, star: false }); } +/// `X.prototype` -> `X`. +fn prototype_of<'a>(cx: &Cx<'a>, n: Node) -> Option<&'a str> { + if n.kind() != "member_expression" || n.child_by_field_name("property").map(|p| cx.text(p)) != Some("prototype") { + return None; + } + let object = n.child_by_field_name("object").filter(|o| o.kind() == "identifier")?; + Some(cx.text(object)) +} + +/// The ES5 constructor an object literal's members are given to: `X.prototype = { ... }` or +/// `Object.assign(X.prototype, { ... })`. +fn prototype_object_owner<'a>(cx: &Cx<'a>, object: Node) -> Option<&'a str> { + let parent = object.parent()?; + match parent.kind() { + "assignment_expression" if parent.child_by_field_name("right") == Some(object) => prototype_of(cx, parent.child_by_field_name("left")?), + "arguments" => { + let call = parent.parent().filter(|c| c.kind() == "call_expression")?; + if call.child_by_field_name("function").map(|f| cx.text(f)) != Some("Object.assign") { + return None; + } + prototype_of(cx, parent.named_child(0)?) + } + _ => None, + } +} + +/// The function declaration a node sits in, when its name reads as a constructor's (`Foo`). +fn enclosing_constructor<'a>(cx: &Cx<'a>, n: Node) -> Option<&'a str> { + let mut cur = n.parent(); + while let Some(p) = cur { + if matches!(p.kind(), "function_declaration" | "function_expression" | "function" | "arrow_function" | "method_definition" | "class_body") { + let name = (p.kind() == "function_declaration").then(|| p.child_by_field_name("name")).flatten().map(|nm| cx.text(nm))?; + return name.starts_with(|c: char| c.is_ascii_uppercase()).then_some(name); + } + cur = p.parent(); + } + None +} + fn function(cx: &mut Cx, n: Node, scope: u32) { // The kernel labels `obj.method = function method() {}` as a Function. Keep // evidence of the member assignment so an unknown receiver cannot match an - // unrelated, plain function with the same name. + // unrelated, plain function with the same name. A member given to an ES5 + // constructor's prototype (`X.prototype.m = ...`, `this.m = ...` in `function X`) + // records the constructor's name. if let Some(parent) = n.parent() { - let member = match parent.kind() { - "object" if n.kind() == "method_definition" => n.child_by_field_name("name"), - "assignment_expression" if parent.child_by_field_name("right") == Some(n) => parent - .child_by_field_name("left") - .filter(|left| matches!(left.kind(), "member_expression" | "subscript_expression")) - .and_then(|left| left.child_by_field_name("property").or_else(|| left.child_by_field_name("index").filter(|index| index.kind() == "string"))), - "pair" if parent.child_by_field_name("value") == Some(n) => parent.child_by_field_name("key"), - _ => None, + let (member, owner) = match parent.kind() { + "object" if n.kind() == "method_definition" => (n.child_by_field_name("name"), prototype_object_owner(cx, parent)), + "assignment_expression" if parent.child_by_field_name("right") == Some(n) => { + let left = parent.child_by_field_name("left").filter(|left| matches!(left.kind(), "member_expression" | "subscript_expression")); + let member = left.and_then(|left| { + left.child_by_field_name("property").or_else(|| left.child_by_field_name("index").filter(|index| index.kind() == "string")) + }); + let object = left.and_then(|left| left.child_by_field_name("object")); + let owner = match object { + Some(o) if o.kind() == "this" => enclosing_constructor(cx, parent), + Some(o) => prototype_of(cx, o), + None => None, + }; + (member, owner) + } + "pair" if parent.child_by_field_name("value") == Some(n) => { + (parent.child_by_field_name("key"), parent.parent().and_then(|o| prototype_object_owner(cx, o))) + } + _ => (None, None), }; if let Some(member) = member { if let Some(row) = cx.row_at(cx.start(n)) { @@ -390,7 +466,7 @@ fn function(cx: &mut Cx, n: Node, scope: u32) { scope, name: cx.text(member).trim_matches(['\'', '"']).to_owned(), kind: BindKind::MemberFn, - decl_type: String::new(), + decl_type: owner.unwrap_or_default().to_owned(), init_ctor: String::new(), line: cx.line(n), node_row: row, @@ -529,10 +605,16 @@ fn var_decl(cx: &mut Cx, n: Node, scope: u32) { continue; } let is_fn = matches!(v.kind(), "arrow_function" | "function_expression" | "function" | "generator_function"); + let constant = kids(n).next().is_some_and(|k| k.kind() == "const"); if is_fn && name.kind() == "identifier" { let nm = cx.text(name).to_owned(); let line = cx.line(d); cx.add_binding(scope, &nm, BindKind::LocalFn, "", "", line); + } else if constant && name.kind() == "identifier" && v.kind() == "identifier" && ty.is_empty() && cx.text(v) != cx.text(name) { + // `const local = other`: the name is the other name + let (nm, other) = (cx.text(name).to_owned(), cx.text(v).to_owned()); + let line = cx.line(d); + cx.add_binding(scope, &nm, BindKind::Alias, "", &other, line); } else { let (vt, init) = value_info(cx, v); let dt = if ty.is_empty() { vt } else { ty }; diff --git a/crates/cg-extract/src/facts/mod.rs b/crates/cg-extract/src/facts/mod.rs index f8f974b..8d6707c 100644 --- a/crates/cg-extract/src/facts/mod.rs +++ b/crates/cg-extract/src/facts/mod.rs @@ -9,7 +9,7 @@ use tree_sitter::{Node, Tree}; pub mod args; mod go; mod java; -mod js; +pub mod js; mod kotlin; mod python; mod rust; @@ -112,6 +112,18 @@ pub enum BindKind { /// Return annotation or a bounded inferred return expression, keyed by the /// function's node. Metadata only; not a lexical binding. ReturnType = 6, + /// `const local = other`: a name bound to nothing but another name (`init_ctor`). A call + /// through it is a call of what the other name is. + Alias = 7, + /// Rust `impl Deref for X { type Target = Y; }`: `name` is the type as the impl writes it, + /// `decl_type` the target. Metadata only; not a lexical binding. + Deref = 8, + /// A pytest fixture, keyed by the function's node: what it returns, as `ReturnType`. + /// Metadata only; not a lexical binding. + Fixture = 9, + /// Rust `type X = Y;` outside an impl, keyed by the alias's node: `decl_type` is `Y`. + /// Metadata only; not a lexical binding. + TypeAlias = 10, } #[derive(Clone, Debug, PartialEq, Eq)] @@ -146,6 +158,10 @@ pub struct Facts { /// Go type declarations whose names stand for other types: true aliases and the type /// parameters of generic types (structural interface matching leaves them open). pub go_types: Vec, + /// Go types a struct or interface embeds, as written (`io.Reader`, `*os.File`, `error`). + pub go_embeds: Vec, + /// Go struct fields of a func type, and the values code puts in struct fields. + pub go_fields: Vec, /// A supplementary parse exhausted its deadline. The pipeline exposes this /// as an incomplete file rather than silently advertising complete facts. pub timed_out: bool, @@ -166,6 +182,30 @@ pub struct GoTypeFact { /// `type A = B` pub alias: bool, pub type_params: Vec, + /// `type HandlerFunc func(...)`: a value of the type is a function. + pub func_type: bool, +} + +/// One embedded type of a Go struct or interface. +#[derive(Clone, Debug, PartialEq, Eq)] +pub struct GoEmbedFact { + /// Kernel node row of the embedding type. + pub row: u32, + /// The package qualifier as written (`io` in `io.Reader`), empty for none. + pub qualifier: String, + /// The embedded type's name. + pub name: String, +} + +#[derive(Clone, Debug, PartialEq, Eq)] +pub enum GoFieldFact { + /// A struct field declared with a literal func type (`refreshf func(ctx) error`). + FuncField { row: u32, field: String }, + /// A value put in a field by a composite literal: `T{F: v}`, or positional `T{a, v}` + /// (`field` is then `#index`). + Literal { ty: String, field: String, value: String, line: u32, col: u32 }, + /// An assignment `x.F = v` / `x.y.F = v`: `receiver` is `x` / `x.y`. + Assign { receiver: String, field: String, value: String, line: u32, col: u32 }, } /// The arguments of one call, keyed like the kernel's call reference: its position and the diff --git a/crates/cg-extract/src/facts/python.rs b/crates/cg-extract/src/facts/python.rs index 8677686..6c01a25 100644 --- a/crates/cg-extract/src/facts/python.rs +++ b/crates/cg-extract/src/facts/python.rs @@ -217,13 +217,29 @@ fn function(cx: &mut Cx, n: Node, scope: u32) { if let Some(body) = n.child_by_field_name("body") { walk(cx, body, sc); if let (Some(row), Some(name)) = (row, n.child_by_field_name("name")) { + let fixture = n + .parent() + .filter(|p| p.kind() == "decorated_definition") + .is_some_and(|p| kids(p).any(|d| d.kind() == "decorator" && cx.text(d).contains("fixture"))); let (decl_type, init_ctor) = if let Some(ret) = n.child_by_field_name("return_type") { let raw = cx.text(ret); let ty = py_type_name(raw); (if ty.is_empty() { raw.to_owned() } else { ty }, String::new()) } else { - inferred_return(cx, n, body).unwrap_or_default() + inferred_return(cx, n, body, fixture).unwrap_or_default() }; + if fixture { + // pytest hands a test what the fixture returns: the decorator changes nothing there + cx.facts.bindings.push(BindingFact { + scope, + name: cx.text(name).to_owned(), + kind: BindKind::Fixture, + decl_type: decl_type.clone(), + init_ctor: init_ctor.clone(), + line: cx.line(n), + node_row: row, + }); + } if !decl_type.is_empty() || !init_ctor.is_empty() { cx.facts.bindings.push(BindingFact { scope: sc, @@ -242,11 +258,12 @@ fn function(cx: &mut Cx, n: Node, scope: u32) { } } -/// Infer only a uniform, explicit return expression. A final return rules out -/// implicit None; nested definitions, generators and arbitrary decorators must -/// not supply evidence for the enclosing function. -fn inferred_return(cx: &Cx, function: Node, body: Node) -> Option<(String, String)> { - if kids(function).any(|n| n.kind() == "async") || function.parent().is_some_and(|p| p.kind() == "decorated_definition") { +/// Infer only explicit return expressions, each a call. A final return rules out implicit +/// None; nested definitions, generators and arbitrary decorators must not supply evidence for +/// the enclosing function. Different calls in different branches (`return A()` / `return B()`) +/// are kept as `A()|B()`, for the resolver to type by their common base class. +fn inferred_return(cx: &Cx, function: Node, body: Node, fixture: bool) -> Option<(String, String)> { + if kids(function).any(|n| n.kind() == "async") || (!fixture && function.parent().is_some_and(|p| p.kind() == "decorated_definition")) { return None; } if kids(body).filter(|n| n.kind() != "comment").last()?.kind() != "return_statement" { @@ -271,10 +288,15 @@ fn inferred_return(cx: &Cx, function: Node, body: Node) -> Option<(String, Strin } _ => return None, }; - if result.as_ref().is_some_and(|previous| *previous != inferred) { - return None; + match result.as_mut() { + Some((_, previous)) if previous.split('|').any(|p| *p == inferred.1) => {} + Some((_, previous)) if previous.split('|').count() < 4 => { + previous.push('|'); + previous.push_str(&inferred.1); + } + Some(_) => return None, + None => *result = Some(inferred), } - *result = Some(inferred); return Some(()); } _ => {} diff --git a/crates/cg-extract/src/facts/rust.rs b/crates/cg-extract/src/facts/rust.rs index 61aefbc..abd3ed4 100644 --- a/crates/cg-extract/src/facts/rust.rs +++ b/crates/cg-extract/src/facts/rust.rs @@ -106,10 +106,41 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) { "function_item" | "function_signature_item" => function(cx, n, scope), "closure_expression" => closure(cx, n, scope), "impl_item" | "trait_item" => { + let generics = generics_of(cx, n); + cx.generics.push(generics); let sc = cx.push_scope(ScopeKind::Class, scope, n); + if n.kind() == "impl_item" { + deref_impl(cx, n, scope); + } walk_children(cx, n, sc); + cx.generics.pop(); + } + "type_item" if cx.facts.scopes[scope as usize].kind != ScopeKind::Class => { + // `type Outcome = plugin::Outcome`: a type whose members are the target's + let generics = generics_of(cx, n); + cx.generics.push(generics); + let target = n.child_by_field_name("type").map(|t| subst_generics(cx, &rust_type_name(cx, t))).unwrap_or_default(); + cx.generics.pop(); + if let (Some(name), Some(row), false) = (n.child_by_field_name("name"), cx.row_at(cx.start(n)), target.is_empty()) { + let name = cx.text(name).to_owned(); + let line = cx.line(n); + cx.facts.bindings.push(BindingFact { + scope, + name, + kind: BindKind::TypeAlias, + decl_type: target, + init_ctor: String::new(), + line, + node_row: row, + }); + } + } + "struct_item" => { + let generics = generics_of(cx, n); + cx.generics.push(generics); + struct_item(cx, n, scope); + cx.generics.pop(); } - "struct_item" => struct_item(cx, n, scope), "let_declaration" => { let_decl(cx, n, scope); walk_children(cx, n, scope); @@ -161,6 +192,120 @@ fn walk(cx: &mut Cx, n: Node, scope: u32) { } } +/// Bounds that add nothing a call goes through. +const MARKER_TRAITS: &[&str] = + &["Clone", "Copy", "Send", "Sync", "Sized", "Debug", "Default", "Eq", "PartialEq", "Hash", "Ord", "PartialOrd", "Display", "Unpin", "Any"]; + +/// The traits a `trait_bounds` node names, marker traits and lifetimes left out. +fn bound_names(cx: &Cx, b: Node) -> Vec { + kids(b) + .filter_map(|c| match c.kind() { + "type_identifier" | "scoped_type_identifier" => Some(cx.text(c)), + "generic_type" => c.child_by_field_name("type").map(|t| cx.text(t)), + _ => None, + }) + .filter(|b| !MARKER_TRAITS.contains(&b.rsplit("::").next().unwrap_or(b))) + .map(str::to_owned) + .collect() +} + +/// The type parameters a declaration introduces, with the bounds of its parameter list and its +/// `where` clause: `T: Svc + Clone` -> (`T`, `impl Svc`), an unbounded `U` -> (`U`, `_`). +fn generics_of(cx: &Cx, n: Node) -> Vec<(String, String)> { + let mut out: Vec<(String, Vec)> = Vec::new(); + if let Some(tp) = n.child_by_field_name("type_parameters") { + for p in kids(tp).filter(|c| c.kind() == "type_parameter") { + let Some(name) = p.child_by_field_name("name") else { continue }; + let bounds = p.child_by_field_name("bounds").map(|b| bound_names(cx, b)).unwrap_or_default(); + out.push((cx.text(name).to_owned(), bounds)); + } + } + for w in kids(n).filter(|c| c.kind() == "where_clause") { + for pred in kids(w).filter(|c| c.kind() == "where_predicate") { + let (Some(left), Some(b)) = (pred.child_by_field_name("left"), pred.child_by_field_name("bounds")) else { continue }; + if left.kind() != "type_identifier" { + continue; + } + let name = cx.text(left); + match out.iter_mut().find(|(n, _)| n == name) { + Some(entry) => entry.1.extend(bound_names(cx, b)), + // a bound on a parameter of the enclosing impl + None => { + let outer = cx.generics.iter().rev().find_map(|g| g.iter().find(|(n, _)| n == name)).is_some(); + if outer { + out.push((name.to_owned(), bound_names(cx, b))); + } + } + } + } + } + out.into_iter().map(|(n, b)| (n, if b.is_empty() { "_".to_owned() } else { format!("impl {}", b.join(" + ")) })).collect() +} + +/// A written type with the type parameters in scope replaced by their bounds: `Arc` with +/// `T: Svc` is `Arc`; a parameter with no bound is `_`, which names no type. +fn subst_generics(cx: &Cx, ty: &str) -> String { + if cx.generics.iter().all(Vec::is_empty) || ty.is_empty() { + return ty.to_owned(); + } + let lookup = |w: &str| cx.generics.iter().rev().find_map(|g| g.iter().find(|(n, _)| n == w).map(|(_, b)| b.clone())); + let b = ty.as_bytes(); + let mut out = String::with_capacity(ty.len()); + let mut i = 0; + while i < b.len() { + if b[i].is_ascii_alphabetic() || b[i] == b'_' { + let start = i; + while i < b.len() && (b[i].is_ascii_alphanumeric() || b[i] == b'_') { + i += 1; + } + let word = &ty[start..i]; + // a path segment (`T::Output`, `a::T`) is no parameter + let in_path = ty[..start].ends_with("::") || ty[i..].starts_with("::") || ty[..start].ends_with('\''); + match lookup(word).filter(|_| !in_path) { + Some(bound) => out.push_str(&bound), + None => out.push_str(word), + } + } else { + out.push(b[i] as char); + i += 1; + } + } + out +} + +/// `impl Deref for Wrapper { type Target = Inner; }`: method calls on a `Wrapper` reach `Inner`. +fn deref_impl(cx: &mut Cx, n: Node, scope: u32) { + let deref = n.child_by_field_name("trait").map(|t| cx.text(t)).is_some_and(|t| t == "Deref" || t.ends_with("::Deref")); + let (Some(ty), Some(body)) = (n.child_by_field_name("type"), n.child_by_field_name("body")) else { return }; + if !deref { + return; + } + let owner = match ty.kind() { + "generic_type" => ty.child_by_field_name("type").map(|t| cx.text(t)).unwrap_or(""), + _ => cx.text(ty), + } + .to_owned(); + for item in kids(body).filter(|c| c.kind() == "type_item") { + if item.child_by_field_name("name").map(|nm| cx.text(nm)) != Some("Target") { + continue; + } + let Some(target) = item.child_by_field_name("type") else { continue }; + let target = subst_generics(cx, &rust_type_name(cx, target)); + if !owner.is_empty() && !target.is_empty() { + let line = cx.line(n); + cx.facts.bindings.push(BindingFact { + scope, + name: owner.clone(), + kind: BindKind::Deref, + decl_type: target, + init_ctor: String::new(), + line, + node_row: NONE, + }); + } + } +} + fn rust_type_name(cx: &Cx, n: Node) -> String { match n.kind() { "type_identifier" | "scoped_type_identifier" => cx.text(n).to_owned(), @@ -239,7 +384,7 @@ fn value_path(cx: &Cx, v: Node, depth: u8) -> String { fn let_decl(cx: &mut Cx, n: Node, scope: u32) { let Some(p) = n.child_by_field_name("pattern") else { return }; - let ty = n.child_by_field_name("type").map(|t| rust_type_name(cx, t)).unwrap_or_default(); + let ty = n.child_by_field_name("type").map(|t| subst_generics(cx, &rust_type_name(cx, t))).unwrap_or_default(); let init = n.child_by_field_name("value").map(|v| value_info(cx, v)).unwrap_or_default(); let line = cx.line(n); let target = if p.kind() == "mut_pattern" { p.named_child(0).unwrap_or(p) } else { p }; @@ -259,7 +404,7 @@ fn params(cx: &mut Cx, list: Node, sc: u32) { cx.add_binding(sc, "self", BindKind::Param, "Self", "", line); } "parameter" => { - let ty = p.child_by_field_name("type").map(|t| rust_type_name(cx, t)).unwrap_or_default(); + let ty = p.child_by_field_name("type").map(|t| subst_generics(cx, &rust_type_name(cx, t))).unwrap_or_default(); if let Some(pat) = p.child_by_field_name("pattern") { let target = if pat.kind() == "mut_pattern" { pat.named_child(0).unwrap_or(pat) } else { pat }; if target.kind() == "identifier" { @@ -282,13 +427,21 @@ fn params(cx: &mut Cx, list: Node, sc: u32) { } fn function(cx: &mut Cx, n: Node, scope: u32) { + let generics = generics_of(cx, n); + cx.generics.push(generics); + function_body(cx, n, scope); + cx.generics.pop(); +} + +fn function_body(cx: &mut Cx, n: Node, scope: u32) { if let (Some(name), Some(ret)) = (n.child_by_field_name("name"), n.child_by_field_name("return_type")) { if let Some(row) = cx.row_at(cx.start(n)) { + let decl_type = subst_generics(cx, cx.text(ret)); cx.facts.bindings.push(BindingFact { scope, name: cx.text(name).to_owned(), kind: BindKind::ReturnType, - decl_type: cx.text(ret).to_owned(), + decl_type, init_ctor: String::new(), line: cx.line(n), node_row: row, @@ -327,7 +480,7 @@ fn struct_item(cx: &mut Cx, n: Node, scope: u32) { let Some(body) = n.child_by_field_name("body") else { return }; for f in kids(body) { if f.kind() == "field_declaration" { - let ty = f.child_by_field_name("type").map(|t| rust_type_name(cx, t)).unwrap_or_default(); + let ty = f.child_by_field_name("type").map(|t| subst_generics(cx, &rust_type_name(cx, t))).unwrap_or_default(); if let Some(nm) = f.child_by_field_name("name") { let t = cx.text(nm).to_owned(); let line = cx.line(f); diff --git a/crates/cg-extract/src/facts/util.rs b/crates/cg-extract/src/facts/util.rs index 839acc1..941ed1d 100644 --- a/crates/cg-extract/src/facts/util.rs +++ b/crates/cg-extract/src/facts/util.rs @@ -9,6 +9,9 @@ pub struct Cx<'a> { pub rows: FileRows<'a>, pub facts: Facts, pub parse_timeout: std::time::Duration, + /// Rust: the type parameters in scope, innermost last: (name, bounds written as + /// `impl A + B`, or `_` for none). + pub generics: Vec>, line_starts: Vec, row_by_start: Option>, /// Last (row, byte, UTF-16 column) computed: positions are asked in source order, and on @@ -26,6 +29,7 @@ impl<'a> Cx<'a> { rows, facts, parse_timeout: std::time::Duration::from_secs(5), + generics: Vec::new(), line_starts, row_by_start: None, last_pos: std::cell::Cell::new((usize::MAX, 0, 0)), @@ -113,13 +117,11 @@ impl<'a> Cx<'a> { pub fn lookup_binding(&self, mut scope: u32, name: &str) -> Option<(String, String)> { loop { if self.facts.scopes[scope as usize].kind != ScopeKind::Class { - if let Some(b) = self - .facts - .bindings - .iter() - .rev() - .find(|b| b.scope == scope && b.name == name && !matches!(b.kind, BindKind::MemberFn | BindKind::ReturnType)) - { + if let Some(b) = self.facts.bindings.iter().rev().find(|b| { + b.scope == scope + && b.name == name + && !matches!(b.kind, BindKind::MemberFn | BindKind::ReturnType | BindKind::Deref | BindKind::Fixture | BindKind::TypeAlias) + }) { return Some((b.decl_type.clone(), b.init_ctor.clone())); } } diff --git a/crates/cg-resolve/src/engine.rs b/crates/cg-resolve/src/engine.rs index b214a03..9649577 100644 --- a/crates/cg-resolve/src/engine.rs +++ b/crates/cg-resolve/src/engine.rs @@ -47,6 +47,17 @@ pub struct Engine<'v> { /// JavaScript functions with syntax evidence that they implement an object member. pub member_functions: FxHashMap<&'v str, SmallVec<[NodeIdx; 2]>>, pub return_types: FxHashMap, + /// ES5 constructor functions: the functions given to each one's prototype + /// (`X.prototype.m = function m() {}`), by member name. + pub proto: FxHashMap>, + /// A prototype member's constructor (what `this` is in it). + pub proto_owner: FxHashMap, + /// Rust `impl Deref` facts by the name of the type they are written for. + pub derefs: FxHashMap<&'v str, SmallVec<[(FileId, &'v cg_store::BindingRec); 1]>>, + /// pytest fixtures by name. + pub fixtures: FxHashMap<&'v str, SmallVec<[(FileId, &'v cg_store::BindingRec); 1]>>, + /// Rust `type X = Y;` targets by the alias's node. + pub type_aliases: FxHashMap, pub django_model: Option, pub n_files: usize, } @@ -114,10 +125,39 @@ impl<'v> Engine<'v> { .filter(|b| b.kind == BindKind::ReturnType as u8 && b.node.is_some()) .map(|b| (b.node, b)) .collect(), + proto: FxHashMap::default(), + proto_owner: FxHashMap::default(), + derefs: { + let mut m: FxHashMap<&'v str, SmallVec<[(FileId, &'v cg_store::BindingRec); 1]>> = FxHashMap::default(); + for &f in files.iter().filter(|f| view.file(**f).lang() == Lang::Rust) { + for b in view.bindings_of(f).iter().filter(|b| b.kind == BindKind::Deref as u8) { + let name = view.text_str(b.name); + m.entry(name.rsplit("::").next().unwrap_or(name)).or_default().push((f, b)); + } + } + m + }, + fixtures: { + let mut m: FxHashMap<&'v str, SmallVec<[(FileId, &'v cg_store::BindingRec); 1]>> = FxHashMap::default(); + for &f in files.iter().filter(|f| view.file(**f).lang() == Lang::Python) { + for b in view.bindings_of(f).iter().filter(|b| b.kind == BindKind::Fixture as u8) { + m.entry(view.text_str(b.name)).or_default().push((f, b)); + } + } + m + }, + type_aliases: files + .iter() + .filter(|f| view.file(**f).lang() == Lang::Rust) + .flat_map(|&f| view.bindings_of(f)) + .filter(|b| b.kind == BindKind::TypeAlias as u8 && b.node.is_some()) + .map(|b| (b.node, b)) + .collect(), django_model: None, n_files, }; e.build_exports(&files); + e.build_prototypes(&files); if project.py_deps.contains("django") { if let Some(f) = files.iter().copied().find(|f| view.file(*f).lang() == Lang::Python) { if let ImportTarget::File(base) = e.modules.resolve_import(Lang::Python, "django.db.models.base", f) { @@ -131,7 +171,7 @@ impl<'v> Engine<'v> { e } - fn top_level_nodes(&self, f: FileId) -> impl Iterator + '_ { + fn top_level_nodes(&self, f: FileId) -> impl Iterator + use<'_, 'v> { let range = self.view.nodes_in_file(f); range.map(NodeIdx).filter(move |n| { let h = self.view.hot(*n); @@ -171,6 +211,9 @@ impl<'v> Engine<'v> { // Explicit export facts (JS re-exports, `pub use`) and default exports for ex in view.exports_of(f) { let name = view.text_str(ex.name); + if name == cg_extract::facts::js::AMBIENT_MODULE { + continue; // a `declare module` block (see `Modules::ambient`) + } let spec = view.text_str(ex.from_spec); if name == COMMONJS_MODULE && spec.is_empty() && view.text_str(ex.from_name) == "*" { map.insert(name, ExportEntry::Module(f)); @@ -267,6 +310,37 @@ impl<'v> Engine<'v> { } } + /// Prototype members by their ES5 constructor, the function of that name at the top of the + /// member's file. + fn build_prototypes(&mut self, files: &[FileId]) { + let view = self.view; + for &f in files { + if view.file(f).lang().family() != LangFamily::Js { + continue; + } + for b in view.bindings_of(f) { + if b.kind != BindKind::MemberFn as u8 || b.node.0 == cg_core::NONE || b.decl_type.0 == cg_core::NONE { + continue; + } + let owner = view.text_str(b.decl_type); + if owner.is_empty() { + continue; + } + let ctor = self.top_level_nodes(f).find(|n| view.node(*n).name() == owner && view.node(*n).kind() == NodeKind::Function).or_else(|| { + self.top_level_nodes(f).find(|n| view.node(*n).name() == owner && matches!(view.node(*n).kind(), NodeKind::Variable | NodeKind::Constant)) + }); + let Some(ctor) = ctor.filter(|c| *c != b.node) else { continue }; + self.proto.entry(ctor).or_default().push((view.text_str(b.name), b.node)); + self.proto_owner.insert(b.node, ctor); + } + } + } + + /// A function given to the prototype of ES5 constructor `t` under `name`. + pub fn proto_member(&self, t: NodeIdx, name: &str) -> Option { + self.proto.get(&t)?.iter().find(|(n, _)| *n == name).map(|(_, m)| *m) + } + fn build_pkg_defs(&mut self, files: &[FileId]) { let view = self.view; let mut defs: Vec>> = (0..self.modules.pkgs.len()).map(|_| FxHashMap::default()).collect(); @@ -300,7 +374,7 @@ impl<'v> Engine<'v> { /// Module file a re-export (`export ... from 'x'`) points to, or NONE. pub fn export_target(&self, f: FileId, ex: &cg_store::ExportRec) -> u32 { - if ex.from_spec.0 == NONE { + if ex.from_spec.0 == NONE || self.view.text_str(ex.name) == cg_extract::facts::js::AMBIENT_MODULE { return NONE; } let spec = self.view.text_str(ex.from_spec); @@ -504,11 +578,11 @@ impl<'v> Engine<'v> { /// Member lookup through the hierarchy. pub fn find_member(&self, t: NodeIdx, name: &str) -> Option { - if let Some(m) = self.direct_member(t, name) { + if let Some(m) = self.direct_member(t, name).or_else(|| self.proto_member(t, name)) { return Some(m); } for a in self.ancestors(t) { - if let Some(m) = self.direct_member(a, name) { + if let Some(m) = self.direct_member(a, name).or_else(|| self.proto_member(a, name)) { return Some(m); } } diff --git a/crates/cg-resolve/src/frameworks/mod.rs b/crates/cg-resolve/src/frameworks/mod.rs index 22f1fea..0300adc 100644 --- a/crates/cg-resolve/src/frameworks/mod.rs +++ b/crates/cg-resolve/src/frameworks/mod.rs @@ -17,6 +17,11 @@ pub fn hooks(project: &ProjectInfo) -> Vec> { let mut v: Vec> = vec![ Arc::new(crate::synth::sites::SiteScanner), Arc::new(crate::synth::sites::GoTypeSites), + Arc::new(crate::synth::gin::GinScanner), + Arc::new(crate::synth::js_proto::InheritScanner), + Arc::new(crate::synth::redux::ThunkScanner), + Arc::new(crate::synth::jvm::TestRequestScanner), + Arc::new(crate::synth::bridges::BridgeScanner), Arc::new(crate::synth::dynamic::CallbackScanner), Arc::new(crate::synth::dynamic::RegistryScanner), Arc::new(crate::synth::dynamic::TierScanner), diff --git a/crates/cg-resolve/src/modules.rs b/crates/cg-resolve/src/modules.rs index b3cfef5..4b72d71 100644 --- a/crates/cg-resolve/src/modules.rs +++ b/crates/cg-resolve/src/modules.rs @@ -51,12 +51,20 @@ pub struct Modules<'v> { rs_mod_to_file: FxHashMap<(u32, String), FileId>, /// Rust: per file (crate index, module path). rs_file_mod: FxHashMap, + /// TypeScript: the declaration file a `declare module 'name' { ... }` block declares each + /// module in (upstream `ambient-modules.ts`), first by path. + ambient: FxHashMap<&'v str, FileId>, } fn dir_of(p: &str) -> &str { parent_dir(p) } +/// A TypeScript declaration file: `.d.ts`, `.d.mts`, `.d.cts`. +fn is_declaration_file(path: &str) -> bool { + path.ends_with(".d.ts") || path.ends_with(".d.mts") || path.ends_with(".d.cts") +} + impl<'v> Modules<'v> { pub fn build(view: &'v View, project: &'v ProjectInfo) -> Modules<'v> { let n = view.base.p.files.len() + view.n_files().saturating_sub(view.base.p.files.len()); @@ -72,6 +80,7 @@ impl<'v> Modules<'v> { go_dir_pkgs: FxHashMap::default(), rs_mod_to_file: FxHashMap::default(), rs_file_mod: FxHashMap::default(), + ambient: FxHashMap::default(), }; let mut files: Vec = view.files().collect(); // Delta file ids are appended, unlike a fresh build's path-ordered ids. Package @@ -91,6 +100,14 @@ impl<'v> Modules<'v> { Lang::Java | Lang::Kotlin => m.index_jvm(f), Lang::Go => m.index_go(f, path), Lang::Rust => m.index_rust(f, path), + Lang::TypeScript if is_declaration_file(path) => { + // a script declaration file (no top-level import or export) declares its modules + for ex in view.exports_of(f) { + if view.text_str(ex.name) == cg_extract::facts::js::AMBIENT_MODULE && view.text_str(ex.from_name) == "script" { + m.ambient.entry(view.text_str(ex.from_spec)).or_insert(f); + } + } + } _ => {} } } @@ -397,6 +414,13 @@ impl<'v> Modules<'v> { } } } + // a module the repository declares itself (`declare module 'mammoth' { ... }`): the + // checker reads it before the package, and only TypeScript reads it at all + if from_lang.is_typescript() { + if let Some(f) = self.ambient.get(spec) { + return ImportTarget::File(*f); + } + } ImportTarget::External } diff --git a/crates/cg-resolve/src/resolve.rs b/crates/cg-resolve/src/resolve.rs index a26c563..1b5e712 100644 --- a/crates/cg-resolve/src/resolve.rs +++ b/crates/cg-resolve/src/resolve.rs @@ -107,6 +107,29 @@ enum StdWrapper { Result, Option, Pointer, + /// `Mutex` / `RwLock` (std, tokio, parking_lot): `lock()` gives a guard. + Lock, + /// A lock's guard: it dereferences to the value, and std's `unwrap` keeps it. + Guard, +} + +/// The methods of a lock that hand out a guard. +fn lock_method(method: &str) -> bool { + matches!( + method, + "lock" + | "read" + | "write" + | "try_lock" + | "try_read" + | "try_write" + | "blocking_lock" + | "blocking_read" + | "blocking_write" + | "lock_owned" + | "read_owned" + | "write_owned" + ) } /// A known project type, possibly still inside a standard Rust container. @@ -131,7 +154,7 @@ impl ValueType { if self.wrappers.contains(&StdWrapper::Pointer) && pointer_method(method) { return None; } - self.wrappers.iter().all(|w| *w == StdWrapper::Pointer).then_some(self.node) + self.wrappers.iter().all(|w| matches!(w, StdWrapper::Pointer | StdWrapper::Guard)).then_some(self.node) } } @@ -179,6 +202,52 @@ fn split_call(value: &str) -> Option<(&str, &str)> { None } +/// The name a binding's initializer hands on (upstream `aliasTargetName`), read from the +/// declaration's signature: `= realImpl` (a cast allowed), or with `member` the value of that +/// key in an object literal (`= { run: impl }`, shorthand `= { impl }`). +fn alias_target_name<'s>(sig: &'s str, member: Option<&str>) -> Option<&'s str> { + let init = sig.trim().strip_prefix('=')?.trim().trim_end_matches(';').trim(); + let is_ident = + |s: &str| s.chars().next().is_some_and(|c| c.is_alphabetic() || c == '_' || c == '$') && s.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '$'); + let Some(key) = member else { + let name = init.split_once(" as ").map_or(init, |(a, _)| a).trim(); + return is_ident(name).then_some(name); + }; + let body = init.strip_prefix('{')?; + let (mut depth, mut start) = (0i32, 0usize); + let mut entries = Vec::new(); + for (i, c) in body.char_indices() { + match c { + '(' | '[' | '{' => depth += 1, + ')' | ']' => depth -= 1, + '}' if depth == 0 => { + entries.push(&body[start..i]); + start = body.len(); + break; + } + '}' => depth -= 1, + ',' if depth == 0 => { + entries.push(&body[start..i]); + start = i + 1; + } + _ => {} + } + } + entries.extend((start < body.len()).then(|| &body[start..])); + for entry in entries { + let entry = entry.trim(); + match entry.split_once(':') { + Some((k, v)) if k.trim().trim_matches(['\'', '"']) == key => { + let v = v.trim(); + return is_ident(v).then_some(v); + } + None if entry == key && is_ident(entry) => return Some(entry), + _ => {} + } + } + None +} + /// Per-file lookup structures, built once per file and reused for all its references. pub struct FileCx<'e, 'v> { pub e: &'e Engine<'v>, @@ -208,11 +277,13 @@ pub struct WorkerCx<'e, 'v> { cache_bytes: usize, field_cache: FxHashMap, field_cache_bytes: usize, + /// Rust: the type `Self` / `self` stand for in the call being resolved. + self_ty: Option, } impl<'e, 'v> WorkerCx<'e, 'v> { pub fn new() -> Self { - WorkerCx { cache: Vec::new(), cache_bytes: 0, field_cache: FxHashMap::default(), field_cache_bytes: 0 } + WorkerCx { cache: Vec::new(), cache_bytes: 0, field_cache: FxHashMap::default(), field_cache_bytes: 0, self_ty: None } } pub fn file_cx(&mut self, e: &'e Engine<'v>, f: FileId) -> Rc> { @@ -389,7 +460,13 @@ impl<'e, 'v> FileCx<'e, 'v> { if let Some(list) = self.bindings.get(&(s, name)) { let mut best = None; for b in list { - if b.kind == BindKind::Package as u8 || b.kind == BindKind::MemberFn as u8 || b.kind == BindKind::ReturnType as u8 { + if b.kind == BindKind::Package as u8 + || b.kind == BindKind::MemberFn as u8 + || b.kind == BindKind::ReturnType as u8 + || b.kind == BindKind::Deref as u8 + || b.kind == BindKind::Fixture as u8 + || b.kind == BindKind::TypeAlias as u8 + { continue; } if b.line == 0 || b.line <= line { @@ -476,6 +553,13 @@ impl<'e, 'v> FileCx<'e, 'v> { best.map(|b| b.0) } + /// The fields type `t` declares in its body in this file, in declaration order. + pub(crate) fn declared_fields(&self, t: NodeIdx) -> Vec<&'v BindingRec> { + let Some(cs) = self.class_scope_for(t) else { return Vec::new() }; + let scope = self.scope_base.wrapping_add(cs); + self.view.bindings_of(self.file).iter().filter(|b| b.scope.0 == scope && b.kind == BindKind::Field as u8).collect() + } + /// Binding of a file-level variable called `name` (module singletons such as `app = Flask()`). pub fn file_binding(&self, name: &str) -> Option<&'v BindingRec> { self.bindings.get(&(0, name)).and_then(|l| l.iter().copied().find(|b| b.kind == BindKind::Local as u8 || b.kind == BindKind::Field as u8)) @@ -599,10 +683,19 @@ impl<'e, 'v> FileCx<'e, 'v> { return self.type_value(wcx, dt, None, 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 { + return self.fixture_value(wcx, self.view.text_str(b.name), depth + 1); + } if init.is_empty() || init == "embedded" { return None; } let scope = b.scope.0.wrapping_sub(self.scope_base); + if b.kind == BindKind::Alias as u8 { + // `const s = service`: what the other name holds + if let Some(other) = self.lookup_binding(init, scope, b.line, false).filter(|o| !std::ptr::eq(*o, b)) { + return self.binding_value(wcx, other, depth + 1); + } + } // The new variable is not in scope in its own initializer (`let rt = rt()`). let shadows = init.split(['.', ':', '(']).next() == Some(self.view.text_str(b.name)); let line = if shadows { b.line.saturating_sub(1) } else { b.line }; @@ -613,6 +706,16 @@ impl<'e, 'v> FileCx<'e, 'v> { if depth >= 16 { return None; } + // `A()|B()`: a factory returning different classes in different branches is typed by + // the nearest class they all derive from, or not at all + if self.family == LangFamily::Python && init.contains('|') { + let mut types = Vec::new(); + for alt in init.split('|') { + let v = self.expression_value(wcx, alt, scope, line, depth + 1)?; + types.push(v.receiver()?); + } + return self.common_base(&types).map(ValueType::plain); + } if let Some(inner) = init.strip_suffix('?') { let mut value = self.expression_value(wcx, inner, scope, line, depth + 1)?; if !matches!(value.wrappers.pop()?, StdWrapper::Result | StdWrapper::Option) { @@ -643,6 +746,65 @@ impl<'e, 'v> FileCx<'e, 'v> { self.type_value(wcx, init, None, depth + 1) } + /// A pytest fixture's value (upstream `pythonFixtureReturnType`): in a test file or a + /// `conftest.py`, a parameter named after a fixture declared in the same file or in a + /// `conftest.py` of its directory or above holds what the fixture returns. + fn fixture_value(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str, depth: u8) -> Option { + if !(self.is_test || self.path.ends_with("conftest.py")) || matches!(name, "self" | "cls") { + return None; + } + let dir = crate::project::parent_dir(self.path); + let mut best: Option<(usize, FileId, &'v BindingRec)> = None; + for &(f, b) in self.e.fixtures.get(name)?.iter() { + let path = self.view.file(f).path(); + let fdir = crate::project::parent_dir(path); + let visible = f == self.file || (path.ends_with("conftest.py") && (fdir.is_empty() || dir == fdir || dir.starts_with(&format!("{fdir}/")))); + if !visible { + continue; + } + // the nearest declaration wins: this file, then the deepest conftest + let rank = if f == self.file { usize::MAX } else { fdir.len() }; + match best { + Some((r, ..)) if r > rank => {} + Some((r, ..)) if r == rank => return None, + _ => best = Some((rank, f, b)), + } + } + let (_, f, b) = best?; + 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); + } + cx.expression_value(wcx, self.view.text_str(b.init_ctor), b.scope.0.wrapping_sub(cx.scope_base), u32::MAX, depth + 1) + } + + /// The nearest type each of `types` is or derives from. + fn common_base(&self, types: &[NodeIdx]) -> Option { + let first = *types.first()?; + let mut chain = vec![first]; + chain.extend(self.e.ancestors(first)); + chain.into_iter().find(|c| types.iter().all(|t| t == c || self.e.ancestors(*t).contains(c))) + } + + /// The class a scope sits in (`self` / `cls` / `this` there), through enclosing scopes. + fn class_at(&self, mut scope: u32) -> Option { + loop { + let s = self.scopes.get(scope as usize)?; + if s.kind == ScopeKind::Class as u8 { + let start = s.start; + return self.view.nodes_in_file(self.file).map(NodeIdx).find(|n| { + let node = self.view.node(*n); + node.kind().is_type() + && pack_pos(node.start_line(), node.start_col()) <= start + && start <= pack_pos(node.end_line(), node.end_col()) + && pack_pos(node.start_line(), 0) == pack_pos((start >> 32) as u32, 0) + }); + } + scope = self.scope_parent(scope)?; + } + } + fn std_wrapper(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str) -> Option { let (prefix, leaf) = name.rsplit_once("::").unwrap_or(("", name)); if prefix.is_empty() { @@ -656,6 +818,7 @@ impl<'e, 'v> FileCx<'e, 'v> { } ("std::result" | "core::result" | "std::io", "Result") => Some(StdWrapper::Result), ("std::option" | "core::option", "Option") => Some(StdWrapper::Option), + ("std::sync" | "tokio::sync" | "parking_lot" | "futures::lock" | "async_lock" | "spin", "Mutex" | "RwLock") => Some(StdWrapper::Lock), _ => None, }; } @@ -664,11 +827,13 @@ impl<'e, 'v> FileCx<'e, 'v> { "Result" => StdWrapper::Result, "Option" => StdWrapper::Option, "Arc" | "Rc" | "Box" => StdWrapper::Pointer, + "Mutex" | "RwLock" => StdWrapper::Lock, _ => return None, }; let standard = match prefix { "std::result" | "core::result" | "std::io" => wrapper == StdWrapper::Result, "std::option" | "core::option" => wrapper == StdWrapper::Option, + "std::sync" | "tokio::sync" | "parking_lot" | "futures::lock" if wrapper == StdWrapper::Lock => true, "std::sync" | "alloc::sync" => leaf == "Arc", "std::rc" | "alloc::rc" => leaf == "Rc", "std::boxed" | "alloc::boxed" => leaf == "Box", @@ -695,9 +860,19 @@ impl<'e, 'v> FileCx<'e, 'v> { name = name.strip_prefix("mut ").unwrap_or(name).trim_start(); } if matches!(name, "Self" | "self" | "this") { - return receiver.map(ValueType::plain); + return receiver.or(wcx.self_ty).map(ValueType::plain); } if self.family == LangFamily::Rust { + if name == "_" { + return None; // a type parameter with no bound + } + // `impl Svc + Send` / `dyn Svc`: the first bound that is a project trait + if let Some(bounds) = name.strip_prefix("impl ").or_else(|| name.strip_prefix("dyn ")) { + return bounds.split('+').map(str::trim).filter(|b| !b.starts_with('\'')).find_map(|b| match self.lookup_type(wcx, b) { + TypeRes::Node(t) => Some(ValueType::plain(t)), + _ => None, + }); + } if let Some(open) = name.find('<') { let base = name[..open].trim(); let Some(wrapper) = self.std_wrapper(wcx, base) else { @@ -727,6 +902,16 @@ impl<'e, 'v> FileCx<'e, 'v> { } match self.lookup_type(wcx, name) { TypeRes::Node(t) => Some(ValueType::plain(t)), + _ if self.family == LangFamily::Js => self.es5_constructor(wcx, name).map(ValueType::plain), + _ => None, + } + } + + /// An ES5 constructor function called `name` (one with prototype members): what `new X()` + /// makes in code written before classes. + fn es5_constructor(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str) -> Option { + match self.resolve_name(wcx, name, Accept::Callable, 0, u32::MAX, None, false) { + NameRes::Found(n, ..) if self.e.proto.contains_key(&n) => Some(n), _ => None, } } @@ -773,8 +958,17 @@ impl<'e, 'v> FileCx<'e, 'v> { } } } - let receiver = if let Some(b) = self.lookup_binding(owner, scope, line, false) { + let implicit = matches!(owner, "self" | "cls" | "this") && matches!(self.family, LangFamily::Python | LangFamily::Js); + let receiver = if let Some(b) = self + .lookup_binding(owner, scope, line, false) + .filter(|b| !(implicit && self.view.text_str(b.decl_type).is_empty() && self.view.text_str(b.init_ctor).is_empty())) + { self.binding_value(wcx, b, depth + 1) + } else if implicit { + // `self.make()`: a method of the class the code sits in + self.class_at(scope).map(ValueType::plain) + } 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) }; @@ -788,7 +982,40 @@ impl<'e, 'v> FileCx<'e, 'v> { } } + /// The value a field path holds (`self.state`): the declared type of each field in turn, + /// standard wrappers kept; a smart pointer or a lock guard is looked through on the way. + fn path_value(&self, wcx: &mut WorkerCx<'e, 'v>, path: &str, scope: u32, line: u32, depth: u8) -> Option { + if depth >= 16 { + return None; + } + let mut segs = path.split('.'); + let root = segs.next()?; + let b = self.lookup_binding(root, scope, line, false)?; + let mut value = self.binding_value(wcx, b, depth + 1)?; + for f in segs { + let t = value.method_receiver("")?; + value = self.field_value(wcx, t, f, depth + 1)?; + } + Some(value) + } + + /// The declared value of field `name` of type `t` (through the hierarchy), wrappers kept. + fn field_value(&self, wcx: &mut WorkerCx<'e, 'v>, t: NodeIdx, name: &str, depth: u8) -> Option { + for ty in std::iter::once(t).chain(self.e.ancestors(t)) { + let cx = wcx.file_cx(self.e, self.view.hot(ty).file); + let Some(cs) = cx.class_scope_for(ty) else { continue }; + if let Some(b) = cx.bindings.get(&(cs, name)).and_then(|l| l.iter().copied().find(|b| b.kind == BindKind::Field as u8)) { + return cx.binding_value(wcx, b, depth + 1); + } + } + None + } + fn member_return(&self, wcx: &mut WorkerCx<'e, 'v>, mut receiver: ValueType, method: &str, depth: u8) -> Option { + // a smart pointer's own methods stay its own; any other call goes through to the value + while receiver.wrappers.last() == Some(&StdWrapper::Pointer) && !pointer_method(method) && receiver.wrappers.len() > 1 { + receiver.wrappers.pop(); + } if receiver.wrappers.last().is_some_and(|w| matches!(w, StdWrapper::Result | StdWrapper::Option)) { return match method { "unwrap" | "expect" | "unwrap_or" | "unwrap_or_else" | "unwrap_or_default" => { @@ -809,9 +1036,66 @@ impl<'e, 'v> FileCx<'e, 'v> { _ => {} } } + match receiver.wrappers.last() { + Some(StdWrapper::Lock) if lock_method(method) => { + receiver.wrappers.pop(); + receiver.wrappers.push(StdWrapper::Guard); + return Some(receiver); + } + Some(StdWrapper::Lock) => return None, + // std's `lock()` returns a `LockResult` of the guard + Some(StdWrapper::Guard) if matches!(method, "unwrap" | "expect" | "unwrap_or_else") => return Some(receiver), + _ => {} + } receiver.method_receiver(method)?; - let m = self.e.find_member(receiver.node, method)?; - self.returned_value(wcx, m, Some(receiver.node), depth + 1) + let (m, owner) = self.rust_member(wcx, receiver.node, method)?; + self.returned_value(wcx, m, Some(owner), depth + 1) + } + + /// Member `method` of `t`, else (Rust) of what `t` dereferences to through `impl Deref`, + /// a few hops at most: the member and the type that declares it. + fn rust_member(&self, wcx: &mut WorkerCx<'e, 'v>, t: NodeIdx, method: &str) -> Option<(NodeIdx, NodeIdx)> { + let mut cur = t; + for _ in 0..4 { + if let Some(m) = self.e.find_member(cur, method) { + return Some((m, cur)); + } + if self.family != LangFamily::Rust { + return None; + } + // `type Outcome = plugin::Outcome` has the members of what it names + 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()) + } + None => self.deref_target(wcx, cur), + }; + cur = next?; + if cur == t { + return None; + } + } + None + } + + /// What Rust type `t` dereferences to (`impl Deref for T { type Target = U; }`), the impl + /// found by the type's name and checked to name `t` where it is written. + fn deref_target(&self, wcx: &mut WorkerCx<'e, 'v>, t: NodeIdx) -> Option { + let entries = self.e.derefs.get(self.view.node(t).name())?; + let mut found = None; + for &(f, b) in entries { + let cx = wcx.file_cx(self.e, f); + 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()?; + if found.is_some_and(|x| x != target) { + return None; + } + found = Some(target); + } + found } /// Declared type of field `name` of type `t` (through the hierarchy). @@ -1139,6 +1423,59 @@ impl<'e, 'v> FileCx<'e, 'v> { None } + /// `const local = other`: resolve `other` where the alias is declared, through at most a few + /// aliases of aliases. A local value that is not a function stays local. + #[allow(clippy::too_many_arguments)] + fn follow_alias( + &self, + wcx: &mut WorkerCx<'e, 'v>, + mut b: &'v BindingRec, + accept: Accept, + from: Option, + allow_global: bool, + args: Option<(&CallArgs<'v>, &RefRec)>, + ) -> NameRes { + for _ in 0..4 { + let target = self.view.text_str(b.init_ctor); + let scope = b.scope.0.wrapping_sub(self.scope_base); + match self.lookup_binding(target, scope, b.line, false) { + Some(next) if next.kind == BindKind::Alias as u8 && !std::ptr::eq(next, b) => b = next, + Some(next) if next.kind == BindKind::LocalFn as u8 && next.node.0 != NONE => return NameRes::Found(next.node, by::LEXICAL, 95), + Some(_) => return NameRes::Local, + None => { + return match self.resolve_name_args(wcx, target, accept, scope, b.line, from, allow_global, args) { + NameRes::Found(n, by_, conf) => NameRes::Found(n, by_, conf.min(95)), + other => other, + }; + } + } + } + NameRes::Local + } + + /// The callable a JS constant or variable hands on (upstream `alias-binding`): `export const + /// alias = realImpl` for a call of `alias`, `export const api = { run: impl }` for `api.run`. + /// Resolved where the binding is declared; nothing when the initializer is anything else or + /// names nothing callable. + pub(crate) fn forward_alias_node(&self, wcx: &mut WorkerCx<'e, 'v>, n: NodeIdx, member: Option<&str>) -> Option { + let view = self.view; + let node = view.node(n); + if !matches!(node.kind(), NodeKind::Constant | NodeKind::Variable | NodeKind::Property) || view.file(node.file()).lang().family() != LangFamily::Js { + return None; + } + let target = alias_target_name(node.signature()?, member)?; + if member.is_none() && target == node.name() { + return None; + } + let cx = wcx.file_cx(self.e, node.file()); + match cx.resolve_name(wcx, target, Accept::Callable, 0, node.start_line(), None, true) { + NameRes::Found(m, ..) if m != n && matches!(kind_of(view, m), NodeKind::Function | NodeKind::Method | NodeKind::Class | NodeKind::Component) => { + Some(m) + } + _ => None, + } + } + /// Resolve a simple name (design doc 6.3 steps 2, 3, 6, 7). #[allow(clippy::too_many_arguments)] pub fn resolve_name( @@ -1172,6 +1509,9 @@ impl<'e, 'v> FileCx<'e, 'v> { // 2. lexical scope if accept != Accept::Type { if let Some(b) = self.lookup_binding(name, scope, line, false) { + if b.kind == BindKind::Alias as u8 { + return self.follow_alias(wcx, b, accept, from, allow_global, args); + } if b.kind == BindKind::LocalFn as u8 && b.node.0 != NONE { return NameRes::Found(b.node, by::LEXICAL, 98); } @@ -1330,8 +1670,8 @@ impl<'e, 'v> FileCx<'e, 'v> { return Outcome::found(m, by::RECEIVER, conf); } } - match self.e.find_member(t, method) { - Some(m) => Outcome::found(m, by::RECEIVER, conf), + match self.rust_member(wcx, t, method) { + Some((m, _)) => Outcome::found(m, by::RECEIVER, conf), None => Outcome::st(status::NO_CANDIDATE), } } @@ -1409,9 +1749,18 @@ impl<'e, 'v> FileCx<'e, 'v> { } } + fn is_call(r: &RefRec) -> bool { + RefKind::from_wire(r.kind) == Some(RefKind::Edge(EdgeKind::Calls)) + } + fn call_bare(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str, accept: Accept, args: Option<&CallArgs<'v>>) -> Outcome { let scope = self.scope_at(r.line, r.col); match self.resolve_name_args(wcx, name, accept, scope, r.line, Some(r.from), true, args.map(|a| (a, r))) { + // a call through `export const alias = realImpl` is a call of `realImpl` + NameRes::Found(n, b, c) if self.family == LangFamily::Js && Self::is_call(r) => match self.forward_alias_node(wcx, n, None) { + Some(m) => Outcome::found(m, b, c.min(90)), + None => Outcome::found(n, b, c), + }, NameRes::Found(n, b, c) => Outcome::found(n, b, c), NameRes::Local => Outcome::st(status::LOCAL_BINDING), NameRes::External | NameRes::Module(_) | NameRes::Pkg(_) => Outcome::st(status::EXTERNAL), @@ -1442,6 +1791,12 @@ impl<'e, 'v> FileCx<'e, 'v> { if !self.view.text_str(b.decl_type).is_empty() { return Outcome::st(status::NO_CANDIDATE); } + // `const api = { run: impl }` at the top of this file: `api.run()` runs `impl` + if self.family == LangFamily::Js && path.is_empty() && b.scope.0 == self.scope_base && Self::is_call(r) { + if let Some(m) = self.e.lookup_top_level(self.file, root).and_then(|n| self.forward_alias_node(wcx, n, Some(method))) { + return Outcome::found(m, by::LEXICAL, 90); + } + } // An initializer rooted in an explicit external import // does not justify guessing an unrelated project member. let init = self.view.text_str(b.init_ctor); @@ -1487,6 +1842,12 @@ impl<'e, 'v> FileCx<'e, 'v> { if k.is_type() { return self.member_call(wcx, n, path, method, 92, call); } + // an imported `export const api = { run: impl }`: `api.run()` runs `impl` + if self.family == LangFamily::Js && path.is_empty() && Self::is_call(r) { + if let Some(m) = self.forward_alias_node(wcx, n, Some(method)) { + return Outcome::found(m, by::IMPORT, 90); + } + } // an imported module-level variable: the type of its initialiser { let owner = view.hot(n).file; @@ -1590,7 +1951,9 @@ impl<'e, 'v> FileCx<'e, 'v> { } fn call_this(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, kw: &str, fields: &[&str], method: &str, args: Option<&CallArgs<'v>>) -> Outcome { - let Some(t) = self.enclosing_type(r.from) else { + // in a function given to an ES5 constructor's prototype, `this` is the constructor's object + let t = self.enclosing_type(r.from).or_else(|| self.e.proto_owner.get(&r.from).copied().filter(|_| self.family == LangFamily::Js)); + let Some(t) = t else { return if fields.is_empty() { self.fallback_method(method) } else { Outcome::st(status::NO_CANDIDATE) }; }; let _ = kw; @@ -1651,7 +2014,17 @@ impl<'e, 'v> FileCx<'e, 'v> { pub fn resolve_call_name(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str) -> Outcome { let args = CallArgs::of(self.view, r); - self.call_name_with(wcx, r, name, args.as_ref()) + if self.family != LangFamily::Rust || !name.contains("self") { + return self.call_name_with(wcx, r, name, args.as_ref()); + } + // `self.state.lock()`: `self` is the type the calling method belongs to + let previous = wcx.self_ty.replace(self.enclosing_type(r.from).unwrap_or(NodeIdx(NONE))); + if wcx.self_ty == Some(NodeIdx(NONE)) { + wcx.self_ty = None; + } + let o = self.call_name_with(wcx, r, name, args.as_ref()); + wcx.self_ty = previous; + o } fn call_name_with(&self, wcx: &mut WorkerCx<'e, 'v>, r: &RefRec, name: &str, args: Option<&CallArgs<'v>>) -> Outcome { @@ -1663,7 +2036,10 @@ impl<'e, 'v> FileCx<'e, 'v> { Shape::New { class, method } => match self.lookup_type(wcx, class) { TypeRes::Node(t) => self.member_call(wcx, t, &[], method, 90, args.map(|a| (a, r))), TypeRes::External => Outcome::st(status::EXTERNAL), - _ => Outcome::st(status::NO_CANDIDATE), + _ => match self.es5_constructor(wcx, class).filter(|_| self.family == LangFamily::Js) { + Some(c) => self.member_call(wcx, c, &[], method, 85, args.map(|a| (a, r))), + None => Outcome::st(status::NO_CANDIDATE), + }, }, Shape::CallResult { inner, method } => self.call_result(wcx, r, inner, method, args), Shape::ValueResult { inner, method } => { @@ -1825,6 +2201,19 @@ impl<'e, 'v> FileCx<'e, 'v> { 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), + // `util.inherits(Child, Parent)`: an ES5 constructor extends another constructor + RefKind::Edge(EdgeKind::Extends) if self.family == LangFamily::Js => { + let o = self.resolve_type_ref(wcx, r, name, Accept::Type); + if o.status != status::NO_CANDIDATE { + return o; + } + let c = self.resolve_type_ref(wcx, r, name, Accept::Callable); + if c.status == status::RESOLVED && kind_of(self.view, NodeIdx(c.target)) == NodeKind::Function { + c + } else { + o + } + } RefKind::Edge(EdgeKind::Extends | EdgeKind::Implements | EdgeKind::TypeOf | EdgeKind::Returns) => self.resolve_type_ref(wcx, r, name, Accept::Type), RefKind::Edge(EdgeKind::Instantiates) => { let o = self.resolve_type_ref(wcx, r, name, Accept::Type); diff --git a/crates/cg-resolve/src/synth/bridges.rs b/crates/cg-resolve/src/synth/bridges.rs new file mode 100644 index 0000000..e2eb792 --- /dev/null +++ b/crates/cg-resolve/src/synth/bridges.rs @@ -0,0 +1,212 @@ +//! Calls that cross from JavaScript into another language of the same app. +//! +//! * `tauri-command` (upstream `tauri-synthesizer.ts`): the page calls `invoke('save_file', ...)` +//! and the Rust `#[tauri::command] fn save_file` answers. A command answers only when a handler +//! list registers it (`generate_handler![...]`, tauri-specta's `collect_commands![...]`), and +//! names are looked up per app (the directory holding `src-tauri/`): a monorepo's two `greet`s +//! stay apart, and a tie is nothing. +//! * `rn-native-module` (upstream `rnXPlatEdges`, the Android half): `NativeModules.Calendar +//! .createEvent(...)` (or a module destructured from `NativeModules`) reaches the +//! `@ReactMethod createEvent` of the Java / Kotlin module whose `getName()` returns `Calendar`. +//! +//! Event channels across the bridges and the iOS side are not read. + +use super::scan::{enclosing_row, is_minified, LineIndex, FN_KINDS}; +use super::sites::PASS_BRIDGE; +use super::{order_of, Out}; +use crate::engine::Engine; +use cg_core::{EdgeKind, FileId, Lang, LangFamily, NodeIdx, NodeKind, StrId, NONE}; +use cg_extract::{FileHook, HookCtx, HookOut, SiteFact}; +use regex::Regex; +use rustc_hash::FxHashMap; +use std::sync::OnceLock; + +pub const ROLE_TAURI_COMMAND: u8 = 0; +pub const ROLE_TAURI_REGISTERED: u8 = 1; +pub const ROLE_TAURI_INVOKE: u8 = 2; +pub const ROLE_RN_METHOD: u8 = 3; +pub const ROLE_RN_CALL: u8 = 4; + +macro_rules! re { + ($name:ident, $pat:expr) => { + fn $name() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new($pat).expect("static regex")) + } + }; +} + +re!(tauri_command_re, r"#\[\s*(?:tauri\s*::\s*)?command\b[^\]]*\]\s*(?:#\[[^\]]*\]\s*)*(?:pub(?:\s*\([^)]*\))?\s+)?(?:async\s+)?fn\s+([A-Za-z_]\w*)"); +re!(tauri_handlers_re, r"\b(?:generate_handler|collect_commands)\s*!\s*\[([^\]]*)\]"); +re!(invoke_re, r#"(?:^|[^\w$.])(?:[\w$]+\s*\.\s*)*invoke\s*(?:<[^>()]*>)?\s*\(\s*(['"`])([A-Za-z_][\w]*)['"`]"#); +re!(rn_get_name_re, r#"\bgetName\s*\(\s*\)[^{=]*(?:\{\s*return\s+|=\s*)"([A-Za-z_][\w]*)""#); +re!( + rn_method_re, + r"@ReactMethod(?:\s*\([^)]*\))?\s*(?:(?:public|private|protected|internal|override|suspend|final)\s+)*(?:fun\s+|[\w<>\[\],\s]+?\s+)([A-Za-z_]\w*)\s*\(" +); +re!(rn_call_re, r"\bNativeModules\s*\.\s*([A-Za-z_$][\w$]*)\s*\.\s*([A-Za-z_$][\w$]*)\s*\("); +re!(rn_destructure_re, r"\{([^{}]*)\}\s*=\s*NativeModules\b"); + +fn site(role: u8, key_a: &str, key_b: &str, node_row: u32, line: u32) -> SiteFact { + SiteFact { pass: PASS_BRIDGE, role, key_kind: 0, key_a: key_a.to_owned(), key_b: key_b.to_owned(), node_row, line, payload: String::new() } +} + +/// The function node called `name` declared on `line` or just below it. +fn fn_row_at(ctx: &HookCtx<'_>, name: &str, line: u32) -> Option { + ctx.rows + .nodes() + .enumerate() + .find(|(_, n)| matches!(n.kind(), NodeKind::Function | NodeKind::Method) && n.name() == name && n.start_line() >= line && n.start_line() <= line + 3) + .map(|(i, _)| i as u32) +} + +pub struct BridgeScanner; + +impl FileHook for BridgeScanner { + fn name(&self) -> &'static str { + "bridge-sites" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + matches!(lang.family(), LangFamily::Js | LangFamily::Rust | LangFamily::Jvm) + } + fn triggers(&self) -> &'static [&'static str] { + &["command", "generate_handler", "collect_commands", "invoke", "ReactMethod", "NativeModules"] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + if is_minified(ctx.src) { + return; + } + let safe = cg_extract::post::strip_js_comments(ctx.src); + let lines = LineIndex::new(&safe); + match ctx.lang.family() { + LangFamily::Rust => { + if safe.contains("command") && safe.contains("tauri") { + for c in tauri_command_re().captures_iter(&safe) { + let name = c.get(1).unwrap(); + let line = lines.line(name.start()); + if let Some(row) = fn_row_at(ctx, name.as_str(), line.saturating_sub(1)) { + out.sites.push(site(ROLE_TAURI_COMMAND, name.as_str(), "", row, line)); + } + } + } + for c in tauri_handlers_re().captures_iter(&safe) { + let line = lines.line(c.get(0).unwrap().start()); + for item in c[1].split(',') { + let name = item.trim().rsplit("::").next().unwrap_or("").trim(); + if !name.is_empty() { + out.sites.push(site(ROLE_TAURI_REGISTERED, name, "", NONE, line)); + } + } + } + } + LangFamily::Jvm => { + let Some(module) = rn_get_name_re().captures(&safe).map(|c| c[1].to_owned()) else { return }; + for c in rn_method_re().captures_iter(&safe) { + let name = c.get(1).unwrap(); + let line = lines.line(name.start()); + if let Some(row) = fn_row_at(ctx, name.as_str(), line.saturating_sub(1)) { + out.sites.push(site(ROLE_RN_METHOD, &module, name.as_str(), row, line)); + } + } + } + _ => { + for c in invoke_re().captures_iter(&safe) { + let at = c.get(2).unwrap().start(); + let line = lines.line(at); + let row = enclosing_row(ctx, line, FN_KINDS).unwrap_or(NONE); + out.sites.push(site(ROLE_TAURI_INVOKE, &c[2], "", row, line)); + } + for c in rn_call_re().captures_iter(&safe) { + let line = lines.line(c.get(0).unwrap().start()); + let row = enclosing_row(ctx, line, FN_KINDS).unwrap_or(NONE); + out.sites.push(site(ROLE_RN_CALL, &c[1], &c[2], row, line)); + } + // `const { Calendar } = NativeModules;` then `Calendar.createEvent(...)` + for d in rn_destructure_re().captures_iter(&safe) { + for module in d[1].split(',').filter_map(|m| m.split(':').next_back()).map(str::trim).filter(|m| !m.is_empty()) { + let pat = format!(r"\b{}\s*\.\s*([A-Za-z_$][\w$]*)\s*\(", regex::escape(module)); + let Ok(re) = Regex::new(&pat) else { continue }; + for c in re.captures_iter(&safe) { + let line = lines.line(c.get(0).unwrap().start()); + let row = enclosing_row(ctx, line, FN_KINDS).unwrap_or(NONE); + out.sites.push(site(ROLE_RN_CALL, module, &c[1], row, line)); + } + } + } + } + } + } +} + +/// The Tauri app a file belongs to: the directory holding the `src-tauri/` its path runs +/// through, or the longest such directory above a frontend file. `""` with no `src-tauri/`. +fn app_of<'a>(path: &str, roots: &[&'a str]) -> &'a str { + if let Some(i) = path.find("src-tauri/") { + if let Some(r) = roots.iter().find(|r| **r == &path[..i]) { + return r; + } + } + roots.iter().filter(|r| path.starts_with(**r)).max_by_key(|r| r.len()).copied().unwrap_or("") +} + +pub(crate) fn link(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + let mut commands: Vec<(FileId, NodeIdx, &str)> = Vec::new(); + let mut registered: Vec<(FileId, &str)> = Vec::new(); + let mut invokes: Vec<(FileId, NodeIdx, u32, &str)> = Vec::new(); + let mut rn_methods: FxHashMap<(&str, &str), Vec> = FxHashMap::default(); + let mut rn_calls: Vec<(FileId, NodeIdx, u32, &str, &str)> = Vec::new(); + for f in view.files() { + let file_node = view.file(f).rec.node_start; + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_BRIDGE) { + let (a, b) = (view.text_str(StrId(s.key_a)), view.text_str(StrId(s.key_b))); + let from = if s.node.0 == NONE { file_node } else { s.node }; + match s.role { + ROLE_TAURI_COMMAND => commands.push((f, s.node, a)), + ROLE_TAURI_REGISTERED => registered.push((f, a)), + ROLE_TAURI_INVOKE => invokes.push((f, from, s.line, a)), + ROLE_RN_METHOD => rn_methods.entry((a, b)).or_default().push(s.node), + ROLE_RN_CALL => rn_calls.push((f, from, s.line, a, b)), + _ => {} + } + } + } + if !invokes.is_empty() && !commands.is_empty() { + let mut roots: Vec<&str> = commands.iter().filter_map(|(f, ..)| view.file(*f).path().find("src-tauri/").map(|i| &view.file(*f).path()[..i])).collect(); + roots.sort_unstable(); + roots.dedup(); + let app = |f: FileId| app_of(view.file(f).path(), &roots); + let mut answers: FxHashMap<(&str, &str), Vec> = FxHashMap::default(); + for &(f, node, name) in &commands { + if registered.iter().any(|(rf, r)| *r == name && app(*rf) == app(f)) { + answers.entry((app(f), name)).or_default().push(node); + } + } + for (f, from, line, name) in invokes { + let Some([target]) = answers.get(&(app(f), name)).map(Vec::as_slice) else { continue }; + out.push( + EdgeKind::Calls, + from, + *target, + line, + "tauri-command", + Some(format!("{}:{}", view.file(f).path(), line)), + Some(("via", name)), + order_of("tauri-command"), + ); + } + } + for (f, from, line, module, method) in rn_calls { + let Some([target]) = rn_methods.get(&(module, method)).map(Vec::as_slice) else { continue }; + out.push( + EdgeKind::Calls, + from, + *target, + line, + "rn-native-module", + Some(format!("{}:{}", view.file(f).path(), line)), + Some(("via", &format!("{module}.{method}"))), + order_of("rn-native-module"), + ); + } +} diff --git a/crates/cg-resolve/src/synth/dynamic.rs b/crates/cg-resolve/src/synth/dynamic.rs index 2e9b513..30c0468 100644 --- a/crates/cg-resolve/src/synth/dynamic.rs +++ b/crates/cg-resolve/src/synth/dynamic.rs @@ -761,8 +761,8 @@ impl FileHook for TierScanner { } /// An HTTP route: its node, method and path segments. -struct HttpRoute { - node: NodeIdx, +pub(crate) struct HttpRoute { + pub(crate) node: NodeIdx, method: String, segs: Vec, excluded: Vec<(Option, Vec)>, @@ -831,7 +831,7 @@ fn mount_prefixes(e: &Engine<'_>) -> FxHashMap { prefix_of } -fn http_routes(e: &Engine<'_>) -> Vec { +pub(crate) fn http_routes(e: &Engine<'_>) -> Vec { let view = e.view; let prefixes = mount_prefixes(e); let nest_paths = crate::frameworks::nest::effective_paths(e); @@ -910,7 +910,7 @@ fn score_path(client: &[String], route: &[String]) -> Option { } /// Segments of a client path (`${…}` as `*`), and whether a base URL hides its prefix. -fn client_path(raw: &str, base: Option<&str>) -> Option<(Vec, bool)> { +pub(crate) fn client_path(raw: &str, base: Option<&str>) -> Option<(Vec, bool)> { let mut p = &raw[..raw.find(['?', '#']).unwrap_or(raw.len())]; let mut suffix = false; let owned; @@ -962,7 +962,7 @@ fn client_path(raw: &str, base: Option<&str>) -> Option<(Vec, bool)> { Some((segs, suffix)) } -fn match_http<'r>(method: &str, segs: &[String], suffix: bool, routes: &'r [HttpRoute]) -> Option<&'r HttpRoute> { +pub(crate) fn match_http<'r>(method: &str, segs: &[String], suffix: bool, routes: &'r [HttpRoute]) -> Option<&'r HttpRoute> { let mut best: Option<(&HttpRoute, u32)> = None; let mut tied = false; for r in routes { diff --git a/crates/cg-resolve/src/synth/gin.rs b/crates/cg-resolve/src/synth/gin.rs new file mode 100644 index 0000000..7ede724 --- /dev/null +++ b/crates/cg-resolve/src/synth/gin.rs @@ -0,0 +1,184 @@ +//! Gin's middleware chain (upstream `ginMiddlewareChainEdges`). Gin runs every handler through +//! one computed call in `(*Context).Next` (`c.handlers[c.index](c)`), which names nothing, so a +//! flow from `ServeHTTP` dead-ends there. The dispatcher - a Go method whose body calls a +//! `handlers` slice by index - links to every named handler a registration call (`.Use`, `.GET`, +//! ..., `.Handle`) passes: `gin.Logger()` is `Logger`, a closure is anonymous and skipped. A +//! deliberate, capped over-approximation (any registered handler can run for some route), gated +//! on the dispatcher existing. A handler name must reach one function: the only one in the +//! project, or the only one in the registering file's directory. + +use super::scan::{enclosing_row, is_minified, LineIndex}; +use super::sites::PASS_GIN; +use super::{order_of, Out}; +use crate::engine::Engine; +use crate::frameworks::href::match_bracket; +use crate::project::parent_dir; +use cg_core::{EdgeKind, Lang, NodeIdx, NodeKind, StrId, NONE}; +use cg_extract::{FileHook, HookCtx, HookOut, SiteFact}; +use regex::Regex; +use std::sync::OnceLock; + +pub const ROLE_GIN_DISPATCHER: u8 = 0; +pub const ROLE_GIN_HANDLER: u8 = 1; + +const MAX_HANDLERS: usize = 40; + +fn dispatch_re() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new(r"\.handlers\s*\[[^\]]*\]\s*\(").expect("static regex")) +} + +fn registration_re() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new(r"\.(?:Use|GET|POST|PUT|PATCH|DELETE|OPTIONS|HEAD|Any|Handle)\s*\(").expect("static regex")) +} + +/// Top-level comma-separated items of a call's arguments. +fn split_args(args: &str) -> Vec<&str> { + let mut out = Vec::new(); + let (mut depth, mut start) = (0i32, 0); + for (i, c) in args.char_indices() { + match c { + '(' | '[' | '{' => depth += 1, + ')' | ']' | '}' => depth -= 1, + ',' if depth == 0 => { + out.push(&args[start..i]); + start = i + 1; + } + _ => {} + } + } + if !args[start..].trim().is_empty() { + out.push(&args[start..]); + } + out +} + +/// The name a handler argument ends in: `gin.Logger()` -> `Logger`, `mw` -> `mw`; nothing for a +/// path string or an inline closure. +fn handler_ident(arg: &str) -> Option<&str> { + let a = arg.trim(); + let a = a.strip_suffix("()").map(str::trim_end).unwrap_or(a); + if a.is_empty() || a.starts_with(['"', '`']) || a.starts_with("func") { + return None; + } + let tail = a.rsplit('.').next().unwrap_or(a); + let ok = tail.chars().next().is_some_and(|c| c.is_alphabetic() || c == '_') && tail.chars().all(|c| c.is_alphanumeric() || c == '_'); + ok.then_some(tail) +} + +/// Gin's dispatcher methods and the handlers registration calls pass. +pub struct GinScanner; + +impl FileHook for GinScanner { + fn name(&self) -> &'static str { + "gin-sites" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + lang == Lang::Go + } + fn triggers(&self) -> &'static [&'static str] { + &[".handlers", ".Use(", ".GET(", ".POST(", ".PUT(", ".PATCH(", ".DELETE(", ".OPTIONS(", ".HEAD(", ".Any(", ".Handle("] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + if is_minified(ctx.src) { + return; + } + let safe = cg_extract::post::strip_js_comments(ctx.src); + let lines = LineIndex::new(&safe); + let site = |role: u8, key_a: &str, node_row: u32, line: u32| SiteFact { + pass: PASS_GIN, + role, + key_kind: 0, + key_a: key_a.to_owned(), + key_b: String::new(), + node_row, + line, + payload: String::new(), + }; + if safe.contains(".handlers") { + for (row, n) in ctx.rows.nodes().enumerate() { + if n.kind() == NodeKind::Method && dispatch_re().is_match(lines.lines(n.start_line(), n.end_line())) { + out.sites.push(site(ROLE_GIN_DISPATCHER, "", row as u32, n.start_line())); + } + } + } + for m in registration_re().find_iter(&safe) { + let open = m.end() - 1; + let Some(close) = match_bracket(&safe, open) else { continue }; + let line = lines.line(m.start()); + let row = enclosing_row(ctx, line, super::scan::FN_KINDS).unwrap_or(NONE); + for arg in split_args(&safe[open + 1..close]) { + if let Some(name) = handler_ident(arg) { + out.sites.push(site(ROLE_GIN_HANDLER, name, row, line)); + } + } + } + } +} + +pub(crate) fn middleware_chain(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + let mut dispatchers: Vec = Vec::new(); + // (name, file path, line), first registration per name in path order + let mut registered: Vec<(&str, &str, u32)> = Vec::new(); + for f in view.files() { + if view.file(f).lang() != Lang::Go { + continue; + } + let path = view.file(f).path(); + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_GIN) { + match s.role { + ROLE_GIN_DISPATCHER if s.node.0 != NONE => dispatchers.push(s.node), + ROLE_GIN_HANDLER => registered.push((view.text_str(StrId(s.key_a)), path, s.line)), + _ => {} + } + } + } + if dispatchers.is_empty() || registered.is_empty() { + return; + } + registered.sort_by(|a, b| (a.1, a.2, a.0).cmp(&(b.1, b.2, b.0))); + let mut seen = rustc_hash::FxHashSet::default(); + registered.retain(|r| seen.insert(r.0)); + let stable = |n: &NodeIdx| (view.file(view.node(*n).file()).path(), view.node(*n).start_line(), *n); + dispatchers.sort_by_key(stable); + dispatchers.dedup(); + // the one function a name reaches: unique in the project, else in the registering directory + let handler = |name: &str, at: &str| -> Option { + let cands: Vec = view + .by_name(name) + .filter(|n| matches!(view.node(*n).kind(), NodeKind::Function | NodeKind::Method) && view.file(view.node(*n).file()).lang() == Lang::Go) + .collect(); + if cands.len() == 1 { + return Some(cands[0]); + } + let dir = parent_dir(at); + let mut local = cands.into_iter().filter(|n| parent_dir(view.file(view.node(*n).file()).path()) == dir); + let first = local.next()?; + local.next().is_none().then_some(first) + }; + for disp in dispatchers { + let mut added = 0; + for &(name, path, line) in ®istered { + if added >= MAX_HANDLERS { + break; + } + let Some(h) = handler(name, path) else { continue }; + if h == disp { + continue; + } + out.push( + EdgeKind::Calls, + disp, + h, + view.node(disp).start_line(), + "gin-middleware-chain", + Some(format!("{path}:{line}")), + Some(("via", name)), + order_of("gin-middleware-chain"), + ); + added += 1; + } + } +} diff --git a/crates/cg-resolve/src/synth/go.rs b/crates/cg-resolve/src/synth/go.rs index fcb53a6..eb6af7e 100644 --- a/crates/cg-resolve/src/synth/go.rs +++ b/crates/cg-resolve/src/synth/go.rs @@ -4,8 +4,12 @@ //! unexported method name only matches within its package. A type is compared by its name with //! the package qualifier left off (the two declarations may sit in different packages); a true //! alias, a type parameter and a `func`/`struct`/`interface` literal type rule nothing out. +//! +//! A type embedded from outside the index brings what `go_std` knows of it (`io.Reader`'s +//! `Read`, `error`'s `Error`), by name and counts; any other outside type is opaque, and an +//! interface embedding one is satisfied only by a type that embeds the same one. -use super::sites::{PASS_GO_TYPES, ROLE_GO_ALIAS, ROLE_GO_GENERIC}; +use super::sites::{PASS_GO_EMBEDS, PASS_GO_TYPES, ROLE_GO_ALIAS, ROLE_GO_GENERIC}; use super::{Out, MAX_GO_IMPLEMENTERS}; use crate::engine::Engine; use crate::project::parent_dir; @@ -161,6 +165,28 @@ fn name_key(view: &cg_store::View, file: cg_core::FileId, name: &str) -> String } } +/// [`method_key`] of a standard-library method: an unexported one is spelled so that no +/// project directory matches it. +fn std_key(path: &str, name: &str) -> String { + if name.chars().next().is_some_and(char::is_uppercase) { + name.to_owned() + } else { + format!("std:{path}\0{name}") + } +} + +/// What the types a Go type embeds from outside the index bring. +#[derive(Default)] +struct Outside { + /// Standard-library methods: key, parameter count, result count. + std: Vec<(String, usize, usize)>, + /// Outside types the table does not know, by import path (or directory) and name. + opaque: Vec, +} + +/// One member of a method set: a declaration, or a standard-library method with no node. +type Member = (Option, Option<(usize, usize, Sig)>); + pub(super) fn go_implements(e: &Engine<'_>, out: &mut Out) { let view = e.view; let mut implementers: Vec = Vec::new(); @@ -248,8 +274,56 @@ pub(super) fn go_implements(e: &Engine<'_>, out: &mut Out) { fields_of.entry(t).or_default().push(name_key(view, f, view.text_str(b.name))); } } + // Embedded types the resolver did not bind. The kernel writes an embedding without its + // package (`sandbox.Manager` is `Manager`), so the written qualifier finds a project type the + // name alone could not; a type from outside the project brings what the standard-library table + // knows of it, or stays opaque. + let mut extra: FxHashMap> = FxHashMap::default(); + let mut outside: FxHashMap = FxHashMap::default(); + for f in view.files() { + if view.file(f).lang() != Lang::Go { + continue; + } + let dir = parent_dir(view.file(f).path()); + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_GO_EMBEDS && s.node.0 != cg_core::NONE) { + let (qualifier, name) = (view.text_str(cg_core::StrId(s.key_a)), view.text_str(cg_core::StrId(s.key_b))); + if e.direct_supers(s.node).iter().any(|t| is_go_type.contains(t) && view.node(*t).name() == name) { + continue; // bound: its methods come through the closure + } + let path = view.text_str(s.payload); + let target = if qualifier.is_empty() { crate::modules::ImportTarget::NotFound } else { e.modules.resolve_import(Lang::Go, path, f) }; + let project = match target { + _ if qualifier.is_empty() => types_in_dir.get(&(dir, name)).copied(), + crate::modules::ImportTarget::Pkg(p) => { + e.pkg_defs.get(p.0 as usize).and_then(|m| m.get(name)).and_then(|l| l.iter().copied().find(|n| is_go_type.contains(n))) + } + _ => None, + }; + if let Some(t) = project.filter(|t| *t != s.node) { + extra.entry(s.node).or_default().push(t); + continue; + } + if !qualifier.is_empty() && !matches!(target, crate::modules::ImportTarget::External) { + continue; // a project package without the type: nothing is known of it + } + let entry = outside.entry(s.node).or_default(); + match super::go_std::methods(path, name) { + Some(ms) => entry.std.extend(ms.into_iter().map(|(m, p, r)| (std_key(path, m), p, r))), + None if !path.is_empty() => entry.opaque.push(format!("{path}.{name}")), + None => entry.opaque.push(format!("{dir}#{name}")), + } + } + } // embedding: Go records it as `extends` - let embeds = |t: NodeIdx| -> Vec { e.direct_supers(t).iter().copied().filter(|s| is_go_type.contains(s)).collect() }; + let embeds = |t: NodeIdx| -> Vec { + let mut v: Vec = e.direct_supers(t).iter().copied().filter(|s| is_go_type.contains(s)).collect(); + for x in extra.get(&t).into_iter().flatten() { + if !v.contains(x) { + v.push(*x); + } + } + v + }; let closure = |t: NodeIdx| -> Vec { let mut seen = vec![t]; let mut i = 0; @@ -263,43 +337,60 @@ pub(super) fn go_implements(e: &Engine<'_>, out: &mut Out) { } seen }; + let std_member = |p: usize, r: usize| -> Member { (None, Some((p, r, vec![None; p + r]))) }; + let opaque_of = + |t: NodeIdx| -> FxHashSet<&str> { closure(t).iter().filter_map(|ty| outside.get(ty)).flat_map(|o| o.opaque.iter().map(String::as_str)).collect() }; // For interfaces retain declarations from embedded interfaces. For concrete types, Go // selects the shallowest embedded method; two paths at that depth are ambiguous even if // they eventually lead to the same declaration (a diamond). - type MethodSet = FxHashMap)>>; + type MethodSet = FxHashMap>; let interface_methods = |t: NodeIdx| -> MethodSet { let mut set: MethodSet = FxHashMap::default(); for ty in closure(t) { for &m in methods_of.get(&ty).map(Vec::as_slice).unwrap_or(&[]) { - set.entry(method_key(view, m)).or_default().push((m, view.node(m).signature().and_then(method_types))); + set.entry(method_key(view, m)).or_default().push((Some(m), view.node(m).signature().and_then(method_types))); + } + for (key, p, r) in outside.get(&ty).map(|o| o.std.as_slice()).unwrap_or(&[]) { + set.entry(key.clone()).or_default().push(std_member(*p, *r)); } } set }; let concrete_methods = |t: NodeIdx| -> MethodSet { - let mut selected: FxHashMap, bool)> = FxHashMap::default(); + // key -> (depth, declarations reached there, standard-library methods reached there, a field) + type Selected = FxHashMap, Vec<(usize, usize)>, bool)>; + let mut selected: Selected = FxHashMap::default(); let mut frontier = vec![(t, vec![t])]; + // standard-library types embedded one level below the current frontier + let mut std_next: Vec<&(String, usize, usize)> = Vec::new(); for depth in 0..10 { - if frontier.is_empty() { + if frontier.is_empty() && std_next.is_empty() { break; } if frontier.len() > 1024 { return MethodSet::default(); } + for (key, p, r) in std::mem::take(&mut std_next) { + let entry = selected.entry(key.clone()).or_insert_with(|| (depth, Vec::new(), Vec::new(), false)); + if entry.0 == depth { + entry.2.push((*p, *r)); + } + } let mut next = Vec::new(); for (ty, path) in frontier { for &m in methods_of.get(&ty).map(Vec::as_slice).unwrap_or(&[]) { - let entry = selected.entry(method_key(view, m)).or_insert_with(|| (depth, Vec::new(), false)); + let entry = selected.entry(method_key(view, m)).or_insert_with(|| (depth, Vec::new(), Vec::new(), false)); if entry.0 == depth { entry.1.push(m); } } for key in fields_of.get(&ty).into_iter().flatten() { - let entry = selected.entry(key.clone()).or_insert_with(|| (depth, Vec::new(), false)); + let entry = selected.entry(key.clone()).or_insert_with(|| (depth, Vec::new(), Vec::new(), false)); if entry.0 == depth { - entry.2 = true; + entry.3 = true; } } + std_next.extend(outside.get(&ty).map(|o| o.std.as_slice()).unwrap_or(&[])); for embedded in embeds(ty) { if !path.contains(&embedded) { let mut child = path.clone(); @@ -312,7 +403,17 @@ pub(super) fn go_implements(e: &Engine<'_>, out: &mut Out) { } selected .into_iter() - .filter_map(|(k, (_, ms, field))| (ms.len() == 1 && !field).then(|| (k, vec![(ms[0], view.node(ms[0]).signature().and_then(method_types))]))) + .filter_map(|(k, (_, ms, std, field))| { + if field || ms.len() + std.len() != 1 { + return None; + } + let member = match (ms.first(), std.first()) { + (Some(m), _) => (Some(*m), view.node(*m).signature().and_then(method_types)), + (None, Some((p, r))) => std_member(*p, *r), + _ => return None, + }; + Some((k, vec![member])) + }) .collect() }; let open = |t: &Option| -> bool { @@ -327,17 +428,19 @@ pub(super) fn go_implements(e: &Engine<'_>, out: &mut Out) { let (Some(h), Some(w)) = (have, want) else { return true }; h.0 == w.0 && h.1 == w.1 && h.2.iter().zip(&w.2).all(|(a, b)| a == b || open(a) || open(b)) }; - let sets: Vec<(NodeIdx, MethodSet)> = implementers.iter().map(|&t| (t, concrete_methods(t))).filter(|(_, s)| !s.is_empty()).collect(); + let sets: Vec<(NodeIdx, MethodSet, FxHashSet<&str>)> = + implementers.iter().map(|&t| (t, concrete_methods(t), opaque_of(t))).filter(|(_, s, o)| !s.is_empty() || !o.is_empty()).collect(); for &iface in &interfaces { let want = interface_methods(iface); - if want.is_empty() { + let want_opaque = opaque_of(iface); + if want.is_empty() && want_opaque.is_empty() { continue; // `any` would match everything } let mut direct = Vec::new(); let mut promoted = Vec::new(); - for (t, have) in &sets { + for (t, have, have_opaque) in &sets { let covers = want.iter().all(|(key, wanted)| have.get(key).is_some_and(|got| wanted.iter().all(|(_, w)| got.iter().any(|(_, g)| fits(g, w))))); - if !covers || embeds(*t).contains(&iface) { + if !covers || !want_opaque.is_subset(have_opaque) || embeds(*t).contains(&iface) { continue; } let own_covers = want.keys().all(|key| methods_of.get(t).is_some_and(|ms| ms.iter().any(|m| &method_key(view, *m) == key))); @@ -362,13 +465,15 @@ pub(super) fn go_implements(e: &Engine<'_>, out: &mut Out) { ); // Link the interface method to the actual declaration selected by Go's embedded // method set, including methods declared in another file or embedded type. - if let Some((_, have)) = sets.iter().find(|(ty, _)| *ty == t) { + if let Some((_, have, _)) = sets.iter().find(|(ty, ..)| *ty == t) { let mut added = 0; let mut wanted_methods: Vec<_> = want.iter().collect(); wanted_methods.sort_by_key(|(key, _)| *key); for (key, wanted) in wanted_methods { for (bm, _) in wanted { + let Some(bm) = bm else { continue }; for (m, _) in have.get(key).into_iter().flatten() { + let Some(m) = m else { continue }; if added >= super::MAX_IMPLS_PER_PAIR || bm == m { continue; } @@ -393,6 +498,115 @@ pub(super) fn go_implements(e: &Engine<'_>, out: &mut Out) { } } +/// The receiver methods of each Go struct, by the struct's directory and name. +fn struct_methods(e: &Engine<'_>) -> (Vec, FxHashMap>) { + let view = e.view; + let mut structs = Vec::new(); + let mut by_name: FxHashMap<(&str, &str), NodeIdx> = FxHashMap::default(); + let mut receiver_methods = Vec::new(); + for f in view.files() { + if view.file(f).lang() != Lang::Go { + continue; + } + let dir = parent_dir(view.file(f).path()); + for n in view.nodes_in_file(f).map(NodeIdx) { + let node = view.node(n); + match node.kind() { + NodeKind::Struct => { + structs.push(n); + by_name.entry((dir, node.name())).or_insert(n); + } + NodeKind::Method => receiver_methods.push(n), + _ => {} + } + } + } + let mut methods: FxHashMap> = FxHashMap::default(); + for m in receiver_methods { + let node = view.node(m); + let Some((owner, name)) = node.qname().rsplit_once("::") else { continue }; + if name != node.name() || owner.contains("::") { + continue; + } + if let Some(&t) = by_name.get(&(parent_dir(view.file(node.file()).path()), owner)) { + methods.entry(t).or_default().push(m); + } + } + (structs, methods) +} + +/// A protoc-generated `Unimplemented*Server` stub stands for the service the project implements +/// by hand (upstream `goGrpcStubImplEdges`): each stub method links to the same-named method of +/// every hand-written struct that has all of the stub's methods. The stub must sit in a +/// generated file, and generated siblings (`UnsafeFooServer`, clients) are never candidates. +pub(super) fn grpc_stubs(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + let generated = |n: NodeIdx| view.file(view.node(n).file()).flags() & cg_extract::fileflags::GENERATED != 0; + let (mut structs, methods) = struct_methods(e); + let stubs: Vec = structs + .iter() + .copied() + .filter(|s| { + let name = view.node(*s).name(); + name.starts_with("Unimplemented") && name.ends_with("Server") && name.len() > "UnimplementedServer".len() && generated(*s) + }) + .collect(); + if stubs.is_empty() { + return; + } + let stable = |n: &NodeIdx| (view.file(view.node(*n).file()).path(), view.node(*n).start_line(), *n); + structs.sort_by_key(stable); + let names = |t: NodeIdx| -> FxHashSet<&str> { methods.get(&t).into_iter().flatten().map(|m| view.node(*m).name()).collect() }; + for stub in stubs { + let mut rpcs: Vec = methods + .get(&stub) + .into_iter() + .flatten() + .copied() + .filter(|m| { + let n = view.node(*m).name(); + !n.starts_with("mustEmbed") && n != "testEmbeddedByValue" + }) + .collect(); + if rpcs.is_empty() { + continue; + } + rpcs.sort_by_key(stable); + for &cand in &structs { + if cand == stub || generated(cand) { + continue; + } + let have = names(cand); + if !rpcs.iter().all(|m| have.contains(view.node(*m).name())) { + continue; + } + let mut impls: Vec = methods.get(&cand).cloned().unwrap_or_default(); + impls.sort_by_key(stable); + let mut added = 0; + for &sm in &rpcs { + for &cm in impls.iter().filter(|cm| view.node(**cm).name() == view.node(sm).name()) { + if added >= MAX_GO_IMPLEMENTERS { + break; + } + let method = view.node(cm); + let at = format!("{}:{}", view.file(method.file()).path(), method.start_line()); + out.push( + EdgeKind::Calls, + sm, + cm, + view.node(sm).start_line(), + "go-grpc-stub-impl", + Some(at), + Some(("via", method.name())), + super::order_of("go-grpc-stub-impl"), + ); + added += 1; + } + } + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/crates/cg-resolve/src/synth/go_fields.rs b/crates/cg-resolve/src/synth/go_fields.rs new file mode 100644 index 0000000..0d00af3 --- /dev/null +++ b/crates/cg-resolve/src/synth/go_fields.rs @@ -0,0 +1,284 @@ +//! Go func-field dispatch (upstream `go-func-field-synthesizer.ts`): a struct holds a function +//! in a field and calls it through the field (`d.refreshf(ctx)`), which no method answers. The +//! call reaches every function or method value the field is given: +//! +//! * a composite literal, keyed `S{F: v}` or positional `S{a, v}`; +//! * an assignment `x.F = v` / `x.y.F = v`; +//! * one level of field-to-field copy: `S{F: o.G}` gives `S.F` what `T.G` is given directly. +//! +//! A field is a func field when its type is a literal `func(...)` or a project type declared as +//! one (`type HandlerFunc func(...)`). A call or an assignment counts only where the receiver's +//! type is known; one field hop is allowed, and a field one embedded struct promotes. A value is +//! what the reference written there resolves to, else the method `m` of `x.m`'s type (an +//! interface's, whose interface-impl edges carry on). A conversion to a func type is the value it +//! converts. Generated files are not read, and a value a package's test puts in a field links no +//! call outside that package's directory. + +use super::sites::{PASS_GO_FIELDS, ROLE_GO_FIELD_ASSIGN, ROLE_GO_FIELD_LITERAL, ROLE_GO_FUNC_FIELD, ROLE_GO_FUNC_TYPE}; +use super::{order_of, Out}; +use crate::engine::Engine; +use crate::project::parent_dir; +use crate::resolve::{Accept, FileCx, NameRes, TypeRes, WorkerCx}; +use crate::shape::{parse_shape, Shape}; +use cg_core::{EdgeKind, FileId, Lang, NodeIdx, NodeKind, RefKind, StrId, NONE}; +use cg_extract::fileflags; +use cg_store::{status, BindingRec, SiteRec, View}; +use rustc_hash::{FxHashMap, FxHashSet}; + +const MAX_VALUES_PER_FIELD: usize = 40; + +/// A function put in a field. +#[derive(Clone)] +struct Value { + target: NodeIdx, + file: FileId, + line: u32, + test: bool, + /// The field the value was copied from (`Options.RefreshF`). + via: Option, +} + +type Channel = (NodeIdx, String); + +fn text(view: &View, id: u32) -> &str { + view.text_str(StrId(id)) +} + +fn is_fn(view: &View, n: NodeIdx) -> bool { + matches!(view.node(n).kind(), NodeKind::Function | NodeKind::Method) +} + +/// The type `x` (or `x.y`, one field hop) holds where it is written. +fn receiver_type<'e, 'v>(cx: &FileCx<'e, 'v>, wcx: &mut WorkerCx<'e, 'v>, receiver: &str, scope: u32, line: u32) -> Option { + let (root, hop) = match receiver.split_once('.') { + Some((a, b)) => (a, Some(b)), + None => (receiver, None), + }; + let b = cx.lookup_binding(root, scope, line, false).or_else(|| cx.file_binding(root))?; + let mut t = cx.binding_type(wcx, b)?; + if let Some(h) = hop { + t = cx.field_type(wcx, t, h)?; + } + Some(t) +} + +/// What the value written at `line` names: the resolved reference there, else the method of +/// `x.m`'s type, a package's function `pkg.F` or a function in scope. +fn value_target<'e, 'v>(e: &'e Engine<'v>, cx: &FileCx<'e, 'v>, wcx: &mut WorkerCx<'e, 'v>, value: &str, scope: u32, line: u32) -> Option { + let view = e.view; + let tail = value.rsplit('.').next().unwrap_or(value); + let mut hit = None; + for r in view.refs_of_file(cx.file) { + if r.line != line { + continue; + } + if !matches!(RefKind::from_wire(r.kind), Some(RefKind::FunctionRef | RefKind::Edge(EdgeKind::References))) { + continue; + } + let name = view.text_str(r.name); + if name != value && name != tail { + continue; + } + let o = cx.resolve_ref(wcx, r); + if o.status != status::RESOLVED { + continue; + } + let t = NodeIdx(o.target); + if hit.is_some_and(|h| h != t) { + return None; + } + hit = Some(t); + } + if let Some(t) = hit.filter(|t| is_fn(view, *t)) { + return Some(t); + } + match value.split_once('.') { + Some((root, m)) if !m.contains('.') => { + if let Some(b) = cx.lookup_binding(root, scope, line, false).or_else(|| cx.file_binding(root)) { + let t = cx.binding_type(wcx, b)?; + return e.find_member(t, m).filter(|n| is_fn(view, *n)); + } + match cx.resolve_name(wcx, root, Accept::Any, scope, line, None, false) { + NameRes::Pkg(p) => e.pkg_defs.get(p.0 as usize)?.get(m)?.iter().copied().find(|n| is_fn(view, *n)), + _ => None, + } + } + None => match cx.resolve_name(wcx, value, Accept::Callable, scope, line, None, true) { + NameRes::Found(n, ..) if is_fn(view, n) => Some(n), + _ => None, + }, + _ => None, + } +} + +pub(crate) fn func_fields<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, 'v>) { + let view = e.view; + let go_files: Vec = view.files().filter(|f| view.file(*f).lang() == Lang::Go).collect(); + let generated = |f: FileId| view.file(f).flags() & fileflags::GENERATED != 0; + let mut func_types: FxHashSet<(&str, &str)> = FxHashSet::default(); + let mut fields: FxHashMap> = FxHashMap::default(); + let mut writes: Vec<(FileId, &SiteRec)> = Vec::new(); + for &f in &go_files { + let dir = parent_dir(view.file(f).path()); + for s in view.sites_of(f).iter().filter(|s| s.pass == PASS_GO_FIELDS) { + match s.role { + ROLE_GO_FUNC_TYPE => { + func_types.insert((dir, text(view, s.key_a))); + } + ROLE_GO_FUNC_FIELD if s.node.0 != NONE => { + fields.entry(s.node).or_default().insert(text(view, s.key_a)); + } + ROLE_GO_FIELD_LITERAL | ROLE_GO_FIELD_ASSIGN if !generated(f) => writes.push((f, s)), + _ => {} + } + } + } + if writes.is_empty() { + return; + } + let is_func_type = |n: NodeIdx| -> bool { + let node = view.node(n); + func_types.contains(&(parent_dir(view.file(node.file()).path()), node.name())) + }; + // fields declared with a project func type (`h HandlerFunc`) + let mut declared: FxHashMap> = FxHashMap::default(); + let type_names: FxHashSet<&str> = func_types.iter().map(|(_, n)| *n).collect(); + for &f in &go_files { + let structs: Vec = view.nodes_in_file(f).map(NodeIdx).filter(|n| view.node(*n).kind() == NodeKind::Struct).collect(); + if structs.is_empty() { + continue; + } + let cx = wcx.file_cx(e, f); + for t in structs { + let list = cx.declared_fields(t); + for b in &list { + let dt = view.text_str(b.decl_type); + if dt.is_empty() || !type_names.contains(dt.rsplit('.').next().unwrap_or(dt)) { + continue; + } + if let TypeRes::Node(ft) = cx.lookup_type(wcx, dt) { + if is_func_type(ft) { + fields.entry(t).or_default().insert(view.text_str(b.name)); + } + } + } + declared.insert(t, list); + } + } + // the field a write or a call reaches: the holder's own, or one an embedded struct promotes + let channel = |holder: NodeIdx, field: &str| -> Option { + if fields.get(&holder).is_some_and(|s| s.contains(field)) { + return Some((holder, field.to_owned())); + } + if declared.get(&holder).is_some_and(|l| l.iter().any(|b| view.text_str(b.name) == field)) { + return None; // a field of its own shadows a promoted one + } + let mut hits = e.direct_supers(holder).iter().copied().filter(|s| fields.get(s).is_some_and(|set| set.contains(field))); + let first = hits.next()?; + hits.next().is_none().then(|| (first, field.to_owned())) + }; + + let mut direct: FxHashMap> = FxHashMap::default(); + let mut copies: Vec<(Channel, Channel, FileId, u32)> = Vec::new(); + for (f, s) in writes { + let cx = wcx.file_cx(e, f); + let payload = view.text_str(s.payload); + let Some((col, value)) = payload.split_once('|') else { continue }; + let scope = cx.scope_at(s.line, col.parse().unwrap_or(0)); + let holder = match s.role { + ROLE_GO_FIELD_LITERAL => match cx.lookup_type(wcx, text(view, s.key_a)) { + TypeRes::Node(t) if view.node(t).kind() == NodeKind::Struct => Some(t), + _ => None, + }, + _ => receiver_type(&cx, wcx, text(view, s.key_a), scope, s.line), + }; + let Some(holder) = holder else { continue }; + let mut field = text(view, s.key_b).to_owned(); + if let Some(i) = field.strip_prefix('#').and_then(|i| i.parse::().ok()) { + match declared.get(&holder).and_then(|l| l.get(i)) { + Some(b) => field = view.text_str(b.name).to_owned(), + None => continue, + } + } + let Some(ch) = channel(holder, &field) else { continue }; + // `HandlerFunc(h.serve)` is the value it converts + let value = match value.split_once('(') { + Some((callee, rest)) => match cx.lookup_type(wcx, callee) { + TypeRes::Node(t) if is_func_type(t) => rest.strip_suffix(')').unwrap_or(rest), + _ => continue, + }, + None => value, + }; + // a value copied from another struct's func field: `S{F: o.G}` + if let Some((root, g)) = value.split_once('.').filter(|(_, g)| !g.contains('.')) { + if let Some(src) = receiver_type(&cx, wcx, root, scope, s.line).and_then(|t| channel(t, g)) { + copies.push((ch, src, f, s.line)); + continue; + } + } + if let Some(target) = value_target(e, &cx, wcx, value, scope, s.line) { + direct.entry(ch).or_default().push(Value { target, file: f, line: s.line, test: cx.is_test, via: None }); + } + } + let mut values = direct.clone(); + for (dst, src, f, line) in copies { + let Some(given) = direct.get(&src) else { continue }; + let via = format!("{}.{}", view.node(src.0).name(), src.1); + let test = view.file(f).flags() & fileflags::TEST != 0; + let list = values.entry(dst).or_default(); + for v in given { + // the copy's own wiring is where the value reaches this field + list.push(Value { target: v.target, file: f, line, test: test || v.test, via: Some(via.clone()) }); + } + } + // production wiring before test wiring, then by place; one value per target + for list in values.values_mut() { + list.sort_by(|a, b| (a.test, view.file(a.file).path(), a.line, a.target).cmp(&(b.test, view.file(b.file).path(), b.line, b.target))); + let mut seen = FxHashSet::default(); + list.retain(|v| seen.insert(v.target)); + list.truncate(MAX_VALUES_PER_FIELD); + } + if values.is_empty() { + return; + } + let names: FxHashSet<&str> = values.keys().map(|(_, f)| f.as_str()).collect(); + for &f in &go_files { + if generated(f) { + continue; + } + let dir = parent_dir(view.file(f).path()); + for r in view.refs_of_file(f) { + if RefKind::from_wire(r.kind) != Some(RefKind::Edge(EdgeKind::Calls)) { + continue; + } + let Shape::Member { root, path, method } = parse_shape(view.text_str(r.name)) else { continue }; + if path.len() > 1 || !names.contains(method) { + continue; + } + let cx = wcx.file_cx(e, f); + let scope = cx.scope_at(r.line, r.col); + let receiver = match path.first() { + Some(hop) => format!("{root}.{hop}"), + None => root.to_owned(), + }; + let Some(t) = receiver_type(&cx, wcx, &receiver, scope, r.line) else { continue }; + let Some(ch) = channel(t, method) else { continue }; + let Some(list) = values.get(&ch) else { continue }; + let field = format!("{}.{}", view.node(ch.0).name(), ch.1); + for v in list { + if v.target == r.from || (v.test && parent_dir(view.file(v.file).path()) != dir) { + continue; + } + let mut meta = serde_json::json!({ + "synthesizedBy": "go-func-field", + "field": field, + "registeredAt": format!("{}:{}", view.file(v.file).path(), v.line), + }); + if let Some(via) = &v.via { + meta["via"] = via.as_str().into(); + } + out.push_meta(EdgeKind::Calls, r.from, v.target, r.line, NONE, meta, order_of("go-func-field")); + } + } + } +} diff --git a/crates/cg-resolve/src/synth/go_std.rs b/crates/cg-resolve/src/synth/go_std.rs new file mode 100644 index 0000000..ead9440 --- /dev/null +++ b/crates/cg-resolve/src/synth/go_std.rs @@ -0,0 +1,173 @@ +//! The methods of the standard-library types Go code embeds (upstream `go-stdlib-methods.ts`), +//! for Go's implicit interface satisfaction. An embedded type from outside the index +//! (`io.Closer`, `http.ResponseWriter`, `*os.File`) resolves to nothing, so without this an +//! interface embedding `io.Reader` asked only for its own methods, and a struct embedding +//! `*os.File` had no `Read`. +//! +//! Each method is listed as `name:params/results`, the counts a stored signature is compared +//! by. An interface lists every method, embedded ones included, and so does a struct with those +//! its embedded fields promote. Unexported names are kept where an interface requires one +//! (`testing.TB`'s `private`): only an embedding of the same type satisfies those. + +/// (import path, type, `name:params/results` methods). Read from Go's source (go1.27.3) by the +/// upstream `scripts/gen-go-stdlib-methods.mjs`; see `go_std.rs` for what is listed. +const TABLE: &[(&str, &str, &str)] = &[ + ("io", "Reader", "Read:1/2"), + ("io", "Writer", "Write:1/2"), + ("io", "Closer", "Close:0/1"), + ("io", "Seeker", "Seek:2/2"), + ("io", "ReadWriter", "Read:1/2 Write:1/2"), + ("io", "ReadCloser", "Close:0/1 Read:1/2"), + ("io", "WriteCloser", "Close:0/1 Write:1/2"), + ("io", "ReadWriteCloser", "Close:0/1 Read:1/2 Write:1/2"), + ("io", "ReadSeeker", "Read:1/2 Seek:2/2"), + ("io", "ReadSeekCloser", "Close:0/1 Read:1/2 Seek:2/2"), + ("io", "WriteSeeker", "Seek:2/2 Write:1/2"), + ("io", "ReadWriteSeeker", "Read:1/2 Seek:2/2 Write:1/2"), + ("io", "ReaderFrom", "ReadFrom:1/2"), + ("io", "WriterTo", "WriteTo:1/2"), + ("io", "ReaderAt", "ReadAt:2/2"), + ("io", "WriterAt", "WriteAt:2/2"), + ("io", "ByteReader", "ReadByte:0/2"), + ("io", "ByteScanner", "ReadByte:0/2 UnreadByte:0/1"), + ("io", "ByteWriter", "WriteByte:1/1"), + ("io", "RuneReader", "ReadRune:0/3"), + ("io", "RuneScanner", "ReadRune:0/3 UnreadRune:0/1"), + ("io", "StringWriter", "WriteString:1/2"), + ("io", "PipeReader", "Close:0/1 CloseWithError:1/1 Read:1/2"), + ("io", "PipeWriter", "Close:0/1 CloseWithError:1/1 Write:1/2"), + ("io", "SectionReader", "Outer:0/3 Read:1/2 ReadAt:2/2 Seek:2/2 Size:0/1"), + ("io/fs", "FS", "Open:1/2"), + ("io/fs", "File", "Close:0/1 Read:1/2 Stat:0/2"), + ("io/fs", "DirEntry", "Info:0/2 IsDir:0/1 Name:0/1 Type:0/1"), + ("io/fs", "FileInfo", "IsDir:0/1 ModTime:0/1 Mode:0/1 Name:0/1 Size:0/1 Sys:0/1"), + ("io/fs", "ReadDirFile", "Close:0/1 Read:1/2 ReadDir:1/2 Stat:0/2"), + ("io/fs", "ReadDirFS", "Open:1/2 ReadDir:1/2"), + ("io/fs", "ReadFileFS", "Open:1/2 ReadFile:1/2"), + ("io/fs", "StatFS", "Open:1/2 Stat:1/2"), + ("io/fs", "SubFS", "Open:1/2 Sub:1/2"), + ("io/fs", "GlobFS", "Glob:1/2 Open:1/2"), + ("os", "File", "Chdir:0/1 Chmod:1/1 Chown:2/1 Close:0/1 Fd:0/1 Name:0/1 PollFD:0/1 Read:1/2 ReadAt:2/2 ReadDir:1/2 ReadFrom:1/2 Readdir:1/2 Readdirnames:1/2 Seek:2/2 SetDeadline:1/1 SetReadDeadline:1/1 SetWriteDeadline:1/1 Stat:0/2 Sync:0/1 SyscallConn:0/2 Truncate:1/1 Write:1/2 WriteAt:2/2 WriteString:1/2 WriteTo:1/2"), + ("os", "FileInfo", "IsDir:0/1 ModTime:0/1 Mode:0/1 Name:0/1 Size:0/1 Sys:0/1"), + ("os", "DirEntry", "Info:0/2 IsDir:0/1 Name:0/1 Type:0/1"), + ("os", "Signal", "Signal:0/0 String:0/1"), + ("fmt", "Stringer", "String:0/1"), + ("fmt", "GoStringer", "GoString:0/1"), + ("fmt", "Formatter", "Format:2/0"), + ("fmt", "State", "Flag:1/1 Precision:0/2 Width:0/2 Write:1/2"), + ("fmt", "Scanner", "Scan:2/1"), + ("fmt", "ScanState", "Read:1/2 ReadRune:0/3 SkipSpace:0/0 Token:2/2 UnreadRune:0/1 Width:0/2"), + ("sort", "Interface", "Len:0/1 Less:2/1 Swap:2/0"), + ("container/heap", "Interface", "Len:0/1 Less:2/1 Pop:0/1 Push:1/0 Swap:2/0"), + ("context", "Context", "Deadline:0/2 Done:0/1 Err:0/1 Value:1/1"), + ("sync", "Locker", "Lock:0/0 Unlock:0/0"), + ("sync", "Mutex", "Lock:0/0 TryLock:0/1 Unlock:0/0"), + ("sync", "RWMutex", "Lock:0/0 RLock:0/0 RLocker:0/1 RUnlock:0/0 TryLock:0/1 TryRLock:0/1 Unlock:0/0"), + ("net", "Conn", "Close:0/1 LocalAddr:0/1 Read:1/2 RemoteAddr:0/1 SetDeadline:1/1 SetReadDeadline:1/1 SetWriteDeadline:1/1 Write:1/2"), + ("net", "PacketConn", "Close:0/1 LocalAddr:0/1 ReadFrom:1/3 SetDeadline:1/1 SetReadDeadline:1/1 SetWriteDeadline:1/1 WriteTo:2/2"), + ("net", "Listener", "Accept:0/2 Addr:0/1 Close:0/1"), + ("net", "Addr", "Network:0/1 String:0/1"), + ("net", "Error", "Error:0/1 Temporary:0/1 Timeout:0/1"), + ("net", "TCPConn", "Close:0/1 CloseRead:0/1 CloseWrite:0/1 File:0/2 LocalAddr:0/1 MultipathTCP:0/2 Read:1/2 ReadFrom:1/2 RemoteAddr:0/1 SetDeadline:1/1 SetKeepAlive:1/1 SetKeepAliveConfig:1/1 SetKeepAlivePeriod:1/1 SetLinger:1/1 SetNoDelay:1/1 SetReadBuffer:1/1 SetReadDeadline:1/1 SetWriteBuffer:1/1 SetWriteDeadline:1/1 SyscallConn:0/2 Write:1/2 WriteTo:1/2"), + ("net", "UDPConn", "Close:0/1 File:0/2 LocalAddr:0/1 Read:1/2 ReadFrom:1/3 ReadFromUDP:1/3 ReadFromUDPAddrPort:1/3 ReadMsgUDP:2/5 ReadMsgUDPAddrPort:2/5 RemoteAddr:0/1 SetDeadline:1/1 SetReadBuffer:1/1 SetReadDeadline:1/1 SetWriteBuffer:1/1 SetWriteDeadline:1/1 SyscallConn:0/2 Write:1/2 WriteMsgUDP:3/3 WriteMsgUDPAddrPort:3/3 WriteTo:2/2 WriteToUDP:2/2 WriteToUDPAddrPort:2/2"), + ("net", "UnixConn", "Close:0/1 CloseRead:0/1 CloseWrite:0/1 File:0/2 LocalAddr:0/1 Read:1/2 ReadFrom:1/3 ReadFromUnix:1/3 ReadMsgUnix:2/5 RemoteAddr:0/1 SetDeadline:1/1 SetReadBuffer:1/1 SetReadDeadline:1/1 SetWriteBuffer:1/1 SetWriteDeadline:1/1 SyscallConn:0/2 Write:1/2 WriteMsgUnix:3/3 WriteTo:2/2 WriteToUnix:2/2"), + ("net", "TCPListener", "Accept:0/2 AcceptTCP:0/2 Addr:0/1 Close:0/1 File:0/2 SetDeadline:1/1 SyscallConn:0/2"), + ("net", "UnixListener", "Accept:0/2 AcceptUnix:0/2 Addr:0/1 Close:0/1 File:0/2 SetDeadline:1/1 SetUnlinkOnClose:1/0 SyscallConn:0/2"), + ("net/http", "ResponseWriter", "Header:0/1 Write:1/2 WriteHeader:1/0"), + ("net/http", "Hijacker", "Hijack:0/3"), + ("net/http", "Flusher", "Flush:0/0"), + ("net/http", "CloseNotifier", "CloseNotify:0/1"), + ("net/http", "Pusher", "Push:2/1"), + ("net/http", "Handler", "ServeHTTP:2/0"), + ("net/http", "RoundTripper", "RoundTrip:1/2"), + ("net/http", "File", "Close:0/1 Read:1/2 Readdir:1/2 Seek:2/2 Stat:0/2"), + ("net/http", "FileSystem", "Open:1/2"), + ("net/http", "CookieJar", "Cookies:1/1 SetCookies:2/0"), + ("net/http", "ServeMux", "Handle:2/0 HandleFunc:2/0 Handler:1/2 ServeHTTP:2/0"), + ("net/http", "Transport", "CancelRequest:1/0 Clone:0/1 CloseIdleConnections:0/0 NewClientConn:3/2 RegisterProtocol:2/0 RoundTrip:1/2"), + ("net/http/httptest", "ResponseRecorder", "Flush:0/0 Header:0/1 Result:0/1 Write:1/2 WriteHeader:1/0 WriteString:1/2"), + ("bufio", "Reader", "Buffered:0/1 Discard:1/2 Peek:1/2 Read:1/2 ReadByte:0/2 ReadBytes:1/2 ReadLine:0/3 ReadRune:0/3 ReadSlice:1/2 ReadString:1/2 Reset:1/0 Size:0/1 UnreadByte:0/1 UnreadRune:0/1 WriteTo:1/2"), + ("bufio", "Writer", "Available:0/1 AvailableBuffer:0/1 Buffered:0/1 Flush:0/1 ReadFrom:1/2 Reset:1/0 Size:0/1 Write:1/2 WriteByte:1/1 WriteRune:1/2 WriteString:1/2"), + ("bufio", "ReadWriter", "Available:0/1 AvailableBuffer:0/1 Buffered:0/1 Discard:1/2 Flush:0/1 Peek:1/2 Read:1/2 ReadByte:0/2 ReadBytes:1/2 ReadFrom:1/2 ReadLine:0/3 ReadRune:0/3 ReadSlice:1/2 ReadString:1/2 Reset:1/0 Size:0/1 UnreadByte:0/1 UnreadRune:0/1 Write:1/2 WriteByte:1/1 WriteRune:1/2 WriteString:1/2 WriteTo:1/2"), + ("bytes", "Buffer", "Available:0/1 AvailableBuffer:0/1 Bytes:0/1 Cap:0/1 Grow:1/0 Len:0/1 Next:1/1 Peek:1/2 Read:1/2 ReadByte:0/2 ReadBytes:1/2 ReadFrom:1/2 ReadRune:0/3 ReadString:1/2 Reset:0/0 String:0/1 Truncate:1/0 UnreadByte:0/1 UnreadRune:0/1 Write:1/2 WriteByte:1/1 WriteRune:1/2 WriteString:1/2 WriteTo:1/2"), + ("bytes", "Reader", "Len:0/1 Read:1/2 ReadAt:2/2 ReadByte:0/2 ReadRune:0/3 Reset:1/0 Seek:2/2 Size:0/1 UnreadByte:0/1 UnreadRune:0/1 WriteTo:1/2"), + ("strings", "Reader", "Len:0/1 Read:1/2 ReadAt:2/2 ReadByte:0/2 ReadRune:0/3 Reset:1/0 Seek:2/2 Size:0/1 UnreadByte:0/1 UnreadRune:0/1 WriteTo:1/2"), + ("strings", "Builder", "Cap:0/1 Grow:1/0 Len:0/1 Reset:0/0 String:0/1 Write:1/2 WriteByte:1/1 WriteRune:1/2 WriteString:1/2"), + ("testing", "TB", "ArtifactDir:0/1 Attr:2/0 Chdir:1/0 Cleanup:1/0 Context:0/1 Error:1/0 Errorf:2/0 Fail:0/0 FailNow:0/0 Failed:0/1 Fatal:1/0 Fatalf:2/0 Helper:0/0 Log:1/0 Logf:2/0 Name:0/1 Output:0/1 Setenv:2/0 Skip:1/0 SkipNow:0/0 Skipf:2/0 Skipped:0/1 TempDir:0/1 private:0/0"), + ("testing", "T", "ArtifactDir:0/1 Attr:2/0 Chdir:1/0 Cleanup:1/0 Context:0/1 Deadline:0/2 Error:1/0 Errorf:2/0 Fail:0/0 FailNow:0/0 Failed:0/1 Fatal:1/0 Fatalf:2/0 Helper:0/0 Log:1/0 Logf:2/0 Name:0/1 Output:0/1 Parallel:0/0 Run:2/1 Setenv:2/0 Skip:1/0 SkipNow:0/0 Skipf:2/0 Skipped:0/1 TempDir:0/1 private:0/0"), + ("testing", "B", "ArtifactDir:0/1 Attr:2/0 Chdir:1/0 Cleanup:1/0 Context:0/1 Elapsed:0/1 Error:1/0 Errorf:2/0 Fail:0/0 FailNow:0/0 Failed:0/1 Fatal:1/0 Fatalf:2/0 Helper:0/0 Log:1/0 Logf:2/0 Loop:0/1 Name:0/1 Output:0/1 ReportAllocs:0/0 ReportMetric:2/0 ResetTimer:0/0 Run:2/1 RunParallel:1/0 SetBytes:1/0 SetParallelism:1/0 Setenv:2/0 Skip:1/0 SkipNow:0/0 Skipf:2/0 Skipped:0/1 StartTimer:0/0 StopTimer:0/0 TempDir:0/1 private:0/0"), + ("testing", "F", "Add:1/0 ArtifactDir:0/1 Attr:2/0 Chdir:1/0 Cleanup:1/0 Context:0/1 Error:1/0 Errorf:2/0 Fail:0/0 FailNow:0/0 Failed:0/1 Fatal:1/0 Fatalf:2/0 Fuzz:1/0 Helper:0/0 Log:1/0 Logf:2/0 Name:0/1 Output:0/1 Setenv:2/0 Skip:1/0 SkipNow:0/0 Skipf:2/0 Skipped:0/1 TempDir:0/1 private:0/0"), + ("log/slog", "Handler", "Enabled:2/1 Handle:2/1 WithAttrs:1/1 WithGroup:1/1"), + ("log/slog", "JSONHandler", "Enabled:2/1 Handle:2/1 WithAttrs:1/1 WithGroup:1/1"), + ("log/slog", "TextHandler", "Enabled:2/1 Handle:2/1 WithAttrs:1/1 WithGroup:1/1"), + ("encoding", "BinaryMarshaler", "MarshalBinary:0/2"), + ("encoding", "BinaryUnmarshaler", "UnmarshalBinary:1/1"), + ("encoding", "TextMarshaler", "MarshalText:0/2"), + ("encoding", "TextUnmarshaler", "UnmarshalText:1/1"), + ("encoding/json", "Marshaler", "MarshalJSON:0/2"), + ("encoding/json", "Unmarshaler", "UnmarshalJSON:1/1"), + ("encoding/json", "RawMessage", "MarshalJSON:0/2 UnmarshalJSON:1/1"), + ("encoding/xml", "Marshaler", "MarshalXML:2/1"), + ("encoding/xml", "Unmarshaler", "UnmarshalXML:2/1"), + ("encoding/gob", "GobEncoder", "GobEncode:0/2"), + ("encoding/gob", "GobDecoder", "GobDecode:1/1"), + ("expvar", "Var", "String:0/1"), + ("hash", "Hash", "BlockSize:0/1 Reset:0/0 Size:0/1 Sum:1/1 Write:1/2"), + ("hash", "Hash32", "BlockSize:0/1 Reset:0/0 Size:0/1 Sum:1/1 Sum32:0/1 Write:1/2"), + ("hash", "Hash64", "BlockSize:0/1 Reset:0/0 Size:0/1 Sum:1/1 Sum64:0/1 Write:1/2"), + ("crypto", "Signer", "Public:0/1 Sign:3/2"), + ("crypto", "Decrypter", "Decrypt:3/2 Public:0/1"), + ("crypto/elliptic", "Curve", "Add:4/2 Double:2/2 IsOnCurve:2/1 Params:0/1 ScalarBaseMult:1/2 ScalarMult:3/2"), + ("crypto/cipher", "Block", "BlockSize:0/1 Decrypt:2/0 Encrypt:2/0"), + ("crypto/cipher", "Stream", "XORKeyStream:2/0"), + ("crypto/cipher", "AEAD", "NonceSize:0/1 Open:4/2 Overhead:0/1 Seal:4/1"), + ("crypto/cipher", "BlockMode", "BlockSize:0/1 CryptBlocks:2/0"), + ("crypto/tls", "Conn", "Close:0/1 CloseWrite:0/1 ConnectionState:0/1 Handshake:0/1 HandshakeContext:1/1 LocalAddr:0/1 NetConn:0/1 OCSPResponse:0/1 Read:1/2 RemoteAddr:0/1 SetDeadline:1/1 SetReadDeadline:1/1 SetWriteDeadline:1/1 VerifyHostname:1/1 Write:1/2"), + ("image", "Image", "At:2/1 Bounds:0/1 ColorModel:0/1"), + ("image", "PalettedImage", "At:2/1 Bounds:0/1 ColorIndexAt:2/1 ColorModel:0/1"), + ("image/color", "Color", "RGBA:0/4"), + ("image/color", "Model", "Convert:1/1"), + ("database/sql", "Scanner", "Scan:1/1"), + ("database/sql/driver", "Valuer", "Value:0/2"), + ("database/sql/driver", "Driver", "Open:1/2"), + ("database/sql/driver", "Conn", "Begin:0/2 Close:0/1 Prepare:1/2"), + ("database/sql/driver", "Stmt", "Close:0/1 Exec:1/2 NumInput:0/1 Query:1/2"), + ("database/sql/driver", "Tx", "Commit:0/1 Rollback:0/1"), + ("database/sql/driver", "Rows", "Close:0/1 Columns:0/1 Next:1/1"), + ("database/sql/driver", "Result", "LastInsertId:0/2 RowsAffected:0/2"), + ("flag", "Value", "Set:1/1 String:0/1"), + ("flag", "Getter", "Get:0/1 Set:1/1 String:0/1"), + ("runtime", "Error", "Error:0/1 RuntimeError:0/0"), + ("reflect", "Type", "Align:0/1 AssignableTo:1/1 Bits:0/1 CanSeq:0/1 CanSeq2:0/1 ChanDir:0/1 Comparable:0/1 ConvertibleTo:1/1 Elem:0/1 Field:1/1 FieldAlign:0/1 FieldByIndex:1/1 FieldByName:1/2 FieldByNameFunc:1/2 Fields:0/1 Implements:1/1 In:1/1 Ins:0/1 IsVariadic:0/1 Key:0/1 Kind:0/1 Len:0/1 Method:1/1 MethodByName:1/2 Methods:0/1 Name:0/1 NumField:0/1 NumIn:0/1 NumMethod:0/1 NumOut:0/1 Out:1/1 Outs:0/1 OverflowComplex:1/1 OverflowFloat:1/1 OverflowInt:1/1 OverflowUint:1/1 PkgPath:0/1 Size:0/1 String:0/1 common:0/1 uncommon:0/1"), + ("time", "Time", "Add:1/1 AddDate:3/1 After:1/1 AppendBinary:1/2 AppendFormat:2/1 AppendText:1/2 Before:1/1 Clock:0/3 Compare:1/1 Date:0/3 Day:0/1 Equal:1/1 Format:1/1 GoString:0/1 GobDecode:1/1 GobEncode:0/2 Hour:0/1 ISOWeek:0/2 In:1/1 IsDST:0/1 IsZero:0/1 Local:0/1 Location:0/1 MarshalBinary:0/2 MarshalJSON:0/2 MarshalText:0/2 Minute:0/1 Month:0/1 Nanosecond:0/1 Round:1/1 Second:0/1 String:0/1 Sub:1/1 Truncate:1/1 UTC:0/1 Unix:0/1 UnixMicro:0/1 UnixMilli:0/1 UnixNano:0/1 UnmarshalBinary:1/1 UnmarshalJSON:1/1 UnmarshalText:1/1 Weekday:0/1 Year:0/1 YearDay:0/1 Zone:0/2 ZoneBounds:0/2"), + ("compress/gzip", "Reader", "Close:0/1 Multistream:1/0 Read:1/2 Reset:1/1"), + ("compress/gzip", "Writer", "Close:0/1 Flush:0/1 Reset:1/0 Write:1/2"), +]; + +/// The methods of standard-library type `name` declared by the package at `path`, or of the +/// predeclared `error` when `path` is empty: (method, parameter count, result count). +pub fn methods(path: &str, name: &str) -> Option> { + let listed = if path.is_empty() { (name == "error").then_some("Error:0/1")? } else { TABLE.iter().find(|(p, t, _)| *p == path && *t == name)?.2 }; + Some( + listed + .split(' ') + .filter_map(|entry| { + let (m, arity) = entry.split_once(':')?; + let (p, r) = arity.split_once('/')?; + Some((m, p.parse().ok()?, r.parse().ok()?)) + }) + .collect(), + ) +} + +#[cfg(test)] +mod tests { + #[test] + fn table_lookups() { + assert_eq!(super::methods("io", "Reader"), Some(vec![("Read", 1, 2)])); + assert_eq!(super::methods("", "error"), Some(vec![("Error", 0, 1)])); + assert!(super::methods("", "Reader").is_none()); + assert!(super::methods("testing", "TB").unwrap().iter().any(|m| m.0 == "private")); + assert!(super::methods("example.com/io", "Reader").is_none()); + } +} diff --git a/crates/cg-resolve/src/synth/js_alias.rs b/crates/cg-resolve/src/synth/js_alias.rs new file mode 100644 index 0000000..5a68bad --- /dev/null +++ b/crates/cg-resolve/src/synth/js_alias.rs @@ -0,0 +1,229 @@ +//! A module-level JS/TS value that hands a function on (upstream `const-alias-synthesizer.ts`): +//! +//! ```text +//! export const extname = (isWin ? win32.extname : posix.extname); +//! export const lexer = _Lexer.lex; +//! export const parse = marked; +//! ``` +//! +//! Calling `extname` runs one of two functions, yet no call to either is written anywhere. The +//! value links to every function its initializer (kept in the declaration's signature) can hand +//! on: each arm of a conditional, `??` and `||`. Every name must reach a real binding - the +//! file's own declaration or one of its imports, then members of that - so `const keys = +//! Object.keys` links nothing, and a name that ends on data links nothing. + +use super::{order_of, Out}; +use crate::engine::{Bound, Engine}; +use crate::resolve::{Accept, FileCx, NameRes, WorkerCx}; +use cg_core::{EdgeKind, LangFamily, NodeIdx, NodeKind, NONE}; +use cg_extract::fileflags; + +/// How far a conditional may nest in its own arms. +const MAX_OPERANDS: usize = 8; + +const NOT_A_BINDING: &[&str] = &["true", "false", "null", "undefined", "this", "super", "NaN", "Infinity"]; + +fn is_ident(s: &str) -> bool { + s.chars().next().is_some_and(|c| c.is_alphabetic() || c == '_' || c == '$') && s.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '$') +} + +/// Byte offsets of `pat` at bracket depth 0, outside strings. +fn top_level(code: &str, pat: &str) -> Vec { + let b = code.as_bytes(); + let mut out = Vec::new(); + let (mut depth, mut i) = (0i32, 0usize); + while i < b.len() { + match b[i] { + b'"' | b'\'' | b'`' => { + let q = b[i]; + i += 1; + while i < b.len() && b[i] != q { + i += if b[i] == b'\\' { 2 } else { 1 }; + } + } + b'(' | b'[' | b'{' => depth += 1, + b')' | b']' | b'}' => depth -= 1, + _ if depth == 0 && code[i..].starts_with(pat) => { + out.push(i); + i += pat.len(); + continue; + } + _ => {} + } + i += 1; + } + out +} + +fn strip_parens(mut s: &str) -> &str { + loop { + let t = s.trim(); + match t.strip_prefix('(').and_then(|r| r.strip_suffix(')')) { + // `(a) || (b)` is not one parenthesized expression + Some(inner) if top_level(inner, ")").is_empty() => s = inner, + _ => return t, + } + } +} + +/// The names an initializer can hand on: the arms of a conditional, the operands of `??` / +/// `||`, each with parentheses, casts and `!` taken off. `None` when it is anything else. +fn operands(init: &str, out: &mut Vec, depth: usize) -> Option<()> { + if depth > MAX_OPERANDS || out.len() > MAX_OPERANDS { + return None; + } + let code = strip_parens(init); + if code.contains("=>") || code.starts_with("function") || code.starts_with("new ") { + return None; + } + // `cond ? a : b`: the first top-level `?` that is no `?.` / `??` + let question = top_level(code, "?").into_iter().find(|&i| !code[i..].starts_with("?.") && !code[i..].starts_with("??") && !code[..i].ends_with('?')); + if let Some(q) = question { + let rest = &code[q + 1..]; + let colon = *top_level(rest, ":").first()?; + operands(&rest[..colon], out, depth + 1)?; + return operands(&rest[colon + 1..], out, depth + 1); + } + for sep in ["??", "||"] { + let at = top_level(code, sep); + if !at.is_empty() { + let mut start = 0; + for i in at.into_iter().chain(std::iter::once(code.len())) { + operands(&code[start..i], out, depth + 1)?; + start = i + sep.len(); + } + return Some(()); + } + } + let mut name = code; + for cast in [" as ", " satisfies "] { + if let Some((value, ty)) = name.split_once(cast) { + if ty.contains("Mock") { + return None; // a test's mock setup runs nothing it names + } + name = value.trim(); + } + } + let name = strip_parens(name.trim_end_matches('!')); + let flat: String = name.split_whitespace().collect::().replace("?.", "."); + if !flat.split('.').all(is_ident) || NOT_A_BINDING.contains(&flat.as_str()) { + return None; + } + out.push(flat); + Some(()) +} + +/// The function a name path reaches from the top level of `cx`'s file. +fn reach<'e, 'v>(e: &'e Engine<'v>, cx: &FileCx<'e, 'v>, wcx: &mut WorkerCx<'e, 'v>, path: &[&str], line: u32) -> Option { + let view = e.view; + let callable = |n: NodeIdx| matches!(view.node(n).kind(), NodeKind::Function | NodeKind::Method | NodeKind::Component); + let first = match cx.resolve_name(wcx, path[0], Accept::Any, 0, line, None, false) { + NameRes::Found(n, ..) => Bound::Node(n), + NameRes::Module(m) => Bound::Module(m), + // a value declared at the top of this file + NameRes::Local => Bound::Node(e.lookup_top_level(cx.file, path[0])?), + _ => return None, + }; + match (first, path.len()) { + (Bound::Node(n), 1) => { + if callable(n) { + Some(n) + } else { + cx.forward_alias_node(wcx, n, None).filter(|m| callable(*m)) + } + } + (Bound::Node(n), 2) => match view.node(n).kind() { + NodeKind::Class => e.find_member(n, path[1]).filter(|m| callable(*m)), + NodeKind::Constant | NodeKind::Variable => cx.forward_alias_node(wcx, n, Some(path[1])).filter(|m| callable(*m)), + _ => None, + }, + (Bound::Module(m), 2) => match e.lookup_export(m, path[1], 0)? { + Bound::Node(n) if callable(n) => Some(n), + _ => None, + }, + (Bound::Module(m), 3) => match e.lookup_export(m, path[1], 0)? { + Bound::Node(c) if view.node(c).kind() == NodeKind::Class => e.find_member(c, path[2]).filter(|n| callable(*n)), + _ => None, + }, + _ => None, + } +} + +pub(crate) fn const_aliases<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, 'v>) { + let view = e.view; + for f in view.files() { + let fr = view.file(f); + if fr.lang().family() != LangFamily::Js || fr.flags() & fileflags::GENERATED != 0 { + continue; + } + let values: Vec = view + .nodes_in_file(f) + .map(NodeIdx) + .filter(|n| { + let node = view.node(*n); + matches!(node.kind(), NodeKind::Constant | NodeKind::Variable) && node.parent().is_some_and(|p| view.node(p).kind() == NodeKind::File) + }) + .collect(); + if values.is_empty() { + continue; + } + let cx = wcx.file_cx(e, f); + for n in values { + let node = view.node(n); + let Some(init) = node.signature().and_then(|s| s.trim().strip_prefix('=')) else { continue }; + // a cut signature leaves its last operand unread + let (init, cut) = match init.trim_end().strip_suffix("...") { + Some(head) => (head, true), + None => (init, false), + }; + let mut names = Vec::new(); + if operands(init, &mut names, 0).is_none() { + continue; + } + if cut { + names.pop(); + } + let mut linked: Vec = Vec::new(); + for name in &names { + let path: Vec<&str> = name.split('.').collect(); + if path.len() > 3 { + continue; + } + let Some(target) = reach(e, &cx, wcx, &path, node.start_line()) else { continue }; + if target == n || linked.contains(&target) { + continue; + } + linked.push(target); + let meta = serde_json::json!({ + "synthesizedBy": "const-alias", + "via": name, + "registeredAt": format!("{}:{}", fr.path(), node.start_line()), + }); + out.push_meta(EdgeKind::Calls, n, target, node.start_line(), NONE, meta, order_of("const-alias")); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::operands; + + fn names(init: &str) -> Option> { + let mut out = Vec::new(); + operands(init, &mut out, 0).map(|_| out) + } + + #[test] + fn initializer_operands() { + assert_eq!(names(" (isWin ? win32.extname : posix.extname)"), Some(vec!["win32.extname".into(), "posix.extname".into()])); + assert_eq!(names("_Lexer.lex"), Some(vec!["_Lexer.lex".into()])); + assert_eq!(names("a ?? b || c"), Some(vec!["a".into(), "b".into(), "c".into()])); + assert_eq!(names("opts?.handler ?? noop"), Some(vec!["opts.handler".into(), "noop".into()])); + assert_eq!(names("useAuth as jest.Mock"), None); + assert_eq!(names("createIcon('x')"), None); + assert_eq!(names("x => x ? a : b"), None); + assert_eq!(names("{ a: 1 }"), None); + assert_eq!(names("true"), None); + } +} diff --git a/crates/cg-resolve/src/synth/js_proto.rs b/crates/cg-resolve/src/synth/js_proto.rs new file mode 100644 index 0000000..a55eab5 --- /dev/null +++ b/crates/cg-resolve/src/synth/js_proto.rs @@ -0,0 +1,60 @@ +//! ES5 inheritance between constructor functions, written as a call rather than `extends`: +//! `util.inherits(Child, Parent)`, `Child.prototype = Object.create(Parent.prototype)`, +//! `Object.setPrototypeOf(Child.prototype, Parent.prototype)` and `Child.prototype.__proto__ = +//! Parent.prototype`. Each becomes an `extends` reference from the child constructor's node, so +//! the hierarchy (and member lookup through it) treats the constructors as it treats classes. + +use cg_core::{EdgeKind, Lang, LangFamily, NodeKind, RefKind}; +use cg_extract::{ExtraRef, FileHook, HookCtx, HookOut}; +use regex::Regex; +use std::sync::OnceLock; + +fn inherit_re() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| { + let id = r"[A-Za-z_$][\w$]*"; + let path = r"[A-Za-z_$][\w$]*(?:\.[A-Za-z_$][\w$]*)*"; + Regex::new(&format!( + r"(?:\binherits\s*\(\s*({id})\s*,\s*({path})\s*\))|(?:\b({id})\.prototype\s*=\s*Object\.create\(\s*({path})\.prototype\b)|(?:Object\.setPrototypeOf\(\s*({id})\.prototype\s*,\s*({path})\.prototype\s*\))|(?:\b({id})\.prototype\.__proto__\s*=\s*({path})\.prototype\b)" + )) + .expect("static regex") + }) +} + +pub struct InheritScanner; + +impl FileHook for InheritScanner { + fn name(&self) -> &'static str { + "es5-inherits" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + lang.family() == LangFamily::Js + } + fn triggers(&self) -> &'static [&'static str] { + &["inherits", "Object.create", "setPrototypeOf", "__proto__"] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + if super::scan::is_minified(ctx.src) { + return; + } + let safe = cg_extract::post::strip_js_comments(ctx.src); + let lines = super::scan::LineIndex::new(&safe); + for c in inherit_re().captures_iter(&safe) { + let pair = (1..=7).step_by(2).find_map(|i| Some((c.get(i)?, c.get(i + 1)?))); + let Some((child, parent)) = pair else { continue }; + if child.as_str() == parent.as_str() { + continue; + } + // the child constructor declared in this file + let row = ctx.rows.nodes().enumerate().find(|(_, n)| n.kind() == NodeKind::Function && n.name() == child.as_str()).map(|(i, _)| i as u32); + let Some(row) = row else { continue }; + out.refs.push(ExtraRef { + from_row: row, + kind: RefKind::Edge(EdgeKind::Extends), + name: parent.as_str().to_owned(), + line: lines.line(parent.start()), + col: lines.col(parent.start()), + }); + } + } +} diff --git a/crates/cg-resolve/src/synth/jvm.rs b/crates/cg-resolve/src/synth/jvm.rs new file mode 100644 index 0000000..4b5deb0 --- /dev/null +++ b/crates/cg-resolve/src/synth/jvm.rs @@ -0,0 +1,269 @@ +//! Java / Kotlin links no call writes. +//! +//! * `test-request` (upstream `testRequestEdges`): a Spring test reaches a controller by URL - +//! MockMvc's `perform(post("/owners/new"))`, Kotlin's `mockMvc.get("/x")`, WebTestClient's +//! `.get().uri("/x")`, TestRestTemplate and RestAssured. The test method links to the route +//! the method and path match; the route's own edge reaches the handler. +//! * `kotlin-expect-actual` (upstream `kotlinExpectActualEdges`): a Multiplatform `expect` +//! declaration links to each `actual` of the same qualified name in another file, so editing +//! a platform implementation surfaces the common declaration and its callers. + +use super::dynamic::{client_path, http_routes, match_http}; +use super::scan::{enclosing_row, is_minified, LineIndex, FN_KINDS}; +use super::sites::PASS_TEST_REQUEST; +use super::{order_of, Out}; +use crate::engine::Engine; +use crate::frameworks::href::{match_bracket, HOLE}; +use cg_core::{EdgeKind, Lang, NodeIdx, NodeKind, StrId, NONE}; +use cg_extract::{FileHook, HookCtx, HookOut, SiteFact}; +use regex::Regex; +use std::sync::OnceLock; + +const MAX_ACTUALS: usize = 40; + +macro_rules! re { + ($name:ident, $pat:expr) => { + fn $name() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new($pat).expect("static regex")) + } + }; +} + +const VERBS: &str = "get|post|put|patch|delete|head|options"; +re!(gate_re, r"[mM]ockMvc|[wW]ebTestClient\b|[rR]estTemplate\b|\bRestAssured\b|\.\s*when\s*\(\s*\)"); +re!(mockmvc_builder_re, &format!(r"(?:^|[^\w$.])(?:MockMvcRequestBuilders\s*\.\s*)?({VERBS}|multipart)\s*\(")); +re!(mockmvc_request_re, r"(?:^|[^\w$.])(?:MockMvcRequestBuilders\s*\.\s*)?request\s*\(\s*HttpMethod\s*\.\s*([A-Z]+)\s*,"); +re!(kotlin_mockmvc_re, &format!(r"\b\w*[mM]ockMvc\s*\.\s*({VERBS})\s*\(")); +re!(web_test_client_re, &format!(r"\.\s*({VERBS})\s*\(\s*\)\s*\.\s*uri\s*\(")); +re!( + rest_template_re, + r"\b\w*[Rr]estTemplate\s*\.\s*(getForEntity|getForObject|postForEntity|postForObject|postForLocation|put|delete|patchForObject|exchange)\s*\(" +); +re!(rest_assured_re, &format!(r"(?:\.\s*when\s*\(\s*\)\s*|\bRestAssured\s*)\.\s*({VERBS})\s*\(")); +re!(http_method_re, r"HttpMethod\s*\.\s*([A-Z]+)"); + +/// The path a request's first argument builds from `at` (just past its `(`): string literals +/// verbatim, each computed piece and each Spring `{var}` as a hole. `None` unless it starts with +/// a literal. +fn request_path(src: &str, at: usize) -> Option { + let b = src.as_bytes(); + let mut i = at; + let skip = |i: &mut usize| { + while *i < b.len() && b[*i].is_ascii_whitespace() { + *i += 1; + } + }; + skip(&mut i); + let mut out = String::new(); + loop { + if b.get(i) == Some(&b'"') { + let start = i + 1; + let mut j = start; + while j < b.len() && b[j] != b'"' && b[j] != b'\n' { + j += if b[j] == b'\\' { 2 } else { 1 }; + } + let lit = src.get(start..j.min(b.len()))?; + // `{id}` placeholders and Kotlin `$id` / `${id}` templates are holes + let mut depth = 0; + for c in lit.chars() { + match c { + '{' => { + depth += 1; + if depth == 1 { + out.push(HOLE); + } + } + '}' => depth -= 1, + '$' if depth == 0 => out.push(HOLE), + _ if depth == 0 && !(out.ends_with(HOLE) && (c.is_alphanumeric() || c == '_')) => out.push(c), + _ => {} + } + } + i = j + 1; + } else if out.is_empty() { + return None; + } else { + // a computed operand + let rest = &src[i..]; + let end = rest.find(['+', ',', ')']).unwrap_or(rest.len()); + if end == 0 { + return None; + } + out.push(HOLE); + i += end; + } + skip(&mut i); + if b.get(i) == Some(&b'+') { + i += 1; + skip(&mut i); + continue; + } + return Some(out); + } +} + +/// Requests Spring tests make, with the method and the path they name. +pub struct TestRequestScanner; + +impl FileHook for TestRequestScanner { + fn name(&self) -> &'static str { + "test-request-sites" + } + fn applies(&self, lang: Lang, rel: &str) -> bool { + matches!(lang, Lang::Java | Lang::Kotlin) && cg_scan::testpath::is_test_path(rel) + } + fn triggers(&self) -> &'static [&'static str] { + &["ockMvc", "ebTestClient", "estTemplate", "RestAssured", "when("] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + if is_minified(ctx.src) || !gate_re().is_match(ctx.src) { + return; + } + let safe = cg_extract::post::strip_js_comments(ctx.src); + let lines = LineIndex::new(&safe); + let mut add = |at: usize, arg: usize, method: &str, callee: &str| { + let Some(path) = request_path(&safe, arg) else { return }; + let line = lines.line(at); + let row = enclosing_row(ctx, line, FN_KINDS).unwrap_or(NONE); + out.sites.push(SiteFact { + pass: PASS_TEST_REQUEST, + role: 0, + key_kind: 0, + key_a: method.to_ascii_uppercase(), + key_b: path, + node_row: row, + line, + payload: callee.to_owned(), + }); + }; + if safe.contains("MockMvcRequestBuilders") { + for c in mockmvc_builder_re().captures_iter(&safe) { + let m = c.get(1).unwrap(); + let method = if &c[1] == "multipart" { "post" } else { &c[1] }; + add(m.start(), c.get(0).unwrap().end(), method, &c[1]); + } + for c in mockmvc_request_re().captures_iter(&safe) { + add(c.get(0).unwrap().start(), c.get(0).unwrap().end(), &c[1], "request"); + } + } + for (re, callee) in [(kotlin_mockmvc_re(), "mockMvc."), (web_test_client_re(), "uri:"), (rest_assured_re(), "")] { + for c in re.captures_iter(&safe) { + add(c.get(0).unwrap().start(), c.get(0).unwrap().end(), &c[1], &format!("{callee}{}", &c[1])); + } + } + for c in rest_template_re().captures_iter(&safe) { + let name = &c[1]; + let open = c.get(0).unwrap().end() - 1; + let method = match name { + n if n.starts_with("get") => Some("GET".to_owned()), + n if n.starts_with("post") => Some("POST".to_owned()), + "put" => Some("PUT".to_owned()), + "delete" => Some("DELETE".to_owned()), + n if n.starts_with("patch") => Some("PATCH".to_owned()), + _ => match_bracket(&safe, open).and_then(|close| http_method_re().captures(&safe[open..close]).map(|m| m[1].to_owned())), + }; + if let Some(method) = method { + add(c.get(0).unwrap().start(), open + 1, &method, &format!("restTemplate.{name}")); + } + } + } +} + +pub(crate) fn test_requests(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + let mut sites = Vec::new(); + for f in view.files() { + if matches!(view.file(f).lang(), Lang::Java | Lang::Kotlin) { + sites.extend(view.sites_of(f).iter().filter(|s| s.pass == PASS_TEST_REQUEST).map(|s| (f, s))); + } + } + if sites.is_empty() { + return; + } + let routes = http_routes(e); + if routes.is_empty() { + return; + } + let mut seen = rustc_hash::FxHashSet::default(); + for (f, s) in sites { + let from = if s.node.0 == NONE { view.file(f).rec.node_start } else { s.node }; + let Some((segs, suffix)) = client_path(view.text_str(StrId(s.key_b)), None) else { continue }; + let Some(route) = match_http(view.text_str(StrId(s.key_a)), &segs, suffix, &routes) else { continue }; + if !seen.insert((from, route.node)) { + continue; + } + out.push( + EdgeKind::Calls, + from, + route.node, + s.line, + "test-request", + Some(format!("{}:{}", view.file(f).path(), s.line)), + Some(("via", view.text_str(s.payload))), + order_of("test-request"), + ); + } +} + +/// Kinds an `expect` / `actual` pair may straddle (`expect class` fulfilled by an `actual +/// typealias`). +fn kmp_kinds_compatible(a: NodeKind, b: NodeKind) -> bool { + let typey = |k: NodeKind| matches!(k, NodeKind::Class | NodeKind::Interface | NodeKind::Struct | NodeKind::Enum | NodeKind::TypeAlias); + a == b || (typey(a) && typey(b)) +} + +pub(crate) fn kotlin_expect_actual(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + let marked = |n: NodeIdx, word: &str| view.node(n).decorators().iter().any(|d| d.split_whitespace().any(|w| w == word)); + for f in view.files() { + if view.file(f).lang() != Lang::Kotlin { + continue; + } + for act in view.nodes_in_file(f).map(NodeIdx) { + if !marked(act, "actual") { + continue; + } + let an = view.node(act); + let mut decls: Vec = view + .by_qname(an.qname()) + .filter(|c| { + let cn = view.node(*c); + *c != act + && cn.file() != an.file() + && view.file(cn.file()).lang() == Lang::Kotlin + && kmp_kinds_compatible(cn.kind(), an.kind()) + && !marked(*c, "actual") + }) + .collect(); + decls.sort_by_key(|n| (view.file(view.node(*n).file()).path(), view.node(*n).start_line())); + for decl in decls.into_iter().take(MAX_ACTUALS) { + out.push( + EdgeKind::Calls, + decl, + act, + view.node(decl).start_line(), + "kotlin-expect-actual", + Some(format!("{}:{}", view.file(f).path(), an.start_line())), + Some(("via", an.name())), + order_of("kotlin-expect-actual"), + ); + } + } + } +} + +#[cfg(test)] +mod tests { + use super::request_path; + use crate::frameworks::href::HOLE; + + #[test] + fn request_paths() { + assert_eq!(request_path("\"/owners/new\")", 0).as_deref(), Some("/owners/new")); + assert_eq!(request_path(" \"/owners/{id}/edit\", 1)", 0), Some(format!("/owners/{HOLE}/edit"))); + assert_eq!(request_path("\"/owners/\" + id)", 0), Some(format!("/owners/{HOLE}"))); + assert_eq!(request_path("\"/pets/$id\")", 0), Some(format!("/pets/{HOLE}"))); + assert_eq!(request_path("url)", 0), None); + } +} diff --git a/crates/cg-resolve/src/synth/mod.rs b/crates/cg-resolve/src/synth/mod.rs index f27be97..5a238e0 100644 --- a/crates/cg-resolve/src/synth/mod.rs +++ b/crates/cg-resolve/src/synth/mod.rs @@ -2,8 +2,16 @@ //! view (hierarchy, members, persisted sites), so the same code serves full builds and the //! incremental sync, which re-runs it and rewrites the files whose synthesised edges changed. +pub mod bridges; pub mod dynamic; +pub mod gin; pub mod go; +pub mod go_fields; +pub mod go_std; +pub mod js_alias; +pub mod js_proto; +pub mod jvm; +pub mod redux; pub mod scan; pub mod sites; @@ -423,6 +431,16 @@ const GLOBAL_PASSES: &[&str] = &[ "event-bus", "celery-dispatch", "spring-event", + "go-func-field", + "go-grpc-stub-impl", + "gin-middleware-chain", + "const-alias", + "redux-thunk", + "rtk-query", + "test-request", + "kotlin-expect-actual", + "tauri-command", + "rn-native-module", ]; /// Merge order of a synthesis pass (upstream `SYNTH_PASSES`): of two synthesized edges @@ -433,13 +451,23 @@ pub(crate) fn order_of(by: &str) -> u16 { "go-implements" | "go-method-contains" => 10, "callback" => 100, "http-client" | "queue-job" | "event-bus" => 120, + "tauri-command" => 125, + "test-request" => 130, "event-emitter" => 150, "react-render" => 160, "jsx-render" => 170, "interface-impl" => 260, + "kotlin-expect-actual" => 270, + "go-grpc-stub-impl" => 280, + "rn-native-module" => 320, + "gin-middleware-chain" => 340, + "redux-thunk" => 350, + "const-alias" => 360, + "rtk-query" => 400, "object-registry" => 390, "celery-dispatch" => 430, "spring-event" => 440, + "go-func-field" => 480, "next-link" => 510, "next-proxy" => 520, "react-router-link" => 530, @@ -475,9 +503,18 @@ fn dedupe_pairs(out: &mut Out) { fn run_global<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, 'v>) { dynamic::callbacks(e, out, wcx); dynamic::cross_tier(e, out, wcx); + redux::thunks(e, out, wcx); + js_alias::const_aliases(e, out, wcx); dynamic::registries(e, out, wcx); + redux::rtk_query(e, out); dynamic::celery(e, out); dynamic::spring_events(e, out); + bridges::link(e, out); + jvm::test_requests(e, out); + jvm::kotlin_expect_actual(e, out); + go_fields::func_fields(e, out, wcx); + go::grpc_stubs(e, out); + gin::middleware_chain(e, out); crate::frameworks::nav::navigation(e, out); crate::frameworks::nav::next_proxy(e, out); dedupe_pairs(out); diff --git a/crates/cg-resolve/src/synth/redux.rs b/crates/cg-resolve/src/synth/redux.rs new file mode 100644 index 0000000..4aed532 --- /dev/null +++ b/crates/cg-resolve/src/synth/redux.rs @@ -0,0 +1,186 @@ +//! Redux Toolkit chains the graph cannot see. +//! +//! * `redux-thunk` (upstream `reduxThunkEdges`): `export const X = createAsyncThunk(prefix, async +//! (a, api) => { ... dispatch(next(...)) })` passes its body as an argument, so the calls that +//! drive a thunk chain forward hang off no function. Each thunk constant links to what its body +//! dispatches: the name as the file binds it (an import or a declaration of its own), else a +//! thunk constant, then any constant, of that name when only one fits. +//! * `rtk-query` (upstream `rtkQueryEdges`): a generated hook (`useGetPostsQuery`, extracted with +//! a sentinel signature from `export const { ... } = api`) runs the endpoint its name derives +//! from (`getPosts`), declared in the same file. + +use super::scan::{is_minified, LineIndex}; +use super::sites::PASS_THUNK; +use super::{order_of, Out}; +use crate::engine::Engine; +use crate::resolve::{Accept, NameRes, WorkerCx}; +use cg_core::{EdgeKind, Lang, LangFamily, NodeIdx, NodeKind, StrId, NONE}; +use cg_extract::{FileHook, HookCtx, HookOut, SiteFact}; +use regex::Regex; +use std::sync::OnceLock; + +const THUNK_FANOUT_CAP: usize = 24; +/// The signature the kernel gives a generated RTK Query hook. +const RTK_GENERATED_HOOK: &str = "= RTK Query generated hook"; + +fn thunk_decl_re() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new(r"create(?:Async)?Thunk").expect("static regex")) +} + +fn dispatch_re() -> &'static Regex { + static R: OnceLock = OnceLock::new(); + R.get_or_init(|| Regex::new(r"\bdispatch\s*\(\s*([A-Za-z_]\w*)\s*[(),]").expect("static regex")) +} + +/// The names a thunk constant's body dispatches. +pub struct ThunkScanner; + +impl FileHook for ThunkScanner { + fn name(&self) -> &'static str { + "redux-thunk-sites" + } + fn applies(&self, lang: Lang, _rel: &str) -> bool { + lang.family() == LangFamily::Js + } + fn triggers(&self) -> &'static [&'static str] { + &["Thunk"] + } + fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + if is_minified(ctx.src) { + return; + } + let safe = cg_extract::post::strip_js_comments(ctx.src); + let lines = LineIndex::new(&safe); + for (row, n) in ctx.rows.nodes().enumerate() { + if n.kind() != NodeKind::Constant || !n.signature().is_some_and(|s| thunk_decl_re().is_match(s)) { + continue; + } + let body = lines.lines(n.start_line(), n.end_line()); + for (i, m) in dispatch_re().captures_iter(body).enumerate() { + if i >= THUNK_FANOUT_CAP * 4 { + break; + } + let name = &m[1]; + if name == n.name() { + continue; // a recursive thunk + } + let line = n.start_line() + body[..m.get(0).unwrap().start()].matches('\n').count() as u32; + out.sites.push(SiteFact { + pass: PASS_THUNK, + role: 0, + key_kind: 0, + key_a: name.to_owned(), + key_b: String::new(), + node_row: row as u32, + line, + payload: String::new(), + }); + } + } + } +} + +pub(crate) fn thunks<'e, 'v>(e: &'e Engine<'v>, out: &mut Out, wcx: &mut WorkerCx<'e, 'v>) { + let view = e.view; + let is_thunk = |n: NodeIdx| view.node(n).kind() == NodeKind::Constant && view.node(n).signature().is_some_and(|s| thunk_decl_re().is_match(s)); + for f in view.files() { + let sites: Vec<_> = view.sites_of(f).iter().filter(|s| s.pass == PASS_THUNK && s.node.0 != NONE).collect(); + if sites.is_empty() { + continue; + } + let cx = wcx.file_cx(e, f); + let mut added: rustc_hash::FxHashMap = rustc_hash::FxHashMap::default(); + let mut seen = rustc_hash::FxHashSet::default(); + for s in sites { + let name = view.text_str(StrId(s.key_a)); + let count = added.entry(s.node).or_default(); + if *count >= THUNK_FANOUT_CAP { + continue; + } + // what the file binds the name to, else the one thunk / constant of that name + let target = match cx.resolve_name(wcx, name, Accept::Callable, 0, u32::MAX, None, false) { + NameRes::Found(n, ..) => Some(n), + _ => { + let cands: Vec = view + .by_name(name) + .filter(|n| matches!(view.node(*n).kind(), NodeKind::Constant | NodeKind::Function | NodeKind::Method)) + .filter(|n| view.file(view.node(*n).file()).lang().family() == LangFamily::Js) + .collect(); + let thunks: Vec = cands.iter().copied().filter(|n| is_thunk(*n)).collect(); + let consts: Vec = cands.iter().copied().filter(|n| view.node(*n).kind() == NodeKind::Constant).collect(); + match (thunks.as_slice(), consts.as_slice(), cands.as_slice()) { + ([one], ..) | ([], [one], _) | ([], [], [one]) => Some(*one), + _ => None, + } + } + }; + let Some(target) = target.filter(|t| *t != s.node && matches!(view.node(*t).kind(), NodeKind::Constant | NodeKind::Function | NodeKind::Method)) + else { + continue; + }; + if !seen.insert((s.node, target)) { + continue; + } + *count += 1; + let meta = serde_json::json!({ + "synthesizedBy": "redux-thunk", + "via": name, + "registeredAt": format!("{}:{}", view.file(f).path(), s.line), + }); + out.push_meta(EdgeKind::Calls, s.node, target, s.line, NONE, meta, order_of("redux-thunk")); + } + } +} + +/// `useLazyGetRecordsQuery` -> `getRecords`. +fn endpoint_of(hook: &str) -> Option { + let mid = hook.strip_prefix("use")?; + let mid = mid.strip_suffix("Query").or_else(|| mid.strip_suffix("Mutation"))?; + let mid = mid.strip_prefix("Lazy").unwrap_or(mid); + let mut chars = mid.chars(); + let first = chars.next().filter(|c| c.is_ascii_uppercase())?; + Some(format!("{}{}", first.to_ascii_lowercase(), chars.as_str())) +} + +pub(crate) fn rtk_query(e: &Engine<'_>, out: &mut Out) { + let view = e.view; + for f in view.files() { + if view.file(f).lang().family() != LangFamily::Js { + continue; + } + let nodes: Vec = view.nodes_in_file(f).map(NodeIdx).collect(); + for &hook in &nodes { + let node = view.node(hook); + if node.kind() != NodeKind::Function || node.signature() != Some(RTK_GENERATED_HOOK) { + continue; + } + let Some(endpoint) = endpoint_of(node.name()) else { continue }; + let Some(&target) = nodes.iter().find(|n| **n != hook && view.node(**n).kind() == NodeKind::Function && view.node(**n).name() == endpoint) else { + continue; + }; + out.push( + EdgeKind::Calls, + hook, + target, + node.start_line(), + "rtk-query", + Some(format!("{}:{}", view.file(f).path(), node.start_line())), + Some(("via", &endpoint)), + order_of("rtk-query"), + ); + } + } +} + +#[cfg(test)] +mod tests { + #[test] + fn endpoint_names() { + assert_eq!(super::endpoint_of("useGetPostsQuery").as_deref(), Some("getPosts")); + assert_eq!(super::endpoint_of("useLazyGetRecordsQuery").as_deref(), Some("getRecords")); + assert_eq!(super::endpoint_of("useUpdateUserMutation").as_deref(), Some("updateUser")); + assert_eq!(super::endpoint_of("useQuery"), None); + assert_eq!(super::endpoint_of("useThing"), None); + } +} diff --git a/crates/cg-resolve/src/synth/sites.rs b/crates/cg-resolve/src/synth/sites.rs index ce2fb02..7930218 100644 --- a/crates/cg-resolve/src/synth/sites.rs +++ b/crates/cg-resolve/src/synth/sites.rs @@ -34,13 +34,31 @@ pub const PASS_SPRING: u16 = 13; pub const PASS_REACT_TABLE: u16 = 14; /// Nest modules, router registrations and application bootstrap configuration. pub const PASS_NEST: u16 = 15; +/// Go types a struct or interface embeds, with the import path they are written through. +pub const PASS_GO_EMBEDS: u16 = 16; +/// Go func-typed struct fields, func types and the values put in struct fields. +pub const PASS_GO_FIELDS: u16 = 17; +/// Gin's handler-chain dispatcher and the handlers registration calls pass (`gin`). +pub const PASS_GIN: u16 = 18; +/// What a Redux thunk constant's body dispatches (`redux`). +pub const PASS_THUNK: u16 = 19; +/// Requests Spring tests make (`jvm`). +pub const PASS_TEST_REQUEST: u16 = 20; +/// Tauri commands and React Native native modules across the language bridge (`bridges`). +pub const PASS_BRIDGE: u16 = 21; pub const ROLE_EMIT: u8 = 0; pub const ROLE_ON: u8 = 1; pub const ROLE_GO_ALIAS: u8 = 0; pub const ROLE_GO_GENERIC: u8 = 1; +/// `PASS_GO_FIELDS` roles. +pub const ROLE_GO_FUNC_FIELD: u8 = 0; +pub const ROLE_GO_FIELD_LITERAL: u8 = 1; +pub const ROLE_GO_FIELD_ASSIGN: u8 = 2; +pub const ROLE_GO_FUNC_TYPE: u8 = 3; -/// Go true aliases and generic type parameters, from the facts of the file. +/// Go true aliases and generic type parameters, embedded types and the facts the +/// func-field pass reads, from the facts of the file. pub struct GoTypeSites; impl FileHook for GoTypeSites { @@ -51,17 +69,51 @@ impl FileHook for GoTypeSites { lang == Lang::Go } fn run(&self, ctx: &HookCtx<'_>, out: &mut HookOut) { + let site = |pass: u16, role: u8, key_a: &str, key_b: &str, node_row: u32, line: u32, payload: String| SiteFact { + pass, + role, + key_kind: 0, + key_a: key_a.to_owned(), + key_b: key_b.to_owned(), + node_row, + line, + payload, + }; for t in &ctx.facts.go_types { - out.sites.push(SiteFact { - pass: PASS_GO_TYPES, - role: if t.alias { ROLE_GO_ALIAS } else { ROLE_GO_GENERIC }, - key_kind: 0, - key_a: t.name.clone(), - key_b: String::new(), - node_row: cg_core::NONE, - line: 0, - payload: t.type_params.join(","), - }); + if t.alias || !t.type_params.is_empty() { + let role = if t.alias { ROLE_GO_ALIAS } else { ROLE_GO_GENERIC }; + out.sites.push(site(PASS_GO_TYPES, role, &t.name, "", cg_core::NONE, 0, t.type_params.join(","))); + } + if t.func_type { + out.sites.push(site(PASS_GO_FIELDS, ROLE_GO_FUNC_TYPE, &t.name, "", cg_core::NONE, 0, String::new())); + } + } + // the import path an embedded type is written through (`io.Reader` -> `io`) + for e in &ctx.facts.go_embeds { + let path = if e.qualifier.is_empty() { + String::new() + } else { + match ctx.facts.imports.iter().find(|i| i.local == e.qualifier) { + Some(i) => i.spec.clone(), + None => continue, + } + }; + out.sites.push(site(PASS_GO_EMBEDS, 0, &e.qualifier, &e.name, e.row, 0, path)); + } + for f in &ctx.facts.go_fields { + match f { + cg_extract::facts::GoFieldFact::FuncField { row, field } => { + out.sites.push(site(PASS_GO_FIELDS, ROLE_GO_FUNC_FIELD, field, "", *row, 0, String::new())); + } + cg_extract::facts::GoFieldFact::Literal { ty, field, value, line, col } => { + let row = super::scan::enclosing_row(ctx, *line, super::scan::FN_KINDS).unwrap_or(cg_core::NONE); + out.sites.push(site(PASS_GO_FIELDS, ROLE_GO_FIELD_LITERAL, ty, field, row, *line, format!("{col}|{value}"))); + } + cg_extract::facts::GoFieldFact::Assign { receiver, field, value, line, col } => { + let row = super::scan::enclosing_row(ctx, *line, super::scan::FN_KINDS).unwrap_or(cg_core::NONE); + out.sites.push(site(PASS_GO_FIELDS, ROLE_GO_FIELD_ASSIGN, receiver, field, row, *line, format!("{col}|{value}"))); + } + } } } } diff --git a/crates/cg-resolve/tests/gap_closure.rs b/crates/cg-resolve/tests/gap_closure.rs new file mode 100644 index 0000000..9353354 --- /dev/null +++ b/crates/cg-resolve/tests/gap_closure.rs @@ -0,0 +1,471 @@ +//! Resolution and synthesis rules ported from upstream for the supported languages: each test +//! checks the edge the rule adds and the edges it must not invent. + +use cg_resolve::{build_seg, IndexOptions}; +use cg_store::{dump, View}; +use std::sync::Arc; + +fn project(files: &[(&str, &str)]) -> (tempfile::TempDir, View) { + let dir = tempfile::tempdir().unwrap(); + for (path, contents) in files { + let dest = dir.path().join(path); + std::fs::create_dir_all(dest.parent().unwrap()).unwrap(); + std::fs::write(dest, contents).unwrap(); + } + let seg = build_seg(dir.path(), &IndexOptions { threads: Some(2), ..Default::default() }).unwrap().seg; + let view = View::new(Arc::new(seg)).with_root(dir.path().to_path_buf()); + (dir, view) +} + +fn edges(files: &[(&str, &str)]) -> Vec { + let (_dir, view) = project(files); + dump::dump_edges(&view) +} + +#[track_caller] +fn has(edges: &[String], e: &str) { + assert!(edges.iter().any(|x| x == e), "missing {e}\n{edges:#?}"); +} + +#[track_caller] +fn lacks(edges: &[String], e: &str) { + assert!(!edges.iter().any(|x| x == e), "unexpected {e}\n{edges:#?}"); +} + +const GO_MOD: (&str, &str) = ("go.mod", "module example.com/demo\n\ngo 1.22\n"); + +#[test] +fn go_embedded_standard_library_types_take_part_in_interface_satisfaction() { + let e = edges(&[ + GO_MOD, + ( + "p/p.go", + r#"package p + +import ( + "io" + "os" + "net/http" +) + +type ReadCloser interface { + io.Reader + Close() error +} + +type OnlyClose struct{} + +func (OnlyClose) Close() error { return nil } + +type Both struct{} + +func (Both) Read(b []byte) (int, error) { return 0, nil } +func (Both) Close() error { return nil } + +type FileBox struct{ *os.File } + +type Wrapped interface { + http.Handler + Name() string +} + +type Named struct{} + +func (Named) Name() string { return "" } + +type Embeds struct { + http.Handler + Named +} + +type Fails interface { + error + Code() int +} + +type Coded struct{} + +func (Coded) Code() int { return 1 } + +type CodedErr struct{} + +func (CodedErr) Code() int { return 1 } +func (CodedErr) Error() string { return "" } +"#, + ), + ]); + // io.Reader asks for Read: a type with only Close no longer satisfies the interface + lacks(&e, "p/p.go::OnlyClose -implements-> p/p.go::ReadCloser"); + has(&e, "p/p.go::Both -implements-> p/p.go::ReadCloser"); + has(&e, "p/p.go::ReadCloser::Close -calls-> p/p.go::Both::Close"); + // *os.File brings Read and Close + has(&e, "p/p.go::FileBox -implements-> p/p.go::ReadCloser"); + // http.Handler is opaque past its ServeHTTP: only a type embedding it as well satisfies it + lacks(&e, "p/p.go::Named -implements-> p/p.go::Wrapped"); + has(&e, "p/p.go::Embeds -implements-> p/p.go::Wrapped"); + // the predeclared error + lacks(&e, "p/p.go::Coded -implements-> p/p.go::Fails"); + has(&e, "p/p.go::CodedErr -implements-> p/p.go::Fails"); +} + +#[test] +fn go_func_fields_reach_the_functions_put_in_them() { + let e = edges(&[ + GO_MOD, + ( + "disc/disc.go", + r#"package disc + +import "context" + +type Group struct{} + +type Options struct { + RefreshF func(ctx context.Context) ([]*Group, error) + Name string +} + +type Discovery struct { + refreshf func(ctx context.Context) ([]*Group, error) + opts *Options +} + +type HandlerFunc func(n int) + +type Router struct { + handle HandlerFunc + Other func() +} + +func NewDiscovery(o Options) *Discovery { + return &Discovery{refreshf: o.RefreshF, opts: &o} +} + +func (d *Discovery) Run(ctx context.Context) { + d.refreshf(ctx) +} + +func (r *Router) Serve() { + r.handle(1) + r.Other() +} + +func serve(n int) {} +func other() {} +func unrelated() {} + +func Wire() *Router { + r := &Router{handle: HandlerFunc(serve)} + r.Other = other + return r +} +"#, + ), + ( + "ec2/ec2.go", + r#"package ec2 + +import ( + "context" + + "example.com/demo/disc" +) + +type EC2 struct{} + +func (e *EC2) refresh(ctx context.Context) ([]*disc.Group, error) { return nil, nil } + +func New() *disc.Discovery { + e := &EC2{} + return disc.NewDiscovery(disc.Options{RefreshF: e.refresh, Name: "ec2"}) +} +"#, + ), + ]); + // copied through Options.RefreshF into Discovery.refreshf + has(&e, "disc/disc.go::Discovery::Run -calls-> ec2/ec2.go::EC2::refresh"); + // a named func type, through a conversion; an assignment + has(&e, "disc/disc.go::Router::Serve -calls-> disc/disc.go::serve"); + has(&e, "disc/disc.go::Router::Serve -calls-> disc/disc.go::other"); + lacks(&e, "disc/disc.go::Router::Serve -calls-> disc/disc.go::unrelated"); +} + +#[test] +fn go_grpc_stubs_link_to_hand_written_servers_only() { + let e = edges(&[ + GO_MOD, + ( + "api/svc_grpc.pb.go", + "// Code generated by protoc-gen-go-grpc. DO NOT EDIT.\npackage api\n\ntype UnimplementedBankServer struct{}\n\nfunc (UnimplementedBankServer) Send() error { return nil }\nfunc (UnimplementedBankServer) MultiSend() error { return nil }\nfunc (UnimplementedBankServer) mustEmbedUnimplementedBankServer() {}\n\ntype UnsafeBankServer struct{}\n\nfunc (UnsafeBankServer) Send() error { return nil }\nfunc (UnsafeBankServer) MultiSend() error { return nil }\n", + ), + ( + "keeper/server.go", + "package keeper\n\ntype msgServer struct{}\n\nfunc (msgServer) Send() error { return nil }\nfunc (msgServer) MultiSend() error { return nil }\n\ntype partial struct{}\n\nfunc (partial) Send() error { return nil }\n", + ), + ]); + has(&e, "api/svc_grpc.pb.go::UnimplementedBankServer::Send -calls-> keeper/server.go::msgServer::Send"); + has(&e, "api/svc_grpc.pb.go::UnimplementedBankServer::MultiSend -calls-> keeper/server.go::msgServer::MultiSend"); + lacks(&e, "api/svc_grpc.pb.go::UnimplementedBankServer::Send -calls-> keeper/server.go::partial::Send"); + lacks(&e, "api/svc_grpc.pb.go::UnimplementedBankServer::Send -calls-> api/svc_grpc.pb.go::UnsafeBankServer::Send"); +} + +#[test] +fn gin_dispatcher_reaches_registered_handlers() { + let e = edges(&[ + ("go.mod", "module github.com/gin-gonic/gin\n\ngo 1.22\n"), + ( + "context.go", + "package gin\n\ntype HandlerFunc func(*Context)\n\ntype Context struct {\n\thandlers []HandlerFunc\n\tindex int\n}\n\nfunc (c *Context) Next() {\n\tfor c.index < len(c.handlers) {\n\t\tc.handlers[c.index](c)\n\t\tc.index++\n\t}\n}\n", + ), + ("logger.go", "package gin\n\nfunc Logger() HandlerFunc { return nil }\n"), + ( + "app/main.go", + "package main\n\nimport \"github.com/gin-gonic/gin\"\n\nfunc auth(c *gin.Context) {}\n\nfunc main() {\n\tr := gin.New()\n\tr.Use(gin.Logger(), func(c *gin.Context) {})\n\tr.GET(\"/x\", auth)\n}\n", + ), + ]); + has(&e, "context.go::Context::Next -calls-> logger.go::Logger"); + has(&e, "context.go::Context::Next -calls-> app/main.go::auth"); +} + +#[test] +fn js_calls_through_aliases_reach_the_aliased_function() { + let e = edges(&[ + ("package.json", r#"{"name":"demo"}"#), + ( + "src/impl.ts", + "export function realImpl(x: number) { return x; }\nexport function win() { return 1; }\nexport function posix() { return 2; }\nexport class Lexer { static lex() {} }\nconst isWin = false;\nexport const alias = realImpl;\nexport const pick = isWin ? win : posix;\nexport const api = { run: realImpl, posix };\nexport const lex = Lexer.lex;\nexport const keys = Object.keys;\nexport const data = { width: 5 };\n", + ), + ( + "src/use.ts", + "import { alias, pick, api, data } from './impl';\nimport { realImpl } from './impl';\nexport function caller() {\n alias(1);\n pick();\n api.run(2);\n const local = realImpl;\n local(3);\n data.width;\n}\n", + ), + ]); + // a call through a bare alias, a local alias and an object literal member + has(&e, "src/use.ts::caller -calls-> src/impl.ts::realImpl"); + lacks(&e, "src/use.ts::caller -calls-> src/impl.ts::alias"); + // a conditional value: the call lands on the constant, which hands on both arms + has(&e, "src/use.ts::caller -calls-> src/impl.ts::pick"); + has(&e, "src/impl.ts::pick -calls-> src/impl.ts::win"); + has(&e, "src/impl.ts::pick -calls-> src/impl.ts::posix"); + has(&e, "src/impl.ts::alias -calls-> src/impl.ts::realImpl"); + has(&e, "src/impl.ts::lex -calls-> src/impl.ts::Lexer::lex"); + // `Object.keys` is no project function; an object of data hands nothing on + assert!(!e.iter().any(|x| x.starts_with("src/impl.ts::keys -calls->")), "{e:#?}"); + assert!(!e.iter().any(|x| x.starts_with("src/impl.ts::data -calls->")), "{e:#?}"); +} + +#[test] +fn es5_constructors_type_their_instances_through_prototypes() { + let (_dir, view) = project(&[ + ("package.json", r#"{"name":"demo"}"#), + ( + "src/proto.js", + "var util = require('util');\nfunction Base() {}\nBase.prototype.stop = function stop() {};\nfunction Params(opts) { this.opts = opts; }\nutil.inherits(Params, Base);\nParams.prototype.render = function render() { return this.save(); };\nParams.prototype.save = function save() { return 1; };\nfunction Other() {}\nOther.prototype.render = function render() { return 2; };\nfunction use() {\n var p = new Params({});\n p.render();\n p.stop();\n new Params({}).save();\n}\nmodule.exports = use;\n", + ), + ]); + let e = dump::dump_edges(&view); + has(&e, "src/proto.js::Params -extends-> src/proto.js::Base"); + has(&e, "src/proto.js::use -calls-> src/proto.js::stop"); + has(&e, "src/proto.js::use -calls-> src/proto.js::save"); + // `p.render()` reaches Params' render (line 6), never Other's (line 9) + let lines: Vec = view + .files() + .flat_map(|f| view.nodes_in_file(f)) + .map(cg_core::NodeIdx) + .filter(|n| view.node(*n).name() == "use") + .flat_map(|n| view.out_edges(n).filter(|ed| ed.kind == cg_core::EdgeKind::Calls).map(|ed| ed.dst).collect::>()) + .filter(|d| view.node(*d).name() == "render") + .map(|d| view.node(d).start_line()) + .collect(); + assert_eq!(lines, vec![6], "{e:#?}"); + // `this.save()` inside a prototype member is the constructor's own save + has(&e, "src/proto.js::render -calls-> src/proto.js::save"); +} + +#[test] +fn typescript_ambient_modules_bind_imports_of_their_name() { + let e = edges(&[ + ("package.json", r#"{"name":"demo","dependencies":{"mammoth":"1"}}"#), + ( + "typings/mammoth/index.d.ts", + "declare module 'mammoth' {\n export interface MammothOptions { styleMap?: string }\n export function convertToHtml(input: string, options?: MammothOptions): Promise;\n}\ndeclare module '*.svg' {\n const url: string;\n export default url;\n}\n", + ), + ( + "typings/augment.d.ts", + "import 'express';\ndeclare module 'express' {\n export function augmented(): void;\n}\n", + ), + ("src/doc.ts", "import { convertToHtml, MammothOptions } from 'mammoth';\nimport { augmented } from 'express';\nexport async function render(o: MammothOptions) {\n augmented();\n return convertToHtml('x', o);\n}\n"), + ("src/plain.js", "const { convertToHtml } = require('mammoth');\nexport function run() { return convertToHtml('x'); }\n"), + ]); + has(&e, "src/doc.ts::render -calls-> typings/mammoth/index.d.ts::convertToHtml"); + has(&e, "src/doc.ts::render -references-> typings/mammoth/index.d.ts::MammothOptions"); + // an augmentation in a module file declares no module; JavaScript never reads typings + lacks(&e, "src/doc.ts::render -calls-> typings/augment.d.ts::augmented"); + lacks(&e, "src/plain.js::run -calls-> typings/mammoth/index.d.ts::convertToHtml"); +} + +#[test] +fn redux_thunks_and_rtk_hooks_link_to_what_they_run() { + let e = edges(&[ + ("package.json", r#"{"name":"demo","dependencies":{"@reduxjs/toolkit":"2"}}"#), + ( + "src/thunks.ts", + "import { createAsyncThunk } from '@reduxjs/toolkit';\nimport { loadUser } from './user';\nexport const signIn = createAsyncThunk('auth/signIn', async (arg, { dispatch }) => {\n await dispatch(loadUser(arg));\n dispatch(finish());\n});\nexport const finish = createAsyncThunk('auth/finish', async () => {\n return 1;\n});\n", + ), + ("src/user.ts", "import { createAsyncThunk } from '@reduxjs/toolkit';\nexport const loadUser = createAsyncThunk('user/load', async (id) => id);\n"), + ( + "src/api.ts", + "import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';\nexport const api = createApi({\n baseQuery: fetchBaseQuery({ baseUrl: '/' }),\n endpoints: (b) => ({\n getPosts: b.query({ query: () => 'posts' }),\n }),\n});\nexport const { useGetPostsQuery, useLazyGetPostsQuery } = api;\n", + ), + ]); + has(&e, "src/thunks.ts::signIn -calls-> src/user.ts::loadUser"); + has(&e, "src/thunks.ts::signIn -calls-> src/thunks.ts::finish"); + let rtk: Vec<&String> = e.iter().filter(|x| x.contains("useGetPostsQuery -calls->") || x.contains("useLazyGetPostsQuery -calls->")).collect(); + assert_eq!(rtk.len(), 2, "{e:#?}"); +} + +#[test] +fn rust_generic_bounds_deref_and_lock_guards_type_receivers() { + let e = edges(&[ + ("Cargo.toml", "[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n"), + ( + "src/lib.rs", + r#"use std::ops::Deref; +use std::sync::{Arc, Mutex}; + +pub trait Service { + fn call(&self) -> u32; +} +pub struct T; +impl T { + pub fn call(&self) -> u32 { 0 } +} +pub struct Real; +impl Service for Real { + fn call(&self) -> u32 { 1 } +} +pub fn run(svc: S) -> u32 { svc.call() } +pub fn run_where(svc: &S) -> u32 where S: Service { svc.call() } +pub fn generic_t(t: T) -> u32 { t.call() } +pub fn unbounded(t: T) -> u32 { t.call() } + +pub struct Inner; +impl Inner { + pub fn work(&self) {} +} +pub struct Wrapper { inner: Inner } +impl Deref for Wrapper { + type Target = Inner; + fn deref(&self) -> &Inner { &self.inner } +} +pub fn use_wrapper(w: Wrapper) { w.work(); } + +pub struct Shared { state: Arc> } +impl Shared { + pub fn tick(&self) { self.state.lock().unwrap().work(); } +} +"#, + ), + ]); + has(&e, "src/lib.rs::run -calls-> src/lib.rs::Service::call"); + has(&e, "src/lib.rs::run_where -calls-> src/lib.rs::Service::call"); + // a type parameter is never the project's type of the same name + has(&e, "src/lib.rs::generic_t -calls-> src/lib.rs::Service::call"); + lacks(&e, "src/lib.rs::generic_t -calls-> src/lib.rs::T::call"); + lacks(&e, "src/lib.rs::unbounded -calls-> src/lib.rs::T::call"); + has(&e, "src/lib.rs::use_wrapper -calls-> src/lib.rs::Inner::work"); + has(&e, "src/lib.rs::Shared::tick -calls-> src/lib.rs::Inner::work"); +} + +#[test] +fn python_factories_field_flow_and_pytest_fixtures_type_receivers() { + let e = edges(&[ + ("app/__init__.py", ""), + ( + "app/models.py", + "class Base:\n def run(self):\n return 0\n\nclass A(Base):\n def run(self):\n return 1\n\nclass B(Base):\n def run(self):\n return 2\n\nclass Lone:\n def run(self):\n return 3\n\ndef make(kind):\n if kind:\n return A()\n return B()\n\ndef mixed(kind):\n if kind:\n return A()\n return Lone()\n\nclass Worker:\n def go(self):\n pass\n\nclass Service:\n def __init__(self):\n self.worker = self.build()\n\n def build(self) -> Worker:\n return Worker()\n\n def start(self):\n self.worker.go()\n\ndef use(kind):\n make(kind).run()\n mixed(kind).run()\n", + ), + ("tests/conftest.py", "import pytest\nfrom app.models import Service\n\n@pytest.fixture\ndef service():\n return Service()\n"), + ("tests/test_service.py", "def test_start(service):\n service.start()\n\ndef helper(service):\n service.start()\n"), + ("other/test_other.py", "def test_elsewhere(service):\n service.start()\n"), + ]); + // branches returning A() and B() type the call by their common base + has(&e, "app/models.py::use -calls-> app/models.py::Base::run"); + lacks(&e, "app/models.py::use -calls-> app/models.py::A::run"); + lacks(&e, "app/models.py::use -calls-> app/models.py::Lone::run"); + // `self.worker = self.build()` in one method types `self.worker` in another + has(&e, "app/models.py::Service::start -calls-> app/models.py::Worker::go"); + // a fixture parameter holds what the fixture returns, within the conftest's directory + has(&e, "tests/test_service.py::test_start -calls-> app/models.py::Service::start"); + lacks(&e, "other/test_other.py::test_elsewhere -calls-> app/models.py::Service::start"); +} + +#[test] +fn spring_test_requests_and_kotlin_expect_actual_link() { + let e = edges(&[ + ("pom.xml", "spring-boot-starter-web"), + ( + "src/main/java/app/OwnerController.java", + "package app;\n\nimport org.springframework.web.bind.annotation.*;\n\n@RestController\n@RequestMapping(\"/owners\")\npublic class OwnerController {\n @PostMapping(\"/new\")\n public String create() { return \"\"; }\n\n @GetMapping(\"/{id}\")\n public String show() { return \"\"; }\n}\n", + ), + ( + "src/test/java/app/OwnerControllerTests.java", + "package app;\n\nimport static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;\n\nclass OwnerControllerTests {\n void creates() throws Exception {\n mockMvc.perform(post(\"/owners/new\"));\n }\n void shows() throws Exception {\n mockMvc.perform(get(\"/owners/{id}\", 1));\n }\n void misses() throws Exception {\n mockMvc.perform(get(\"/nothing/here\"));\n }\n}\n", + ), + ("shared/src/commonMain/kotlin/app/Platform.kt", "package app\n\nexpect fun platformName(): String\n\nfun greet(): String = platformName()\n"), + ("shared/src/jvmMain/kotlin/app/Platform.kt", "package app\n\nactual fun platformName(): String = \"jvm\"\n"), + ("shared/src/iosMain/kotlin/app/Platform.kt", "package app\n\nactual fun platformName(): String = \"ios\"\n"), + ]); + let route_edge = |from: &str, to: &str| e.iter().any(|x| x.starts_with(&format!("{from} -calls-> ")) && x.contains(to)); + assert!(route_edge("src/test/java/app/OwnerControllerTests.java::app::OwnerControllerTests::creates", "POST:/owners/new"), "{e:#?}"); + assert!(route_edge("src/test/java/app/OwnerControllerTests.java::app::OwnerControllerTests::shows", "GET:/owners/{id}"), "{e:#?}"); + assert!(!e.iter().any(|x| x.contains("::misses -calls->")), "{e:#?}"); + has(&e, "shared/src/commonMain/kotlin/app/Platform.kt::app::platformName -calls-> shared/src/jvmMain/kotlin/app/Platform.kt::app::platformName"); + has(&e, "shared/src/commonMain/kotlin/app/Platform.kt::app::platformName -calls-> shared/src/iosMain/kotlin/app/Platform.kt::app::platformName"); + lacks(&e, "shared/src/jvmMain/kotlin/app/Platform.kt::app::platformName -calls-> shared/src/iosMain/kotlin/app/Platform.kt::app::platformName"); +} + +#[test] +fn tauri_commands_and_react_native_modules_cross_the_bridge() { + let e = edges(&[ + ("package.json", r#"{"name":"demo","dependencies":{"@tauri-apps/api":"2","react-native":"0.74"}}"#), + ( + "src-tauri/src/main.rs", + "#[tauri::command]\nfn save_file(path: String) {}\n\n#[tauri::command]\nfn unregistered() {}\n\nfn main() {\n tauri::Builder::default().invoke_handler(tauri::generate_handler![save_file]);\n}\n", + ), + ("src/app.ts", "import { invoke } from '@tauri-apps/api/core';\nexport async function save() {\n await invoke('save_file', { path: 'x' });\n await invoke('unregistered');\n}\n"), + ( + "android/app/src/main/java/com/app/CalendarModule.java", + "package com.app;\n\nimport com.facebook.react.bridge.ReactMethod;\n\npublic class CalendarModule extends ReactContextBaseJavaModule {\n @Override\n public String getName() {\n return \"Calendar\";\n }\n\n @ReactMethod\n public void createEvent(String name) {}\n}\n", + ), + ("src/calendar.js", "import { NativeModules } from 'react-native';\nconst { Calendar } = NativeModules;\nexport function add() {\n NativeModules.Calendar.createEvent('a');\n}\nexport function addAgain() {\n Calendar.createEvent('b');\n}\n"), + ]); + has(&e, "src/app.ts::save -calls-> src-tauri/src/main.rs::save_file"); + lacks(&e, "src/app.ts::save -calls-> src-tauri/src/main.rs::unregistered"); + has(&e, "src/calendar.js::add -calls-> android/app/src/main/java/com/app/CalendarModule.java::com.app::CalendarModule::createEvent"); + has(&e, "src/calendar.js::addAgain -calls-> android/app/src/main/java/com/app/CalendarModule.java::com.app::CalendarModule::createEvent"); +} + +#[test] +fn qualified_project_embeds_and_rust_type_aliases_keep_their_members() { + let go = edges(&[ + GO_MOD, + ("sandbox/manager.go", "package sandbox\n\ntype Manager interface {\n\tStart() error\n}\n"), + ("other/manager.go", "package other\n\ntype Manager interface {\n\tStop() error\n}\n"), + ( + "svc/svc.go", + "package svc\n\nimport \"example.com/demo/sandbox\"\n\ntype Installer interface {\n\tsandbox.Manager\n\tBind(dir string) error\n}\n\ntype impl struct{}\n\nfunc (impl) Start() error { return nil }\nfunc (impl) Bind(dir string) error { return nil }\n\ntype partial struct{}\n\nfunc (partial) Bind(dir string) error { return nil }\n", + ), + ]); + // the embedded interface is the project's `sandbox.Manager`, though the kernel writes `Manager` + has(&go, "svc/svc.go::impl -implements-> svc/svc.go::Installer"); + lacks(&go, "svc/svc.go::partial -implements-> svc/svc.go::Installer"); + let rs = edges(&[ + ("Cargo.toml", "[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n"), + ( + "src/lib.rs", + "pub struct Outcome { items: Vec }\nimpl Outcome {\n pub fn summaries(&self) -> usize { self.items.len() }\n}\npub type Loaded = Outcome;\npub fn load() -> Loaded { Outcome { items: Vec::new() } }\npub fn use_it() -> usize {\n let loaded = load();\n loaded.summaries()\n}\n", + ), + ]); + has(&rs, "src/lib.rs::use_it -calls-> src/lib.rs::Outcome::summaries"); +} diff --git a/crates/cg-sync/src/sync.rs b/crates/cg-sync/src/sync.rs index eeaf457..9cb006c 100644 --- a/crates/cg-sync/src/sync.rs +++ b/crates/cg-sync/src/sync.rs @@ -259,16 +259,24 @@ type ExportSig = Vec<(String, String, String, u8)>; type ResolverFactSig = BTreeSet<(u8, String, String, String, String)>; /// These facts change resolution even when the kernel's declaration identity -/// stays the same (e.g. `obj.old = function internal` -> `obj.new = ...`). +/// stays the same (e.g. `obj.old = function internal` -> `obj.new = ...`, or the target of a +/// Rust `impl Deref`, which has no node of its own). fn resolver_fact_sig(view: &View, f: FileId) -> ResolverFactSig { view.bindings_of(f) .iter() - .filter(|b| (b.kind == BindKind::MemberFn as u8 || b.kind == BindKind::ReturnType as u8) && b.node.is_some()) + .filter(|b| { + ((b.kind == BindKind::MemberFn as u8 + || b.kind == BindKind::ReturnType as u8 + || b.kind == BindKind::Fixture as u8 + || b.kind == BindKind::TypeAlias as u8) + && b.node.is_some()) + || b.kind == BindKind::Deref as u8 + }) .map(|b| { ( b.kind, view.text_str(b.name).to_owned(), - view.node(b.node).qname().to_owned(), + if b.node.is_some() { view.node(b.node).qname().to_owned() } else { String::new() }, view.text_str(b.decl_type).to_owned(), view.text_str(b.init_ctor).to_owned(), ) @@ -527,6 +535,12 @@ pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOp let mut definitions_changed = false; let mut export_changed: Vec = Vec::new(); let mut remap: FxHashMap = FxHashMap::default(); + // pytest injects a fixture by parameter name, with no import: a changed fixture re-resolves + // the Python files of its directory and below + let mut fixture_dirs: Vec = Vec::new(); + let fixture_dir = |view: &View, f: FileId, facts: &ResolverFactSig| -> Option { + facts.iter().any(|fact| fact.0 == BindKind::Fixture as u8).then(|| cg_resolve::project::parent_dir(view.file(f).path()).to_owned()) + }; for (i, f) in ids.iter().enumerate() { let old = if states[i] == DeltaState::Replaced { defs_of(&base_view, *f) } else { BTreeMap::new() }; let new = defs_of(&view1, *f); @@ -537,6 +551,7 @@ pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOp definitions_changed = true; d_names.extend(old_facts.iter().chain(&new_facts).map(|fact| fact.1.clone())); export_changed.push(*f); + fixture_dirs.extend(fixture_dir(&view1, *f, &new_facts).or_else(|| fixture_dir(&view1, *f, &old_facts))); } for (k, details) in &new { // a changed signature can move a call to another overload of the name, even one @@ -558,7 +573,9 @@ pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOp } } for f in &removed { - d_names.extend(resolver_fact_sig(&base_view, *f).into_iter().map(|fact| fact.1)); + let facts = resolver_fact_sig(&base_view, *f); + fixture_dirs.extend(fixture_dir(&base_view, *f, &facts)); + d_names.extend(facts.into_iter().map(|fact| fact.1)); for k in defs_of(&base_view, *f).keys() { d_names.insert(k.name.clone()); } @@ -635,6 +652,14 @@ pub fn sync_locked(store: &Store, guard: &WriteGuard, root: &Path, opts: &SyncOp } } } + for dir in &fixture_dirs { + for (fi, rec) in base.p.files.iter().enumerate() { + let path = base.paths.get(rec.path); + if path.ends_with(".py") && (dir.is_empty() || path.starts_with(&format!("{dir}/"))) { + consumer_files.insert(fi as u32); + } + } + } let all_refs_of_file = |f: u32, affected: &mut FxHashSet| { let rec = &base.p.files[f as usize]; for id in rec.ref_start..rec.ref_start + rec.ref_count { diff --git a/crates/cg-sync/tests/gap_closure.rs b/crates/cg-sync/tests/gap_closure.rs new file mode 100644 index 0000000..052547d --- /dev/null +++ b/crates/cg-sync/tests/gap_closure.rs @@ -0,0 +1,119 @@ +//! The rules ported for resolution gaps and dynamic dispatch must keep an incremental sync equal +//! to a fresh index when either side of a link changes. +use cg_resolve::{index_project, IndexOptions}; +use cg_store::{dump, Store}; +use cg_sync::{sync, SyncOptions}; +use std::path::Path; +use std::time::{Duration, SystemTime}; + +const FILES: &[(&str, &str)] = &[ + ("go.mod", "module example.com/demo\n\ngo 1.22\n"), + ( + "disc/disc.go", + "package disc\n\nimport \"io\"\n\ntype ReadCloser interface {\n\tio.Reader\n\tClose() error\n}\n\ntype Options struct {\n\tRefreshF func() error\n}\n\ntype Discovery struct {\n\trefreshf func() error\n}\n\nfunc New(o Options) *Discovery {\n\treturn &Discovery{refreshf: o.RefreshF}\n}\n\nfunc (d *Discovery) Run() {\n\td.refreshf()\n}\n", + ), + ("ec2/ec2.go", "package ec2\n\nimport \"example.com/demo/disc\"\n\ntype EC2 struct{}\n\nfunc (e *EC2) refresh() error { return nil }\nfunc (e *EC2) Read(b []byte) (int, error) { return 0, nil }\nfunc (e *EC2) Close() error { return nil }\n\nfunc Wire() *disc.Discovery {\n\te := &EC2{}\n\treturn disc.New(disc.Options{RefreshF: e.refresh})\n}\n"), + ("package.json", r#"{"name":"demo"}"#), + ("src/impl.ts", "export function realImpl() { return 1; }\nexport function other() { return 2; }\nexport const alias = realImpl;\n"), + ("src/use.ts", "import { alias } from './impl';\nexport function caller() {\n alias();\n}\n"), + ("src/proto.js", "function Params() {}\nParams.prototype.render = function render() { return 1; };\nfunction use() {\n var p = new Params();\n p.render();\n}\nmodule.exports = use;\n"), + ("Cargo.toml", "[package]\nname = \"demo\"\nversion = \"0.1.0\"\nedition = \"2021\"\n"), + ("src/lib.rs", "pub mod wrap;\npub trait Service {\n fn call(&self) -> u32;\n}\npub fn run(s: S) -> u32 {\n s.call()\n}\n"), + ("src/wrap.rs", "use std::ops::Deref;\npub struct Inner;\nimpl Inner {\n pub fn work(&self) {}\n}\npub struct Other;\nimpl Other {\n pub fn work(&self) {}\n}\npub struct Wrapper { inner: Inner }\nimpl Deref for Wrapper {\n type Target = Inner;\n fn deref(&self) -> &Inner { &self.inner }\n}\npub fn go(w: Wrapper) {\n w.work();\n}\n"), + ("app/__init__.py", ""), + ("app/models.py", "class Worker:\n def go(self):\n pass\n\nclass Other:\n def go(self):\n pass\n"), + ("tests/conftest.py", "import pytest\nfrom app.models import Worker\n\n@pytest.fixture\ndef worker():\n return Worker()\n"), + ("tests/test_worker.py", "def test_go(worker):\n worker.go()\n"), +]; + +fn opts() -> IndexOptions { + IndexOptions { threads: Some(2), ..Default::default() } +} +fn write(root: &Path, rel: &str, text: &str, step: u64) { + let path = root.join(rel); + std::fs::create_dir_all(path.parent().unwrap()).unwrap(); + std::fs::write(&path, text).unwrap(); + let stamp = SystemTime::UNIX_EPOCH + Duration::from_secs(1_700_000_000 + step); + std::fs::OpenOptions::new().write(true).open(path).unwrap().set_modified(stamp).unwrap(); +} +fn graph(root: &Path) -> Vec { + let snap = Store::locate(root).unwrap().open_snapshot().unwrap(); + dump::canonical(&snap.view) +} +fn edges(root: &Path) -> Vec { + let snap = Store::locate(root).unwrap().open_snapshot().unwrap(); + dump::dump_edges(&snap.view) +} +fn fresh(root: &Path) -> Vec { + let temp = tempfile::tempdir().unwrap(); + for (rel, _) in FILES { + let text = std::fs::read_to_string(root.join(rel)).unwrap(); + write(temp.path(), rel, &text, 1); + } + index_project(temp.path(), &opts()).unwrap(); + graph(temp.path()) +} + +#[test] +fn gap_rule_edits_remain_equal_to_full_rebuild() { + let temp = tempfile::tempdir().unwrap(); + let root = temp.path(); + for (i, (rel, contents)) in FILES.iter().enumerate() { + write(root, rel, contents, i as u64); + } + index_project(root, &opts()).unwrap(); + let has = |needle: &str| edges(root).iter().any(|e| e == needle); + assert!(has("disc/disc.go::Discovery::Run -calls-> ec2/ec2.go::EC2::refresh"), "{:#?}", edges(root)); + assert!(has("ec2/ec2.go::EC2 -implements-> disc/disc.go::ReadCloser")); + assert!(has("src/use.ts::caller -calls-> src/impl.ts::realImpl")); + assert!(has("src/proto.js::use -calls-> src/proto.js::render")); + assert!(has("src/lib.rs::run -calls-> src/lib.rs::Service::call")); + assert!(has("src/wrap.rs::go -calls-> src/wrap.rs::Inner::work")); + assert!(has("tests/test_worker.py::test_go -calls-> app/models.py::Worker::go")); + let changes = [ + ("ec2/ec2.go", "RefreshF: e.refresh", "RefreshF: nil"), + ("ec2/ec2.go", "RefreshF: nil", "RefreshF: e.refresh"), + ("ec2/ec2.go", "func (e *EC2) Read(b []byte) (int, error) { return 0, nil }\n", ""), + ( + "ec2/ec2.go", + "func (e *EC2) Close() error { return nil }\n", + "func (e *EC2) Close() error { return nil }\nfunc (e *EC2) Read(b []byte) (int, error) { return 0, nil }\n", + ), + ("src/impl.ts", "export const alias = realImpl;", "export const alias = other;"), + ("src/proto.js", "Params.prototype.render = function render()", "Params.prototype.paint = function render()"), + ("src/proto.js", "Params.prototype.paint = function render()", "Params.prototype.render = function render()"), + ("src/lib.rs", "run", "run"), + ("src/wrap.rs", "type Target = Inner;", "type Target = Other;"), + ("tests/conftest.py", "return Worker()", "return Other()"), + ("app/models.py", "class Other:\n def go(self):", "class Other:\n def gone(self):"), + ]; + for (i, (file, from, to)) in changes.iter().enumerate() { + let current = std::fs::read_to_string(root.join(file)).unwrap(); + assert!(current.contains(from), "step {i}: {file} lacks {from:?}"); + write(root, file, ¤t.replacen(from, to, 1), 100 + i as u64); + let report = sync(root, &SyncOptions { index: opts(), ..Default::default() }).unwrap(); + assert!(report.full_rebuild.is_none(), "step {i} used a full rebuild: {:?}", report.full_rebuild); + let (inc, full) = (graph(root), fresh(root)); + assert_eq!(inc, full, "incremental graph differs at step {i}: {file}"); + match i { + 0 => assert!(!has("disc/disc.go::Discovery::Run -calls-> ec2/ec2.go::EC2::refresh"), "stale func-field edge"), + 1 => assert!(has("disc/disc.go::Discovery::Run -calls-> ec2/ec2.go::EC2::refresh")), + 2 => assert!(!has("ec2/ec2.go::EC2 -implements-> disc/disc.go::ReadCloser"), "io.Reader asks for Read"), + 3 => assert!(has("ec2/ec2.go::EC2 -implements-> disc/disc.go::ReadCloser")), + 4 => { + assert!(has("src/use.ts::caller -calls-> src/impl.ts::other")); + assert!(!has("src/use.ts::caller -calls-> src/impl.ts::realImpl")); + } + 5 => assert!(!has("src/proto.js::use -calls-> src/proto.js::render")), + 6 => assert!(has("src/proto.js::use -calls-> src/proto.js::render")), + 7 => assert!(!has("src/lib.rs::run -calls-> src/lib.rs::Service::call")), + 8 => { + assert!(has("src/wrap.rs::go -calls-> src/wrap.rs::Other::work")); + assert!(!has("src/wrap.rs::go -calls-> src/wrap.rs::Inner::work")); + } + 9 => assert!(has("tests/test_worker.py::test_go -calls-> app/models.py::Other::go")), + 10 => assert!(!has("tests/test_worker.py::test_go -calls-> app/models.py::Other::go")), + _ => {} + } + } +}