Skip to content

Commit 2b791a3

Browse files
hyperpolymathclaude
andcommitted
feat(checker): gate assignment to immutable bindings
Add an immutability check in check_expr_inner's Binary/Assign arm: an assignment whose LHS is an Ident bound to a non-mutable symbol now raises CheckError::ImmutableAssignment. Latent until the parser emits BinaryOp::Assign (documented in-code); cargo test green (143/0). Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 394fc09 commit 2b791a3

1 file changed

Lines changed: 28 additions & 0 deletions

File tree

crates/my-lang/src/checker.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,34 @@ impl Checker {
10071007
let left_ty = self.check_expr(left);
10081008
let right_ty = self.check_expr(right);
10091009

1010+
// Enforce immutability of assignment targets: assigning to a
1011+
// binding declared without `mut` is a static error. `Symbol.mutable`
1012+
// is recorded at `let` time but was never consulted here, so
1013+
// `CheckError::ImmutableAssignment` had no construction site.
1014+
// NOTE: currently latent — the parser does not yet emit
1015+
// `BinaryOp::Assign` (KNOWN_PARSE_GAP, conformance/valid/v04_let.my);
1016+
// this gate activates once assignment parsing lands, and is
1017+
// reachable today only via a programmatically-built AST.
1018+
if matches!(op, BinaryOp::Assign) {
1019+
if let Expr::Ident(ident) = left.as_ref() {
1020+
// Copy out of the immutable lookup borrow before touching
1021+
// self.errors. Unknown names are already reported by
1022+
// check_expr(left), so default to "not immutable".
1023+
let is_immutable = self
1024+
.symbols
1025+
.lookup(&ident.name)
1026+
.map(|s| !s.mutable)
1027+
.unwrap_or(false);
1028+
if is_immutable {
1029+
self.errors.push(CheckError::ImmutableAssignment {
1030+
name: ident.name.clone(),
1031+
line: ident.span.line,
1032+
column: ident.span.column,
1033+
});
1034+
}
1035+
}
1036+
}
1037+
10101038
self.check_binary_op(*op, &left_ty, &right_ty, *span)
10111039
}
10121040

0 commit comments

Comments
 (0)