Rename t1 -> lhs, t2 -> rhs in unify.

This commit is contained in:
KtorZ 2024-03-21 16:13:43 +01:00 committed by Lucas
parent dc9bab4f5c
commit 25e9db4f6c
1 changed files with 32 additions and 32 deletions

View File

@ -1399,36 +1399,36 @@ impl<'a> Environment<'a> {
#[allow(clippy::only_used_in_recursion)] #[allow(clippy::only_used_in_recursion)]
pub fn unify( pub fn unify(
&mut self, &mut self,
t1: Rc<Type>, lhs: Rc<Type>,
t2: Rc<Type>, rhs: Rc<Type>,
location: Span, location: Span,
allow_cast: bool, allow_cast: bool,
) -> Result<(), Error> { ) -> Result<(), Error> {
if t1 == t2 { if lhs == rhs {
return Ok(()); return Ok(());
} }
// TODO: maybe we also care to check is_link? // TODO: maybe we also care to check is_link?
if allow_cast if allow_cast
&& (t1.is_data() || t2.is_data()) && (lhs.is_data() || rhs.is_data())
&& !(t1.is_unbound() || t2.is_unbound()) && !(lhs.is_unbound() || rhs.is_unbound())
&& !(t1.is_function() || t2.is_function()) && !(lhs.is_function() || rhs.is_function())
&& !(t1.is_generic() || t2.is_generic()) && !(lhs.is_generic() || rhs.is_generic())
&& !(t1.is_string() || t2.is_string()) && !(lhs.is_string() || rhs.is_string())
&& !t1.contains_opaque() && !lhs.contains_opaque()
{ {
return Ok(()); return Ok(());
} }
if allow_cast && t1.contains_opaque() { if allow_cast && lhs.contains_opaque() {
return Err(Error::ExpectOnOpaqueType { location }); return Err(Error::ExpectOnOpaqueType { location });
} }
// Collapse right hand side type links. Left hand side will be collapsed in the next block. // Collapse right hand side type links. Left hand side will be collapsed in the next block.
if let Type::Var { tipo, alias } = t2.deref() { if let Type::Var { tipo, alias } = rhs.deref() {
if let TypeVar::Link { tipo } = tipo.borrow().deref() { if let TypeVar::Link { tipo } = tipo.borrow().deref() {
return self.unify( return self.unify(
t1, lhs,
Type::with_alias(tipo.clone(), alias.clone()), Type::with_alias(tipo.clone(), alias.clone()),
location, location,
allow_cast, allow_cast,
@ -1436,7 +1436,7 @@ impl<'a> Environment<'a> {
} }
} }
if let Type::Var { tipo, alias } = t1.deref() { if let Type::Var { tipo, alias } = lhs.deref() {
enum Action { enum Action {
Unify(Rc<Type>), Unify(Rc<Type>),
CouldNotUnify, CouldNotUnify,
@ -1449,12 +1449,12 @@ impl<'a> Environment<'a> {
} }
TypeVar::Unbound { id } => { TypeVar::Unbound { id } => {
unify_unbound_type(t2.clone(), *id, location)?; unify_unbound_type(rhs.clone(), *id, location)?;
Action::Link Action::Link
} }
TypeVar::Generic { id } => { TypeVar::Generic { id } => {
if let Type::Var { tipo, alias: _ } = t2.deref() { if let Type::Var { tipo, alias: _ } = rhs.deref() {
if tipo.borrow().is_unbound() { if tipo.borrow().is_unbound() {
*tipo.borrow_mut() = TypeVar::Generic { id: *id }; *tipo.borrow_mut() = TypeVar::Generic { id: *id };
return Ok(()); return Ok(());
@ -1466,29 +1466,29 @@ impl<'a> Environment<'a> {
return match action { return match action {
Action::Link => { Action::Link => {
*tipo.borrow_mut() = TypeVar::Link { tipo: t2 }; *tipo.borrow_mut() = TypeVar::Link { tipo: rhs };
Ok(()) Ok(())
} }
Action::Unify(t) => self.unify(t, t2, location, allow_cast), Action::Unify(t) => self.unify(t, rhs, location, allow_cast),
Action::CouldNotUnify => Err(Error::CouldNotUnify { Action::CouldNotUnify => Err(Error::CouldNotUnify {
location, location,
expected: t1.clone(), expected: lhs.clone(),
given: t2, given: rhs,
situation: None, situation: None,
rigid_type_names: HashMap::new(), rigid_type_names: HashMap::new(),
}), }),
}; };
} }
if let Type::Var { .. } = t2.deref() { if let Type::Var { .. } = rhs.deref() {
return self return self
.unify(t2, t1, location, allow_cast) .unify(rhs, lhs, location, allow_cast)
.map_err(|e| e.flip_unify()); .map_err(|e| e.flip_unify());
} }
match (t1.deref(), t2.deref()) { match (lhs.deref(), rhs.deref()) {
( (
Type::App { Type::App {
module: m1, module: m1,
@ -1509,8 +1509,8 @@ impl<'a> Environment<'a> {
) if m1 == m2 && n1 == n2 && args1.len() == args2.len() => { ) if m1 == m2 && n1 == n2 && args1.len() == args2.len() => {
for (a, b) in args1.iter().zip(args2) { for (a, b) in args1.iter().zip(args2) {
unify_enclosed_type( unify_enclosed_type(
t1.clone(), lhs.clone(),
t2.clone(), rhs.clone(),
self.unify(a.clone(), b.clone(), location, allow_cast), self.unify(a.clone(), b.clone(), location, allow_cast),
)?; )?;
} }
@ -1529,8 +1529,8 @@ impl<'a> Environment<'a> {
) if elems1.len() == elems2.len() => { ) if elems1.len() == elems2.len() => {
for (a, b) in elems1.iter().zip(elems2) { for (a, b) in elems1.iter().zip(elems2) {
unify_enclosed_type( unify_enclosed_type(
t1.clone(), lhs.clone(),
t2.clone(), rhs.clone(),
self.unify(a.clone(), b.clone(), location, allow_cast), self.unify(a.clone(), b.clone(), location, allow_cast),
)?; )?;
} }
@ -1553,8 +1553,8 @@ impl<'a> Environment<'a> {
self.unify(a.clone(), b.clone(), location, allow_cast) self.unify(a.clone(), b.clone(), location, allow_cast)
.map_err(|_| Error::CouldNotUnify { .map_err(|_| Error::CouldNotUnify {
location, location,
expected: t1.clone(), expected: lhs.clone(),
given: t2.clone(), given: rhs.clone(),
situation: None, situation: None,
rigid_type_names: HashMap::new(), rigid_type_names: HashMap::new(),
})?; })?;
@ -1562,8 +1562,8 @@ impl<'a> Environment<'a> {
self.unify(retrn1.clone(), retrn2.clone(), location, allow_cast) self.unify(retrn1.clone(), retrn2.clone(), location, allow_cast)
.map_err(|_| Error::CouldNotUnify { .map_err(|_| Error::CouldNotUnify {
location, location,
expected: t1.clone(), expected: lhs.clone(),
given: t2.clone(), given: rhs.clone(),
situation: None, situation: None,
rigid_type_names: HashMap::new(), rigid_type_names: HashMap::new(),
}) })
@ -1571,8 +1571,8 @@ impl<'a> Environment<'a> {
_ => Err(Error::CouldNotUnify { _ => Err(Error::CouldNotUnify {
location, location,
expected: t1.clone(), expected: lhs.clone(),
given: t2.clone(), given: rhs.clone(),
situation: None, situation: None,
rigid_type_names: HashMap::new(), rigid_type_names: HashMap::new(),
}), }),