cleanup some error reporting code

This commit is contained in:
pca006132 2021-07-30 13:50:46 +08:00
parent 743a9384a3
commit 7ad8e2d81d

View File

@ -308,11 +308,8 @@ impl Unifier {
(TVar { meta: Record(map), id, range, .. }, TObj { fields, .. }) => { (TVar { meta: Record(map), id, range, .. }, TObj { fields, .. }) => {
self.occur_check(a, b)?; self.occur_check(a, b)?;
for (k, v) in map.borrow().iter() { for (k, v) in map.borrow().iter() {
if let Some(ty) = fields.get(k) { let ty = fields.get(k).ok_or_else(|| format!("No such attribute {}", k))?;
self.unify(*ty, *v)?; self.unify(*ty, *v)?;
} else {
return Err(format!("No such attribute {}", k));
}
} }
let x = self.check_var_compatibility(*id, b, &range.borrow())?.unwrap_or(b); let x = self.check_var_compatibility(*id, b, &range.borrow())?.unwrap_or(b);
self.unify(x, b)?; self.unify(x, b)?;
@ -323,14 +320,11 @@ impl Unifier {
let ty = self.get_ty(*ty); let ty = self.get_ty(*ty);
if let TObj { fields, .. } = ty.as_ref() { if let TObj { fields, .. } = ty.as_ref() {
for (k, v) in map.borrow().iter() { for (k, v) in map.borrow().iter() {
if let Some(ty) = fields.get(k) { let ty = fields.get(k).ok_or_else(|| format!("No such attribute {}", k))?;
if !matches!(self.get_ty(*ty).as_ref(), TFunc { .. }) { if !matches!(self.get_ty(*ty).as_ref(), TFunc { .. }) {
return Err(format!("Cannot access field {} for virtual type", k)); return Err(format!("Cannot access field {} for virtual type", k));
}
self.unify(*v, *ty)?;
} else {
return Err(format!("No such attribute {}", k));
} }
self.unify(*v, *ty)?;
} }
} else { } else {
// require annotation... // require annotation...
@ -400,11 +394,11 @@ impl Unifier {
if let Some(i) = required.iter().position(|v| v == k) { if let Some(i) = required.iter().position(|v| v == k) {
required.remove(i); required.remove(i);
} }
if let Some(i) = all_names.iter().position(|v| &v.0 == k) { let i = all_names
self.unify(all_names.remove(i).1, *t)?; .iter()
} else { .position(|v| &v.0 == k)
return Err(format!("Unknown keyword argument {}", k)); .ok_or_else(|| format!("Unknown keyword argument {}", k))?;
} self.unify(all_names.remove(i).1, *t)?;
} }
if !required.is_empty() { if !required.is_empty() {
return Err("Expected more arguments".to_string()); return Err("Expected more arguments".to_string());