| @@ -10,8 +10,6 @@ |
| 10 | 10 | //! `IntLit`): those equalities ([`Eqs`]) follow the search into the other |
| 11 | 11 | //! columns. |
| 12 | 12 | |
| 13 | | −use std::collections::HashMap; |
| 14 | | − |
| 15 | 13 | use louise_diag::Diagnostic; |
| 16 | 14 | use louise_hir::body::{Body, Expr, Lit, Pat, PatId, Stmt}; |
| 17 | 15 | use louise_hir::{DefId, DefKind, Hir, TypeBody}; |
| @@ -19,6 +17,7 @@ use louise_span::{FileId, Span}; |
| 19 | 17 | |
| 20 | 18 | use crate::codes; |
| 21 | 19 | use crate::env::Env; |
| 20 | +use crate::refine::Refinements; |
| 22 | 21 | use crate::ty::Ty; |
| 23 | 22 | |
| 24 | 23 | #[derive(Clone, PartialEq, Debug)] |
| @@ -45,7 +44,7 @@ enum Witness { |
| 45 | 44 | |
| 46 | 45 | /// What rigid variables are known to be, under the constructors matched |
| 47 | 46 | /// so far. |
| 48 | | −type Eqs = HashMap<u32, Ty>; |
| 47 | +type Eqs = Refinements; |
| 49 | 48 | |
| 50 | 49 | pub struct Checker<'a, 'h> { |
| 51 | 50 | pub env: &'a Env<'h>, |
| @@ -128,7 +127,12 @@ impl<'a, 'h> Checker<'a, 'h> { |
| 128 | 127 | return; |
| 129 | 128 | } |
| 130 | 129 | let row = vec![self.lower(p)]; |
| 131 | | − if let Some(w) = self.useful(&[row], &[DPat::Wild], &[self.ty_of(p).clone()], &Eqs::new()) { |
| 130 | + if let Some(w) = self.useful( |
| 131 | + &[row], |
| 132 | + &[DPat::Wild], |
| 133 | + &[self.ty_of(p).clone()], |
| 134 | + &Eqs::default(), |
| 135 | + ) { |
| 132 | 136 | diags.push( |
| 133 | 137 | Diagnostic::error( |
| 134 | 138 | codes::REFUTABLE, |
| @@ -163,7 +167,7 @@ impl<'a, 'h> Checker<'a, 'h> { |
| 163 | 167 | for arm in arms { |
| 164 | 168 | let row = vec![self.lower(arm.pat)]; |
| 165 | 169 | if self |
| 166 | | − .useful(&rows, &row, std::slice::from_ref(&ty), &Eqs::new()) |
| 170 | + .useful(&rows, &row, std::slice::from_ref(&ty), &Eqs::default()) |
| 167 | 171 | .is_none() |
| 168 | 172 | { |
| 169 | 173 | diags.push(Diagnostic::warning( |
| @@ -177,7 +181,7 @@ impl<'a, 'h> Checker<'a, 'h> { |
| 177 | 181 | rows.push(row); |
| 178 | 182 | } |
| 179 | 183 | } |
| 180 | | − if let Some(w) = self.useful(&rows, &[DPat::Wild], &[ty], &Eqs::new()) { |
| 184 | + if let Some(w) = self.useful(&rows, &[DPat::Wild], &[ty], &Eqs::default()) { |
| 181 | 185 | let scrutinee_span = Span { |
| 182 | 186 | file: self.file, |
| 183 | 187 | range: self.body.expr_span(scrutinee), |
| @@ -253,7 +257,7 @@ impl<'a, 'h> Checker<'a, 'h> { |
| 253 | 257 | /// unknown), in which case only a wildcard covers it. A refined |
| 254 | 258 | /// constructor that can't build a `ty` is not one of them. |
| 255 | 259 | fn all_ctors(&self, ty: &Ty, eqs: &Eqs) -> Option<Vec<Ctor>> { |
| 256 | | − match resolve(ty, eqs) { |
| 260 | + match eqs.resolve(ty) { |
| 257 | 261 | Ty::Tuple(elems) => Some(vec![Ctor::Tuple(elems.len())]), |
| 258 | 262 | ty @ Ty::Con(def, _) => match &self.hir().def(def).kind { |
| 259 | 263 | DefKind::Type(t) => match &t.body { |
| @@ -289,7 +293,7 @@ impl<'a, 'h> Checker<'a, 'h> { |
| 289 | 293 | fn field_tys(&self, c: &Ctor, ty: &Ty, eqs: &mut Eqs) -> Option<Vec<Ty>> { |
| 290 | 294 | let n = self.arity(c); |
| 291 | 295 | let unknown = vec![Ty::Error; n]; |
| 292 | | − match (c, resolve(ty, eqs)) { |
| 296 | + match (c, eqs.resolve(ty)) { |
| 293 | 297 | (Ctor::Tuple(_), Ty::Tuple(elems)) => Some(elems), |
| 294 | 298 | (Ctor::Variant(def), Ty::Con(_, args)) => { |
| 295 | 299 | let Some(scheme) = self.env.schemes.get(def) else { |
| @@ -509,22 +513,15 @@ fn lit_key(lit: &Lit) -> String { |
| 509 | 513 | } |
| 510 | 514 | } |
| 511 | 515 | |
| 512 | | −/// `ty`, or what the rigid variable it is is known to be. |
| 513 | | −fn resolve(ty: &Ty, eqs: &Eqs) -> Ty { |
| 514 | | − match ty { |
| 515 | | − Ty::Rigid(r) => match eqs.get(r) { |
| 516 | | − Some(t) => resolve(t, eqs), |
| 517 | | − None => ty.clone(), |
| 518 | | − }, |
| 519 | | − _ => ty.clone(), |
| 520 | | − } |
| 521 | | −} |
| 522 | | − |
| 523 | 516 | /// Match a refined constructor's result index `r` (variables as `Gen`) |
| 524 | 517 | /// against a type argument `s`: assign the variables, learn what rigid |
| 525 | | −/// variables are. False if no value can have both types. |
| 518 | +/// variables are. False if no value can have both types. Inference's |
| 519 | +/// `match_index` decides what an arm's code may assume; this only asks |
| 520 | +/// whether a constructor is possible, so what it can't tell (an unknown |
| 521 | +/// type, a variable the index doesn't fix) counts as possible (issue |
| 522 | +/// #224 records why the two walks stay apart). |
| 526 | 523 | fn index_match(r: &Ty, s: &Ty, assign: &mut [Option<Ty>], eqs: &mut Eqs) -> bool { |
| 527 | | − let s = resolve(s, eqs); |
| 524 | + let s = eqs.resolve(s); |
| 528 | 525 | if let Ty::Gen(i) = r { |
| 529 | 526 | let i = *i as usize; |
| 530 | 527 | return match assign[i].clone() { |
| @@ -541,8 +538,9 @@ fn index_match(r: &Ty, s: &Ty, assign: &mut [Option<Ty>], eqs: &mut Eqs) -> bool |
| 541 | 538 | .iter() |
| 542 | 539 | .map(|a| a.clone().unwrap_or(Ty::Error)) |
| 543 | 540 | .collect(); |
| 544 | | − eqs.insert(*k, r.subst_gen(&args)); |
| 545 | | − true |
| 541 | + // No value has a type that is a part of itself: a refused |
| 542 | + // equality makes the constructor impossible here (issue #224). |
| 543 | + eqs.insert(*k, r.subst_gen(&args)) |
| 546 | 544 | } |
| 547 | 545 | Ty::Con(d2, a2) => match r { |
| 548 | 546 | Ty::Con(d1, a1) if d1 == d2 && a1.len() == a2.len() => a1 |
| @@ -566,7 +564,7 @@ fn index_match(r: &Ty, s: &Ty, assign: &mut [Option<Ty>], eqs: &mut Eqs) -> bool |
| 566 | 564 | /// Whether two types could be equal: false only when they differ in a |
| 567 | 565 | /// named type or a tuple's size. |
| 568 | 566 | fn compatible(a: &Ty, b: &Ty, eqs: &Eqs) -> bool { |
| 569 | | − match (resolve(a, eqs), resolve(b, eqs)) { |
| 567 | + match (eqs.resolve(a), eqs.resolve(b)) { |
| 570 | 568 | (Ty::Con(d1, a1), Ty::Con(d2, a2)) => { |
| 571 | 569 | d1 == d2 |
| 572 | 570 | && a1.len() == a2.len() |