Check for mutability when doing a let

This commit is contained in:
Sofia 2025-07-21 10:43:24 +03:00
parent 5763df948f
commit 37386db437
2 changed files with 10 additions and 1 deletions

View File

@ -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(

View File

@ -7,7 +7,7 @@ fn changer(param: &mut u32) {
fn main() -> u32 {
let value = 6;
changer(&value);
let mut a = &value;
return value;
}