TL;DR
Generative compilation introduces a way to provide real-time, compiler-grade feedback to Large Language Models (LLMs) while they are actively generating code. By using a lightweight syntactic transformation called a "sealor" to turn partial code snippets into complete programs, this approach catches complex semantic errors (like Rust borrow-checking violations) early, prevents error cascades, and works seamlessly with black-box model APIs.
What Problem Does This Paper Address?
While LLMs are increasingly capable of writing code, their outputs frequently suffer from syntax and semantic errors. This is especially true for languages with strict static semantics like Rust, where safety guarantees are enforced by a rigorous compile-time borrow checker.
Currently, developers use two main strategies to fix this, but both have major drawbacks:
- Post-Generation Feedback: The system waits for the LLM to finish generating the entire file, runs the compiler, and feeds the errors back to the model. By this time, an early mistake has often snowballed into a massive, confusing cascade of downstream errors, making it difficult for the model to find the root cause.
- Constrained Decoding: The system filters out invalid tokens during the generation process itself. However, this requires "white-box" access to the model's internal probability distributions (ruling out commercial APIs like OpenAI or Anthropic) and requires a massive, complex reimplementation of the language's type system to check partial code.
The authors bridge this gap with Generative Compilation, providing the rich, helpful feedback of a compiler on-the-fly during generation without needing white-box model access or a custom type-checker.
Methods
The core innovation of generative compilation is the sealor: a lightweight, mostly syntax-guided transformation that takes an incomplete, partial program and "seals" it into a complete program that a standard compiler (like rustc) can understand.
Instead of evaluating the types itself, the sealor inserts well-typed placeholders into the unfinished parts of the code:
holediv()(implemented aspanic!()in Rust): Used for control-flow branches that don't return values. Because it diverges, the compiler skips downstream borrow-checking obligations that might cause false positives.holeval(): A generic helper function that acts as a placeholder for missing expressions, inferring whatever type the surrounding context expects.
The system runs concurrently alongside the LLM generation process using a "latest-wins" strategy. As the LLM streams tokens, the generative compiler continually grabs the latest prefix, seals it, and runs it through the compiler. If the compiler rejects it, generation is immediately aborted, and the model is restarted with a targeted error message mapped precisely back to the original partial code.
The authors formally proved the soundness and completeness of this approach on a core Rust calculus called Featherweight Rust (FR) using the Lean theorem prover, before scaling the implementation up to real-world Rust.
Datasets
The approach was evaluated on two repository-level Rust coding tasks:
- Translation: A benchmark consisting of 20 complex library translation tasks adapted from CRUST-Bench, where models must transpile C code into safe Rust.
- UpdatedAPI: A newly introduced benchmark of 30 single-file projects. It tests whether models can adapt to recent third-party Rust library updates that occurred after the models' training knowledge cutoff dates, forcing them to rely on compiler feedback to fix deprecated API usage.
Key Insights and Ideas
- Completeness over Soundness: For on-the-fly feedback, it is far more critical to avoid false rejections (global completeness) than it is to catch every single error immediately (global soundness). If the sealor accidentally lets a bad prefix pass, a later check or the final compilation will still catch it. But if it falsely rejects a valid prefix, it permanently derails the model.
- Stopping the "Error Snowball": Compiling a completed file with an early error often results in hundreds of lines of confusing, unrelated compiler diagnostics. Interrupting the model at the exact moment an unrecoverable error occurs keeps the diagnostic reports small, clean, and highly focused on the root cause.
- Leveraging Existing Infrastructure: Instead of spending years writing a custom partial-program type checker for Rust, the authors showed that a simple syntactic wrapper allows developers to reuse 100% of the existing production compiler (
rustc).
Key Results
The authors evaluated generative compilation (GC) against raw LLM generation and standard post-generation feedback (PC) across seven frontier and open-weight models (including Claude 4.8 Opus, GPT-5.3 Codex, and Gemini 3.5 Flash).
- Error Reduction: GC reduced the average compiler error rate across all configurations to 13.1%, compared to 20.7% for post-generation feedback and 65.9% for the raw LLM. For Claude Opus on the UpdatedAPI task, GC eliminated compiler errors entirely (0.0%).
- Better Correctness: GC achieved the highest functional correctness in 11 out of 14 model-task configurations.
- Faster Generation: Even though GC runs the compiler multiple times during generation, it actually decreased overall runtime for models that struggle with coding (like Qwen 9B, where runtime was cut in half). This is because it prevents the model from wasting time completing long files that are fundamentally broken from line 3.
- Focused Diagnostics: 65% of GC error reports contained only 1 or 2 distinct diagnostics, compared to just 40% for post-generation feedback.
Limitations and Future Work
- Hand-Written Sealors: The rules for sealing partial Rust constructs are currently hand-written. A promising future direction is automating the synthesis of sealor rules directly from a language's formal specification.
- Raw Compiler Messages: The framework passes raw
rustcdiagnostics back to the LLM. The authors note that translating or augmenting these error messages specifically for consumption by AI agents (rather than humans) could further boost repair accuracy. - Formalizing Error Correctness: While the authors proved that the verdict (compile vs. fail) is correct, they have not yet formally proven that the text of the error message generated from a sealed program always accurately reflects the true defect in the partial program.
Summary updated