From 37386db4376239bafab8f44c22859f0ee5c90c4f Mon Sep 17 00:00:00 2001 From: sofia Date: Mon, 21 Jul 2025 10:43:24 +0300 Subject: [PATCH] Check for mutability when doing a let --- reid/src/mir/typecheck.rs | 9 +++++++++ reid_src/borrow_hard.reid | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/reid/src/mir/typecheck.rs b/reid/src/mir/typecheck.rs index 48c1b60..3004d7b 100644 --- a/reid/src/mir/typecheck.rs +++ b/reid/src/mir/typecheck.rs @@ -63,6 +63,8 @@ pub enum ErrorKind { TypesDifferMutability(TypeKind, TypeKind), #[error("Cannot mutably borrow variable {0}, which is not declared as mutable")] ImpossibleMutableBorrow(String), + #[error("Cannot declare variable {0} as mutable, when it's type is immutable")] + ImpossibleMutLet(String), } /// Struct used to implement a type-checking pass that can be performed on the @@ -225,6 +227,13 @@ impl Block { variable_reference.2 + expression.1, ); + if *mutable && !res_t.is_mutable() { + state.note_errors( + &vec![ErrorKind::ImpossibleMutLet(variable_reference.1.clone())], + variable_reference.2, + ); + } + let res_t = if res_t.known().is_err() { // Unable to infer variable type even from expression! Default it let res_t = state.or_else( diff --git a/reid_src/borrow_hard.reid b/reid_src/borrow_hard.reid index d3cffbe..8fc9e78 100644 --- a/reid_src/borrow_hard.reid +++ b/reid_src/borrow_hard.reid @@ -7,7 +7,7 @@ fn changer(param: &mut u32) { fn main() -> u32 { let value = 6; - changer(&value); + let mut a = &value; return value; }