| @@ -87,6 +87,49 @@ pub struct BodyTypes { |
| 87 | 87 | pub locals: Vec<Ty>, |
| 88 | 88 | } |
| 89 | 89 | |
| 90 | +/// What inference decided at a site from the type it saw there, for Core |
| 91 | +/// to follow. Inside a GADT arm that type is refined, but the stored one |
| 92 | +/// is the arm's unrefined rigid, so Core cannot decide again from it |
| 93 | +/// (issue #235). |
| 94 | +#[derive(Debug, Default)] |
| 95 | +pub struct SiteDecisions { |
| 96 | + /// The index of the field each `base.name` reads, by that expression. |
| 97 | + pub fields: HashMap<ExprId, u32>, |
| 98 | + /// How each HTML attribute value or child hole is built, by the |
| 99 | + /// hole's expression. |
| 100 | + pub html: HashMap<ExprId, HtmlHole>, |
| 101 | +} |
| 102 | + |
| 103 | +/// How an HTML hole is built (ADR 0009, ADR 0034). |
| 104 | +#[derive(Clone, Copy, Debug, PartialEq, Eq)] |
| 105 | +pub enum HtmlHole { |
| 106 | + /// An attribute's value: a `String` as it is. |
| 107 | + StringAttr, |
| 108 | + /// An attribute's value: an `Int`, shown. |
| 109 | + IntAttr, |
| 110 | + /// An attribute's value: a `Bool`, as a flag. |
| 111 | + FlagAttr, |
| 112 | + /// A child: a `List` of elements. |
| 113 | + Fragment, |
| 114 | + /// A child: a `String`, as escaped text. |
| 115 | + Text, |
| 116 | + /// A child: an `Element`. |
| 117 | + Element, |
| 118 | + /// A child of any other type, shown as text with the `Show` wanted at |
| 119 | + /// the node. |
| 120 | + Shown, |
| 121 | +} |
| 122 | + |
| 123 | +/// What inferring one body produced, before its constraints are solved. |
| 124 | +pub struct Inferred { |
| 125 | + pub types: BodyTypes, |
| 126 | + pub sites: SiteDecisions, |
| 127 | + pub wanted: Vec<Wanted>, |
| 128 | + pub arms: Vec<ArmScope>, |
| 129 | + pub caps: CapSites, |
| 130 | + pub mono_calls: Vec<(ExprId, Ty)>, |
| 131 | +} |
| 132 | + |
| 90 | 133 | pub struct FnInfer<'a, 'h> { |
| 91 | 134 | pub env: &'a mut Env<'h>, |
| 92 | 135 | pub ctx: &'a mut InferCtx, |
| @@ -99,6 +142,7 @@ pub struct FnInfer<'a, 'h> { |
| 99 | 142 | /// when checking a signature) and, for new names, flexible ones. |
| 100 | 143 | pub tyvars: VarScope, |
| 101 | 144 | pub types: BodyTypes, |
| 145 | + pub sites: SiteDecisions, |
| 102 | 146 | pub wanted: Vec<Wanted>, |
| 103 | 147 | /// The effect row the code being checked may perform: the enclosing |
| 104 | 148 | /// function's (or lambda's) row, extended inside `handle` bodies. |
| @@ -135,6 +179,18 @@ pub struct FnInfer<'a, 'h> { |
| 135 | 179 | const EARLY_RETURN: &str = "early `return`"; |
| 136 | 180 | |
| 137 | 181 | impl<'a, 'h> FnInfer<'a, 'h> { |
| 182 | + /// The results of the body, once it has been checked. |
| 183 | + pub fn finish(self) -> Inferred { |
| 184 | + Inferred { |
| 185 | + types: self.types, |
| 186 | + sites: self.sites, |
| 187 | + wanted: self.wanted, |
| 188 | + arms: self.arms, |
| 189 | + caps: self.caps, |
| 190 | + mono_calls: self.mono_calls, |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 138 | 194 | // Each argument is a distinct borrow or value the inference keeps; a |
| 139 | 195 | // struct grouping them would only be taken apart here. |
| 140 | 196 | #[allow(clippy::too_many_arguments)] |
| @@ -164,6 +220,7 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 164 | 220 | mono, |
| 165 | 221 | tyvars, |
| 166 | 222 | types, |
| 223 | + sites: SiteDecisions::default(), |
| 167 | 224 | wanted: Vec::new(), |
| 168 | 225 | effect, |
| 169 | 226 | scopes: Vec::new(), |
| @@ -489,7 +546,7 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 489 | 546 | args, |
| 490 | 547 | spread, |
| 491 | 548 | } => self.call(*callee, args, *spread, range), |
| 492 | | − Expr::Field { base, name } => self.field(*base, name, range), |
| 549 | + Expr::Field { base, name } => self.field(e, *base, name, range), |
| 493 | 550 | Expr::TupleIndex { base, index } => { |
| 494 | 551 | let base_ty = self.infer_expr(*base); |
| 495 | 552 | match self.ctx.shallow(&base_ty) { |
| @@ -661,14 +718,19 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 661 | 718 | continue; |
| 662 | 719 | }; |
| 663 | 720 | let t = self.infer_expr(e); |
| 664 | | − match self.ctx.shallow(&t) { |
| 665 | | − Ty::Con(d, _) if [wk.string, wk.int, wk.bool].contains(&Some(d)) => {} |
| 666 | | − Ty::Error => {} |
| 667 | | − Ty::Var(_) => self.unify_at( |
| 668 | | − &WellKnown::con(wk.string, vec![]), |
| 669 | | − &t, |
| 670 | | − self.body.expr_span(e), |
| 671 | | − ), |
| 721 | + let hole = match self.ctx.shallow(&t) { |
| 722 | + Ty::Con(d, _) if Some(d) == wk.int => HtmlHole::IntAttr, |
| 723 | + Ty::Con(d, _) if Some(d) == wk.bool => HtmlHole::FlagAttr, |
| 724 | + Ty::Con(d, _) if Some(d) == wk.string => HtmlHole::StringAttr, |
| 725 | + Ty::Error => HtmlHole::StringAttr, |
| 726 | + Ty::Var(_) => { |
| 727 | + self.unify_at( |
| 728 | + &WellKnown::con(wk.string, vec![]), |
| 729 | + &t, |
| 730 | + self.body.expr_span(e), |
| 731 | + ); |
| 732 | + HtmlHole::StringAttr |
| 733 | + } |
| 672 | 734 | other => { |
| 673 | 735 | let shown = self.show(&other); |
| 674 | 736 | self.error( |
| @@ -679,8 +741,10 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 679 | 741 | ), |
| 680 | 742 | self.body.expr_span(e), |
| 681 | 743 | ); |
| 744 | + HtmlHole::StringAttr |
| 682 | 745 | } |
| 683 | | − } |
| 746 | + }; |
| 747 | + self.sites.html.insert(e, hole); |
| 684 | 748 | } |
| 685 | 749 | self.html_children(site, &node.children, msg); |
| 686 | 750 | } |
| @@ -701,18 +765,27 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 701 | 765 | HtmlChild::Expr(e) => { |
| 702 | 766 | let t = self.infer_expr(*e); |
| 703 | 767 | let range = self.body.expr_span(*e); |
| 704 | | − match self.ctx.shallow(&t) { |
| 768 | + let hole = match self.ctx.shallow(&t) { |
| 705 | 769 | Ty::Con(d, args) if Some(d) == wk.list => { |
| 706 | | − self.unify_at(&element, &args[0], range) |
| 770 | + self.unify_at(&element, &args[0], range); |
| 771 | + HtmlHole::Fragment |
| 707 | 772 | } |
| 708 | | − Ty::Con(d, _) if Some(d) == wk.string => {} |
| 773 | + Ty::Con(d, _) if Some(d) == wk.string => HtmlHole::Text, |
| 709 | 774 | Ty::Con(d, _) if Some(d) == wk.element => { |
| 710 | | − self.unify_at(&element, &t, range) |
| 775 | + self.unify_at(&element, &t, range); |
| 776 | + HtmlHole::Element |
| 711 | 777 | } |
| 712 | | − Ty::Var(_) => self.unify_at(&element, &t, range), |
| 713 | | − Ty::Error => {} |
| 714 | | − _ => self.want(wk.show, &t, range, Some(site)), |
| 715 | | − } |
| 778 | + Ty::Var(_) => { |
| 779 | + self.unify_at(&element, &t, range); |
| 780 | + HtmlHole::Element |
| 781 | + } |
| 782 | + Ty::Error => HtmlHole::Element, |
| 783 | + _ => { |
| 784 | + self.want(wk.show, &t, range, Some(site)); |
| 785 | + HtmlHole::Shown |
| 786 | + } |
| 787 | + }; |
| 788 | + self.sites.html.insert(*e, hole); |
| 716 | 789 | } |
| 717 | 790 | } |
| 718 | 791 | } |
| @@ -988,7 +1061,7 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 988 | 1061 | |
| 989 | 1062 | /// `record.field`: the base's type must be known and have a single |
| 990 | 1063 | /// constructor with that labelled field. |
| 991 | | − fn field(&mut self, base: ExprId, name: &str, range: TextRange) -> Ty { |
| 1064 | + fn field(&mut self, e: ExprId, base: ExprId, name: &str, range: TextRange) -> Ty { |
| 992 | 1065 | let base_ty = self.infer_expr(base); |
| 993 | 1066 | let base_ty = self.ctx.shallow(&base_ty); |
| 994 | 1067 | let (def, args) = match &base_ty { |
| @@ -1068,6 +1141,7 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 1068 | 1141 | ); |
| 1069 | 1142 | return Ty::Error; |
| 1070 | 1143 | }; |
| 1144 | + self.sites.fields.insert(e, index as u32); |
| 1071 | 1145 | match self.env.schemes.get(ctor).map(|s| s.ty.subst_gen(&args)) { |
| 1072 | 1146 | Some(Ty::Fn(fields, _, _)) => fields[index].clone(), |
| 1073 | 1147 | _ => Ty::Error, |