LLM-Based Rust Code Generation with On-the-Fly Compiler Feedback

Hristo Venev, Niels Mündler, Jingxuan He, Dawn Song, Martin VechevDL4C @ ICML 2026

TL;DR

This paper introduces a method to give Large Language Models (LLMs) immediate, "on-the-fly" feedback from the Rust compiler while they are still generating code. By using a lightweight "extractor" to turn partial snippets into valid programs, the authors achieve a 50% reduction in runtime and significantly improve the functional correctness of generated Rust code without needing internal access to the models.

What Problem Does This Paper Address?

Generating Rust code is a notorious "final boss" for LLMs. Unlike Python, Rust’s strict type system—specifically its ownership and lifetime rules—requires a level of semantic precision that probabilistic models often miss.

Currently, there are two ways to fix this:

  1. Post-hoc Feedback: Let the model finish the whole program, run the compiler, and feed the errors back. This is slow and often results in a "wall of errors" that confuses the model.
  2. Constrained Decoding: Check every single token as it's generated. This is incredibly complex to implement for a language as sophisticated as Rust and usually requires "white-box" access to the model’s internal probabilities.

The authors identify a "missing middle": a way to get the benefits of the compiler's deep wisdom early in the generation process, without the massive engineering overhead of rewriting a compiler from scratch.

Methods

The core innovation is the Extractor. Since the standard Rust compiler (rustc) can only process complete programs, the authors designed a lightweight transformation tool that:

  • Closes partial structures: If an LLM has only generated half an if statement or a loop, the extractor adds the necessary braces and keywords to make it syntactically "complete."
  • Inserts Placeholders: It uses generic placeholders (like panic!() or custom __X() functions) to fill in missing logic.
  • Preserves Errors: Crucially, the extractor is "conservative." It ensures that if the compiler finds an error in the transformed code, that error was actually present in the LLM's partial output, not introduced by the transformation itself.

The system follows a simple loop: the LLM generates a segment of code; the Extractor "completes" it; the real Rust compiler checks it. If the compiler complains, the generation is aborted immediately, and the LLM is given the error message to try again from that specific point.

Datasets

The researchers evaluated their method on two main fronts:

  1. API-UPGRADE (New Benchmark): The authors created this to simulate a real-world problem: using libraries that changed their API after the LLM's training cutoff. It consists of 30 single-file projects where the model must implement logic using updated third-party crates.
  2. CRUSTBENCH: An existing benchmark for transpiling C code into safe Rust, consisting of 284 completion tasks across 100 medium-sized C libraries.

Key Insights and Ideas

  • The Power of "Panic": One of the cleverest tricks is using panic!() at the end of partial loops. This tells the compiler that the code won't reach a second iteration yet, allowing the borrow checker to validate the first iteration without throwing "uninitialized variable" errors that would be false positives.
  • Incremental Correction: By catching a borrow-check error at token 50, the model doesn't waste time (and tokens) generating another 300 tokens of "hallucinated" logic built on a broken foundation.
  • Black-Box Compatibility: Because this method works at the text level (transforming strings and reading compiler output), it works perfectly with closed-source models like GPT-4 and Claude 3.5, not just open-source ones.

Key Results

The "On-The-Fly" (OTF) method outperformed both vanilla generation and standard post-hoc feedback (FINAL) across the board:

  • Error Reduction: OTF decreased compiler errors by an average of 48.8% in synthesis tasks compared to vanilla generation.
  • Functional Correctness: On synthesis tasks, OTF improved functional correctness by 11 percentage points over the standard feedback loop.
  • Efficiency: Because the system stops the model as soon as it makes a mistake, the median runtime was slashed by 50%.
  • Model Scaling: Interestingly, "stronger" models (like Qwen 397B or Claude 3.5) were better at utilizing this early feedback than smaller models, suggesting this technique scales well with model capability.

Limitations and Future Work

While highly effective, the authors note a few hurdles:

  • Use-Before-Definition: To remain "conservative" (avoiding false errors), the system currently suppresses errors about undefined functions. This means it can't yet catch when a model "hallucinates" a function name that doesn't exist.
  • Type Annotation Sensitivity: If a model leaves out a type annotation that the borrow checker needs, the system might miss a memory safety error until later in the process.
  • Rollback Depth: Currently, when an error is found, the model restarts. Future work could explore "constrained decoding" where the model only has to backtrack a few tokens rather than restarting the entire segment.

Overall, this paper provides a practical, high-performance bridge between the "move fast" nature of LLMs and the "stay safe" requirements of the Rust programming language.

Summary updated

BibTeX
@inproceedings{venev2026llm,
  author = {Hristo Venev and Niels Mündler and Jingxuan He and Dawn Song and Martin Vechev},
  title = {{LLM-Based Rust Code Generation with On-the-Fly Compiler Feedback}},
  booktitle = {DL4C @ ICML},
  year = {2026},
  url = {https://openreview.net/forum?id=CeOC6w3gPj},
}
Figure