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

Wenzhang Yang, Xiaoning Ren, Cuifeng Gao, Yinxing XueEMNLP 2025

TL;DR

This paper introduces Rust-doctor, a novel approach to automatically repair Rust's ownership and lifetime errors (OLEs). By generating a balanced dataset of erroneous Rust code and leveraging the Mid-level Intermediate Representation (MIR) as enhanced features, their fine-tuned LLM, LLaRRA, significantly outperforms existing state-of-the-art models in OLE repair.

What Problem Does This Paper Address?

Rust is a powerful systems programming language known for its memory safety, achieved through strict compile-time checks like ownership and lifetime rules. However, these very rules, while beneficial, present a steep learning curve for developers, often leading to frustrating compilation errors. Existing automated program repair (APR) techniques struggle with Rust's OLEs due to a lack of high-quality training data and the difficulty of capturing Rust's unique semantics. The paper aims to bridge this gap by creating a robust dataset and a specialized LLM capable of understanding and fixing these specific Rust errors.

Methods

Rust-doctor employs a multi-pronged approach:

  • OLE Injection Framework: To generate a large and balanced training dataset, the authors developed a framework that mutates existing, compilable Rust functions from popular libraries. This mutation process is guided by LLMs and error-specific prompts, aiming to trigger OLEs.
  • Balanced Dataset Generation: To ensure diversity and prevent bias, they use k-means clustering on MIR code similarity to categorize functions and then select functions evenly from each category for mutation.
  • MIR as Enhanced Features: Recognizing that Rust's OLE checkers operate on the Mid-level Intermediate Representation (MIR), the paper extracts semantic information from MIR. This involves constructing control flow graphs, encoding basic blocks using CodeBERT, and then pooling these embeddings to create a graph-level representation. This MIR representation is then used as an auxiliary input to the LLM.
  • LLaRRA Model: A fine-tuned LLM, LLaRRA (Large Language and Rust Repair Assistant), is developed. It's based on the Llama3.1-8b-instruct model and incorporates the generated dataset and MIR features. LoRA (Low-Rank Adaptation) is used for efficient fine-tuning.

Datasets

The paper introduces a newly generated Rust OLE dataset. This dataset comprises 9,467 unique Rust OLE fixing samples, including erroneous Rust functions, their corresponding MIR, and compilation error messages. The dataset is split into training (80%) and testing (20%) sets. The authors claim this dataset achieves 71.58% edge coverage of OLE checkers, indicating comprehensive coverage of core OLE checking algorithms.

Key Insights and Ideas

  • MIR is Crucial for OLE Repair: The paper strongly emphasizes that understanding Rust's OLEs requires going beyond source code and leveraging the MIR, as this is the representation the compiler's borrow checker directly analyzes.
  • Data Generation is Key: The scarcity of real-world OLE examples is a major bottleneck. Rust-doctor's error injection framework demonstrates a viable path to creating large-scale, balanced datasets for specialized repair tasks.
  • LLMs Benefit from Structured Features: Simply fine-tuning LLMs on source code and error messages isn't enough. Incorporating structured semantic features like MIR representations significantly enhances their ability to understand and fix complex code errors.
  • Balanced Datasets Matter: The ablation study shows that both balanced data generation and the MIR features contribute significantly to LLaRRA's performance, highlighting the importance of a well-curated training set.

Key Results

LLaRRA, the proposed model, significantly outperforms other state-of-the-art LLMs on the Rust OLE repair task.

  • Pass@K: LLaRRA achieved an average Pass@K increase of 12.61% compared to other models.
  • Acc@K: LLaRRA achieved an average Acc@K increase of 18.30%.
  • Practicality of Patches: The difference between Pass@5 and Acc@5 for LLaRRA was only 0.46%, indicating that its generated patches are not only compilable but also likely correct and not simply line deletions.
  • Unique OLE Fixes: LLaRRA fixes the most unique OLEs among the evaluated models, demonstrating broad coverage.

Limitations and Future Work

  • Regular Syntax Errors: The current work focuses exclusively on OLEs and does not address regular syntax errors, which have been effectively handled by prior work.
  • Generalization: The dataset is generated from the top 30 most downloaded crates. While the methodology is expected to generalize, the current evaluation might not cover the full spectrum of Rust coding scenarios.
  • Data Synthesis Uncertainty: The dataset is synthesized using LLMs, and while efforts were made to ensure accuracy, there's an inherent uncertainty about whether these injected errors perfectly mirror real-world Rust OLEs. The use of compile_error! macros could also introduce noise.
  • Future Work: The authors suggest exploring methods for models to learn from iterative queries to improve performance as K increases. They also highlight the potential for output fusion across distinct models to further enhance OLE repair performance.

Summary updated

BibTeX
@inproceedings{yang2025rust,
  author = {Wenzhang Yang and Xiaoning Ren and Cuifeng Gao and Yinxing Xue},
  title = {{Rust-doctor: Enhanced Feature for Rust Ownership and Lifetime Repair with Balanced Training Data Generation}},
  booktitle = {Conference on Empirical Methods in Natural Language Processing},
  year = {2025},
}
Figure