| @@ -384,6 +384,22 @@ struct BodyLowerer<'c, 'a, 'd> { |
| 384 | 384 | pattern_bindings: Option<PatternBindings>, |
| 385 | 385 | } |
| 386 | 386 | |
| 387 | +/// A `_` among a call's arguments, with its index among the non-spread |
| 388 | +/// arguments. |
| 389 | +#[derive(Clone, Copy)] |
| 390 | +struct Hole { |
| 391 | + index: usize, |
| 392 | + range: TextRange, |
| 393 | +} |
| 394 | + |
| 395 | +/// What a call's arguments say about `_`. |
| 396 | +enum Placeholder { |
| 397 | + None, |
| 398 | + One(Hole), |
| 399 | + /// Several `_`, or one with a spread: reported. |
| 400 | + Invalid, |
| 401 | +} |
| 402 | + |
| 387 | 403 | #[derive(Default)] |
| 388 | 404 | struct PatternBindings { |
| 389 | 405 | /// Names bound so far, with their locals. |
| @@ -433,6 +449,7 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 433 | 449 | let local = self.body.locals.alloc(Local { |
| 434 | 450 | name: name.clone(), |
| 435 | 451 | span: range, |
| 452 | + synthetic: false, |
| 436 | 453 | }); |
| 437 | 454 | self.scopes |
| 438 | 455 | .last_mut() |
| @@ -441,6 +458,16 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 441 | 458 | local |
| 442 | 459 | } |
| 443 | 460 | |
| 461 | + /// A local introduced by desugaring: in no scope, so no user name can |
| 462 | + /// refer to it. |
| 463 | + fn synthetic_local(&mut self, name: &str, range: TextRange) -> LocalId { |
| 464 | + self.body.locals.alloc(Local { |
| 465 | + name: name.to_owned(), |
| 466 | + span: range, |
| 467 | + synthetic: true, |
| 468 | + }) |
| 469 | + } |
| 470 | + |
| 444 | 471 | fn lower_fn_body(&mut self, params: Option<ast::ParamList>, block: &ast::Block) { |
| 445 | 472 | if let Some(params) = params { |
| 446 | 473 | for p in params.params() { |
| @@ -491,18 +518,7 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 491 | 518 | ast::Expr::Block(b) => self.lower_block(b), |
| 492 | 519 | ast::Expr::BinExpr(b) => self.lower_binary(b, range), |
| 493 | 520 | ast::Expr::PrefixExpr(p) => self.lower_prefix(p, range), |
| 494 | | − ast::Expr::CallExpr(c) => { |
| 495 | | − let callee = self.lower_opt_expr(c.callee(), range); |
| 496 | | − let (args, spread) = self.lower_args(c.args()); |
| 497 | | − self.alloc_expr( |
| 498 | | − Expr::Call { |
| 499 | | − callee, |
| 500 | | − args, |
| 501 | | − spread, |
| 502 | | − }, |
| 503 | | − range, |
| 504 | | − ) |
| 505 | | − } |
| 521 | + ast::Expr::CallExpr(c) => self.lower_call(c, range), |
| 506 | 522 | ast::Expr::IfExpr(i) => { |
| 507 | 523 | let cond = self.lower_opt_expr(i.condition(), range); |
| 508 | 524 | let then_branch = match i.then_branch() { |
| @@ -567,6 +583,14 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 567 | 583 | self.alloc_expr(Expr::Region { body }, range) |
| 568 | 584 | } |
| 569 | 585 | ast::Expr::HtmlElement(el) => self.lower_html(el), |
| 586 | + ast::Expr::PlaceholderExpr(_) => { |
| 587 | + self.cx.error( |
| 588 | + codes::MISPLACED_PLACEHOLDER, |
| 589 | + "`_` can only stand for one of a call's arguments, as in `f(a, _)` or `x |> f(a, _)`", |
| 590 | + range, |
| 591 | + ); |
| 592 | + self.missing(range) |
| 593 | + } |
| 570 | 594 | } |
| 571 | 595 | } |
| 572 | 596 | |
| @@ -667,13 +691,115 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 667 | 691 | } |
| 668 | 692 | } |
| 669 | 693 | |
| 670 | | − fn lower_args(&mut self, args: Vec<ast::ArgOrSpread>) -> (Vec<Arg>, Option<ExprId>) { |
| 694 | + /// A call. With a `_` among its arguments it is a capture (ADR 0023): |
| 695 | + /// `f(a, _)` is `fn(v) { f(a, v) }`, so the callee and the other |
| 696 | + /// arguments are evaluated at each call of the function, as in Gleam. |
| 697 | + fn lower_call(&mut self, c: &ast::CallExpr, range: TextRange) -> ExprId { |
| 698 | + let args = c.args(); |
| 699 | + let hole = match self.placeholder(&args) { |
| 700 | + Placeholder::One(hole) => hole, |
| 701 | + Placeholder::Invalid => return self.missing(range), |
| 702 | + Placeholder::None => { |
| 703 | + let callee = self.lower_opt_expr(c.callee(), range); |
| 704 | + let (args, spread) = self.lower_args(args, None); |
| 705 | + return self.alloc_expr( |
| 706 | + Expr::Call { |
| 707 | + callee, |
| 708 | + args, |
| 709 | + spread, |
| 710 | + }, |
| 711 | + range, |
| 712 | + ); |
| 713 | + } |
| 714 | + }; |
| 715 | + self.with_scope(|this| { |
| 716 | + let param = this.synthetic_local("_", hole.range); |
| 717 | + let pat = this.alloc_pat(Pat::Bind(param), hole.range); |
| 718 | + let value = this.alloc_expr(Expr::Local(param), hole.range); |
| 719 | + let callee = this.lower_opt_expr(c.callee(), range); |
| 720 | + let (args, spread) = this.lower_args(args, Some((hole.index, value))); |
| 721 | + let body = this.alloc_expr( |
| 722 | + Expr::Call { |
| 723 | + callee, |
| 724 | + args, |
| 725 | + spread, |
| 726 | + }, |
| 727 | + range, |
| 728 | + ); |
| 729 | + this.alloc_expr( |
| 730 | + Expr::Lambda { |
| 731 | + params: vec![(pat, None)], |
| 732 | + ret: None, |
| 733 | + body, |
| 734 | + }, |
| 735 | + range, |
| 736 | + ) |
| 737 | + }) |
| 738 | + } |
| 739 | + |
| 740 | + /// The `_` among a call's arguments. Several, or one with a spread, |
| 741 | + /// are reported; the call then lowers to `Missing`, so no type error |
| 742 | + /// follows from a guess. |
| 743 | + fn placeholder(&mut self, args: &[ast::ArgOrSpread]) -> Placeholder { |
| 744 | + let mut holes = Vec::new(); |
| 745 | + let mut index = 0; |
| 746 | + let mut spread = false; |
| 747 | + for arg in args { |
| 748 | + match arg { |
| 749 | + ast::ArgOrSpread::Arg(a) => { |
| 750 | + if let Some(ast::Expr::PlaceholderExpr(p)) = a.expr() { |
| 751 | + holes.push(Hole { |
| 752 | + index, |
| 753 | + range: p.syntax().text_range(), |
| 754 | + }); |
| 755 | + } |
| 756 | + index += 1; |
| 757 | + } |
| 758 | + ast::ArgOrSpread::Spread(_) => spread = true, |
| 759 | + } |
| 760 | + } |
| 761 | + let Some(&first) = holes.first() else { |
| 762 | + return Placeholder::None; |
| 763 | + }; |
| 764 | + if let Some(extra) = holes.get(1) { |
| 765 | + self.cx.error( |
| 766 | + codes::MISPLACED_PLACEHOLDER, |
| 767 | + "a call can have only one `_` placeholder; write a `fn` to take more parameters", |
| 768 | + extra.range, |
| 769 | + ); |
| 770 | + return Placeholder::Invalid; |
| 771 | + } |
| 772 | + if spread { |
| 773 | + self.cx.error( |
| 774 | + codes::MISPLACED_PLACEHOLDER, |
| 775 | + "a `_` placeholder cannot be combined with a `..` spread", |
| 776 | + first.range, |
| 777 | + ); |
| 778 | + return Placeholder::Invalid; |
| 779 | + } |
| 780 | + Placeholder::One(first) |
| 781 | + } |
| 782 | + |
| 783 | + /// Lower a call's arguments; `fill` gives the expression standing for |
| 784 | + /// the `_` at that index (among the non-spread arguments). Any other |
| 785 | + /// `_` was reported by [`Self::placeholder`]. |
| 786 | + fn lower_args( |
| 787 | + &mut self, |
| 788 | + args: Vec<ast::ArgOrSpread>, |
| 789 | + fill: Option<(usize, ExprId)>, |
| 790 | + ) -> (Vec<Arg>, Option<ExprId>) { |
| 671 | 791 | let mut lowered = Vec::new(); |
| 672 | 792 | let mut spread = None; |
| 673 | 793 | for arg in args { |
| 674 | 794 | match arg { |
| 675 | 795 | ast::ArgOrSpread::Arg(a) => { |
| 676 | | − let expr = self.lower_opt_expr(a.expr(), a.syntax().text_range()); |
| 796 | + let expr = match (a.expr(), fill) { |
| 797 | + (_, Some((i, value))) if i == lowered.len() => value, |
| 798 | + (Some(ast::Expr::PlaceholderExpr(p)), _) => { |
| 799 | + self.missing(p.syntax().text_range()) |
| 800 | + } |
| 801 | + (expr, _) => self.lower_opt_expr(expr, a.syntax().text_range()), |
| 802 | + }; |
| 677 | 803 | lowered.push(Arg { |
| 678 | 804 | label: a.label().map(|l| l.text()), |
| 679 | 805 | expr, |
| @@ -687,6 +813,18 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 687 | 813 | (lowered, spread) |
| 688 | 814 | } |
| 689 | 815 | |
| 816 | + /// Whether evaluating `e` has no effect and cannot fail, so it may move |
| 817 | + /// before or after other expressions. |
| 818 | + fn is_inert(&self, e: ExprId) -> bool { |
| 819 | + match &self.body.exprs[e] { |
| 820 | + Expr::Missing | Expr::Lit(_) | Expr::Local(_) | Expr::Def(_) | Expr::Lambda { .. } => { |
| 821 | + true |
| 822 | + } |
| 823 | + Expr::Field { base, .. } | Expr::TupleIndex { base, .. } => self.is_inert(*base), |
| 824 | + _ => false, |
| 825 | + } |
| 826 | + } |
| 827 | + |
| 690 | 828 | fn lower_binary(&mut self, b: &ast::BinExpr, range: TextRange) -> ExprId { |
| 691 | 829 | let Some(op) = b.op() else { |
| 692 | 830 | return self.missing(range); |
| @@ -716,38 +854,70 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 716 | 854 | self.alloc_expr(Expr::Binary { op, lhs, rhs }, range) |
| 717 | 855 | } |
| 718 | 856 | |
| 719 | | − /// `x |> f(a)` is `f(x, a)`; `x |> f` is `f(x)` (Gleam semantics). |
| 857 | + /// `x |> f(a)` is `f(x, a)`; `x |> f(a, _)` is `f(a, x)`; `x |> f` is |
| 858 | + /// `f(x)` (Gleam semantics, ADR 0023). `x` is evaluated first: when |
| 859 | + /// the callee or an argument before `x`'s place could have an effect, |
| 860 | + /// `x` is bound to a local beforehand. |
| 720 | 861 | fn lower_pipe(&mut self, lhs: ExprId, rhs: Option<ast::Expr>, range: TextRange) -> ExprId { |
| 721 | | − let lhs_arg = Arg { |
| 722 | | − label: None, |
| 723 | | − expr: lhs, |
| 724 | | − }; |
| 725 | | − match rhs { |
| 862 | + let first = |expr| Arg { label: None, expr }; |
| 863 | + let (callee, mut args, spread, at) = match rhs { |
| 726 | 864 | Some(ast::Expr::CallExpr(call)) => { |
| 865 | + let call_args = call.args(); |
| 866 | + let hole = self.placeholder(&call_args); |
| 727 | 867 | let callee = self.lower_opt_expr(call.callee(), range); |
| 728 | | − let (rest, spread) = self.lower_args(call.args()); |
| 729 | | − let args = std::iter::once(lhs_arg).chain(rest).collect(); |
| 730 | | − self.alloc_expr( |
| 731 | | − Expr::Call { |
| 732 | | − callee, |
| 733 | | − args, |
| 734 | | − spread, |
| 735 | | − }, |
| 736 | | − range, |
| 737 | | − ) |
| 868 | + match hole { |
| 869 | + Placeholder::Invalid => return self.missing(range), |
| 870 | + Placeholder::One(hole) => { |
| 871 | + let (args, spread) = self.lower_args(call_args, Some((hole.index, lhs))); |
| 872 | + (callee, args, spread, hole.index) |
| 873 | + } |
| 874 | + Placeholder::None => { |
| 875 | + let (rest, spread) = self.lower_args(call_args, None); |
| 876 | + let args = std::iter::once(first(lhs)).chain(rest).collect(); |
| 877 | + (callee, args, spread, 0) |
| 878 | + } |
| 879 | + } |
| 738 | 880 | } |
| 739 | 881 | other => { |
| 740 | 882 | let callee = self.lower_opt_expr(other, range); |
| 741 | | − self.alloc_expr( |
| 742 | | − Expr::Call { |
| 743 | | − callee, |
| 744 | | − args: vec![lhs_arg], |
| 745 | | − spread: None, |
| 746 | | − }, |
| 747 | | − range, |
| 748 | | − ) |
| 883 | + (callee, vec![first(lhs)], None, 0) |
| 749 | 884 | } |
| 885 | + }; |
| 886 | + let evaluated_before = std::iter::once(callee).chain(args[..at].iter().map(|a| a.expr)); |
| 887 | + let bind = !self.is_inert(lhs) && !evaluated_before.into_iter().all(|e| self.is_inert(e)); |
| 888 | + if !bind { |
| 889 | + return self.alloc_expr( |
| 890 | + Expr::Call { |
| 891 | + callee, |
| 892 | + args, |
| 893 | + spread, |
| 894 | + }, |
| 895 | + range, |
| 896 | + ); |
| 750 | 897 | } |
| 898 | + let lhs_range = self.body.expr_span(lhs); |
| 899 | + let local = self.synthetic_local("_pipe", lhs_range); |
| 900 | + let pat = self.alloc_pat(Pat::Bind(local), lhs_range); |
| 901 | + args[at].expr = self.alloc_expr(Expr::Local(local), lhs_range); |
| 902 | + let call = self.alloc_expr( |
| 903 | + Expr::Call { |
| 904 | + callee, |
| 905 | + args, |
| 906 | + spread, |
| 907 | + }, |
| 908 | + range, |
| 909 | + ); |
| 910 | + self.alloc_expr( |
| 911 | + Expr::Block { |
| 912 | + stmts: vec![Stmt::Let { |
| 913 | + pat, |
| 914 | + ty: None, |
| 915 | + init: lhs, |
| 916 | + }], |
| 917 | + tail: Some(call), |
| 918 | + }, |
| 919 | + range, |
| 920 | + ) |
| 751 | 921 | } |
| 752 | 922 | |
| 753 | 923 | fn lower_prefix(&mut self, p: &ast::PrefixExpr, range: TextRange) -> ExprId { |
| @@ -905,8 +1075,16 @@ impl<'c, 'a, 'd> BodyLowerer<'c, 'a, 'd> { |
| 905 | 1075 | let call = u.call(); |
| 906 | 1076 | let (callee, mut args, spread) = match &call { |
| 907 | 1077 | Some(ast::Expr::CallExpr(c)) => { |
| 1078 | + let args = c.args(); |
| 1079 | + if let Placeholder::One(hole) = self.placeholder(&args) { |
| 1080 | + self.cx.error( |
| 1081 | + codes::MISPLACED_PLACEHOLDER, |
| 1082 | + "`use` passes its callback as the last argument and cannot fill a `_`", |
| 1083 | + hole.range, |
| 1084 | + ); |
| 1085 | + } |
| 908 | 1086 | let callee = self.lower_opt_expr(c.callee(), use_range); |
| 909 | | − let (args, spread) = self.lower_args(c.args()); |
| 1087 | + let (args, spread) = self.lower_args(args, None); |
| 910 | 1088 | (callee, args, spread) |
| 911 | 1089 | } |
| 912 | 1090 | other => ( |