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
impl CITL {
/// Create a new CITL builder.
#[must_use]
pub fn builder() -> CITLBuilder {
CITLBuilder::new()
}
/// Compile source code and return structured result.
///
/// # Errors
///
/// Returns error if compilation process fails.
pub fn compile(&self, source: &str) -> CITLResult<CompilationResult> {
self.compiler.compile(source, &CompileOptions::default())
}
/// Encode a diagnostic into an embedding for pattern matching.
#[must_use]
pub fn encode_error(&self, diagnostic: &CompilerDiagnostic, source: &str) -> ErrorEmbedding {
self.encoder.encode(diagnostic, source)
}
/// Search for similar error patterns.
#[must_use]
pub fn search_patterns(&self, embedding: &ErrorEmbedding, k: usize) -> Vec<PatternMatch> {
self.pattern_library.search(embedding, k)
}
/// Add a successful fix to the pattern library (self-training).
pub fn add_pattern(&mut self, error: ErrorEmbedding, fix: FixTemplate, success: bool) {
if self.config.enable_self_training && success {
self.pattern_library.add_pattern(error, fix);
}
}
/// Get the compiler interface.
#[must_use]
pub fn compiler(&self) -> &dyn CompilerInterface {
self.compiler.as_ref()
}
/// Suggest a fix for a compiler diagnostic.
///
/// Encodes the error and searches the pattern library for similar fixes.
/// Returns the best matching fix above the confidence threshold.
///
/// # Arguments
/// * `diagnostic` - The compiler diagnostic to fix
/// * `source` - The source code containing the error
///
/// # Returns
/// `Some(SuggestedFix)` if a fix is found above confidence threshold, `None` otherwise.
#[must_use]
pub fn suggest_fix(
&self,
diagnostic: &CompilerDiagnostic,
source: &str,
) -> Option<SuggestedFix> {
// Encode the error
let embedding = self.encoder.encode(diagnostic, source);
// Search for similar patterns
let matches = self.pattern_library.search(&embedding, 5);
// Find best match above confidence threshold
for m in matches {
if m.similarity >= self.config.confidence_threshold {
let replacement =
self.instantiate_template(&m.pattern.fix_template, diagnostic, source);
return Some(SuggestedFix {
replacement,
confidence: m.similarity,
description: m.pattern.fix_template.description.clone(),
start_offset: diagnostic.span.column_start.saturating_sub(1),
end_offset: diagnostic.span.column_end,
error_code: diagnostic.code.code.clone(),
});
}
}
None
}
/// Apply a suggested fix to source code.
///
/// # Arguments
/// * `source` - The original source code
/// * `fix` - The suggested fix to apply
///
/// # Returns
/// The modified source code with the fix applied.
#[must_use]
pub fn apply_fix(&self, source: &str, fix: &SuggestedFix) -> String {
if fix.start_offset >= source.len() || fix.end_offset > source.len() {
return source.to_string();
}
let mut result = String::with_capacity(source.len() + fix.replacement.len());
result.push_str(&source[..fix.start_offset]);
result.push_str(&fix.replacement);
result.push_str(&source[fix.end_offset..]);
result
}
/// Iteratively fix all errors in source code.
///
/// Implements the CITL feedback loop:
/// 1. Compile source code
/// 2. If errors, suggest and apply fixes
/// 3. Repeat until success or max iterations
///
/// # Arguments
/// * `source` - The source code to fix
///
/// # Returns
/// `FixResult` containing the final state after fixing attempts.
pub fn fix_all(&mut self, source: &str) -> FixResult {
let mut current_source = source.to_string();
let mut iterations = 0;
let mut applied_fixes = Vec::new();
// First check if code already compiles
match self.compile(¤t_source) {
Ok(CompilationResult::Success { .. }) => {
return FixResult {
success: true,
fixed_source: Some(current_source),
iterations: 0,
remaining_errors: Vec::new(),
applied_fixes,
};
}
Ok(CompilationResult::Failure { errors, .. }) if errors.is_empty() => {
return FixResult {
success: true,
fixed_source: Some(current_source),
iterations: 0,
remaining_errors: Vec::new(),
applied_fixes,
};
}
_ => {}
}
while iterations < self.config.max_iterations {
iterations += 1;
// Compile and get errors
let Ok(result) = self.compile(¤t_source) else {
break;
};
match result {
CompilationResult::Success { .. } => {
return FixResult {
success: true,
fixed_source: Some(current_source),
iterations,
remaining_errors: Vec::new(),
applied_fixes,
};
}
CompilationResult::Failure { errors, .. } => {
if errors.is_empty() {
return FixResult {
success: true,
fixed_source: Some(current_source),
iterations,
remaining_errors: Vec::new(),
applied_fixes,
};
}
// Try to fix the first error
let error = &errors[0];
if let Some(fix) = self.suggest_fix(error, ¤t_source) {
applied_fixes.push(fix.description.clone());
current_source = self.apply_fix(¤t_source, &fix);
} else {
// No fix found, return failure
return FixResult {
success: false,
fixed_source: None,
iterations,
remaining_errors: errors.iter().map(|e| e.code.code.clone()).collect(),
applied_fixes,
};
}
}
}
}
// Max iterations reached
let remaining = match self.compile(¤t_source) {
Ok(CompilationResult::Failure { errors, .. }) => {
errors.iter().map(|e| e.code.code.clone()).collect()
}
_ => Vec::new(),
};
FixResult {
success: false,
fixed_source: None,
iterations,
remaining_errors: remaining,
applied_fixes,
}
}
/// Instantiate a fix template with concrete values.
#[allow(clippy::unused_self)]
fn instantiate_template(
&self,
template: &FixTemplate,
diagnostic: &CompilerDiagnostic,
_source: &str,
) -> String {
let mut result = template.pattern.clone();
// Simple placeholder replacement
// $expr -> the expression at the error location
// $type -> expected type
// $found -> found type
if let Some(expected) = &diagnostic.expected {
result = result.replace("$type", &expected.to_string());
}
if let Some(found) = &diagnostic.found {
result = result.replace("$found", &found.to_string());
}
// $expr is typically the problematic code - for now use a placeholder
result = result.replace("$expr", "expr");
result
}
}
/// A suggested fix for a compiler error.
#[derive(Debug, Clone)]
pub struct SuggestedFix {
/// The replacement text
pub replacement: String,
/// Confidence score (0.0 to 1.0)
pub confidence: f32,
/// Human-readable description
pub description: String,
/// Start offset in source (byte position)
pub start_offset: usize,
/// End offset in source (byte position)
pub end_offset: usize,
/// Error code this fix addresses
pub error_code: String,
}
impl SuggestedFix {
/// Create a new suggested fix.
#[must_use]
pub fn new(replacement: String, confidence: f32, description: String) -> Self {
Self {
replacement,
confidence,
description,
start_offset: 0,
end_offset: 0,
error_code: String::new(),
}
}
/// Set the span for replacement.
#[must_use]
pub fn with_span(mut self, start: usize, end: usize) -> Self {
self.start_offset = start;
self.end_offset = end;
self
}
/// Set the error code.
#[must_use]
pub fn with_error_code(mut self, code: &str) -> Self {
self.error_code = code.to_string();
self
}
}
/// Result of an iterative fix attempt.
#[derive(Debug, Clone)]
pub struct FixResult {
/// Whether all errors were fixed
pub success: bool,
/// The fixed source code (if successful)
pub fixed_source: Option<String>,
/// Number of fix iterations performed
pub iterations: usize,
/// Error codes that remain unfixed
pub remaining_errors: Vec<String>,
/// Descriptions of fixes that were applied
pub applied_fixes: Vec<String>,
}
impl FixResult {
/// Create a successful fix result.
#[must_use]
pub fn success(source: String, iterations: usize) -> Self {
Self {
success: true,
fixed_source: Some(source),
iterations,
remaining_errors: Vec::new(),
applied_fixes: Vec::new(),
}
}
/// Create a failed fix result.
#[must_use]
pub fn failure(iterations: usize, remaining: Vec<String>) -> Self {
Self {
success: false,
fixed_source: None,
iterations,
remaining_errors: remaining,
applied_fixes: Vec::new(),
}
}
/// Check if the fix was successful.
#[must_use]
pub fn is_success(&self) -> bool {
self.success
}
/// Add an applied fix description.
#[must_use]
pub fn with_applied_fix(mut self, description: String) -> Self {
self.applied_fixes.push(description);
self
}
}
/// Known Rust error codes with categories and difficulties.
///
/// Based on real-world data from depyler oracle (N=2,100+ samples):
/// - E0308 (20.9%), E0599 (17.9%), E0433 (16.4%), E0432 (14.1%)
pub static RUST_ERROR_CODES: &[ErrorCode] = &[
// Tier 1: Easy - Type System Basics (most common)
ErrorCode {
code: String::new(), // Will be initialized properly
category: ErrorCategory::TypeMismatch,
difficulty: Difficulty::Easy,
},
];
/// Initialize the static error codes properly.
#[must_use]
pub fn rust_error_codes() -> HashMap<String, ErrorCode> {
let mut codes = HashMap::new();
// Tier 1: Easy
codes.insert(
"E0308".to_string(),
ErrorCode::new("E0308", ErrorCategory::TypeMismatch, Difficulty::Easy),
);
codes.insert(
"E0425".to_string(),
ErrorCode::new("E0425", ErrorCategory::Unresolved, Difficulty::Easy),
);
codes.insert(
"E0433".to_string(),
ErrorCode::new("E0433", ErrorCategory::Unresolved, Difficulty::Easy),
);
codes.insert(
"E0432".to_string(),
ErrorCode::new("E0432", ErrorCategory::Import, Difficulty::Easy),
);
codes.insert(
"E0412".to_string(),
ErrorCode::new("E0412", ErrorCategory::Unresolved, Difficulty::Easy),
);
codes.insert(
"E0599".to_string(),
ErrorCode::new("E0599", ErrorCategory::MethodNotFound, Difficulty::Easy),
);
// Tier 2: Medium - Ownership & Borrowing
codes.insert(
"E0382".to_string(),
ErrorCode::new("E0382", ErrorCategory::Ownership, Difficulty::Medium),
);
codes.insert(
"E0502".to_string(),
ErrorCode::new("E0502", ErrorCategory::Borrowing, Difficulty::Medium),
);
codes.insert(
"E0499".to_string(),
ErrorCode::new("E0499", ErrorCategory::Borrowing, Difficulty::Medium),
);
codes.insert(
"E0596".to_string(),
ErrorCode::new("E0596", ErrorCategory::Borrowing, Difficulty::Medium),
);
codes.insert(
"E0507".to_string(),
ErrorCode::new("E0507", ErrorCategory::Ownership, Difficulty::Medium),
);
codes.insert(
"E0282".to_string(),
ErrorCode::new("E0282", ErrorCategory::TypeInference, Difficulty::Medium),
);
// Tier 3: Hard - Lifetimes
codes.insert(
"E0597".to_string(),
ErrorCode::new("E0597", ErrorCategory::Lifetime, Difficulty::Hard),
);
codes.insert(
"E0621".to_string(),
ErrorCode::new("E0621", ErrorCategory::Lifetime, Difficulty::Hard),
);
codes.insert(
"E0106".to_string(),
ErrorCode::new("E0106", ErrorCategory::Lifetime, Difficulty::Medium),
);
codes.insert(
"E0495".to_string(),
ErrorCode::new("E0495", ErrorCategory::Lifetime, Difficulty::Hard),
);
codes.insert(
"E0623".to_string(),
ErrorCode::new("E0623", ErrorCategory::Lifetime, Difficulty::Hard),
);
// Tier 4: Expert - Advanced
codes.insert(
"E0277".to_string(),
ErrorCode::new("E0277", ErrorCategory::TraitBound, Difficulty::Hard),
);
codes.insert(
"E0373".to_string(),
ErrorCode::new("E0373", ErrorCategory::Async, Difficulty::Expert),
);
codes
}
#[cfg(test)]
mod tests;