Add mutability compatibility check to borrows
This commit is contained in:
parent
74ce296a05
commit
1fadaa60f2
@ -385,10 +385,18 @@ impl Collapsable for TypeKind {
|
||||
(TypeKind::Vague(Vague::Unknown), other) | (other, TypeKind::Vague(Vague::Unknown)) => {
|
||||
Ok(other.clone())
|
||||
}
|
||||
(TypeKind::Borrow(val1, mut1), TypeKind::Borrow(val2, mut2)) => Ok(TypeKind::Borrow(
|
||||
Box::new(val1.collapse_into(val2)?),
|
||||
*mut1 && *mut2,
|
||||
)),
|
||||
(TypeKind::Borrow(val1, mut1), TypeKind::Borrow(val2, mut2)) => {
|
||||
// Extracted to give priority for other collapse-error
|
||||
let collapsed = val1.collapse_into(val2)?;
|
||||
if mut1 == mut2 {
|
||||
Ok(TypeKind::Borrow(Box::new(collapsed), *mut1 && *mut2))
|
||||
} else {
|
||||
Err(ErrorKind::TypesDifferMutability(
|
||||
self.clone(),
|
||||
other.clone(),
|
||||
))
|
||||
}
|
||||
}
|
||||
_ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())),
|
||||
}
|
||||
}
|
||||
|
@ -109,7 +109,7 @@ pub enum TypeKind {
|
||||
Array(Box<TypeKind>, u64),
|
||||
#[error("{0}")]
|
||||
CustomType(String),
|
||||
#[error("Borrow({0})")]
|
||||
#[error("Borrow({0}, {1})")]
|
||||
Borrow(Box<TypeKind>, bool),
|
||||
#[error("Ptr({0})")]
|
||||
Ptr(Box<TypeKind>),
|
||||
|
@ -59,6 +59,8 @@ pub enum ErrorKind {
|
||||
InvalidSetExpression,
|
||||
#[error("Can not deref {0}, as it is not a borrow")]
|
||||
AttemptedDerefNonBorrow(String),
|
||||
#[error("Types {0} and {1} differ in mutability")]
|
||||
TypesDifferMutability(TypeKind, TypeKind),
|
||||
}
|
||||
|
||||
/// Struct used to implement a type-checking pass that can be performed on the
|
||||
|
@ -5,7 +5,7 @@ fn changer(param: &mut u32) {
|
||||
}
|
||||
|
||||
fn main() -> u32 {
|
||||
let mut value = 6;
|
||||
let value = 6;
|
||||
|
||||
changer(&mut value);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user