一句话:提出模块化、特征映射与类型兼容性检查的 LLM 代码翻译方法,可将万行级 Go 项目可靠翻译为 Rust。
现有基于大语言模型(LLM)的代码翻译在代码规模超过约 100 行时成功率显著下降。本文提出一种模块化翻译方法:将项目切分为可独立翻译并做 I/O 等价语义校验的小代码片段。针对朴素方法中 LLM 难以处理源语言与目标语言无直接对应特性、且易陷入修复循环的问题,作者引入两个关键机制:(1)feature mapping,将预定义翻译规则与 LLM 翻译结合,引导模型处理细微语言差异并生成语义准确的代码;(2)type-compatibility,在函数签名级别进行局部检查以尽早发现错误,从而缩小潜在修复范围。作者将该方法应用于真实 Go 代码库到 Rust 的翻译,在最大 9700 行、780 个函数的项目上持续生成可靠的 Rust 翻译,平均 73% 的函数通过 I/O 等价验证,显著高于已有工作。
Large language models (LLMs) show promise in code translation due to their ability to generate idiomatic code. However, a significant limitation when using LLMs for code translation is scalability: existing works have shown a drop in translation success rates for code exceeding around 100 lines. We overcome this limitation by developing a modular approach to translation, where we partition the code into small code fragments which can be translated independently and semantically validated (that is, by checking I/O equivalence). When this approach is applied naively, we discover that LLMs are unreliable when translating features of the source language that do not have a direct mapping to the target language, and that the LLM often gets stuck in repair loops when attempting to fix errors. To address these issues, we introduce two key concepts: (1) feature mapping , which integrates predefined translation rules with LLM-based translation to guide the LLM in navigating subtle language differences and producing semantically accurate code; and (2) type-compatibility , which facilitates localized checks at the function signature level to detect errors early, thereby narrowing the scope of potential repairs. We apply our approach to translating real-world Go codebases to Rust, demonstrating that we can consistently generate reliable Rust translations for projects up to 9,700 lines of code and 780 functions, with an average of 73% of functions successfully validated for I/O equivalence, considerably higher than any existing work.
@article{paperbot3112,
title = {Scalable, Validated Code Translation of Entire Projects using Large Language Models},
author = {Hanliang Zhang and Cristina David and Meng Wang and Brandon Paulsen and Daniel Kroening},
journal = {Proceedings of the ACM on Programming Languages},
volume = {9},
number = {PLDI},
year = {2025},
doi = {10.1145/3729315}
}