| @@ -28,6 +28,10 @@ pub enum Ty { |
| 28 | 28 | /// sent to another actor, `<Local>` when it may capture what cannot |
| 29 | 29 | /// (ADR 0004, Revision 6). |
| 30 | 30 | Fn(Vec<Ty>, Box<Ty>, Box<Ty>, Box<Ty>), |
| 31 | + /// A projection `Elem<c>`: the associated type `Elem` (its trait's |
| 32 | + /// declaration) applied to the trait's arguments, then its own (ADR |
| 33 | + /// 0047). It reduces when an impl matches the trait's arguments. |
| 34 | + Proj(DefId, Vec<Ty>), |
| 31 | 35 | /// The empty effect row `<>`: total. |
| 32 | 36 | RowEmpty, |
| 33 | 37 | /// An effect row `<label | rest>`; the label is `Con(effect, args)`. |
| @@ -98,7 +102,9 @@ impl Ty { |
| 98 | 102 | match self { |
| 99 | 103 | Ty::Error => true, |
| 100 | 104 | Ty::App(head, args) => head.contains_error() || args.iter().any(Ty::contains_error), |
| 101 | | − Ty::Con(_, args) | Ty::Tuple(args) => args.iter().any(Ty::contains_error), |
| 105 | + Ty::Con(_, args) | Ty::Tuple(args) | Ty::Proj(_, args) => { |
| 106 | + args.iter().any(Ty::contains_error) |
| 107 | + } |
| 102 | 108 | Ty::Fn(params, ret, eff, caps) => { |
| 103 | 109 | params.iter().any(Ty::contains_error) |
| 104 | 110 | || ret.contains_error() |
| @@ -140,6 +146,7 @@ impl Ty { |
| 140 | 146 | Ty::App(head, args) => Ty::app(head.map(f), args.iter().map(|a| a.map(f)).collect()), |
| 141 | 147 | Ty::Con(d, args) => Ty::Con(*d, args.iter().map(|a| a.map(f)).collect()), |
| 142 | 148 | Ty::Tuple(elems) => Ty::Tuple(elems.iter().map(|a| a.map(f)).collect()), |
| 149 | + Ty::Proj(d, args) => Ty::Proj(*d, args.iter().map(|a| a.map(f)).collect()), |
| 143 | 150 | Ty::Fn(params, ret, eff, caps) => Ty::Fn( |
| 144 | 151 | params.iter().map(|a| a.map(f)).collect(), |
| 145 | 152 | Box::new(ret.map(f)), |
| @@ -155,6 +162,41 @@ impl Ty { |
| 155 | 162 | } |
| 156 | 163 | } |
| 157 | 164 | |
| 165 | + /// Like [`Ty::visit`], but without entering projections: their |
| 166 | + /// arguments are not determined by the projection (ADR 0047). |
| 167 | + pub fn visit_outside_projections(&self, f: &mut impl FnMut(&Ty)) { |
| 168 | + if matches!(self, Ty::Proj(..)) { |
| 169 | + return; |
| 170 | + } |
| 171 | + f(self); |
| 172 | + match self { |
| 173 | + Ty::App(head, args) => { |
| 174 | + head.visit_outside_projections(f); |
| 175 | + args.iter().for_each(|a| a.visit_outside_projections(f)); |
| 176 | + } |
| 177 | + Ty::Con(_, args) | Ty::Tuple(args) => { |
| 178 | + args.iter().for_each(|a| a.visit_outside_projections(f)) |
| 179 | + } |
| 180 | + Ty::Fn(params, ret, eff, caps) => { |
| 181 | + params.iter().for_each(|a| a.visit_outside_projections(f)); |
| 182 | + ret.visit_outside_projections(f); |
| 183 | + eff.visit_outside_projections(f); |
| 184 | + caps.visit_outside_projections(f); |
| 185 | + } |
| 186 | + Ty::RowExtend(l, r) => { |
| 187 | + l.visit_outside_projections(f); |
| 188 | + r.visit_outside_projections(f); |
| 189 | + } |
| 190 | + Ty::Proj(..) |
| 191 | + | Ty::Var(_) |
| 192 | + | Ty::Rigid(_) |
| 193 | + | Ty::Gen(_) |
| 194 | + | Ty::Bound(_) |
| 195 | + | Ty::RowEmpty |
| 196 | + | Ty::Error => {} |
| 197 | + } |
| 198 | + } |
| 199 | + |
| 158 | 200 | pub fn visit(&self, f: &mut impl FnMut(&Ty)) { |
| 159 | 201 | f(self); |
| 160 | 202 | match self { |
| @@ -162,7 +204,9 @@ impl Ty { |
| 162 | 204 | head.visit(f); |
| 163 | 205 | args.iter().for_each(|a| a.visit(f)); |
| 164 | 206 | } |
| 165 | | − Ty::Con(_, args) | Ty::Tuple(args) => args.iter().for_each(|a| a.visit(f)), |
| 207 | + Ty::Con(_, args) | Ty::Tuple(args) | Ty::Proj(_, args) => { |
| 208 | + args.iter().for_each(|a| a.visit(f)) |
| 209 | + } |
| 166 | 210 | Ty::Fn(params, ret, eff, caps) => { |
| 167 | 211 | params.iter().for_each(|a| a.visit(f)); |
| 168 | 212 | ret.visit(f); |
| @@ -263,6 +307,65 @@ pub enum UnifyError { |
| 263 | 307 | /// supply (`Bound`), of this effect: whoever calls a function of that |
| 264 | 308 | /// row would not know to supply it (ADR 0010). |
| 265 | 309 | Receives(DefId), |
| 310 | + /// A projection whose arguments are not known yet, so it cannot be |
| 311 | + /// computed (ADR 0047). |
| 312 | + Stuck, |
| 313 | +} |
| 314 | + |
| 315 | +/// The definitions of associated types (ADR 0047), for reducing |
| 316 | +/// projections during unification. |
| 317 | +#[derive(Default, Debug)] |
| 318 | +pub struct AssocTable { |
| 319 | + /// By declaration: each impl's definition. |
| 320 | + pub defs: HashMap<DefId, Vec<AssocDef>>, |
| 321 | + /// By declaration: how many arguments its trait takes. |
| 322 | + pub trait_arity: HashMap<DefId, usize>, |
| 323 | +} |
| 324 | + |
| 325 | +/// One impl's definition of an associated type. |
| 326 | +#[derive(Clone, Debug)] |
| 327 | +pub struct AssocDef { |
| 328 | + /// The impl's trait arguments, over its variables (`Gen(0..vars)`): |
| 329 | + /// `List<Gen(0)>`. |
| 330 | + pub trait_args: Vec<Ty>, |
| 331 | + /// How many variables the impl has. |
| 332 | + pub vars: usize, |
| 333 | + /// The definition, over the impl's variables then the associated |
| 334 | + /// type's own parameters. |
| 335 | + pub body: Ty, |
| 336 | +} |
| 337 | + |
| 338 | +impl AssocTable { |
| 339 | + /// What `assoc<args>` is, if an impl matches its trait's arguments, |
| 340 | + /// which `resolve` follows to their heads. Impl heads are named types |
| 341 | + /// or tuples applied to distinct variables (ADR 0018), so matching is |
| 342 | + /// one level deep and one-way. |
| 343 | + pub fn reduce(&self, assoc: DefId, args: &[Ty], resolve: impl Fn(&Ty) -> Ty) -> Option<Ty> { |
| 344 | + let n = *self.trait_arity.get(&assoc)?; |
| 345 | + let (trait_args, own) = args.split_at(n.min(args.len())); |
| 346 | + let resolved: Vec<Ty> = trait_args.iter().map(&resolve).collect(); |
| 347 | + 'defs: for def in self.defs.get(&assoc)? { |
| 348 | + let mut subst = vec![None; def.vars]; |
| 349 | + for (pattern, ty) in def.trait_args.iter().zip(&resolved) { |
| 350 | + let (pargs, targs) = match (pattern, ty) { |
| 351 | + (Ty::Con(p, pargs), Ty::Con(t, targs)) if p == t => (pargs, targs), |
| 352 | + (Ty::Tuple(pargs), Ty::Tuple(targs)) => (pargs, targs), |
| 353 | + _ => continue 'defs, |
| 354 | + }; |
| 355 | + if pargs.len() != targs.len() { |
| 356 | + continue 'defs; |
| 357 | + } |
| 358 | + for (pa, ta) in pargs.iter().zip(targs) { |
| 359 | + let Ty::Gen(i) = pa else { continue 'defs }; |
| 360 | + subst[*i as usize] = Some(ta.clone()); |
| 361 | + } |
| 362 | + } |
| 363 | + let mut subst: Vec<Ty> = subst.into_iter().collect::<Option<_>>()?; |
| 364 | + subst.extend(own.iter().cloned()); |
| 365 | + return Some(def.body.subst_gen(&subst)); |
| 366 | + } |
| 367 | + None |
| 368 | + } |
| 266 | 369 | } |
| 267 | 370 | |
| 268 | 371 | /// Unification variables of one inference session (one function or SCC). |
| @@ -295,6 +398,8 @@ pub struct InferCtx { |
| 295 | 398 | /// parameters are not of kind `Type`. A variable application `f<a>` |
| 296 | 399 | /// cannot take those (ADR 0018). |
| 297 | 400 | pub higher_params: std::rc::Rc<HashMap<DefId, Vec<bool>>>, |
| 401 | + /// The impls' definitions of associated types (ADR 0047). |
| 402 | + pub assoc: std::rc::Rc<AssocTable>, |
| 298 | 403 | } |
| 299 | 404 | |
| 300 | 405 | impl InferCtx { |
| @@ -382,12 +487,44 @@ impl InferCtx { |
| 382 | 487 | /// Follow bound variables and assumed equalities at the top of `ty`. |
| 383 | 488 | pub fn shallow(&self, ty: &Ty) -> Ty { |
| 384 | 489 | let mut ty = self.shallow_vars(ty); |
| 385 | | − while let Ty::Rigid(r) = ty |
| 386 | | − && let Some(t) = &self.rigid_eqs[r as usize] |
| 387 | | − { |
| 388 | | − ty = self.shallow_vars(t); |
| 490 | + loop { |
| 491 | + match &ty { |
| 492 | + Ty::Rigid(r) if let Some(t) = &self.rigid_eqs[*r as usize] => { |
| 493 | + ty = self.shallow_vars(t); |
| 494 | + } |
| 495 | + // A projection an impl defines is what the impl says. (A |
| 496 | + // variable is bound to a projection as written, before |
| 497 | + // this, so an arm's equalities never leak through one.) |
| 498 | + Ty::Proj(d, args) |
| 499 | + if let Some(t) = self.assoc.reduce(*d, args, |a| self.shallow(a)) => |
| 500 | + { |
| 501 | + ty = self.shallow_vars(&t); |
| 502 | + } |
| 503 | + _ => return ty, |
| 504 | + } |
| 389 | 505 | } |
| 390 | | − ty |
| 506 | + } |
| 507 | + |
| 508 | + /// `ty` with its variables substituted and every projection an impl |
| 509 | + /// defines reduced, all the way down: what later phases see (ADR |
| 510 | + /// 0047). The arm equalities are not followed. |
| 511 | + pub fn normalize(&self, ty: &Ty) -> Ty { |
| 512 | + self.zonk(ty).map(&mut |t| match t { |
| 513 | + Ty::Proj(d, args) => { |
| 514 | + let args: Vec<Ty> = args.iter().map(|a| self.normalize(a)).collect(); |
| 515 | + let reduced = self.assoc.reduce(*d, &args, Clone::clone)?; |
| 516 | + Some(self.normalize(&reduced)) |
| 517 | + } |
| 518 | + _ => None, |
| 519 | + }) |
| 520 | + } |
| 521 | + |
| 522 | + /// Whether `ty` mentions an inference variable. |
| 523 | + pub fn has_vars(&self, ty: &Ty) -> bool { |
| 524 | + let mut found = false; |
| 525 | + self.zonk(ty) |
| 526 | + .visit(&mut |t| found |= matches!(t, Ty::Var(_))); |
| 527 | + found |
| 391 | 528 | } |
| 392 | 529 | |
| 393 | 530 | /// Follow bound variables at the top of `ty` (not equalities). |
| @@ -490,6 +627,25 @@ impl InferCtx { |
| 490 | 627 | self.unify_all(a1, a2) |
| 491 | 628 | } |
| 492 | 629 | (Ty::Tuple(e1), Ty::Tuple(e2)) if e1.len() == e2.len() => self.unify_all(e1, e2), |
| 630 | + // Projections that do not reduce: equal only to themselves |
| 631 | + // (they are not injective, so their arguments are not |
| 632 | + // unified). One still waiting for its arguments is undecided. |
| 633 | + (Ty::Proj(d1, x1), Ty::Proj(d2, x2)) |
| 634 | + if d1 == d2 |
| 635 | + && x1 |
| 636 | + .iter() |
| 637 | + .map(|t| self.zonk(t)) |
| 638 | + .eq(x2.iter().map(|t| self.zonk(t))) => |
| 639 | + { |
| 640 | + Ok(()) |
| 641 | + } |
| 642 | + (p @ Ty::Proj(..), other) | (other, p @ Ty::Proj(..)) => { |
| 643 | + if self.has_vars(p) || self.has_vars(other) && matches!(other, Ty::Proj(..)) { |
| 644 | + Err(UnifyError::Stuck) |
| 645 | + } else { |
| 646 | + Err(UnifyError::Mismatch) |
| 647 | + } |
| 648 | + } |
| 493 | 649 | (Ty::Fn(p1, r1, f1, c1), Ty::Fn(p2, r2, f2, c2)) if p1.len() == p2.len() => { |
| 494 | 650 | self.unify_all(p1, p2)?; |
| 495 | 651 | self.unify(r1, r2)?; |
| @@ -726,6 +882,18 @@ impl Printer<'_> { |
| 726 | 882 | }), |
| 727 | 883 | Ty::Gen(g) => out.push_str(self.gen_names.get(*g as usize).map_or("?", String::as_str)), |
| 728 | 884 | Ty::Error => out.push_str("<error>"), |
| 885 | + // `Elem<c>`: valid source wherever `Elem` is in scope. |
| 886 | + Ty::Proj(def, args) => { |
| 887 | + out.push_str(&self.hir.def(*def).name); |
| 888 | + out.push('<'); |
| 889 | + for (i, a) in args.iter().enumerate() { |
| 890 | + if i > 0 { |
| 891 | + out.push_str(", "); |
| 892 | + } |
| 893 | + self.write(a, out); |
| 894 | + } |
| 895 | + out.push('>'); |
| 896 | + } |
| 729 | 897 | Ty::Con(def, args) => { |
| 730 | 898 | out.push_str(&self.hir.def(*def).name); |
| 731 | 899 | // Handler instances are never shown (ADR 0010). |