Rust-doctor: Enhanced Feature for Rust Ownership and Lifetime Repair with Balanced Training Data Generation (2025)

Abstract: As a relatively new programming language, Rust has gained significant popularity in recent years due to its safety features during compilation. However, Rust developers often face challenges stemming from its strict compilation checks due to the steep learning curve of safety rules. To make matters worse, the lack of training data and the unique semantics of Rust lead to poor performance in learning-based automated program repair techniques. To address these challenges, we propose a novel error injection approach to generate a balanced training dataset and leverage the Mid-level Intermediate Representation (MIR) as enhanced features for Rust’s unique compilation error repair. Using these innovations, we fine-tuned a new code model, LLaRRA: L arge L anguage a nd R ust R epair A ssistant. Experimental results demonstrate that LLaRRA significantly outperforms state-of-the-art models in terms of Pass@K and Acc@K.

## introduction rust is a newborn system programming language with high performance and memory safety @xcite . to achieve these goals, rust provides strict static rule checkers in compile-time to prevent plausible illegal memory access. ownership and lifetime errors (oles) may occur when the code violates the corresponding rules. benefits from both performance and safety advantages, rust has been used in many safety-critical fundamental software @xcite @xcite @xcite . however, rust's checkers are a double-edge sword. rigorous compiletime checking indeed serves fancy safety features while actually sacrifices the programming capability. therefore, rust developers struggle to fight with the official rust compiler (rustc), and keep the loop of fixing oles and wait to check whether * these authors contributed equally to this work. compilation success. to make things worse, rustc fails to provide enough information for developers @xcite . thus, to help the rust developers, there is an emergency to automatically fix the rust oles. recent years, many automatically program repair (apr) techniques are proposed to fix the program fault, including learning-based and unlearning-based approaches @xcite @xcite @xcite @xcite @xcite . specifically, yang et al. @xcite proposed a rule-based approach, rust-lancet, to fix ownership errors in rust. however, this method is limited to ownership errors and is difficult to extend to lifetime errors. to maximize the effectiveness in fixing oles, learning-based techniques are preferable-assuming the availability of highquality training data. unfortunately, due to rust as a relatively fresh programming language compared to the mainstream languages (c/c++ and java) and few developers tend to upload uncompilable rust program, lacking of enough compilation errors as training data, even the most powerful large language models (llms) cannot repair the ole ideally. hence, the challenge 1 to enhance learning-based techniques is how to obtain pairs of high-quality uncompilable and compilable programs as training data. existing work @xcite usually collect data from students' coding assignments for mainstream programming languages, while rust is not broadly introduced to school yet. the challenge 2 is how to learn the underlying rust program representation where ole checkers are performed. these checkers analyze program in intermediate representation (ir) instead of source code. thus, using pure rust source code while lack of this ir feature can limit llms performance of ole repair. in this paper, we propose rust-doctor to gen-erate ole from real-world rust projects to improve the ole fixing performance of llms with enhanced features. to address challenge 1 , we propose an ole injection framework to generate arbitrarily scale error dataset. in our framework, we use existing popular third-party libraries (crates) as a corpus and leverage llms to mutate compilable rust functions using error-specific prompts. to keep the diversity of dataset, these functions are selected based on code similarity. to tackle challenge 2 , we introduce rustc's mir (mir, 2024) as enhanced features for ole repair. in rust, mir is designed to support more precise static checking, and the ole is raised from mir after the corresponding checking algorithm performed. given the importance of mir, we utilize an auto-encoder technique to capture its representation automatically and leverage the feature to improve repair capability of llms. overall, rust-doctor takes multiple crates and uses k-means @xcite to obtain function categories. after that, rust-doctor selects a rust function from each category evenly, consequently automatically mutates it by llms to trigger ole and save the mutated function if the error is we want. subsequently, a control flow graph is constructed based on mir and its structure information is captured by a graph encoder. finally, we fine-tune llms with rust source code and features produced to fix rust's ole. the fine-tuned models not only enhanced by large scale high quality training data, but also learn the program representation where the ole checkers are performed to fix ole better. the main contributions are as follows: we are the first to propose an automatic rust ole injection framework based on real-world rust projects. furthermore, we construct an open source high-quality rust ole dataset and benchmark for the ole repair community. ## proposed method an overview of the proposed approach is illustrated in figure 2 . the approach comprises three key components: a data generation module, a mir encoding module, and a tuning module. ## experiments in this section, we evaluate rust-doctor by finetuning a new model based on the llama3.1-8binstr from meta @xcite . our goal is to address the following research questions: ## conclusion in this study, we propose rust-doctor, a novel data generation and enhanced feature approach designed to automatically generate rust's ole for fine-tuning large language models (llms). our approach aims to deliver high-quality ole data to benefit the rust program automatic repair community. within the dataset, we successfully collected a total of 359k unique rust ole fixing samples, comprising 136k in mir, 183k in compilation error messages, and 40k in erroneous rust functions. additionally, using the generated training dataset, we fine-tune and release a specialized llm, llarra. experimental results demonstrate that llarra outperforms all popular baseline llms, highlighting its potential as an effective ole repair tool for the rust programming language community.