arXiv:2607.13921cs.PLcs.AI2026-07

让编译器在代码生成时实时反馈,提升AI写Rust代码的正确率。

Generative Compilation: On-the-Fly Compiler Feedback as AI Generates Code

  • 用轻量转换将部分代码转为完整程序,供编译器诊断。
  • 比传统事后检查少40%无法编译的输出,功能正确率显著提升。
  • 适合需要高可靠性代码的AI编程场景,如系统级开发。

具有丰富静态语义的语言(如Rust)能为AI生成代码提供更强保障,但其严格性使生成更困难。现成编译器仅能在生成后提供反馈,无法指导生成过程中的中间步骤。约束解码虽可提前拦截无效词元,但需白盒模型访问且实现成本高。本文提出生成式编译:首次在生成过程中获取编译器对部分程序的反馈。核心是“封存器”——一种轻量、主要基于语法的转换,将部分程序转化为标准编译器可诊断的完整程序。该设计确保可完成的部分程序不会被误拒,同时保留足够上下文以早期发现真正死胡同。我们在一个类Rust演算核心上构建了封存器,并在Lean中形式化证明其性质。进一步扩展至真实Rust的部分程序检查器。在前沿黑盒与开源权重模型上评估,生成式编译显著减少非编译输出,提升功能正确性,通过早期检测广泛错误,避免错误级联,支持精准诊断。这标志着编译器正从生成后的独立检查,转变为生成过程中的主动参与者。

原文摘要 · Abstract (English)

Languages with rich static semantics, such as Rust, provide stronger guarantees for AI-generated code, but their strictness makes generation more difficult. Off-the-shelf compilers can provide useful feedback post-generation, but does not guide intermediate generation steps, such as those during autoregressive LLM decoding. Constrained decoding intervenes earlier by rejecting invalid tokens during sampling, but requires white-box model access and costly reimplementation for semantic constraints. We introduce generative compilation, the first approach to obtaining compiler feedback on partial programs during generation. The core technical device is a sealor: a lightweight, mostly syntax-guided transformation that converts partial programs into complete ones that standard compilers can diagnose. It is designed such that possible-to-complete partial programs are never rejected, while preserving enough code context to catch genuine dead ends early. We construct such a sealor on a core Rust-like calculus and prove that it satisfies these properties, all mechanized in Lean. We extend it to the first partial-program checker for real Rust. We evaluate our method on challenging repository-level Rust coding tasks, across both frontier black-box and open-weight models. We show that generative compilation reduces non-compiling outputs and improves functional correctness, relative to standard post-generation feedback. It does so by detecting a broad range of errors close to their source and early during generation, thereby reducing errors cascades and enabling focused diagnostics. More broadly, generative compilation is a step toward making compilers a first-class citizen of AI-assisted programming active during generation, rather than a separate post-generation check.

代码生成编译器RustAI编程

Thank you to arXiv for use of its open access interoperability. PaperDance 不是 arXiv 官方产品;中文卡片由大模型生成,请以原文为准。