| @@ -261,6 +261,10 @@ pub struct Scheme { |
| 261 | 261 | /// Constraints of marker traits (`Send`): checked like the others, but |
| 262 | 262 | /// erased: no dictionary is passed for them (ADR 0004, Revision 6). |
| 263 | 263 | pub markers: Vec<Constraint>, |
| 264 | + /// Equalities a projection is known to satisfy (`Elem<a> = Int`), |
| 265 | + /// shown inside their trait's constraint: `Container<a, Elem = Int>` |
| 266 | + /// (ADR 0047). |
| 267 | + pub equalities: Vec<(Ty, Ty)>, |
| 264 | 268 | pub ty: Ty, |
| 265 | 269 | } |
| 266 | 270 | |
| @@ -270,6 +274,7 @@ impl Scheme { |
| 270 | 274 | vars: Vec::new(), |
| 271 | 275 | constraints: Vec::new(), |
| 272 | 276 | markers: Vec::new(), |
| 277 | + equalities: Vec::new(), |
| 273 | 278 | ty, |
| 274 | 279 | } |
| 275 | 280 | } |
| @@ -312,6 +317,24 @@ pub enum UnifyError { |
| 312 | 317 | Stuck, |
| 313 | 318 | } |
| 314 | 319 | |
| 320 | +impl Drop for InferCtx { |
| 321 | + /// Every pair [`InferCtx::unify_deferring`] sets aside is taken, or a |
| 322 | + /// projection's equality would be lost silently. |
| 323 | + fn drop(&mut self) { |
| 324 | + if !std::thread::panicking() { |
| 325 | + debug_assert!(self.stuck.is_empty(), "deferred equalities were dropped"); |
| 326 | + } |
| 327 | + } |
| 328 | +} |
| 329 | + |
| 330 | +/// A scheme instantiated with fresh variables. |
| 331 | +pub struct Instance { |
| 332 | + pub ty: Ty, |
| 333 | + pub constraints: Vec<Constraint>, |
| 334 | + pub markers: Vec<Constraint>, |
| 335 | + pub equalities: Vec<(Ty, Ty)>, |
| 336 | +} |
| 337 | + |
| 315 | 338 | /// The definitions of associated types (ADR 0047), for reducing |
| 316 | 339 | /// projections during unification. |
| 317 | 340 | #[derive(Default, Debug)] |
| @@ -400,6 +423,9 @@ pub struct InferCtx { |
| 400 | 423 | pub higher_params: std::rc::Rc<HashMap<DefId, Vec<bool>>>, |
| 401 | 424 | /// The impls' definitions of associated types (ADR 0047). |
| 402 | 425 | pub assoc: std::rc::Rc<AssocTable>, |
| 426 | + /// Pairs [`Self::unify_deferring`] set aside: a projection whose |
| 427 | + /// arguments are not known yet, and the type it must equal. |
| 428 | + stuck: Vec<(Ty, Ty)>, |
| 403 | 429 | } |
| 404 | 430 | |
| 405 | 431 | impl InferCtx { |
| @@ -558,15 +584,20 @@ impl InferCtx { |
| 558 | 584 | }) |
| 559 | 585 | } |
| 560 | 586 | |
| 561 | | − /// A fresh instance of `scheme`: its type, constraints and markers. |
| 562 | | − pub fn instantiate(&mut self, scheme: &Scheme) -> (Ty, Vec<Constraint>, Vec<Constraint>) { |
| 587 | + /// A fresh instance of `scheme`. |
| 588 | + pub fn instantiate(&mut self, scheme: &Scheme) -> Instance { |
| 563 | 589 | let args: Vec<Ty> = scheme.vars.iter().map(|_| self.fresh()).collect(); |
| 564 | 590 | let inst = |cs: &[Constraint]| cs.iter().map(|c| c.subst_gen(&args)).collect(); |
| 565 | | − ( |
| 566 | | − scheme.ty.subst_gen(&args), |
| 567 | | − inst(&scheme.constraints), |
| 568 | | − inst(&scheme.markers), |
| 569 | | − ) |
| 591 | + Instance { |
| 592 | + ty: scheme.ty.subst_gen(&args), |
| 593 | + constraints: inst(&scheme.constraints), |
| 594 | + markers: inst(&scheme.markers), |
| 595 | + equalities: scheme |
| 596 | + .equalities |
| 597 | + .iter() |
| 598 | + .map(|(p, t)| (p.subst_gen(&args), t.subst_gen(&args))) |
| 599 | + .collect(), |
| 600 | + } |
| 570 | 601 | } |
| 571 | 602 | |
| 572 | 603 | /// Bind `v` to `ty`: fails if `ty` contains `v` or a scoped rigid not |
| @@ -591,6 +622,15 @@ impl InferCtx { |
| 591 | 622 | } |
| 592 | 623 | _ => {} |
| 593 | 624 | }); |
| 625 | + // What fails only because of a projection an impl defines holds |
| 626 | + // of what it reduces to: `?v ~ Elem<Wrap<h>>` binds `?v` to `Int` |
| 627 | + // (ADR 0047). Only impls are followed, never an arm's equalities. |
| 628 | + if result.is_err() { |
| 629 | + let reduced = self.normalize(&ty); |
| 630 | + if reduced != self.zonk(&ty) { |
| 631 | + return self.bind(v, reduced); |
| 632 | + } |
| 633 | + } |
| 594 | 634 | result?; |
| 595 | 635 | for u in lower { |
| 596 | 636 | let l = &mut self.var_levels[u as usize]; |
| @@ -600,7 +640,32 @@ impl InferCtx { |
| 600 | 640 | Ok(()) |
| 601 | 641 | } |
| 602 | 642 | |
| 643 | + /// Unify `a` and `b`. A projection that cannot be computed yet makes |
| 644 | + /// it fail (`Stuck`); inference sets those aside instead, with |
| 645 | + /// [`Self::unify_deferring`]. |
| 603 | 646 | pub fn unify(&mut self, a: &Ty, b: &Ty) -> Result<(), UnifyError> { |
| 647 | + let mark = self.stuck.len(); |
| 648 | + let result = self.unify_rec(a, b); |
| 649 | + if self.stuck.len() > mark { |
| 650 | + self.stuck.truncate(mark); |
| 651 | + return result.and(Err(UnifyError::Stuck)); |
| 652 | + } |
| 653 | + result |
| 654 | + } |
| 655 | + |
| 656 | + /// Unify `a` and `b`, setting aside in [`Self::take_stuck`] each pair |
| 657 | + /// of a projection that cannot be computed yet and the type it must |
| 658 | + /// equal, and unifying everything else (ADR 0047). |
| 659 | + pub fn unify_deferring(&mut self, a: &Ty, b: &Ty) -> Result<(), UnifyError> { |
| 660 | + self.unify_rec(a, b) |
| 661 | + } |
| 662 | + |
| 663 | + /// The pairs [`Self::unify_deferring`] set aside since the last call. |
| 664 | + pub fn take_stuck(&mut self) -> Vec<(Ty, Ty)> { |
| 665 | + std::mem::take(&mut self.stuck) |
| 666 | + } |
| 667 | + |
| 668 | + fn unify_rec(&mut self, a: &Ty, b: &Ty) -> Result<(), UnifyError> { |
| 604 | 669 | // Variables first: one is bound to the other side as written, so |
| 605 | 670 | // that a binding made under an assumed equality (ADR 0041) names |
| 606 | 671 | // the rigid, and holds outside the arm too. |
| @@ -619,7 +684,7 @@ impl InferCtx { |
| 619 | 684 | } |
| 620 | 685 | let (ra, rb) = (self.shallow(&a), self.shallow(&b)); |
| 621 | 686 | if ra != a || rb != b { |
| 622 | | − return self.unify(&ra, &rb); |
| 687 | + return self.unify_rec(&ra, &rb); |
| 623 | 688 | } |
| 624 | 689 | match (&a, &b) { |
| 625 | 690 | (Ty::Bound(x), Ty::Bound(y)) if x == y => Ok(()), |
| @@ -641,16 +706,17 @@ impl InferCtx { |
| 641 | 706 | } |
| 642 | 707 | (p @ Ty::Proj(..), other) | (other, p @ Ty::Proj(..)) => { |
| 643 | 708 | if self.has_vars(p) || self.has_vars(other) && matches!(other, Ty::Proj(..)) { |
| 644 | | − Err(UnifyError::Stuck) |
| 709 | + self.stuck.push((p.clone(), other.clone())); |
| 710 | + Ok(()) |
| 645 | 711 | } else { |
| 646 | 712 | Err(UnifyError::Mismatch) |
| 647 | 713 | } |
| 648 | 714 | } |
| 649 | 715 | (Ty::Fn(p1, r1, f1, c1), Ty::Fn(p2, r2, f2, c2)) if p1.len() == p2.len() => { |
| 650 | 716 | self.unify_all(p1, p2)?; |
| 651 | | − self.unify(r1, r2)?; |
| 652 | | − self.unify(f1, f2)?; |
| 653 | | − self.unify(c1, c2) |
| 717 | + self.unify_rec(r1, r2)?; |
| 718 | + self.unify_rec(f1, f2)?; |
| 719 | + self.unify_rec(c1, c2) |
| 654 | 720 | } |
| 655 | 721 | (Ty::App(h1, a1), Ty::App(h2, a2)) if a1.len() <= a2.len() => { |
| 656 | 722 | self.unify_app(h1, a1, h2, a2) |
| @@ -672,7 +738,7 @@ impl InferCtx { |
| 672 | 738 | /// are no type lambdas, so this is the only solution (ADR 0018). |
| 673 | 739 | fn unify_app(&mut self, h1: &Ty, a1: &[Ty], h2: &Ty, a2: &[Ty]) -> Result<(), UnifyError> { |
| 674 | 740 | let k = a2.len() - a1.len(); |
| 675 | | − self.unify(h1, &Ty::app(h2.clone(), a2[..k].to_vec()))?; |
| 741 | + self.unify_rec(h1, &Ty::app(h2.clone(), a2[..k].to_vec()))?; |
| 676 | 742 | self.unify_all(a1, &a2[k..]) |
| 677 | 743 | } |
| 678 | 744 | |
| @@ -694,7 +760,7 @@ impl InferCtx { |
| 694 | 760 | { |
| 695 | 761 | return Err(UnifyError::Mismatch); |
| 696 | 762 | } |
| 697 | | − self.unify(f, &Ty::Con(d, cargs[..k].to_vec()))?; |
| 763 | + self.unify_rec(f, &Ty::Con(d, cargs[..k].to_vec()))?; |
| 698 | 764 | self.unify_all(args, &cargs[k..]) |
| 699 | 765 | } |
| 700 | 766 | |
| @@ -717,8 +783,8 @@ impl InferCtx { |
| 717 | 783 | { |
| 718 | 784 | return Err(UnifyError::Occurs); |
| 719 | 785 | } |
| 720 | | − self.unify(label, &found)?; |
| 721 | | − self.unify(rest, &row_rest) |
| 786 | + self.unify_rec(label, &found)?; |
| 787 | + self.unify_rec(rest, &row_rest) |
| 722 | 788 | } |
| 723 | 789 | |
| 724 | 790 | fn row_tail_var(&self, row: &Ty) -> Option<u32> { |
| @@ -828,7 +894,7 @@ impl InferCtx { |
| 828 | 894 | |
| 829 | 895 | fn unify_all(&mut self, a: &[Ty], b: &[Ty]) -> Result<(), UnifyError> { |
| 830 | 896 | for (x, y) in a.iter().zip(b) { |
| 831 | | − self.unify(x, y)?; |
| 897 | + self.unify_rec(x, y)?; |
| 832 | 898 | } |
| 833 | 899 | Ok(()) |
| 834 | 900 | } |
| @@ -1017,7 +1083,42 @@ impl Printer<'_> { |
| 1017 | 1083 | )), |
| 1018 | 1084 | other => p.ty(other), |
| 1019 | 1085 | }; |
| 1020 | | − let cs: Vec<_> = scheme.all_constraints().map(|c| p.constraint(c)).collect(); |
| 1086 | + // Each equality goes inside its trait's constraint. |
| 1087 | + let mut groups: Vec<(Constraint, Vec<String>)> = scheme |
| 1088 | + .all_constraints() |
| 1089 | + .map(|c| (c.clone(), Vec::new())) |
| 1090 | + .collect(); |
| 1091 | + for (proj, ty) in &scheme.equalities { |
| 1092 | + let Ty::Proj(assoc, args) = proj else { |
| 1093 | + continue; |
| 1094 | + }; |
| 1095 | + let louise_hir::DefKind::AssocType(a) = &self.hir.def(*assoc).kind else { |
| 1096 | + continue; |
| 1097 | + }; |
| 1098 | + let binding = format!("{} = {}", self.hir.def(*assoc).name, p.ty(ty)); |
| 1099 | + match groups |
| 1100 | + .iter_mut() |
| 1101 | + .find(|(c, _)| c.trait_ == a.owner && args.starts_with(&c.args)) |
| 1102 | + { |
| 1103 | + Some((_, bindings)) => bindings.push(binding), |
| 1104 | + None => { |
| 1105 | + let n = args.len() - a.params.len(); |
| 1106 | + let c = Constraint { |
| 1107 | + trait_: a.owner, |
| 1108 | + args: args[..n].to_vec(), |
| 1109 | + }; |
| 1110 | + groups.push((c, vec![binding])); |
| 1111 | + } |
| 1112 | + } |
| 1113 | + } |
| 1114 | + let cs: Vec<String> = groups |
| 1115 | + .iter() |
| 1116 | + .map(|(c, bindings)| { |
| 1117 | + let mut args: Vec<String> = c.args.iter().map(|t| p.ty(t)).collect(); |
| 1118 | + args.extend(bindings.iter().cloned()); |
| 1119 | + format!("{}<{}>", self.hir.def(c.trait_).name, args.join(", ")) |
| 1120 | + }) |
| 1121 | + .collect(); |
| 1021 | 1122 | if !cs.is_empty() { |
| 1022 | 1123 | out.push_str(" where "); |
| 1023 | 1124 | out.push_str(&cs.join(", ")); |