From 7ab2ac2e53f10b8509dc4551deb86c21363afa1b Mon Sep 17 00:00:00 2001 From: Anthony Bullard Date: Wed, 8 Jan 2025 14:45:13 -0600 Subject: [PATCH] Last of the feedback (the part I forgot to commit) --- crates/compiler/can/src/desugar.rs | 34 -------------- crates/compiler/fmt/src/expr.rs | 3 +- crates/compiler/parse/src/expr.rs | 73 +++++++++++++++++++++--------- crates/compiler/problem/src/can.rs | 5 +- 4 files changed, 57 insertions(+), 58 deletions(-) diff --git a/crates/compiler/can/src/desugar.rs b/crates/compiler/can/src/desugar.rs index b13b09a84b..0a487ceccc 100644 --- a/crates/compiler/can/src/desugar.rs +++ b/crates/compiler/can/src/desugar.rs @@ -1186,40 +1186,6 @@ pub fn desugar_expr<'a>( Expr::LowLevelTry(result_expr, ResultTryKind::KeywordPrefix), )) } - PncApply( - Loc { - value: - TrySuffix { - target: TryTarget::Result, - expr: fn_expr, - }, - region: fn_region, - }, - loc_args, - ) => { - let loc_fn = env.arena.alloc(Loc::at(*fn_region, **fn_expr)); - let function = desugar_expr(env, scope, loc_fn); - - let mut desugared_args = Vec::with_capacity_in(loc_args.len(), env.arena); - for loc_arg in &loc_args.items[..] { - desugared_args.push(desugar_expr(env, scope, loc_arg)); - } - - let args_region = Region::span_across( - &loc_args.items[0].region, - &loc_args.items[loc_args.items.len() - 1].region, - ); - - let result_expr = env.arena.alloc(Loc::at( - args_region, - Expr::Apply(function, desugared_args.into_bump_slice(), CalledVia::Try), - )); - - env.arena.alloc(Loc::at( - loc_expr.region, - Expr::LowLevelTry(result_expr, ResultTryKind::OperatorSuffix), - )) - } PncApply(loc_fn, loc_args) => { let mut desugared_args = Vec::with_capacity_in(loc_args.len(), env.arena); diff --git a/crates/compiler/fmt/src/expr.rs b/crates/compiler/fmt/src/expr.rs index e363a97e2f..519ceb131c 100644 --- a/crates/compiler/fmt/src/expr.rs +++ b/crates/compiler/fmt/src/expr.rs @@ -11,7 +11,7 @@ use crate::spaces::{ use crate::Buf; use bumpalo::collections::Vec; use bumpalo::Bump; -use roc_module::called_via::{self, BinOp, CalledVia, UnaryOp}; +use roc_module::called_via::{self, BinOp, UnaryOp}; use roc_parse::ast::{ AssignedField, Base, Collection, CommentOrNewline, Expr, ExtractSpaces, Pattern, Spaceable, Spaces, SpacesAfter, SpacesBefore, TryTarget, WhenBranch, @@ -107,6 +107,7 @@ fn format_expr_only( if buf.flags().parens_and_commas { fmt_pnc_apply(loc_expr, &Collection::with_items(loc_args), indent, buf); } else if !apply_needs_parens || loc_args.is_empty() { + println!("No need parens"); fmt_apply(loc_expr, loc_args, indent, buf); } else { fmt_parens(item, buf, indent); diff --git a/crates/compiler/parse/src/expr.rs b/crates/compiler/parse/src/expr.rs index 5333837326..b64f971aea 100644 --- a/crates/compiler/parse/src/expr.rs +++ b/crates/compiler/parse/src/expr.rs @@ -234,25 +234,38 @@ fn loc_term<'a>() -> impl Parser<'a, Loc>, EExpr<'a>> { zero_or_more(pnc_args()), ), |arena, - (expr, arg_locs_vec): ( + (expr, arg_locs_with_suffixes_vec): ( Loc>, - bumpalo::collections::Vec<'a, Loc>>>, + bumpalo::collections::Vec< + 'a, + ( + Loc>>, + Option>>, + ), + >, )| { let mut e = expr; let orig_region = e.region; - for args_loc in arg_locs_vec.iter() { + for (args_loc, maybe_suffixes) in arg_locs_with_suffixes_vec.iter() { + let value = if matches!( + e, + Loc { + value: Expr::Dbg, + .. + } + ) { + Expr::Apply(arena.alloc(e), args_loc.value.items, CalledVia::Space) + } else if let Some(suffixes) = maybe_suffixes { + apply_expr_access_chain( + arena, + Expr::PncApply(arena.alloc(e), args_loc.value), + suffixes.clone(), + ) + } else { + Expr::PncApply(arena.alloc(e), args_loc.value) + }; e = Loc { - value: if matches!( - e, - Loc { - value: Expr::Dbg, - .. - } - ) { - Expr::Apply(arena.alloc(e), args_loc.value.items, CalledVia::Space) - } else { - Expr::PncApply(arena.alloc(e), args_loc.value) - }, + value, region: Region::span_across(&orig_region, &args_loc.region), }; } @@ -262,9 +275,16 @@ fn loc_term<'a>() -> impl Parser<'a, Loc>, EExpr<'a>> { .trace("term") } -fn pnc_args<'a>() -> impl Parser<'a, Loc>>>, EExpr<'a>> { +fn pnc_args<'a>() -> impl Parser< + 'a, + ( + Loc>>>, + Option>>, + ), + EExpr<'a>, +> { |arena: &'a Bump, state: State<'a>, min_indent: u32| { - map_with_arena( + let args_then_suffixes = and( specialize_err( EExpr::InParens, loc(collection_trailing_sep_e( @@ -275,12 +295,23 @@ fn pnc_args<'a>() -> impl Parser<'a, Loc>>>, EEx Expr::SpaceBefore, )), ), - |arena: &'a Bump, loc_args_coll: Loc>>>| { + optional(record_field_access_chain()), + ); + map_with_arena( + args_then_suffixes, + |arena: &'a Bump, + (loc_args_coll, maybe_suffixes): ( + Loc>>>, + Option>>, + )| { let args = loc_args_coll.value.ptrify_items(arena); - Loc { - region: loc_args_coll.region, - value: args, - } + ( + Loc { + region: loc_args_coll.region, + value: args, + }, + maybe_suffixes, + ) }, ) .parse(arena, state, min_indent) diff --git a/crates/compiler/problem/src/can.rs b/crates/compiler/problem/src/can.rs index 861f80bad9..78dc072046 100644 --- a/crates/compiler/problem/src/can.rs +++ b/crates/compiler/problem/src/can.rs @@ -512,13 +512,14 @@ impl Problem { | Problem::ReturnAtEndOfFunction { region } | Problem::UnboundTypeVarsInAs(region) | Problem::UnsuffixedEffectfulRecordField(region) - | Problem::SuffixedPureRecordField(region) => Some(*region), + | Problem::SuffixedPureRecordField(region) + | Problem::StmtAfterExpr(region) => Some(*region), + Problem::RuntimeError(RuntimeError::CircularDef(cycle_entries)) | Problem::BadRecursion(cycle_entries) => { cycle_entries.first().map(|entry| entry.expr_region) } - Problem::StmtAfterExpr(region) => Some(*region), Problem::RuntimeError(RuntimeError::UnresolvedTypeVar) | Problem::RuntimeError(RuntimeError::ErroneousType) | Problem::RuntimeError(RuntimeError::NonExhaustivePattern)