From 4bb7b84db0a0c2404d22eb0ed7f9e8ae3e9477a0 Mon Sep 17 00:00:00 2001 From: Kiryl Dziamura Date: Fri, 23 Jun 2023 00:56:19 +0200 Subject: [PATCH] Fix desugar step for pizza operator --- .../ast/src/lang/core/expr/expr_to_expr2.rs | 8 +--- crates/compiler/can/src/expr.rs | 11 +++--- crates/compiler/can/src/operator.rs | 18 ++++++++- crates/reporting/tests/test_reporting.rs | 38 +++++++++++++++++++ 4 files changed, 61 insertions(+), 14 deletions(-) diff --git a/crates/ast/src/lang/core/expr/expr_to_expr2.rs b/crates/ast/src/lang/core/expr/expr_to_expr2.rs index cdbc12a054f..c76375d478c 100644 --- a/crates/ast/src/lang/core/expr/expr_to_expr2.rs +++ b/crates/ast/src/lang/core/expr/expr_to_expr2.rs @@ -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: {:#?}", diff --git a/crates/compiler/can/src/expr.rs b/crates/compiler/can/src/expr.rs index 62ee13504ba..0beecfd7004 100644 --- a/crates/compiler/can/src/expr.rs +++ b/crates/compiler/can/src/expr.rs @@ -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: {:#?}", diff --git a/crates/compiler/can/src/operator.rs b/crates/compiler/can/src/operator.rs index 8db8f8ff76c..0efd582a9d0 100644 --- a/crates/compiler/can/src/operator.rs +++ b/crates/compiler/can/src/operator.rs @@ -288,7 +288,7 @@ pub fn desugar_expr<'a>(arena: &'a Bump, loc_expr: &'a Loc>) -> &'a Loc break builder_arg.closure; } - SpaceBefore(expr, _) | SpaceAfter(expr, _) | ParensAround(expr) => { + SpaceBefore(expr, _) | SpaceAfter(expr, _) => { current = *expr; } _ => break loc_arg, @@ -382,7 +382,7 @@ pub fn desugar_expr<'a>(arena: &'a Bump, loc_expr: &'a Loc>) -> &'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( @@ -393,6 +393,20 @@ pub fn desugar_expr<'a>(arena: &'a Bump, loc_expr: &'a Loc>) -> &'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)); diff --git a/crates/reporting/tests/test_reporting.rs b/crates/reporting/tests/test_reporting.rs index 4d51d7c1907..574dba08661 100644 --- a/crates/reporting/tests/test_reporting.rs +++ b/crates/reporting/tests/test_reporting.rs @@ -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. + "### + ); }