Check for mutability when doing a let
This commit is contained in:
parent
5763df948f
commit
37386db437
@ -63,6 +63,8 @@ pub enum ErrorKind {
|
|||||||
TypesDifferMutability(TypeKind, TypeKind),
|
TypesDifferMutability(TypeKind, TypeKind),
|
||||||
#[error("Cannot mutably borrow variable {0}, which is not declared as mutable")]
|
#[error("Cannot mutably borrow variable {0}, which is not declared as mutable")]
|
||||||
ImpossibleMutableBorrow(String),
|
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
|
/// 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,
|
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() {
|
let res_t = if res_t.known().is_err() {
|
||||||
// Unable to infer variable type even from expression! Default it
|
// Unable to infer variable type even from expression! Default it
|
||||||
let res_t = state.or_else(
|
let res_t = state.or_else(
|
||||||
|
@ -7,7 +7,7 @@ fn changer(param: &mut u32) {
|
|||||||
fn main() -> u32 {
|
fn main() -> u32 {
|
||||||
let value = 6;
|
let value = 6;
|
||||||
|
|
||||||
changer(&value);
|
let mut a = &value;
|
||||||
|
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user