Skip to content

feat(rust): add full parser support and resolution for Rust codebases#526

Open
gopinathnelluri wants to merge 2 commits into
tirth8205:mainfrom
retry-labs:feat/rust-compatibility
Open

feat(rust): add full parser support and resolution for Rust codebases#526
gopinathnelluri wants to merge 2 commits into
tirth8205:mainfrom
retry-labs:feat/rust-compatibility

Conversation

@gopinathnelluri
Copy link
Copy Markdown

This PR adds full, comprehensive support for Rust codebases to code-review-graph. Previously, Rust call graphs were sparse and disconnected due to missing type associations, unparsed traits, and lack of path/scoped call resolution.
This implementation enables correct structural parsing of Rust features—including traits, inherent/trait impl blocks, relative/crate imports, and scoped calls—ensuring high-quality execution flow and impact radius analysis for Rust projects.


Key Changes

1. AST Parsing & Extraction (code_review_graph/parser.py)

  • Trait Nodes: Registered trait_item under Rust class types, allowing trait definitions to be parsed as Class nodes.
  • impl Block Scope Association:
    • Inherent/trait impl blocks are now mapped to their target struct/enum scope (stripping generics) rather than the trait name.
    • Methods defined within impl blocks are qualified under the implementing struct (e.g., InMemoryRepo.save), matching how they are concrete-ly invoked.
  • Inheritance Mapping (INHERITS Edges): Added Rust support to _get_bases to emit an INHERITS edge between the implementing struct and the trait in impl Trait for Struct blocks.
  • Nested/Aliased Use Declarations: Implemented recursive flattening (_parse_rust_use_node) for complex Rust use statements (e.g. use a::{b, c::d}) and alias patterns (as).

2. Scoped Path & Module Resolvers (code_review_graph/parser.py)

  • Module-to-File Resolution: Added Rust-specific path resolution to _do_resolve_module. It scans for Cargo.toml to identify the crate root, handles crate:: and self:: segments, resolves nested super:: references, and maps them to absolute .rs or mod.rs files.
  • Scoped Method Call Resolution: Added _resolve_rust_scoped_call and integrated it in _resolve_call_target. It translates double-colon scoped calls (e.g. InMemoryRepo::new()) to dot-separated qualified method targets (e.g., InMemoryRepo.new), bridging the AST call-site to the graph method node.

Verification & Testing

Automated Tests

Extended the test suite in tests/test_multilang.py with:

  • Assertions for trait detection and INHERITS conformance tracking.
  • Assertions for scoped call resolution (InMemoryRepo::new -> InMemoryRepo.new).
  • Direct module path resolution tests under a mock Cargo structure verifying crate:: and super:: resolution.
    All 307 multilang tests pass successfully:
$ uv run pytest tests/test_multilang.py
collected 307 items
======================== 307 passed, 1 warning in 6.02s ========================

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant