mirror of
https://github.com/colbymchenry/codegraph.git
synced 2026-10-18 13:53:01 +08:00
Using an enum only through its variants (`mode::Mode::A` in an expression,
`mode::Mode::A => 1` / `Mode::B => ...` in a match, a `Mode::C(x)` or
`Mode::D { .. }` pattern) recorded no reference to the enum, so code that
builds or matches variants was missing from the enum's callers and impact.
Only a type annotation (`fn takes(_m: Mode)`) produced one.
Cause: Rust was not in the static-member pass, which turns `Enum.value` /
`Type::CONST` reads into references for Java, C#, Kotlin, Swift, Scala,
Dart, PHP and C++, and a path to a variant that is not a call or a struct
literal reaches no other extractor.
Fix:
- Extraction (wasm extractor and native kernel, mirrored): a path whose
last segment is capitalized emits a `references` ref named by its
receiver (the segment before it; `Self` read as the impl's type, nothing
in a trait), positioned at the receiver, so a `mode::` prefix scopes it
exactly as it scopes a type annotation. Covers value reads, match arms,
`|` / `if let` patterns, tuple- and struct-variant patterns and a variant
passed as a value. A call's callee (`Mode::C(1)`, `Foo::new()`), a struct
literal, a longer path's prefix, a `use` tree and a lowercase receiver or
member emit nothing.
- Resolution (isRustNameInScope): a reference written `Name::Variant` or
`Self::Variant` lands only on an enum that declares that variant, after
the usual Rust scoping (module path, `use`, glob, prelude, std and
dependency imports). ripgrep's `Match::None` reaches `ignore`'s enum, not
the matcher's `struct Match`; std variants (`Ordering::Less`,
`Option::Some`) never reach a same-named project enum; associated consts
and functions (`Limits::MAX`, `Mode::ALL`, `Foo::new()`) reference no
type. `Self::X` is read as the impl's type, never through a path written
elsewhere on its line.
Adapted from #2330 by @danusha2345: the extraction half (both walkers) and
most of the tests are theirs. Changed here:
- The resolver accepts a declared variant only. #2330 also accepted any type
whose file holds a variable or const of the item's name; impl consts are
file-level variables in the graph, so that cannot tell a type's associated
const from another type's or a top-level one.
- The walkers check the node kind before looking at its parent, and the
body walk hands the parent down: #2330 called `parent()`, which walks down
from the tree's root, for every node of every body, which nearly doubled
the kernel's Rust extraction time on tokio (2.1 s -> 3.9 s); now within
noise of main.
- EXTRACTION_VERSION is not bumped again (already 28 on main).
Verification: __tests__/rust-enum-variant-reference.test.ts (10 tests):
6 fail on main in wasm mode, in kernel mode, and with this TypeScript over
main's kernel binary; 10/10 pass in both modes. The kernel parity case
covers every shape and asserts the refs exist; the kernel-parity sweep over
ripgrep, alacritty and tokio is 1004/1006 files byte-parity, 0 diffs (2
parse-error files defer to wasm by policy).
Validation (native kernel loaded, main -> branch, edges keyed by
QN/kind/file/line): ripgrep +822/-0, alacritty +917/-0, tokio +665/-0, all
`references -> enum`, node counts unchanged. Every added edge's source
reads `Name::Variant` / `Self::Variant` at its column and its target enum
declares that variant; no std enum targets. New symbol->enum pairs:
ripgrep +220, alacritty +253, tokio +254; enums with no referencer before
and one now: 31 / 13 / 31. One wrong target on tokio
(`oneshot::error::TryRecvError::Empty` -> mpsc's `TryRecvError`) comes
from the existing path rule that checks only the last module segment, which
type annotations share. Index time on tokio stays within noise of main
(interleaved runs; resolveOne time 27.7 s vs 31.2 s summed over workers).
Co-authored-by: danusha2345 <ewidusoc498@gmail.com>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -66,6 +66,11 @@ fn simple_ident_re() -> &'static Regex {
|
||||
static RE: OnceLock<Regex> = OnceLock::new();
|
||||
RE.get_or_init(|| Regex::new(r"^[A-Za-z_][0-9A-Za-z_]*$").unwrap())
|
||||
}
|
||||
/// extractStaticMemberRef's capitalized-receiver test.
|
||||
fn capitalized_re() -> &'static Regex {
|
||||
static RE: OnceLock<Regex> = OnceLock::new();
|
||||
RE.get_or_init(|| Regex::new(r"^[A-Z][A-Za-z0-9_]*$").unwrap())
|
||||
}
|
||||
|
||||
struct Scope {
|
||||
row: u32,
|
||||
@@ -1183,10 +1188,12 @@ impl<'t> Walker<'t> {
|
||||
|
||||
fn visit_function_body(&mut self, body: Node<'t>) {
|
||||
stack_guard!();
|
||||
self.visit_for_calls_and_structure(body);
|
||||
self.visit_for_calls_and_structure(body, None);
|
||||
}
|
||||
|
||||
fn visit_for_calls_and_structure(&mut self, node: Node<'t>) {
|
||||
/// `parent` is `node`'s parent, handed down by the walk (None at a body's
|
||||
/// root): `Node::parent()` walks down from the tree's root on every call.
|
||||
fn visit_for_calls_and_structure(&mut self, node: Node<'t>, parent: Option<Node<'t>>) {
|
||||
stack_guard!();
|
||||
let kind = node.kind();
|
||||
self.maybe_capture_fn_refs(node);
|
||||
@@ -1201,6 +1208,7 @@ impl<'t> Walker<'t> {
|
||||
} else if kind == "struct_expression" {
|
||||
self.extract_instantiation(node);
|
||||
}
|
||||
self.extract_static_member_ref(node, parent);
|
||||
|
||||
// Nested NAMED fns become their own nodes (a nested fn inside an impl
|
||||
// method walks up to the impl and indexes as a METHOD).
|
||||
@@ -1232,11 +1240,69 @@ impl<'t> Walker<'t> {
|
||||
|
||||
for i in 0..node.named_child_count() {
|
||||
if let Some(c) = node.named_child(i) {
|
||||
self.visit_for_calls_and_structure(c);
|
||||
self.visit_for_calls_and_structure(c, Some(node));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// extractStaticMemberRef — the rust branch (#2328): an enum variant
|
||||
/// written as a path (`Mode::A`, `mode::Mode::B`, a `Mode::C(x)` /
|
||||
/// `Mode::D { .. }` pattern, `Self::A` in an impl) references the receiver
|
||||
/// — the segment before the member, at the place it is written; the
|
||||
/// resolver keeps it only on an enum that declares the member. A lowercase
|
||||
/// receiver (module) or member (fn), a call's callee or a struct literal's
|
||||
/// name (both already linked to their member), a path's prefix and a `use`
|
||||
/// tree emit nothing. Mirrored byte-for-byte — change both.
|
||||
fn extract_static_member_ref(&mut self, node: Node<'t>, parent: Option<Node<'t>>) {
|
||||
let kind = node.kind();
|
||||
if (kind != "scoped_identifier" && kind != "scoped_type_identifier") || self.stack.is_empty() {
|
||||
return;
|
||||
}
|
||||
// Looked up only at a body's root (a `const X: M = M::A;` value).
|
||||
let Some(parent) = parent.or_else(|| node.parent()) else { return };
|
||||
let member_path = if kind == "scoped_identifier" {
|
||||
!matches!(
|
||||
parent.kind(),
|
||||
"scoped_identifier"
|
||||
| "scoped_type_identifier"
|
||||
| "use_declaration"
|
||||
| "use_list"
|
||||
| "scoped_use_list"
|
||||
| "use_as_clause"
|
||||
| "use_wildcard"
|
||||
)
|
||||
} else {
|
||||
parent.kind() == "struct_pattern"
|
||||
};
|
||||
if !member_path {
|
||||
return;
|
||||
}
|
||||
if parent.kind() == "call_expression"
|
||||
&& parent.child_by_field_name("function").map(|f| f.start_byte()) == Some(node.start_byte())
|
||||
{
|
||||
return;
|
||||
}
|
||||
let Some(member) = node.child_by_field_name("name") else { return };
|
||||
let mut recv = node.child_by_field_name("path");
|
||||
if let Some(r) = recv {
|
||||
if r.kind() == "scoped_identifier" {
|
||||
recv = r.child_by_field_name("name");
|
||||
}
|
||||
}
|
||||
let Some(recv) = recv else { return };
|
||||
if recv.kind() != "identifier" || !self.text(member).starts_with(|c: char| c.is_ascii_uppercase()) {
|
||||
return;
|
||||
}
|
||||
let mut text = self.text(recv).to_string();
|
||||
if text == "Self" {
|
||||
text = self.receiver_type_of(node).unwrap_or_default();
|
||||
}
|
||||
if capitalized_re().is_match(&text) {
|
||||
let owner = self.top_row();
|
||||
self.push_ref_at(owner, &text, edge_kind_index("references").unwrap(), recv);
|
||||
}
|
||||
}
|
||||
|
||||
// --- fn refs (RUST_SPEC) ----------------------------------------------------
|
||||
|
||||
/// maybeCaptureFnRefs with RUST_SPEC's dispatch: arguments→args,
|
||||
|
||||
Reference in New Issue
Block a user