TL;DR
Translating software projects between programming languages using Large Language Models (LLMs) usually falls apart when the code relies heavily on external libraries. This paper introduces CrossCrate, a framework that pairs a Retrieval-Augmented Generation (RAG) pipeline for precise API mapping with a schema-backed validation engine to safely translate Go projects into Rust. By synthesizing cross-language adapters using exclusively public library APIs, CrossCrate achieves a near-perfect compilation and functional equivalence rate on real-world repositories.
What Problem Does This Paper Address?
Migrating legacy systems code to memory-safe languages like Rust is highly desirable, but doing it manually is tedious and error-prone. While LLMs are excellent at translating standalone code snippets, they struggle with entire repositories that depend on external libraries.
The authors identify two primary roadblocks:
- API Hallucination and Import Failures: LLMs frequently invent non-existent target APIs or fail to generate the complex, non-local import paths required by Rust (such as bringing specific traits into scope so their methods can be called).
- Validation Roadblocks: Existing translation tools validate correctness by checking if the internal structures of data types match across languages. However, external libraries frequently use opaque types (types with private fields) or enforce strict internal invariants. This makes direct field-by-field structural matching or direct cross-language serialization impossible.
Methods
The authors present CrossCrate, a modular, function-level translation and validation pipeline built on top of an existing tool called Oxidizer. It tackles the problem in two distinct stages:
1. RAG-Guided Library Mapping and Import Resolution
To prevent the LLM from hallucinating code, CrossCrate injects precise Rust API suggestions directly into the translation prompt. The framework precomputes a crate-specific knowledge base by parsing Rust documentation, carefully resolving complex module hierarchies and pub use re-exports.
When a Go function calls an external library API, CrossCrate queries this knowledge base using a natural language description of the Go API. A two-stage retrieval pipeline (Dense Passage Retrieval followed by Cross-Encoder and LLM-based reranking) extracts the top Rust API candidates along with their exact, call-enabling import paths.
2. Schema-Backed Cross-Language Validation
To verify the translation without violating library encapsulation, CrossCrate bridges Go and Rust using a shared, language-neutral intermediate data format specified as a Protobuf schema.
- Handling Opaque Types: Complex or private library fields are conservatively mapped to simple byte arrays.
- Adapter Synthesis: The framework prompts an LLM to generate high-level language-local adapters (conversions between the native types and the Protobuf carrier) using only the libraries' public constructors and serialization routines.
- Two-Stage Testing: These adapters are first validated via round-trip checks (ensuring no data is lost during conversion). Once validated, they are used to perform differential I/O testing, running the original Go code and the new Rust translation on identical unit-test inputs to verify semantic equivalence.
Datasets
The framework was evaluated on a curated benchmark suite consisting of six real-world, actively maintained Go repositories from GitHub. These projects were selected because they feature non-trivial external dependencies across diverse cryptographic and utility domains:
pbkdf2: A standard cryptographic key-derivation library.easy-scrypt&simple-scrypt: Memory-hard password hashing libraries.argon2-hashing: Password hashing utilizing structured parameter types and strict API constraints.duration: A domain-specific time and duration parsing library with intricate control flow.age: A modern file encryption tool (complex dependency functions were isolated for testing).
Key Insights and Ideas
- Imports are Half the Battle in Rust: Knowing the right target API name isn't enough. Because Rust relies heavily on trait-provided methods and module re-exports, missing an import path means compilation failure. Providing explicit import paths in the prompt is a strict prerequisite for successful automated translation.
- Behavioral Over Structural Matching: Instead of trying to force open a "black box" opaque type to match its fields across languages, translators should interact with it purely through its public serialization/deserialization interfaces.
- Compositional Scaling: To handle deeply nested or composite types, the framework generates carrier schemas and adapters modularly, systematically reusing previously validated component adapters.
Key Results
- High Success Rates: CrossCrate achieved an average success rate of 95.83% for both compilation and differential I/O equivalence across all benchmarks.
- Perfect Translations: The tool achieved a flawless 100% success rate on five out of the six real-world benchmarks.
- The Necessity of RAG: In ablation studies where RAG or import resolution was turned off (
No RAG/No Imports), the compilation and equivalence rates for functions with external dependencies dropped to 0%. - Baseline Comparison: The state-of-the-art baseline tool, Oxidizer, which relies on hard-coded target crates, achieved 0% success on functions utilizing external APIs. CrossCrate effectively doubled the overall translation success rate on average.
Limitations and Future Work
- Injective Mapping Assumption: The current pipeline assumes a strict one-to-one (injective) mapping where each Go package corresponds to exactly one Rust crate. Real-world projects occasionally require splitting a package's functionality across multiple crates.
- Feature Support Gaps: CrossCrate inherits the language feature limitations of its underlying tool, Oxidizer. For example, complex system interactions like file I/O (found in parts of the
agerepository) are currently unsupported and had to be stripped out prior to evaluation. Extending the framework to handle stateful or I/O-heavy library boundaries is a clear direction for future research.
Summary updated