diff --git a/crates/compiler/can/src/suffixed.rs b/crates/compiler/can/src/suffixed.rs index bcc44a6278..eabd726967 100644 --- a/crates/compiler/can/src/suffixed.rs +++ b/crates/compiler/can/src/suffixed.rs @@ -129,46 +129,7 @@ pub fn unwrap_suffixed_expression<'a>( internal_error!("BinOps should have been desugared in desugar_expr"); } - Expr::LowLevelDbg(dbg_src, arg, rest) => { - if is_expr_suffixed(&arg.value) { - // we cannot unwrap a suffixed expression within dbg - // e.g. dbg (foo! "bar") - return Err(EUnwrapped::Malformed); - } - - match unwrap_suffixed_expression(arena, rest, maybe_def_pat) { - Ok(unwrapped_expr) => { - let new_dbg = arena.alloc(Loc::at( - loc_expr.region, - LowLevelDbg(dbg_src, arg, unwrapped_expr), - )); - return Ok(new_dbg); - } - Err(EUnwrapped::UnwrappedDefExpr(unwrapped_expr)) => { - let new_dbg = arena.alloc(Loc::at( - loc_expr.region, - LowLevelDbg(dbg_src, arg, unwrapped_expr), - )); - Err(EUnwrapped::UnwrappedDefExpr(new_dbg)) - } - Err(EUnwrapped::UnwrappedSubExpr { - sub_arg: unwrapped_expr, - sub_pat, - sub_new, - }) => { - let new_dbg = arena.alloc(Loc::at( - loc_expr.region, - LowLevelDbg(dbg_src, arg, unwrapped_expr), - )); - Err(EUnwrapped::UnwrappedSubExpr { - sub_arg: new_dbg, - sub_pat, - sub_new, - }) - } - Err(EUnwrapped::Malformed) => Err(EUnwrapped::Malformed), - } - } + Expr::LowLevelDbg(..) => unwrap_low_level_dbg(arena, loc_expr, maybe_def_pat), Expr::Expect(condition, continuation) => { if is_expr_suffixed(&condition.value) { @@ -767,6 +728,87 @@ pub fn unwrap_suffixed_expression_defs_help<'a>( } } +fn unwrap_low_level_dbg<'a>( + arena: &'a Bump, + loc_expr: &'a Loc>, + maybe_def_pat: Option<&'a Loc>>, +) -> Result<&'a Loc>, EUnwrapped<'a>> { + match loc_expr.value { + LowLevelDbg(dbg_src, arg, rest) => { + if is_expr_suffixed(&arg.value) { + return match unwrap_suffixed_expression(arena, arg, maybe_def_pat) { + Ok(unwrapped_expr) => { + let new_dbg = arena.alloc(Loc::at( + loc_expr.region, + LowLevelDbg(dbg_src, unwrapped_expr, rest), + )); + + unwrap_low_level_dbg(arena, new_dbg, maybe_def_pat) + } + Err(EUnwrapped::UnwrappedSubExpr { + sub_arg, + sub_pat, + sub_new, + }) => { + let new_dbg = arena.alloc(Loc::at( + loc_expr.region, + LowLevelDbg(dbg_src, sub_new, rest), + )); + + unwrap_suffixed_expression( + arena, + apply_task_await(arena, new_dbg.region, sub_arg, sub_pat, new_dbg), + maybe_def_pat, + ) + } + Err(EUnwrapped::UnwrappedDefExpr(..)) => { + internal_error!( + "unreachable, arg of LowLevelDbg should generate UnwrappedSubExpr instead" + ); + } + Err(EUnwrapped::Malformed) => Err(EUnwrapped::Malformed), + }; + } + + match unwrap_suffixed_expression(arena, rest, maybe_def_pat) { + Ok(unwrapped_expr) => { + let new_dbg = arena.alloc(Loc::at( + loc_expr.region, + LowLevelDbg(dbg_src, arg, unwrapped_expr), + )); + Ok(&*new_dbg) + } + Err(EUnwrapped::UnwrappedDefExpr(unwrapped_expr)) => { + let new_dbg = arena.alloc(Loc::at( + loc_expr.region, + LowLevelDbg(dbg_src, arg, unwrapped_expr), + )); + Err(EUnwrapped::UnwrappedDefExpr(new_dbg)) + } + Err(EUnwrapped::UnwrappedSubExpr { + sub_arg: unwrapped_expr, + sub_pat, + sub_new, + }) => { + let new_dbg = arena.alloc(Loc::at( + loc_expr.region, + LowLevelDbg(dbg_src, arg, unwrapped_expr), + )); + Err(EUnwrapped::UnwrappedSubExpr { + sub_arg: new_dbg, + sub_pat, + sub_new, + }) + } + Err(EUnwrapped::Malformed) => Err(EUnwrapped::Malformed), + } + } + _ => internal_error!( + "unreachable, expected a LowLevelDbg node to be passed into unwrap_low_level_dbg" + ), + } +} + /// Helper for `Task.await (loc_arg) \loc_pat -> loc_new` pub fn apply_task_await<'a>( arena: &'a Bump,