Skip to content

Commit

Permalink
code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
kdziamura committed Jul 23, 2024
1 parent 0086a53 commit 7aa31a1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 10 deletions.
14 changes: 8 additions & 6 deletions crates/compiler/can/src/suffixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub enum EUnwrapped<'a> {
/// Suffixed sub expression
/// e.g. x = first! (second! 42)
/// 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 {
/// the unwrapped expression argument for Task.await
sub_arg: &'a Loc<Expr<'a>>,
Expand Down Expand Up @@ -69,7 +69,7 @@ fn init_unwrapped_err<'a>(
None => {
// Provide an intermediate answer expression and pattern when unwrapping a
// (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 sub_new = arena.alloc(Loc::at(
unwrapped_expr.region,
Expand Down Expand Up @@ -861,7 +861,7 @@ pub fn apply_task_await<'a>(
// \loc_pat -> loc_cont
use roc_parse::ast::*;

// #!a0
// #!0_expr or #!0_stmt
let new_ident = next_unique_suffixed_ident();
let new_ident = match loc_pat.value {
Pattern::Underscore("#!stmt") => format!("{}_stmt", new_ident),
Expand Down Expand Up @@ -930,7 +930,7 @@ pub fn apply_task_await<'a>(
Defs(arena.alloc(defs), new_var),
))
}
_ => {
None => {
// loc_pat = loc_expr!
// loc_cont

Expand All @@ -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
// e.g. `Task.await foo \#!a1 -> Task.ok #!a1` is the same as `foo`
// If the last expression is suffixed - don't await
// e.g.
// \x -> x!
// \x -> x
if is_matching_intermediate_answer(loc_pat, loc_cont) {
return task_await_first_arg;
}
Expand Down
8 changes: 4 additions & 4 deletions crates/compiler/can/tests/test_suffixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ mod suffixed_tests {
* Example with a parens suffixed sub-expression
* 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`.
*
*/
Expand Down Expand Up @@ -162,7 +162,7 @@ mod suffixed_tests {
/**
* Example with a multiple suffixed Var
*
* Note it unwraps into an intermediate answer `#!a0`
* Note it unwraps into an intermediate answer `#!0_arg`
*
*/
#[test]
Expand Down Expand Up @@ -570,10 +570,10 @@ mod test_suffixed_helpers {

#[test]
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 {
module_name: "",
ident: "#!a0",
ident: "#!0_arg",
});

std::assert!(is_matching_intermediate_answer(&loc_pat, &loc_new));
Expand Down

0 comments on commit 7aa31a1

Please sign in to comment.