depyler-oracle 4.1.1

ML-powered compile error classification and auto-fixing using aprender models
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
//! Automatic code transformation based on oracle classifications.
//!
//! Uses the 97% accurate oracle to classify errors and apply fixes.

use crate::{ErrorCategory, Oracle, OracleError};
use regex::Regex;
use std::collections::HashMap;

/// Result of an automatic fix attempt.
#[derive(Debug, Clone)]
pub struct FixResult {
    /// Whether a fix was applied
    pub fixed: bool,
    /// The modified source code (if fixed)
    pub source: String,
    /// Description of what was fixed
    pub description: String,
    /// Confidence of the fix
    pub confidence: f32,
}

/// Automatic code fixer using oracle classifications.
pub struct AutoFixer {
    oracle: Oracle,
    /// Transformation rules per category
    rules: HashMap<ErrorCategory, Vec<TransformRule>>,
}

/// A transformation rule for fixing code.
#[derive(Clone)]
pub struct TransformRule {
    /// Name of the rule
    pub name: String,
    /// Pattern to match in error message
    pub error_pattern: Regex,
    /// Function to apply the fix
    pub transform: fn(&mut FixContext) -> bool,
}

/// Context for applying a fix.
pub struct FixContext<'a> {
    /// The source code
    pub source: &'a mut String,
    /// The error message
    pub error_msg: &'a str,
    /// Line number of the error
    pub line: usize,
    /// Variable name involved (if any)
    pub var_name: Option<String>,
    /// Type information (if any)
    pub type_info: Option<String>,
}

impl AutoFixer {
    /// Create a new AutoFixer with trained oracle.
    pub fn new() -> Result<Self, OracleError> {
        #[cfg(feature = "training")]
        let oracle = Oracle::load_or_train()?;
        #[cfg(not(feature = "training"))]
        let oracle = {
            let path = Oracle::default_model_path();
            if path.exists() {
                Oracle::load(&path)?
            } else {
                Oracle::new()
            }
        };
        let rules = Self::default_rules();
        Ok(Self { oracle, rules })
    }

    /// Create with existing oracle.
    pub fn with_oracle(oracle: Oracle) -> Self {
        let rules = Self::default_rules();
        Self { oracle, rules }
    }

    /// Default transformation rules for each category.
    fn default_rules() -> HashMap<ErrorCategory, Vec<TransformRule>> {
        let mut rules = HashMap::new();

        // BorrowChecker rules
        rules.insert(
            ErrorCategory::BorrowChecker,
            vec![
                TransformRule {
                    name: "pre_compute_is_some".to_string(),
                    error_pattern: Regex::new(r"borrow of moved value.*\.is_some\(\)")
                        .expect("static regex"),
                    transform: fix_pre_compute_is_some,
                },
                TransformRule {
                    name: "pre_compute_is_none".to_string(),
                    error_pattern: Regex::new(r"borrow of moved value.*\.is_none\(\)")
                        .expect("static regex"),
                    transform: fix_pre_compute_is_none,
                },
                TransformRule {
                    name: "clone_before_move".to_string(),
                    error_pattern: Regex::new(r"use of moved value").expect("static regex"),
                    transform: fix_clone_before_move,
                },
            ],
        );

        // TypeMismatch rules
        rules.insert(
            ErrorCategory::TypeMismatch,
            vec![
                TransformRule {
                    name: "regex_new_str".to_string(),
                    error_pattern: Regex::new(r"Regex::new.*expected `&str`, found `String`")
                        .expect("static regex"),
                    transform: fix_regex_new_str,
                },
                TransformRule {
                    name: "string_to_str".to_string(),
                    error_pattern: Regex::new(r"expected `&str`, found `String`")
                        .expect("static regex"),
                    transform: fix_string_to_str,
                },
            ],
        );

        // MissingImport rules
        rules.insert(
            ErrorCategory::MissingImport,
            vec![TransformRule {
                name: "add_command_factory".to_string(),
                error_pattern: Regex::new(r"cannot find value `parser`").expect("static regex"),
                transform: fix_add_command_factory,
            }],
        );

        rules
    }

    /// Attempt to fix compilation errors in source code.
    ///
    /// Takes rustc error output and the source file, returns fixed source.
    pub fn fix(&self, source: &str, errors: &str) -> FixResult {
        let mut fixed_source = source.to_string();
        let mut fixes_applied = Vec::new();
        let mut total_confidence = 0.0;
        let mut fix_count = 0;

        // Parse each error from rustc output
        for error_block in Self::parse_errors(errors) {
            // Classify with oracle using full feature extraction
            if let Ok(classification) = self.oracle.classify_message(&error_block.message) {
                // Try to apply fixes for this category
                if let Some(rules) = self.rules.get(&classification.category) {
                    for rule in rules {
                        if rule.error_pattern.is_match(&error_block.message) {
                            let mut ctx = FixContext {
                                source: &mut fixed_source,
                                error_msg: &error_block.message,
                                line: error_block.line,
                                var_name: Self::extract_var_name(&error_block.message),
                                type_info: Self::extract_type_info(&error_block.message),
                            };

                            if (rule.transform)(&mut ctx) {
                                fixes_applied.push(format!(
                                    "Applied '{}' at line {} ({:?}, {:.0}% confidence)",
                                    rule.name,
                                    error_block.line,
                                    classification.category,
                                    classification.confidence * 100.0
                                ));
                                total_confidence += classification.confidence;
                                fix_count += 1;
                                break; // One fix per error
                            }
                        }
                    }
                }
            }
        }

        let avg_confidence = if fix_count > 0 {
            total_confidence / fix_count as f32
        } else {
            0.0
        };

        FixResult {
            fixed: !fixes_applied.is_empty(),
            source: fixed_source,
            description: fixes_applied.join("\n"),
            confidence: avg_confidence,
        }
    }

    /// Parse rustc error output into structured errors.
    fn parse_errors(errors: &str) -> Vec<ParsedError> {
        let mut parsed = Vec::new();
        let error_re = Regex::new(r"error\[E\d+\]:[^\n]+").expect("static regex");
        let line_re = Regex::new(r"--> [^:]+:(\d+):\d+").expect("static regex");

        let mut current_error = String::new();
        let mut current_line = 0;

        for line in errors.lines() {
            if error_re.is_match(line) {
                if !current_error.is_empty() {
                    parsed.push(ParsedError {
                        message: current_error.clone(),
                        line: current_line,
                    });
                }
                current_error = line.to_string();
                current_line = 0;
            } else if let Some(caps) = line_re.captures(line) {
                current_line = caps[1].parse().unwrap_or(0);
                current_error.push('\n');
                current_error.push_str(line);
            } else if !current_error.is_empty() {
                current_error.push('\n');
                current_error.push_str(line);
            }
        }

        if !current_error.is_empty() {
            parsed.push(ParsedError {
                message: current_error,
                line: current_line,
            });
        }

        parsed
    }

    /// Extract variable name from error message.
    fn extract_var_name(msg: &str) -> Option<String> {
        let re = Regex::new(r"`([a-zA-Z_][a-zA-Z0-9_]*(?:\.[a-zA-Z_][a-zA-Z0-9_]*)?)`")
            .expect("static regex");
        re.captures(msg).map(|c| c[1].to_string())
    }

    /// Extract type information from error message.
    fn extract_type_info(msg: &str) -> Option<String> {
        let re = Regex::new(r"type `([^`]+)`").expect("static regex");
        re.captures(msg).map(|c| c[1].to_string())
    }
}

impl Default for AutoFixer {
    fn default() -> Self {
        Self::new().expect("Failed to create AutoFixer")
    }
}

/// Parsed error from rustc output.
struct ParsedError {
    message: String,
    line: usize,
}

// ============================================================================
// Transformation Functions
// ============================================================================

/// Fix: Pre-compute .is_some() before value is moved.
///
/// Pattern: `let x = foo(args.hash); ... args.hash.is_some()`
/// Fix: `let has_hash = args.hash.is_some(); let x = foo(args.hash); ... has_hash`
fn fix_pre_compute_is_some(ctx: &mut FixContext) -> bool {
    let var = match &ctx.var_name {
        Some(v) => v.clone(),
        None => return false,
    };

    // Find the pattern: var.is_some() after var was moved
    let is_some_pattern = format!("{}.is_some()", var);

    if !ctx.source.contains(&is_some_pattern) {
        return false;
    }

    // Generate the fix variable name
    let fix_var = format!("has_{}", var.split('.').next_back().unwrap_or(&var));

    // First, replace all is_some() usages with the fix variable (except in the declaration we'll add)
    let result = ctx.source.replace(&is_some_pattern, &fix_var);

    // Now find where to insert the pre-computation
    let lines: Vec<&str> = result.lines().collect();
    let mut new_lines = Vec::new();
    let mut inserted = false;

    for line in lines.iter() {
        // Look for a line that uses the var (this is where it gets moved)
        // Insert the pre-computation BEFORE this line
        if !inserted && line.contains(&var) {
            // Insert pre-computation before this line
            let indent = line.len() - line.trim_start().len();
            let indent_str: String = " ".repeat(indent);
            new_lines.push(format!(
                "{}let {} = {};",
                indent_str, fix_var, is_some_pattern
            ));
            inserted = true;
        }
        new_lines.push(line.to_string());
    }

    if !inserted {
        return false;
    }

    *ctx.source = new_lines.join("\n");
    true
}

/// Fix: Pre-compute .is_none() before value is moved.
fn fix_pre_compute_is_none(ctx: &mut FixContext) -> bool {
    let var = match &ctx.var_name {
        Some(v) => v.clone(),
        None => return false,
    };

    let is_none_pattern = format!("{}.is_none()", var);

    if !ctx.source.contains(&is_none_pattern) {
        return false;
    }

    let fix_var = format!("is_{}_none", var.split('.').next_back().unwrap_or(&var));
    let lines: Vec<&str> = ctx.source.lines().collect();
    let mut new_lines = Vec::new();
    let mut inserted = false;

    for line in lines.iter() {
        if !inserted
            && line.contains(&var)
            && !line.contains(".is_none()")
            && (line.contains(&format!("({}", var)) || line.contains(&format!(", {}", var)))
        {
            let indent = line.len() - line.trim_start().len();
            let indent_str: String = " ".repeat(indent);
            new_lines.push(format!(
                "{}let {} = {}.is_none();",
                indent_str, fix_var, var
            ));
            inserted = true;
        }
        new_lines.push(line.to_string());
    }

    if !inserted {
        return false;
    }

    let result = new_lines.join("\n").replace(&is_none_pattern, &fix_var);
    *ctx.source = result;
    true
}

/// Fix: Clone value before it's moved.
fn fix_clone_before_move(ctx: &mut FixContext) -> bool {
    let var = match &ctx.var_name {
        Some(v) => v.clone(),
        None => return false,
    };

    // Simple approach: add .clone() to the moved value
    // This is a conservative fix that always works for Clone types
    let pattern = format!("({})", var);
    let replacement = format!("({}.clone())", var);

    if ctx.source.contains(&pattern) {
        *ctx.source = ctx.source.replace(&pattern, &replacement);
        return true;
    }

    false
}

/// Fix: Regex::new expects &str, not String - remove .to_string()
fn fix_regex_new_str(ctx: &mut FixContext) -> bool {
    // Pattern: Regex::new("...".to_string())
    // Fix: Regex::new("...")
    let re = Regex::new(r#"Regex::new\(\s*"([^"]+)"\.to_string\(\)\s*\)"#).expect("static regex");

    if re.is_match(ctx.source) {
        *ctx.source = re
            .replace_all(ctx.source, r#"Regex::new("$1")"#)
            .to_string();
        return true;
    }

    false
}

/// Fix: Expected &str, found String - add &
fn fix_string_to_str(_ctx: &mut FixContext) -> bool {
    // This is context-dependent, so we use a conservative approach
    // Look for function calls where String is passed but &str expected
    false // Conservative: don't auto-fix without more context
}

/// Fix: parser.print_help() -> Args::command().print_help()
fn fix_add_command_factory(ctx: &mut FixContext) -> bool {
    if ctx.source.contains("parser.print_help()") {
        // Need to add CommandFactory import
        if !ctx.source.contains("CommandFactory") {
            // Update the use statement
            *ctx.source = ctx
                .source
                .replace("use clap::Parser;", "use clap::{CommandFactory, Parser};");
        }
        // Replace parser.print_help() with Args::command().print_help()
        *ctx.source = ctx
            .source
            .replace("parser.print_help()", "Args::command().print_help()?");
        return true;
    }
    false
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_errors() {
        let errors = r#"error[E0382]: borrow of moved value: `args.hash`
   --> src/main.rs:10:5
    |
5   |     let x = foo(args.hash);
    |                 --------- value moved here
...
10  |     args.hash.is_some()
    |     ^^^^^^^^^ value borrowed here after move"#;

        let parsed = AutoFixer::parse_errors(errors);
        assert_eq!(parsed.len(), 1);
        assert_eq!(parsed[0].line, 10);
        assert!(parsed[0].message.contains("E0382"));
    }

    #[test]
    fn test_extract_var_name() {
        let msg = "borrow of moved value: `args.hash`";
        let var = AutoFixer::extract_var_name(msg);
        assert_eq!(var, Some("args.hash".to_string()));
    }

    #[test]
    fn test_fix_pre_compute_is_some() {
        let source = r#"
fn main() {
    let info = get_file_info(args.file, args.hash, args.time_format)?;
    let output = format_output(&info, args.hash.is_some());
}
"#;
        let mut fixed = source.to_string();
        let mut ctx = FixContext {
            source: &mut fixed,
            error_msg: "borrow of moved value: `args.hash`",
            line: 4,
            var_name: Some("args.hash".to_string()),
            type_info: None,
        };

        let result = fix_pre_compute_is_some(&mut ctx);
        assert!(result);
        assert!(fixed.contains("let has_hash = args.hash.is_some();"));
        assert!(fixed.contains("format_output(&info, has_hash)"));
    }

    #[test]
    fn test_fix_regex_new_str() {
        let source = r#"let re = Regex::new("\\d+".to_string()).unwrap();"#;
        let mut fixed = source.to_string();
        let mut ctx = FixContext {
            source: &mut fixed,
            error_msg: "expected `&str`, found `String`",
            line: 1,
            var_name: None,
            type_info: None,
        };

        let result = fix_regex_new_str(&mut ctx);
        assert!(result);
        assert_eq!(fixed, r#"let re = Regex::new("\\d+").unwrap();"#);
    }

    #[test]
    fn test_fix_command_factory() {
        let source = r#"use clap::Parser;

fn main() {
    parser.print_help();
}
"#;
        let mut fixed = source.to_string();
        let mut ctx = FixContext {
            source: &mut fixed,
            error_msg: "cannot find value `parser`",
            line: 4,
            var_name: None,
            type_info: None,
        };

        let result = fix_add_command_factory(&mut ctx);
        assert!(result);
        assert!(fixed.contains("use clap::{CommandFactory, Parser};"));
        assert!(fixed.contains("Args::command().print_help()?"));
    }

    // ============================================
    // EXTREME TDD: Additional tests
    // ============================================

    #[test]
    fn test_default_rules_has_all_categories() {
        let rules = AutoFixer::default_rules();
        // Should have rules for BorrowChecker, TypeMismatch, MissingImport
        assert!(rules.contains_key(&ErrorCategory::BorrowChecker));
        assert!(rules.contains_key(&ErrorCategory::TypeMismatch));
        assert!(rules.contains_key(&ErrorCategory::MissingImport));
    }

    #[test]
    fn test_parse_errors_multiple() {
        let errors = r#"error[E0382]: borrow of moved value
   --> src/main.rs:10:5
error[E0308]: mismatched types
   --> src/main.rs:20:10"#;

        let parsed = AutoFixer::parse_errors(errors);
        assert_eq!(parsed.len(), 2);
        assert_eq!(parsed[0].line, 10);
        assert_eq!(parsed[1].line, 20);
    }

    #[test]
    fn test_parse_errors_empty() {
        let errors = "warning: unused variable";
        let parsed = AutoFixer::parse_errors(errors);
        assert!(parsed.is_empty());
    }

    #[test]
    fn test_extract_var_name_none() {
        let msg = "no var name here";
        let var = AutoFixer::extract_var_name(msg);
        assert!(var.is_none());
    }

    #[test]
    fn test_extract_var_name_complex() {
        // Regex only matches one-level deep: var.field
        let msg = "value borrowed here: `self.data`";
        let var = AutoFixer::extract_var_name(msg);
        assert_eq!(var, Some("self.data".to_string()));
    }

    #[test]
    fn test_fix_result_default() {
        let result = FixResult {
            fixed: false,
            source: String::new(),
            description: "No fix".to_string(),
            confidence: 0.0,
        };
        assert!(!result.fixed);
        assert_eq!(result.confidence, 0.0);
    }

    #[test]
    fn test_transform_rule_pattern_matches() {
        let rule = TransformRule {
            name: "test_rule".to_string(),
            error_pattern: Regex::new(r"borrow.*moved").unwrap(),
            transform: |_| true,
        };
        assert!(rule.error_pattern.is_match("borrow of moved value"));
        assert!(!rule.error_pattern.is_match("type mismatch"));
    }

    #[test]
    fn test_fix_context_modification() {
        let mut source = "let x = 1;".to_string();
        let ctx = FixContext {
            source: &mut source,
            error_msg: "test",
            line: 1,
            var_name: Some("x".to_string()),
            type_info: Some("i32".to_string()),
        };

        // Modify source through context
        *ctx.source = "let x: i32 = 1;".to_string();
        assert_eq!(source, "let x: i32 = 1;");
    }
}