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
/// Mutability Error Checking
///
/// Detects when variables are mutated without `mut` keyword
/// and provides helpful error messages with suggestions.
use crate::parser::*;
use std::collections::HashMap;
#[derive(Debug, Clone)]
pub struct MutabilityError {
pub variable: String,
pub error_type: MutabilityErrorType,
pub location: SourceLocation,
pub suggestion: String,
}
#[derive(Debug, Clone, PartialEq)]
pub enum MutabilityErrorType {
Reassignment,
CompoundAssignment,
FieldMutation,
MutatingMethodCall,
}
impl MutabilityError {
pub fn format_error(&self) -> String {
let error_msg = match self.error_type {
MutabilityErrorType::Reassignment => {
format!(
"cannot assign twice to immutable variable `{}`",
self.variable
)
}
MutabilityErrorType::CompoundAssignment => {
format!(
"cannot use compound assignment on immutable variable `{}`",
self.variable
)
}
MutabilityErrorType::FieldMutation => {
format!(
"cannot mutate field of immutable binding `{}`",
self.variable
)
}
MutabilityErrorType::MutatingMethodCall => {
format!(
"cannot borrow `{}` as mutable, as it is not declared as mutable",
self.variable
)
}
};
// Handle default location if none provided
let (file_display, line, column) = if let Some(loc) = &self.location {
(loc.file.display().to_string(), loc.line, loc.column)
} else {
("unknown".to_string(), 0, 0)
};
format!(
"error: {}\n --> {}:{}:{}\n |\nhelp: {}",
error_msg, file_display, line, column, self.suggestion
)
}
}
pub struct MutabilityChecker {
/// Variables declared in current scope and whether they're mutable
declared_variables: HashMap<String, bool>,
/// Errors found
errors: Vec<MutabilityError>,
/// Current source file (for future error reporting enhancements)
#[allow(dead_code)]
current_file: std::path::PathBuf,
}
impl MutabilityChecker {
pub fn new(file: std::path::PathBuf) -> Self {
MutabilityChecker {
declared_variables: HashMap::new(),
errors: Vec::new(),
current_file: file,
}
}
pub fn check_function(&mut self, func: &FunctionDecl) -> Vec<MutabilityError> {
self.declared_variables.clear();
self.errors.clear();
// NOTE: We do NOT track parameters here!
// Parameter ownership (including &mut inference) is handled by the Analyzer.
// The mutability checker only checks LOCAL VARIABLES declared with `let`.
// This prevents false positives where a parameter like `fn foo(x: T)` gets
// inferred as `fn foo(x: &mut T)` by the analyzer, but the mutability checker
// complains before that inference happens.
// Check function body
self.check_statements(&func.body);
self.errors.clone()
}
fn check_statements<'ast>(&mut self, statements: &[&'ast Statement<'ast>]) {
for stmt in statements {
self.check_statement(stmt);
}
}
fn check_statement(&mut self, stmt: &Statement) {
match stmt {
Statement::Let {
pattern,
mutable,
value,
else_block,
..
} => {
// Track variable declaration
if let Pattern::Identifier(name) = pattern {
self.declared_variables.insert(name.clone(), *mutable);
}
// Check the value expression
self.check_expression(value);
// Check else block if present
if let Some(else_stmts) = else_block {
self.check_statements(else_stmts);
}
}
Statement::Assignment {
target,
value,
compound_op,
location,
} => {
// IMMUTABLE-BY-DEFAULT: All mutations of `let` bindings are errors.
// Users must explicitly write `let mut` when mutation is intended.
if let Some(var_name) = self.get_variable_name(target) {
if let Some(&is_mutable) = self.declared_variables.get(&var_name) {
if !is_mutable {
if self.is_field_access(target) {
// Field mutation: point.x = 10
self.errors.push(MutabilityError {
variable: var_name.clone(),
error_type: MutabilityErrorType::FieldMutation,
location: location.clone(),
suggestion: format!(
"consider changing this to be mutable: `let mut {}`",
var_name
),
});
} else if compound_op.is_some() {
// Compound assignment: count += 1
self.errors.push(MutabilityError {
variable: var_name.clone(),
error_type: MutabilityErrorType::CompoundAssignment,
location: location.clone(),
suggestion: format!(
"consider changing this to be mutable: `let mut {}`",
var_name
),
});
} else {
// Direct reassignment: x = 5; x = 6;
self.errors.push(MutabilityError {
variable: var_name.clone(),
error_type: MutabilityErrorType::Reassignment,
location: location.clone(),
suggestion: format!(
"consider changing this to be mutable: `let mut {}`",
var_name
),
});
}
}
}
}
self.check_expression(value);
}
Statement::Expression { expr, .. } => {
self.check_expression(expr);
}
Statement::Return {
value: Some(expr), ..
} => {
self.check_expression(expr);
}
Statement::Return { value: None, .. } => {}
Statement::If {
condition,
then_block,
else_block,
..
} => {
self.check_expression(condition);
self.check_statements(then_block);
if let Some(else_stmts) = else_block {
self.check_statements(else_stmts);
}
}
Statement::While {
condition, body, ..
} => {
self.check_expression(condition);
self.check_statements(body);
}
Statement::For { iterable, body, .. } => {
self.check_expression(iterable);
self.check_statements(body);
}
Statement::Loop { body, .. } => {
self.check_statements(body);
}
Statement::Match { value, arms, .. } => {
self.check_expression(value);
for arm in arms {
if let Some(guard) = &arm.guard {
self.check_expression(guard);
}
self.check_expression(arm.body);
}
}
_ => {}
}
}
fn check_expression(&mut self, expr: &Expression) {
match expr {
Expression::MethodCall {
object,
method,
arguments,
location,
..
} => {
// IMMUTABLE-BY-DEFAULT: Mutating method calls on `let` bindings are errors.
if self.is_mutating_method(method) {
if let Some(var_name) = self.get_variable_name(object) {
if let Some(&is_mutable) = self.declared_variables.get(&var_name) {
if !is_mutable {
self.errors.push(MutabilityError {
variable: var_name.clone(),
error_type: MutabilityErrorType::MutatingMethodCall,
location: location.clone(),
suggestion: format!(
"consider changing this to be mutable: `let mut {}`",
var_name
),
});
}
}
}
}
self.check_expression(object);
for (_, arg) in arguments {
self.check_expression(arg);
}
}
Expression::Binary { left, right, .. } => {
self.check_expression(left);
self.check_expression(right);
}
Expression::Unary { operand, .. } => {
self.check_expression(operand);
}
Expression::Call { arguments, .. } => {
for (_, arg) in arguments {
self.check_expression(arg);
}
}
Expression::Index { object, index, .. } => {
self.check_expression(object);
self.check_expression(index);
}
Expression::FieldAccess { object, .. } => {
self.check_expression(object);
}
Expression::Block { statements, .. } => {
self.check_statements(statements);
}
_ => {}
}
}
fn get_variable_name(&self, expr: &Expression) -> Option<String> {
match expr {
Expression::Identifier { name, .. } => Some(name.clone()),
Expression::FieldAccess { object, .. } => self.get_variable_name(object),
_ => None,
}
}
fn is_field_access(&self, expr: &Expression) -> bool {
matches!(expr, Expression::FieldAccess { .. })
}
fn is_mutating_method(&self, method: &str) -> bool {
crate::analyzer::stdlib_method_traits::method_mutates_receiver(method)
}
}