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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/cg-core/src/brand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ pub const UPSTREAM_CONFIG_FILE: &str = "codegraph.json";
pub const CONFIG_FILE: &str = "cgr.json";
/// Version of the resolution rules: changing resolver behaviour bumps this, which
/// marks older indexes as "built by an older engine" (design doc 5.8).
pub const ENGINE_VERSION: u32 = 7;
pub const ENGINE_VERSION: u32 = 8;
/// Version of the on-disk segment format; a mismatch forces a full rebuild.
pub const FORMAT_VERSION: u32 = 2;
/// The upstream commit whose kernel is vendored (see crates/cg-kernel/UPSTREAM_COMMIT).
Expand Down
20 changes: 16 additions & 4 deletions crates/cg-extract/src/facts/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
//! * `T:<type>` - a literal, `new T(..)` or a cast: the static type as written
//! * `V:<name>` - a local, parameter or field, typed at resolution from its binding
//! * `F:<name>` - `this.<name>`: a field of the enclosing type
//! * `C:<name>` - a call inside `satisfies`, typed from an unambiguous return annotation
//! * `#this` - `this`
//! * `N` - `null` / `undefined`
//! * `L<k>` - a lambda / function literal taking `k` parameters; `L` alone: a method
Expand Down Expand Up @@ -259,10 +260,14 @@ fn js_type(cx: &Cx, t: Node) -> String {
}

fn js_shape(cx: &Cx, a: Node) -> String {
js_shape_inner(cx, a, false)
}

fn js_shape_inner(cx: &Cx, a: Node, in_satisfies: bool) -> String {
let typed = |t: &str| format!("T:{t}");
match a.kind() {
"string" | "template_string" => typed("string"),
"number" => typed("number"),
"number" => typed(if cx.text(a).ends_with('n') { "bigint" } else { "number" }),
"true" | "false" => typed("boolean"),
"null" | "undefined" => "N".into(),
"this" => "#this".into(),
Expand All @@ -284,13 +289,20 @@ fn js_shape(cx: &Cx, a: Node) -> String {
};
lambda(n)
}
"parenthesized_expression" | "non_null_expression" => a.named_child(0).map(|c| js_shape(cx, c)).unwrap_or_else(|| "?".into()),
"as_expression" | "satisfies_expression" => match a.named_child(1).map(|t| js_type(cx, t)) {
"parenthesized_expression" | "non_null_expression" => a.named_child(0).map(|c| js_shape_inner(cx, c, in_satisfies)).unwrap_or_else(|| "?".into()),
"satisfies_expression" => a.named_child(0).map(|c| js_shape_inner(cx, c, true)).unwrap_or_else(|| "?".into()),
"call_expression" if in_satisfies => match a.child_by_field_name("function") {
Some(f) if f.kind() == "identifier" => format!("C:{}", cx.text(f)),
_ => "?".into(),
},
"as_expression" => match a.named_child(1).map(|t| js_type(cx, t)) {
Some(t) if !t.is_empty() => typed(&t),
_ => "?".into(),
},
"unary_expression" => match a.child_by_field_name("argument") {
Some(o) if o.kind() == "number" && matches!(cx.text(a).as_bytes().first(), Some(b'-' | b'+')) => typed("number"),
Some(o) if o.kind() == "number" && matches!(cx.text(a).as_bytes().first(), Some(b'-' | b'+')) => {
typed(if cx.text(o).ends_with('n') { "bigint" } else { "number" })
}
_ => "?".into(),
},
_ => "?".into(),
Expand Down
30 changes: 28 additions & 2 deletions crates/cg-extract/src/facts/js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,13 +151,39 @@ fn ts_type_name(cx: &Cx, n: Node) -> String {
/// (declared type, initialiser) of a value expression: `new Foo()` -> ("", "Foo"),
/// `foo()` -> ("", "foo()"), `x as Foo` -> ("Foo", "").
fn value_info(cx: &Cx, v: Node) -> (String, String) {
value_info_inner(cx, v, false)
}

fn value_info_inner(cx: &Cx, v: Node, in_satisfies: bool) -> (String, String) {
match v.kind() {
"new_expression" => {
let c = v.child_by_field_name("constructor").map(|c| cx.text(c).to_owned()).unwrap_or_default();
(String::new(), c)
}
"await_expression" | "parenthesized_expression" | "non_null_expression" => v.named_child(0).map(|c| value_info(cx, c)).unwrap_or_default(),
"as_expression" | "satisfies_expression" => {
"await_expression" | "parenthesized_expression" | "non_null_expression" => {
v.named_child(0).map(|c| value_info_inner(cx, c, in_satisfies)).unwrap_or_default()
}
// Preserve the operand's evidence, but mark it so an unknown receiver
// cannot be guessed from an unrelated global method.
"satisfies_expression" => {
let (ty, init) = v.named_child(0).map(|c| value_info_inner(cx, c, true)).unwrap_or_default();
if ty.is_empty() && !init.starts_with(JS_SATISFIES_VALUE) {
(ty, format!("{JS_SATISFIES_VALUE}{init}"))
} else {
(ty, init)
}
}
"identifier" if in_satisfies => (String::new(), format!("{JS_BINDING_ALIAS}{}", cx.text(v))),
"string" | "template_string" if in_satisfies => ("string".into(), String::new()),
"number" if in_satisfies => (if cx.text(v).ends_with('n') { "bigint" } else { "number" }.into(), String::new()),
"true" | "false" if in_satisfies => ("boolean".into(), String::new()),
"unary_expression" if in_satisfies => match v.child_by_field_name("argument") {
Some(n) if n.kind() == "number" && matches!(cx.text(v).as_bytes().first(), Some(b'-' | b'+')) => {
(if cx.text(n).ends_with('n') { "bigint" } else { "number" }.into(), String::new())
}
_ => Default::default(),
},
"as_expression" => {
let t = v.named_child(1).map(|c| ts_type_name(cx, c)).unwrap_or_default();
(t, String::new())
}
Expand Down
9 changes: 8 additions & 1 deletion crates/cg-extract/src/facts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@ pub use util::Cx;
/// `default` export. The leading NUL keeps it out of ordinary source identifiers.
pub const COMMONJS_MODULE: &str = "\0cjs:module.exports";

/// Marks initializer evidence collected through a TypeScript `satisfies` expression.
pub const JS_SATISFIES_VALUE: &str = "\0js:satisfies:";

/// Initializer evidence that names a JavaScript/TypeScript lexical binding.
/// The NUL prefix distinguishes it from a constructor or call path.
pub const JS_BINDING_ALIAS: &str = "\0js:binding:";

#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum ImportKind {
Expand Down Expand Up @@ -134,7 +141,7 @@ pub struct BindingFact {
/// Declared type, usually reduced to its head (`Foo<T>` -> `Foo`). Rust
/// retains generic arguments to distinguish containers and pointer targets.
pub decl_type: String,
/// Constructor or bounded initializer call path (`Foo::new().build()`), or empty.
/// Constructor, bounded initializer call path, tagged lexical alias, or empty.
pub init_ctor: String,
/// 1-based declaration line; the binding is visible from here on (hoisted functions
/// are visible throughout their scope and use line 0).
Expand Down
30 changes: 30 additions & 0 deletions crates/cg-extract/tests/facts_js.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,33 @@ export class Widget extends Base {
assert!(f.scopes.iter().any(|s| s.kind == ScopeKind::Class));
assert!(f.scopes.iter().filter(|s| s.kind == ScopeKind::Function).count() >= 3);
}

#[test]
fn typescript_satisfies_preserves_initializer_evidence() {
let src = r#"const direct = new Service() satisfies Runner;
const factory = makeService() satisfies Runner;
const grouped = (new Service() satisfies Runner) satisfies object;
const asserted = (new Service() as Runner) satisfies object;
const cast = new Service() as Runner;
const unknown = externalValue satisfies Runner;
const annotated: Runner = new Service() satisfies object;
class Holder { service = new Service() satisfies Runner; }
"#;
for lang in [Lang::TypeScript, Lang::Tsx] {
let b = run("satisfies.ts", src, lang);
assert_eq!(b.flags & (fileflags::PARSE_ERROR | fileflags::PANICKED), 0, "{:?}", b.errors);
for (name, decl_type, init_ctor) in [
("direct", "", "\0js:satisfies:Service"),
("factory", "", "\0js:satisfies:makeService()"),
("grouped", "", "\0js:satisfies:Service"),
("asserted", "Runner", ""),
("cast", "Runner", ""),
("unknown", "", "\0js:satisfies:\0js:binding:externalValue"),
("annotated", "Runner", "\0js:satisfies:Service"),
("service", "", "\0js:satisfies:Service"),
] {
let binding = b.facts.bindings.iter().find(|binding| binding.name == name).unwrap();
assert_eq!((binding.decl_type.as_str(), binding.init_ctor.as_str()), (decl_type, init_ctor), "{lang:?} {name}");
}
}
}
128 changes: 118 additions & 10 deletions crates/cg-resolve/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::modules::{ImportTarget, PkgId};
use crate::overload::{self, ArgTy, CallArgs};
use crate::shape::{parse_shape, split_head, Shape};
use cg_core::{text::pack_pos, EdgeKind, FileId, Lang, LangFamily, NodeIdx, NodeKind, RefKind, Visibility, NONE};
use cg_extract::facts::COMMONJS_MODULE;
use cg_extract::facts::{COMMONJS_MODULE, JS_BINDING_ALIAS, JS_SATISFIES_VALUE};
use cg_extract::{BindKind, ImportKind, ScopeKind};
use cg_store::{status, BindingRec, ImportRec, RefRec, ScopeRec, View};
use rustc_hash::{FxHashMap, FxHashSet};
Expand Down Expand Up @@ -562,7 +562,9 @@ impl<'e, 'v> FileCx<'e, 'v> {

/// 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))
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 || b.kind == BindKind::Alias as u8))
}

/// Follow `segs` from a module / type: exports, top-level definitions, submodules, members.
Expand Down Expand Up @@ -695,17 +697,72 @@ impl<'e, 'v> FileCx<'e, 'v> {
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);
}
if self.visible_import(init, scope).is_some() {
return self.imported_binding_value(wcx, init, scope, 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 root = init.strip_prefix(JS_SATISFIES_VALUE).unwrap_or(init);
let root = root.strip_prefix(JS_BINDING_ALIAS).unwrap_or(root);
let shadows = root.split(['.', ':', '(']).next() == Some(self.view.text_str(b.name));
let line = if shadows { b.line.saturating_sub(1) } else { b.line };
self.expression_value(wcx, init, scope, line, depth + 1)
}

fn imported_binding_value(&self, wcx: &mut WorkerCx<'e, 'v>, name: &str, scope: u32, depth: u8) -> Option<ValueType> {
if depth >= 16 {
return None;
}
let import = self.visible_import(name, scope)?;
let Bound::Node(node) = self.e.bind_import(self.file, import as usize) else { return None };
if self.view.node(node).kind().is_type() {
return Some(ValueType::plain(node));
}
let owner = self.view.hot(node).file;
let cx = wcx.file_cx(self.e, owner);
let binding = cx.file_binding(self.view.node(node).name())?;
cx.binding_value(wcx, binding, depth + 1)
}

/// Do not guess a member of an unresolved value when an alias leads back
/// to an explicit type or a `satisfies` operand.
fn blocks_member_fallback(&self, wcx: &mut WorkerCx<'e, 'v>, binding: &BindingRec, depth: u8) -> bool {
if depth >= 16 {
return true;
}
let init = self.view.text_str(binding.init_ctor);
if !self.view.text_str(binding.decl_type).is_empty() || init.starts_with(JS_SATISFIES_VALUE) || init.starts_with(JS_BINDING_ALIAS) {
return true;
}
if binding.kind != BindKind::Alias as u8 {
return false;
}
let scope = binding.scope.0.wrapping_sub(self.scope_base);
if let Some(other) = self.lookup_binding(init, scope, binding.line, false) {
return std::ptr::eq(other, binding) || self.blocks_member_fallback(wcx, other, depth + 1);
}
let Some(import) = self.visible_import(init, scope) else { return false };
let Bound::Node(node) = self.e.bind_import(self.file, import as usize) else { return true };
let cx = wcx.file_cx(self.e, self.view.hot(node).file);
match cx.file_binding(self.view.node(node).name()) {
Some(other) => cx.blocks_member_fallback(wcx, other, depth + 1),
None => true,
}
}

fn expression_value(&self, wcx: &mut WorkerCx<'e, 'v>, init: &str, scope: u32, line: u32, depth: u8) -> Option<ValueType> {
if depth >= 16 {
return None;
}
if let Some(inner) = init.strip_prefix(JS_SATISFIES_VALUE) {
return self.expression_value(wcx, inner, scope, line, depth + 1);
}
if let Some(name) = init.strip_prefix(JS_BINDING_ALIAS) {
if let Some(binding) = self.lookup_binding(name, scope, line, false) {
return self.binding_value(wcx, binding, depth + 1);
}
return self.imported_binding_value(wcx, name, scope, depth + 1);
}
// `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('|') {
Expand Down Expand Up @@ -1153,6 +1210,54 @@ impl<'e, 'v> FileCx<'e, 'v> {
ArgTy::Named(self.view.node(t).name().to_owned(), Some(t))
}

/// Recover a primitive return annotation only from a single unambiguous JS callee.
fn primitive_call_arg(&self, wcx: &mut WorkerCx<'e, 'v>, callee: &str, scope: u32, line: u32) -> Option<ArgTy> {
if self.family != LangFamily::Js || callee.is_empty() || !callee.chars().all(|c| c.is_alphanumeric() || c == '_' || c == '$') {
return None;
}
let NameRes::Found(node, ..) = self.resolve_name(wcx, callee, Accept::Callable, scope, line, None, false) else { return None };
if self.sibling_overloads(node).len() != 1 {
return None;
}
let (_, ret) = self.view.node(node).signature()?.rsplit_once(':')?;
let ret = ret.trim().trim_end_matches(';').trim();
matches!(ret, "string" | "number" | "boolean" | "bigint" | "symbol").then(|| self.named_arg(wcx, ret))
}

fn binding_arg_type(&self, wcx: &mut WorkerCx<'e, 'v>, b: &BindingRec, depth: u8) -> ArgTy {
if depth >= 16 {
return ArgTy::Unknown;
}
let declared = self.view.text_str(b.decl_type);
if !declared.is_empty() {
return self.named_arg(wcx, declared);
}
let init = self.view.text_str(b.init_ctor);
let satisfied = init.strip_prefix(JS_SATISFIES_VALUE);
let value = satisfied.unwrap_or(init);
let alias = value.strip_prefix(JS_BINDING_ALIAS).or_else(|| (b.kind == BindKind::Alias as u8).then_some(value));
let scope = b.scope.0.wrapping_sub(self.scope_base);
if let Some(name) = alias {
if let Some(other) = self.lookup_binding(name, scope, b.line, false).filter(|other| !std::ptr::eq(*other, b)) {
return self.binding_arg_type(wcx, other, depth + 1);
}
if let Some(import) = self.visible_import(name, scope) {
if let Bound::Node(node) = self.e.bind_import(self.file, import as usize) {
let cx = wcx.file_cx(self.e, self.view.hot(node).file);
if let Some(other) = cx.file_binding(self.view.node(node).name()) {
return cx.binding_arg_type(wcx, other, depth + 1);
}
}
}
}
if let Some(callee) = satisfied.and_then(|s| s.strip_suffix("()")) {
if let Some(arg) = self.primitive_call_arg(wcx, callee, scope, b.line) {
return arg;
}
}
self.binding_type(wcx, b).map_or(ArgTy::Unknown, |t| self.node_arg(t))
}

fn arg_type(&self, wcx: &mut WorkerCx<'e, 'v>, shape: &str, scope: u32, r: &RefRec) -> ArgTy {
match shape {
"?" => ArgTy::Unknown,
Expand All @@ -1163,16 +1268,15 @@ impl<'e, 'v> FileCx<'e, 'v> {
if let Some(t) = shape.strip_prefix("T:") {
return self.named_arg(wcx, t);
}
if let Some(callee) = shape.strip_prefix("C:") {
return self.primitive_call_arg(wcx, callee, scope, r.line).unwrap_or(ArgTy::Unknown);
}
if let Some(v) = shape.strip_prefix("V:") {
if let Some(b) = self.lookup_binding(v, scope, r.line, false) {
if b.kind == BindKind::LocalFn as u8 {
return ArgTy::Lambda(None);
}
let dt = self.view.text_str(b.decl_type);
if !dt.is_empty() {
return self.named_arg(wcx, dt);
}
return self.binding_type(wcx, b).map_or(ArgTy::Unknown, |t| self.node_arg(t));
return self.binding_arg_type(wcx, b, 0);
}
// a bare field: Java / Kotlin only (JS reads fields through `this.`)
return if self.family == LangFamily::Jvm { self.field_arg(wcx, r, v) } else { ArgTy::Unknown };
Expand Down Expand Up @@ -1797,9 +1901,13 @@ impl<'e, 'v> FileCx<'e, 'v> {
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);
// Unknown satisfies operands and their aliases do not
// justify guessing an unrelated project member.
if self.blocks_member_fallback(wcx, b, 0) {
return Outcome::st(status::LOCAL_BINDING);
}
// Neither does an initializer rooted in an external import.
let head = init.split(['.', ':', '(']).next().unwrap_or("");
if let Some(i) = self.visible_import(head, scope).filter(|_| self.lookup_binding(head, scope, r.line, false).is_none()) {
if self.e.bind_import(self.file, i as usize) == Bound::External {
Expand Down
Loading
Loading