code cleanup

This commit is contained in:
Kiryl Dziamura 2024-07-22 20:04:43 +02:00
parent 0086a531a2
commit 7aa31a1639
No known key found for this signature in database
GPG key ID: FB539501A4561ACF
2 changed files with 12 additions and 10 deletions

View file

@ -39,7 +39,7 @@ pub enum EUnwrapped<'a> {
/// Suffixed sub expression /// Suffixed sub expression
/// e.g. x = first! (second! 42) /// e.g. x = first! (second! 42)
/// In this example, the second unwrap (after unwrapping the top level `first!`) will produce /// In this example, the second unwrap (after unwrapping the top level `first!`) will produce
/// `UnwrappedSubExpr<{ sub_arg: second 42, sub_pat: #!a0, sub_new: #!a0 }>` /// `UnwrappedSubExpr<{ sub_arg: second 42, sub_pat: #!0_arg, sub_new: #!0_arg }>`
UnwrappedSubExpr { UnwrappedSubExpr {
/// the unwrapped expression argument for Task.await /// the unwrapped expression argument for Task.await
sub_arg: &'a Loc<Expr<'a>>, sub_arg: &'a Loc<Expr<'a>>,
@ -69,7 +69,7 @@ fn init_unwrapped_err<'a>(
None => { None => {
// Provide an intermediate answer expression and pattern when unwrapping a // Provide an intermediate answer expression and pattern when unwrapping a
// (sub) expression. // (sub) expression.
// e.g. `x = foo (bar!)` unwraps to `x = Task.await (bar) \#!a0 -> foo #!a0` // e.g. `x = foo (bar!)` unwraps to `x = Task.await (bar) \#!0_arg -> foo #!0_arg`
let ident = arena.alloc(format!("{}_arg", next_unique_suffixed_ident())); let ident = arena.alloc(format!("{}_arg", next_unique_suffixed_ident()));
let sub_new = arena.alloc(Loc::at( let sub_new = arena.alloc(Loc::at(
unwrapped_expr.region, unwrapped_expr.region,
@ -861,7 +861,7 @@ pub fn apply_task_await<'a>(
// \loc_pat -> loc_cont // \loc_pat -> loc_cont
use roc_parse::ast::*; use roc_parse::ast::*;
// #!a0 // #!0_expr or #!0_stmt
let new_ident = next_unique_suffixed_ident(); let new_ident = next_unique_suffixed_ident();
let new_ident = match loc_pat.value { let new_ident = match loc_pat.value {
Pattern::Underscore("#!stmt") => format!("{}_stmt", new_ident), Pattern::Underscore("#!stmt") => format!("{}_stmt", new_ident),
@ -930,7 +930,7 @@ pub fn apply_task_await<'a>(
Defs(arena.alloc(defs), new_var), Defs(arena.alloc(defs), new_var),
)) ))
} }
_ => { None => {
// loc_pat = loc_expr! // loc_pat = loc_expr!
// loc_cont // loc_cont
@ -940,8 +940,10 @@ pub fn apply_task_await<'a>(
} }
}; };
// If the pattern and the new are matching answers then we don't need to unwrap anything // If the last expression is suffixed - don't await
// e.g. `Task.await foo \#!a1 -> Task.ok #!a1` is the same as `foo` // e.g.
// \x -> x!
// \x -> x
if is_matching_intermediate_answer(loc_pat, loc_cont) { if is_matching_intermediate_answer(loc_pat, loc_cont) {
return task_await_first_arg; return task_await_first_arg;
} }

View file

@ -128,7 +128,7 @@ mod suffixed_tests {
* Example with a parens suffixed sub-expression * Example with a parens suffixed sub-expression
* in the function part of an Apply. * in the function part of an Apply.
* *
* Note how the parens unwraps into an intermediate answer #!a0 instead of * Note how the parens unwraps into an intermediate answer #!0_arg instead of
* unwrapping the def `do`. * unwrapping the def `do`.
* *
*/ */
@ -162,7 +162,7 @@ mod suffixed_tests {
/** /**
* Example with a multiple suffixed Var * Example with a multiple suffixed Var
* *
* Note it unwraps into an intermediate answer `#!a0` * Note it unwraps into an intermediate answer `#!0_arg`
* *
*/ */
#[test] #[test]
@ -570,10 +570,10 @@ mod test_suffixed_helpers {
#[test] #[test]
fn test_matching_answer() { fn test_matching_answer() {
let loc_pat = Loc::at_zero(Pattern::Identifier { ident: "#!a0" }); let loc_pat = Loc::at_zero(Pattern::Identifier { ident: "#!0_arg" });
let loc_new = Loc::at_zero(Expr::Var { let loc_new = Loc::at_zero(Expr::Var {
module_name: "", module_name: "",
ident: "#!a0", ident: "#!0_arg",
}); });
std::assert!(is_matching_intermediate_answer(&loc_pat, &loc_new)); std::assert!(is_matching_intermediate_answer(&loc_pat, &loc_new));