| @@ -86,6 +86,112 @@ pub struct CapSites { |
| 86 | 86 | pub partials: HashMap<ExprId, Vec<CapArg>>, |
| 87 | 87 | /// The function's own capability parameters. |
| 88 | 88 | pub own: Vec<Ty>, |
| 89 | + /// Whether the function takes an evidence parameter, after its own |
| 90 | + /// capabilities: its own row ends in an evidence variable (ADR 0066). |
| 91 | + pub own_evidence: bool, |
| 92 | + /// Per callee of a call that passes row evidence: how it is built. A |
| 93 | + /// bundled closure takes it instead of its capabilities (its `calls` |
| 94 | + /// entry is empty); a function whose own row ends in an evidence |
| 95 | + /// variable takes it after them (ADR 0066). |
| 96 | + pub evidence: HashMap<ExprId, EvArg>, |
| 97 | + /// Per bundled lambda: the capabilities it looks up in the one |
| 98 | + /// evidence argument it receives (its `lambdas` entry is empty). |
| 99 | + pub bundled: HashMap<ExprId, Vec<EvLookup>>, |
| 100 | +} |
| 101 | + |
| 102 | +/// A capability in row evidence (ADR 0066): its label's effect and |
| 103 | +/// `Bound` index, and the capability, by its instance. |
| 104 | +#[derive(Clone, Debug, PartialEq)] |
| 105 | +pub struct EvEntry { |
| 106 | + pub effect: DefId, |
| 107 | + pub index: u32, |
| 108 | + pub instance: Ty, |
| 109 | +} |
| 110 | + |
| 111 | +/// Where the rest of a callee's evidence comes from, after its entries. |
| 112 | +#[derive(Clone, Debug, PartialEq)] |
| 113 | +pub enum EvTail { |
| 114 | + /// Nothing: the callee's row is closed after them. |
| 115 | + Empty, |
| 116 | + /// The evidence of the scope the call is in (a lambda's, or the |
| 117 | + /// function's own for `None`) for the row they share: without the |
| 118 | + /// entries of the scope's own labels before it, the first one of each |
| 119 | + /// key in `drop`. |
| 120 | + Scope { |
| 121 | + owner: Option<ExprId>, |
| 122 | + drop: Vec<(DefId, u32)>, |
| 123 | + }, |
| 124 | +} |
| 125 | + |
| 126 | +/// How a call builds its callee's evidence: `entries`, in row order, then |
| 127 | +/// `tail`'s. |
| 128 | +#[derive(Clone, Debug, PartialEq)] |
| 129 | +pub struct EvArg { |
| 130 | + pub entries: Vec<EvEntry>, |
| 131 | + pub tail: EvTail, |
| 132 | +} |
| 133 | + |
| 134 | +/// A capability a bundled lambda takes from its evidence: the entry of |
| 135 | +/// `entry`'s key at `rank` among the entries with that key (ADR 0066). |
| 136 | +#[derive(Clone, Debug, PartialEq)] |
| 137 | +pub struct EvLookup { |
| 138 | + pub entry: EvEntry, |
| 139 | + pub rank: u32, |
| 140 | +} |
| 141 | + |
| 142 | +/// What inference saw of row evidence (ADR 0066), decided once the body's |
| 143 | +/// types are complete (`finish_evidence` in `lib.rs`). |
| 144 | +#[derive(Debug, Default)] |
| 145 | +pub struct EvidenceRecord { |
| 146 | + /// Per lambda: its row, and the capabilities it received as it began, |
| 147 | + /// in row order. |
| 148 | + pub lambdas: Vec<(ExprId, Ty, Vec<EvEntry>)>, |
| 149 | + /// The handlers lambdas receive through an evidence variable bound |
| 150 | + /// once their bodies began: by lambda, effect, `Bound` index and |
| 151 | + /// occurrence among those. |
| 152 | + pub late: HashMap<(ExprId, DefId, u32, u32), Ty>, |
| 153 | + pub calls: Vec<CallRecord>, |
| 154 | + /// Arrows that are compiled one by one where they are used: a bundled |
| 155 | + /// one is not supported there yet. Where, the arrow, and what it is. |
| 156 | + pub plain_only: Vec<(Span, PlainOnly, &'static str)>, |
| 157 | +} |
| 158 | + |
| 159 | +/// An arrow that must not be bundled: an expression's type, or a type. |
| 160 | +#[derive(Debug)] |
| 161 | +pub enum PlainOnly { |
| 162 | + Expr(ExprId), |
| 163 | + Ty(Ty), |
| 164 | +} |
| 165 | + |
| 166 | +/// Where the rest of a callee's evidence comes from, as a call saw it. |
| 167 | +#[derive(Clone, Debug)] |
| 168 | +pub enum CallTail { |
| 169 | + Empty, |
| 170 | + /// The current scope's (a lambda's, or the function's for `None`), |
| 171 | + /// whose row went on as `at` where the callee's row met it. |
| 172 | + Shared { |
| 173 | + owner: Option<ExprId>, |
| 174 | + at: Ty, |
| 175 | + }, |
| 176 | +} |
| 177 | + |
| 178 | +/// A call, for its evidence (ADR 0066). |
| 179 | +#[derive(Debug)] |
| 180 | +pub struct CallRecord { |
| 181 | + /// The callee: it takes evidence if its type is bundled. |
| 182 | + pub callee: ExprId, |
| 183 | + pub span: Span, |
| 184 | + /// The callee is a closure: its evidence replaces its capabilities. |
| 185 | + pub closure: bool, |
| 186 | + /// The callee is a function whose own row ends in an evidence |
| 187 | + /// variable: its evidence follows its capabilities. |
| 188 | + pub own_tail: bool, |
| 189 | + /// The entries of the `Bound` labels of a closure's row, as called. |
| 190 | + pub explicit: Vec<EvEntry>, |
| 191 | + /// The entries of the labels its evidence variable stands for, as |
| 192 | + /// the call resolved them. |
| 193 | + pub late: Vec<EvEntry>, |
| 194 | + pub tail: CallTail, |
| 89 | 195 | } |
| 90 | 196 | |
| 91 | 197 | /// Types computed for one body (zonked at the end). |
| @@ -205,6 +311,7 @@ pub struct Inferred { |
| 205 | 311 | pub mono_calls: Vec<(ExprId, Ty)>, |
| 206 | 312 | pub closures: Vec<Closure>, |
| 207 | 313 | pub deferred: Vec<Deferred>, |
| 314 | + pub evidence: EvidenceRecord, |
| 208 | 315 | } |
| 209 | 316 | |
| 210 | 317 | pub struct FnInfer<'a, 'h> { |
| @@ -266,9 +373,10 @@ pub struct FnInfer<'a, 'h> { |
| 266 | 373 | /// evidence (ADR 0066). |
| 267 | 374 | owner: Option<(ExprId, u32)>, |
| 268 | 375 | owners: Vec<Option<(ExprId, u32)>>, |
| 269 | | − /// The handler each lambda receives in its evidence for an effect and |
| 270 | | − /// occurrence: a rigid variable of its body's scope. |
| 271 | | − received: HashMap<(ExprId, DefId, u32), Ty>, |
| 376 | + /// What row evidence needs (ADR 0066); its `late` are the handlers |
| 377 | + /// each lambda receives in its evidence once its body began, rigid |
| 378 | + /// variables of its body's scope. |
| 379 | + evidence: EvidenceRecord, |
| 272 | 380 | /// The lambda whose scope binds each local (`None`: the body's). |
| 273 | 381 | local_owner: Vec<Option<usize>>, |
| 274 | 382 | } |
| @@ -288,6 +396,7 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 288 | 396 | mono_calls: self.mono_calls, |
| 289 | 397 | closures: self.closures, |
| 290 | 398 | deferred: self.deferred, |
| 399 | + evidence: self.evidence, |
| 291 | 400 | } |
| 292 | 401 | } |
| 293 | 402 | |
| @@ -341,7 +450,7 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 341 | 450 | lambda_stack: Vec::new(), |
| 342 | 451 | owner: None, |
| 343 | 452 | owners: Vec::new(), |
| 344 | | − received: HashMap::new(), |
| 453 | + evidence: EvidenceRecord::default(), |
| 345 | 454 | local_owner: vec![None; body.locals.len()], |
| 346 | 455 | } |
| 347 | 456 | } |
| @@ -1354,6 +1463,12 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 1354 | 1463 | // checked (they run first, and may pin the callee's type |
| 1355 | 1464 | // variables — e.g. which region a reference belongs to). |
| 1356 | 1465 | let mut pending: Vec<Ty> = Vec::new(); |
| 1466 | + // What the callee's evidence is built from, if it takes one (ADR |
| 1467 | + // 0066): a closure's `Bound` labels; whether it is a function |
| 1468 | + // whose own row ends in an evidence variable. |
| 1469 | + let mut explicit = Vec::new(); |
| 1470 | + let mut own_tail = false; |
| 1471 | + let mut closure = false; |
| 1357 | 1472 | let mut arrow = match self.ctx.shallow(&callee_ty) { |
| 1358 | 1473 | Ty::Fn(params, ret, eff, _) => { |
| 1359 | 1474 | // Capabilities the callee's callers supply get fresh |
| @@ -1369,9 +1484,15 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 1369 | 1484 | { |
| 1370 | 1485 | let own = self.row_instances(&eff); |
| 1371 | 1486 | self.caps.calls.insert(callee, own); |
| 1487 | + own_tail = self.own_tail_evidence(*def); |
| 1372 | 1488 | } |
| 1373 | 1489 | _ => { |
| 1374 | | − self.caps.calls.insert(callee, opened); |
| 1490 | + let mut positional = opened.clone(); |
| 1491 | + positional.sort_by_key(|e| (e.effect, e.index)); |
| 1492 | + let positional = positional.into_iter().map(|e| e.instance).collect(); |
| 1493 | + self.caps.calls.insert(callee, positional); |
| 1494 | + explicit = opened; |
| 1495 | + closure = true; |
| 1375 | 1496 | } |
| 1376 | 1497 | } |
| 1377 | 1498 | // An effect whose argument the arguments' types depend on |
| @@ -1386,6 +1507,7 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 1386 | 1507 | } |
| 1387 | 1508 | } |
| 1388 | 1509 | Ty::Var(_) => { |
| 1510 | + closure = true; |
| 1389 | 1511 | // An unknown callee is applied in full: one fresh |
| 1390 | 1512 | // parameter per argument (the flattened chain is the same |
| 1391 | 1513 | // type). |
| @@ -1560,16 +1682,20 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 1560 | 1682 | self.check_expr(*e, t); |
| 1561 | 1683 | } |
| 1562 | 1684 | pending.push(arrow.eff.clone()); |
| 1563 | | − for eff in &pending { |
| 1564 | | − self.perform(eff, range); |
| 1565 | | − } |
| 1685 | + let how = (closure, own_tail, explicit); |
| 1686 | + self.perform_call(callee, how, &pending, range); |
| 1566 | 1687 | return arrow.ret; |
| 1567 | 1688 | } |
| 1568 | 1689 | let mut failed = false; |
| 1569 | 1690 | for (i, arg) in args.iter().enumerate() { |
| 1570 | 1691 | while arrow.filled.iter().all(|f| *f) { |
| 1571 | 1692 | match self.ctx.shallow(&arrow.ret) { |
| 1572 | | − Ty::Fn(params, ret, eff, _) => { |
| 1693 | + Ty::Fn(params, ret, eff, caps) => { |
| 1694 | + self.evidence.plain_only.push(( |
| 1695 | + self.span(range), |
| 1696 | + PlainOnly::Ty(Ty::Fn(params.clone(), ret.clone(), eff.clone(), caps)), |
| 1697 | + "a function a call returns, applied in the same call", |
| 1698 | + )); |
| 1573 | 1699 | pending.push(std::mem::replace(&mut arrow.eff, Ty::RowEmpty)); |
| 1574 | 1700 | arrow.params = params; |
| 1575 | 1701 | arrow.filled = vec![false; arrow.params.len()]; |
| @@ -1732,12 +1858,18 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 1732 | 1858 | if complete { |
| 1733 | 1859 | pending.push(arrow.eff.clone()); |
| 1734 | 1860 | } |
| 1735 | | − for eff in &pending { |
| 1736 | | − self.perform(eff, range); |
| 1737 | | − } |
| 1861 | + self.perform_call(callee, (closure, own_tail, explicit), &pending, range); |
| 1738 | 1862 | if complete { |
| 1739 | 1863 | arrow.ret |
| 1740 | 1864 | } else { |
| 1865 | + // A partial application passes its callee's capabilities one |
| 1866 | + // by one (ADR 0066). |
| 1867 | + let span = self.span(range); |
| 1868 | + self.evidence.plain_only.push(( |
| 1869 | + span, |
| 1870 | + PlainOnly::Expr(callee), |
| 1871 | + "a function partially applied", |
| 1872 | + )); |
| 1741 | 1873 | let remaining: Vec<FnParam> = arrow |
| 1742 | 1874 | .params |
| 1743 | 1875 | .iter() |
| @@ -1756,6 +1888,55 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 1756 | 1888 | } |
| 1757 | 1889 | } |
| 1758 | 1890 | |
| 1891 | + /// Perform the rows a call completes (`pending`, its callee's own |
| 1892 | + /// first) and record what its callee's evidence is built from (ADR |
| 1893 | + /// 0066). |
| 1894 | + /// `how`: whether the callee is a closure, or a function whose own |
| 1895 | + /// row ends in an evidence variable, and a closure's `Bound` labels. |
| 1896 | + fn perform_call( |
| 1897 | + &mut self, |
| 1898 | + callee: ExprId, |
| 1899 | + how: (bool, bool, Vec<EvEntry>), |
| 1900 | + pending: &[Ty], |
| 1901 | + range: TextRange, |
| 1902 | + ) { |
| 1903 | + let (closure, own_tail, explicit) = how; |
| 1904 | + for (i, eff) in pending.iter().enumerate() { |
| 1905 | + let (late, tail) = self.perform(eff, range); |
| 1906 | + if i == 0 { |
| 1907 | + self.evidence.calls.push(CallRecord { |
| 1908 | + callee, |
| 1909 | + span: self.span(range), |
| 1910 | + closure, |
| 1911 | + own_tail, |
| 1912 | + explicit: explicit.clone(), |
| 1913 | + late, |
| 1914 | + tail, |
| 1915 | + }); |
| 1916 | + } |
| 1917 | + } |
| 1918 | + } |
| 1919 | + |
| 1920 | + /// Whether `def`'s own row ends in an evidence variable: it takes an |
| 1921 | + /// evidence parameter for that tail (ADR 0066). |
| 1922 | + fn own_tail_evidence(&self, def: DefId) -> bool { |
| 1923 | + let Some(scheme) = self.env.schemes.get(&def) else { |
| 1924 | + return false; |
| 1925 | + }; |
| 1926 | + let Ty::Fn(_, _, eff, _) = &scheme.ty else { |
| 1927 | + return false; |
| 1928 | + }; |
| 1929 | + let mut end = &**eff; |
| 1930 | + while let Ty::RowExtend(_, rest) = end { |
| 1931 | + end = rest; |
| 1932 | + } |
| 1933 | + match end { |
| 1934 | + Ty::Gen(i) => scheme.evidence.contains(i), |
| 1935 | + Ty::RowEvidence => true, |
| 1936 | + _ => false, |
| 1937 | + } |
| 1938 | + } |
| 1939 | + |
| 1759 | 1940 | /// A partial application checked against an arrow whose callers |
| 1760 | 1941 | /// supply capabilities: the callee's capabilities that arrow supplies |
| 1761 | 1942 | /// are received from its callers, as a lambda written in its place |
| @@ -2083,6 +2264,15 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 2083 | 2264 | } else { |
| 2084 | 2265 | Ty::row(inner_labels, tail) |
| 2085 | 2266 | }; |
| 2267 | + let entries = received |
| 2268 | + .iter() |
| 2269 | + .map(|((d, k), cap)| EvEntry { |
| 2270 | + effect: *d, |
| 2271 | + index: *k, |
| 2272 | + instance: cap.clone(), |
| 2273 | + }) |
| 2274 | + .collect(); |
| 2275 | + self.evidence.lambdas.push((e, effect.clone(), entries)); |
| 2086 | 2276 | let outer = std::mem::replace(&mut self.effect, inner); |
| 2087 | 2277 | self.scopes.push(outer); |
| 2088 | 2278 | let level = self.ctx.level(); |
| @@ -2190,6 +2380,13 @@ impl<'a, 'h> FnInfer<'a, 'h> { |
| 2190 | 2380 | // A capability it captured lives on this actor's stack. |
| 2191 | 2381 | let captured = args.iter().any(|a| matches!(a, CapArg::Inst(_))); |
| 2192 | 2382 | self.caps.values.insert(e, args); |
| 2383 | + // Its wrapper takes capabilities one by one (ADR 0066). |
| 2384 | + let span = self.span(range); |
| 2385 | + self.evidence.plain_only.push(( |
| 2386 | + span, |
| 2387 | + PlainOnly::Expr(e), |
| 2388 | + "a function name used as a value", |
| 2389 | + )); |
| 2193 | 2390 | match self.ctx.shallow(&ty) { |
| 2194 | 2391 | Ty::Fn(params, ret, eff, _) if captured => { |
| 2195 | 2392 | Ty::fn_(params, *ret, *eff, self.local_row()) |