Add mutability compatibility check to borrows

This commit is contained in:
Sofia 2025-07-21 10:33:37 +03:00
parent 74ce296a05
commit 1fadaa60f2
4 changed files with 16 additions and 6 deletions

View File

@ -385,10 +385,18 @@ impl Collapsable for TypeKind {
(TypeKind::Vague(Vague::Unknown), other) | (other, TypeKind::Vague(Vague::Unknown)) => { (TypeKind::Vague(Vague::Unknown), other) | (other, TypeKind::Vague(Vague::Unknown)) => {
Ok(other.clone()) Ok(other.clone())
} }
(TypeKind::Borrow(val1, mut1), TypeKind::Borrow(val2, mut2)) => Ok(TypeKind::Borrow( (TypeKind::Borrow(val1, mut1), TypeKind::Borrow(val2, mut2)) => {
Box::new(val1.collapse_into(val2)?), // Extracted to give priority for other collapse-error
*mut1 && *mut2, 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())), _ => Err(ErrorKind::TypesIncompatible(self.clone(), other.clone())),
} }
} }

View File

@ -109,7 +109,7 @@ pub enum TypeKind {
Array(Box<TypeKind>, u64), Array(Box<TypeKind>, u64),
#[error("{0}")] #[error("{0}")]
CustomType(String), CustomType(String),
#[error("Borrow({0})")] #[error("Borrow({0}, {1})")]
Borrow(Box<TypeKind>, bool), Borrow(Box<TypeKind>, bool),
#[error("Ptr({0})")] #[error("Ptr({0})")]
Ptr(Box<TypeKind>), Ptr(Box<TypeKind>),

View File

@ -59,6 +59,8 @@ pub enum ErrorKind {
InvalidSetExpression, InvalidSetExpression,
#[error("Can not deref {0}, as it is not a borrow")] #[error("Can not deref {0}, as it is not a borrow")]
AttemptedDerefNonBorrow(String), 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 /// Struct used to implement a type-checking pass that can be performed on the

View File

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