Skip to content

Commit

Permalink
Fix desugar step for pizza operator
Browse files Browse the repository at this point in the history
  • Loading branch information
kdziamura committed Jun 22, 2023
1 parent a9f7961 commit 4bb7b84
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 14 deletions.
8 changes: 2 additions & 6 deletions crates/ast/src/lang/core/expr/expr_to_expr2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -665,14 +665,10 @@ pub fn expr_to_expr2<'a>(
ident,
} => canonicalize_lookup(env, scope, module_name, ident, region),

ParensAround(sub_expr) => expr_to_expr2(env, scope, sub_expr, region),

// Below this point, we shouln't see any of these nodes anymore because
// operator desugaring should have removed them!
bad_expr @ ParensAround(_) => {
panic!(
"A ParensAround did not get removed during operator desugaring somehow: {:#?}",
bad_expr
);
}
bad_expr @ SpaceBefore(_, _) => {
panic!(
"A SpaceBefore did not get removed during operator desugaring somehow: {:#?}",
Expand Down
11 changes: 5 additions & 6 deletions crates/compiler/can/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1403,14 +1403,13 @@ pub fn canonicalize_expr<'a>(

(answer, Output::default())
}
&ast::Expr::ParensAround(sub_expr) => {
let (loc_expr, output) = canonicalize_expr(env, var_store, scope, region, sub_expr);

(loc_expr.value, output)
}
// Below this point, we shouln't see any of these nodes anymore because
// operator desugaring should have removed them!
bad_expr @ ast::Expr::ParensAround(_) => {
internal_error!(
"A ParensAround did not get removed during operator desugaring somehow: {:#?}",
bad_expr
);
}
bad_expr @ ast::Expr::SpaceBefore(_, _) => {
internal_error!(
"A SpaceBefore did not get removed during operator desugaring somehow: {:#?}",
Expand Down
18 changes: 16 additions & 2 deletions crates/compiler/can/src/operator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ pub fn desugar_expr<'a>(arena: &'a Bump, loc_expr: &'a Loc<Expr<'a>>) -> &'a Loc

break builder_arg.closure;
}
SpaceBefore(expr, _) | SpaceAfter(expr, _) | ParensAround(expr) => {
SpaceBefore(expr, _) | SpaceAfter(expr, _) => {
current = *expr;
}
_ => break loc_arg,
Expand Down Expand Up @@ -382,7 +382,7 @@ pub fn desugar_expr<'a>(arena: &'a Bump, loc_expr: &'a Loc<Expr<'a>>) -> &'a Loc
region: loc_expr.region,
})
}
SpaceBefore(expr, _) | SpaceAfter(expr, _) | ParensAround(expr) => {
SpaceBefore(expr, _) | SpaceAfter(expr, _) => {
// Since we've already begun canonicalization, spaces and parens
// are no longer needed and should be dropped.
desugar_expr(
Expand All @@ -393,6 +393,20 @@ pub fn desugar_expr<'a>(arena: &'a Bump, loc_expr: &'a Loc<Expr<'a>>) -> &'a Loc
}),
)
}
ParensAround(expr) => {
let desugared = desugar_expr(
arena,
arena.alloc(Loc {
value: **expr,
region: loc_expr.region,
}),
);

arena.alloc(Loc {
value: ParensAround(&desugared.value),
region: loc_expr.region,
})
}
If(if_thens, final_else_branch) => {
// If does not get desugared into `when` so we can give more targeted error messages during type checking.
let desugared_final_else = &*arena.alloc(desugar_expr(arena, final_else_branch));
Expand Down
38 changes: 38 additions & 0 deletions crates/reporting/tests/test_reporting.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13841,4 +13841,42 @@ In roc, functions are always written as a lambda, like{}
Tip: It looks like it takes too many arguments. I'm seeing 1 extra.
"###
);

test_report!(
pizza_parens_right,
indoc!(
r#"
2 |> (Num.sub 3)
"#
),
@r###"
── TOO FEW ARGS ────────────────────────────────────────── /code/proj/Main.roc ─
The `sub` function expects 2 arguments, but it got only 1:
4│ 2 |> (Num.sub 3)
^^^^^^^
Roc does not allow functions to be partially applied. Use a closure to
make partial application explicit.
"###
);

test_report!(
pizza_parens_middle,
indoc!(
r#"
2 |> (Num.sub 3) |> Num.sub 3
"#
),
@r###"
── TOO FEW ARGS ────────────────────────────────────────── /code/proj/Main.roc ─
The `sub` function expects 2 arguments, but it got only 1:
4│ 2 |> (Num.sub 3) |> Num.sub 3
^^^^^^^
Roc does not allow functions to be partially applied. Use a closure to
make partial application explicit.
"###
);
}

0 comments on commit 4bb7b84

Please sign in to comment.