mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-27 13:59:08 +00:00
WIP try simplification
This commit is contained in:
parent
85b76a14b4
commit
8b9e08bd76
13 changed files with 734 additions and 551 deletions
|
@ -43,22 +43,45 @@ fn next_suffixed_answer_pattern(arena: &Bump) -> (Expr, Pattern) {
|
|||
|
||||
#[derive(Debug)]
|
||||
pub enum EUnwrapped<'a> {
|
||||
/// The expression was unwrapped, consumes the def and following expression
|
||||
/// e.g. `x = foo!` unwrapped `Task.await (foo) \x -> ...`
|
||||
UnwrappedExpr(&'a Loc<Expr<'a>>),
|
||||
UnwrappedDefExpr(&'a Loc<Expr<'a>>),
|
||||
|
||||
/// The expression had a (sub) expression unwrapped, doesn't affect the def or following expression
|
||||
/// e.g. `x = bar! (foo!)` unwrapped `x = Task.await (foo) \#answer1 -> bar! #answer1`
|
||||
UnwrappedSubExpr {
|
||||
/// this is the unwrapped suffixed which will be applied to the Task.await
|
||||
arg: &'a Loc<Expr<'a>>,
|
||||
/// the unwrapped expression argument for Task.await
|
||||
sub_arg: &'a Loc<Expr<'a>>,
|
||||
|
||||
/// this pattern will be used in the closure
|
||||
pat: Loc<Pattern<'a>>,
|
||||
/// the pattern for the closure
|
||||
sub_pat: &'a Loc<Pattern<'a>>,
|
||||
|
||||
/// this expression will replace the suffixed in the parent
|
||||
new: &'a Loc<Expr<'a>>,
|
||||
/// the expression to replace the unwrapped
|
||||
sub_new: &'a Loc<Expr<'a>>,
|
||||
},
|
||||
|
||||
Malformed,
|
||||
}
|
||||
|
||||
fn init_unwrapped_err<'a>(
|
||||
arena: &'a Bump,
|
||||
maybe_def_pat: Option<&'a Loc<Pattern<'a>>>,
|
||||
unwrapped_expr: &'a Loc<Expr<'a>>,
|
||||
) -> Result<&'a Loc<Expr<'a>>, EUnwrapped<'a>> {
|
||||
match maybe_def_pat {
|
||||
Some(..) => {
|
||||
// we have a def pattern, so no need to generate a new pattern
|
||||
// as this should only be created in the first call from a def
|
||||
Err(EUnwrapped::UnwrappedDefExpr(unwrapped_expr))
|
||||
}
|
||||
None => {
|
||||
let (answer_var, answer_pat) = next_suffixed_answer_pattern(arena);
|
||||
let sub_new = arena.alloc(Loc::at(unwrapped_expr.region, answer_var));
|
||||
let sub_pat = arena.alloc(Loc::at(unwrapped_expr.region, answer_pat));
|
||||
|
||||
Err(EUnwrapped::UnwrappedSubExpr {
|
||||
sub_arg: unwrapped_expr,
|
||||
sub_pat,
|
||||
sub_new,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Descend through the AST and unwrap each suffixed expression
|
||||
|
@ -69,10 +92,16 @@ pub fn unwrap_suffixed_expression<'a>(
|
|||
arena: &'a Bump,
|
||||
loc_expr: &'a Loc<Expr<'a>>,
|
||||
|
||||
// was this function called from e.g. ValueDef::Body
|
||||
// we need this to handle the case where we cannot unwrap the expression
|
||||
called_from_def: bool,
|
||||
// passed in from the def, first may have a pattern like "x = foo!" to unwrap
|
||||
//
|
||||
// note be careful with calls to this function
|
||||
// only those from the defs helper should pass a `Some`
|
||||
// only those to helpers should forward the value
|
||||
// all other calls to sub-expressions should pass `None`
|
||||
maybe_def_pat: Option<&'a Loc<Pattern<'a>>>,
|
||||
) -> Result<&'a Loc<Expr<'a>>, EUnwrapped<'a>> {
|
||||
dbg!("unwrap_suffixed_expression", &loc_expr);
|
||||
|
||||
match loc_expr.value {
|
||||
Expr::Var { suffixed, .. } if suffixed == 0 => Ok(loc_expr),
|
||||
|
||||
|
@ -81,69 +110,34 @@ pub fn unwrap_suffixed_expression<'a>(
|
|||
ident,
|
||||
suffixed,
|
||||
} if suffixed > 0 => {
|
||||
if suffixed > 1 {
|
||||
/*
|
||||
## Example with multiple suffix
|
||||
foo!!
|
||||
bar
|
||||
let unwrapped_var = arena.alloc(Loc::at(
|
||||
loc_expr.region,
|
||||
Expr::Var {
|
||||
module_name,
|
||||
ident,
|
||||
suffixed: suffixed.saturating_sub(1),
|
||||
},
|
||||
));
|
||||
|
||||
Task.await (foo) \#!a1 ->
|
||||
(#!a1)!
|
||||
bar
|
||||
|
||||
Task.await (foo) \#!a1 ->
|
||||
Task.await #!a1 \{} -> bar
|
||||
*/
|
||||
|
||||
let (mut answer_var, answer_pat) = next_suffixed_answer_pattern(arena);
|
||||
|
||||
let arg = arena.alloc(Loc::at(
|
||||
loc_expr.region,
|
||||
Var {
|
||||
module_name,
|
||||
ident,
|
||||
suffixed: 0,
|
||||
},
|
||||
));
|
||||
|
||||
let pat = Loc::at(loc_expr.region, answer_pat);
|
||||
|
||||
// we increment the suffix of the answer variable as there may be more than 1 additional suffix
|
||||
answer_var.increment_var_suffix(suffixed.saturating_sub(1));
|
||||
|
||||
let new = arena.alloc(Loc::at(loc_expr.region, answer_var));
|
||||
|
||||
return Err(EUnwrapped::UnwrappedSubExpr { arg, pat, new });
|
||||
}
|
||||
|
||||
if called_from_def {
|
||||
Err(EUnwrapped::UnwrappedExpr(arena.alloc(Loc::at(
|
||||
loc_expr.region,
|
||||
Expr::Var {
|
||||
module_name,
|
||||
ident,
|
||||
suffixed: suffixed.saturating_sub(1),
|
||||
},
|
||||
))))
|
||||
} else {
|
||||
internal_error!("unwrapping Var {:?} within a sub expression... this should have been handled at the previous level", loc_expr);
|
||||
}
|
||||
init_unwrapped_err(arena, maybe_def_pat, unwrapped_var)
|
||||
}
|
||||
|
||||
Expr::Defs(..) => unwrap_suffixed_expression_defs_help(arena, loc_expr, called_from_def),
|
||||
Expr::Defs(..) => unwrap_suffixed_expression_defs_help(arena, loc_expr, maybe_def_pat),
|
||||
|
||||
Expr::Apply(..) => unwrap_suffixed_expression_apply_help(arena, loc_expr, called_from_def),
|
||||
Expr::Apply(..) => unwrap_suffixed_expression_apply_help(arena, loc_expr, maybe_def_pat),
|
||||
|
||||
Expr::SpaceBefore(..) | Expr::SpaceAfter(..) => {
|
||||
internal_error!("unexpected SpaceBefore or SpaceAfter in unwrap_suffixed_expression, should have been removed in desugar_expr before this");
|
||||
}
|
||||
|
||||
Expr::When(..) => unwrap_suffixed_expression_when_help(arena, loc_expr, called_from_def),
|
||||
Expr::When(..) => unwrap_suffixed_expression_when_help(arena, loc_expr, maybe_def_pat),
|
||||
|
||||
Expr::If(..) => {
|
||||
unwrap_suffixed_expression_if_then_else_help(arena, loc_expr, called_from_def)
|
||||
unwrap_suffixed_expression_if_then_else_help(arena, loc_expr, maybe_def_pat)
|
||||
}
|
||||
|
||||
Expr::BinOps(..) => internal_error!("BinOps should have been desugared in desugar_expr"),
|
||||
|
||||
Expr::Closure(args, return_expr) => {
|
||||
let count_suffixed_args = args
|
||||
.iter()
|
||||
|
@ -156,55 +150,85 @@ pub fn unwrap_suffixed_expression<'a>(
|
|||
}
|
||||
|
||||
// check the return expression
|
||||
match unwrap_suffixed_expression(arena, return_expr, false) {
|
||||
match unwrap_suffixed_expression(arena, return_expr, None) {
|
||||
Ok(new_expr) => {
|
||||
Ok(arena.alloc(Loc::at(loc_expr.region, Expr::Closure(args, new_expr))))
|
||||
}
|
||||
Err(..) => {
|
||||
// TODO make this a nicer error
|
||||
|
||||
/*
|
||||
Can we have suffixed expressions in a closure return expression?
|
||||
|
||||
x = \msg -> line! msg
|
||||
|
||||
x "hi"
|
||||
|
||||
*/
|
||||
|
||||
internal_error!(
|
||||
"unexpected unwrapped closure return expression, how is this possible?"
|
||||
);
|
||||
Err(EUnwrapped::UnwrappedDefExpr(..)) => {
|
||||
internal_error!("is this even possible? we aren't a Def");
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedSubExpr {
|
||||
sub_arg,
|
||||
sub_pat,
|
||||
sub_new,
|
||||
}) => {
|
||||
let new_closure =
|
||||
arena.alloc(Loc::at(loc_expr.region, Expr::Closure(args, sub_new)));
|
||||
return Err(EUnwrapped::UnwrappedSubExpr {
|
||||
sub_arg,
|
||||
sub_pat,
|
||||
sub_new: new_closure,
|
||||
});
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
Expr::ParensAround(sub_loc_expr) => {
|
||||
match unwrap_suffixed_expression(
|
||||
arena,
|
||||
arena.alloc(Loc::at_zero(*sub_loc_expr)),
|
||||
called_from_def,
|
||||
) {
|
||||
Ok(new_expr) => Ok(arena.alloc(Loc::at(
|
||||
loc_expr.region,
|
||||
ParensAround(arena.alloc(new_expr.value)),
|
||||
))),
|
||||
Err(EUnwrapped::UnwrappedExpr(new_expr)) => {
|
||||
Err(EUnwrapped::UnwrappedExpr(arena.alloc(Loc::at(
|
||||
match unwrap_suffixed_expression(arena, arena.alloc(Loc::at_zero(*sub_loc_expr)), None)
|
||||
{
|
||||
Ok(new_expr) => {
|
||||
let new_parens = arena.alloc(Loc::at(
|
||||
loc_expr.region,
|
||||
ParensAround(arena.alloc(new_expr.value)),
|
||||
))))
|
||||
));
|
||||
return Ok(new_parens);
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedSubExpr { arg, pat, new }) => {
|
||||
Err(EUnwrapped::UnwrappedSubExpr {
|
||||
arg,
|
||||
pat,
|
||||
new: arena.alloc(Loc::at(
|
||||
loc_expr.region,
|
||||
ParensAround(arena.alloc(new.value)),
|
||||
)),
|
||||
})
|
||||
Err(EUnwrapped::UnwrappedDefExpr(..)) => {
|
||||
internal_error!("is this even possible? we aren't a Def");
|
||||
}
|
||||
// // Err(EUnwrapped::UnwrappedExpr(arena.alloc(Loc::at(
|
||||
// // loc_expr.region,
|
||||
// // ParensAround(arena.alloc(new_expr.value)),
|
||||
// // ))))
|
||||
|
||||
// let new_parens_expr = arena.alloc(Loc::at(
|
||||
// new_expr.region,
|
||||
// Expr::ParensAround(&new_expr.value),
|
||||
// ));
|
||||
|
||||
// if called_from_def {
|
||||
// return Err(EUnwrapped::UnwrappedExpr(new_parens_expr));
|
||||
// } else {
|
||||
// dbg!("UnwrappedSubExpr from ParensAround");
|
||||
|
||||
// let (answer_var, answer_pat) = next_suffixed_answer_pattern(arena);
|
||||
|
||||
// let sub_pat = Loc::at(new_expr.region, answer_pat);
|
||||
|
||||
// return Err(EUnwrapped::PassBackToDef {
|
||||
// sub_arg: new_parens_expr,
|
||||
// sub_pat,
|
||||
// sub_new: arena.alloc(Loc::at(new_expr.region, answer_var)),
|
||||
// });
|
||||
// }
|
||||
// }
|
||||
Err(EUnwrapped::UnwrappedSubExpr {
|
||||
sub_arg,
|
||||
sub_pat,
|
||||
sub_new,
|
||||
}) => {
|
||||
let new_parens = arena.alloc(Loc::at(
|
||||
loc_expr.region,
|
||||
ParensAround(arena.alloc(sub_new.value)),
|
||||
));
|
||||
return Err(EUnwrapped::UnwrappedSubExpr {
|
||||
sub_arg,
|
||||
sub_pat,
|
||||
sub_new: new_parens,
|
||||
});
|
||||
}
|
||||
Err(err) => Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -218,119 +242,111 @@ pub fn unwrap_suffixed_expression<'a>(
|
|||
pub fn unwrap_suffixed_expression_apply_help<'a>(
|
||||
arena: &'a Bump,
|
||||
loc_expr: &'a Loc<Expr<'a>>,
|
||||
called_from_def: bool,
|
||||
|
||||
// passed in from the def, first may have a pattern like "x = foo!" to unwrap
|
||||
//
|
||||
// note be careful with calls to this function
|
||||
// only those from the defs helper should pass a `Some`
|
||||
// only those to helpers should forward the value
|
||||
// all other calls to sub-expressions should pass `None`
|
||||
maybe_def_pat: Option<&'a Loc<Pattern<'a>>>,
|
||||
) -> Result<&'a Loc<Expr<'a>>, EUnwrapped<'a>> {
|
||||
match loc_expr.value {
|
||||
Expr::Apply(function, apply_args, called_via) => {
|
||||
|
||||
// Any suffixed arguments will be innermost, therefore we unwrap those first
|
||||
let local_args = arena.alloc_slice_copy(apply_args);
|
||||
|
||||
for (_, arg) in local_args.iter_mut().enumerate() {
|
||||
/*
|
||||
## Example of suffixed function within an Apply
|
||||
x = bar (foo! "hello")
|
||||
baz x
|
||||
|
||||
x = Task.await (foo "hello") \#!a -> bar (#!a)
|
||||
baz x
|
||||
*/
|
||||
|
||||
// try to unwrap the argument
|
||||
match unwrap_suffixed_expression(arena, arg, false) {
|
||||
match unwrap_suffixed_expression(arena, arg, None) {
|
||||
Ok(new_arg) => {
|
||||
*arg = new_arg;
|
||||
},
|
||||
Err(..) => {
|
||||
todo!();
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedDefExpr(..)) => {
|
||||
internal_error!("is this even possible? we aren't a Def");
|
||||
|
||||
// let (answer_var, answer_pat) = next_suffixed_answer_pattern(arena);
|
||||
|
||||
// let sub_pat = Loc::at(loc_expr.region, answer_pat);
|
||||
|
||||
// *arg = arena.alloc(Loc::at(loc_expr.region, answer_var));
|
||||
|
||||
// let new_apply = arena.alloc(Loc::at(loc_expr.region, Apply(function, local_args, called_via)));
|
||||
|
||||
// return Err(EUnwrapped::PassBackToDef { sub_arg: new_expr, sub_pat, sub_new: new_apply });
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedSubExpr { sub_arg, sub_pat, sub_new: new_arg }) => {
|
||||
|
||||
*arg = new_arg;
|
||||
|
||||
let new_apply = arena.alloc(Loc::at(loc_expr.region, Apply(function, local_args, called_via)));
|
||||
|
||||
return Err(EUnwrapped::UnwrappedSubExpr { sub_arg, sub_pat, sub_new: new_apply});
|
||||
|
||||
// if called_from_def {
|
||||
|
||||
// *arg = sub_new;
|
||||
|
||||
// let new_apply = arena.alloc(Loc::at(loc_expr.region, Apply(function, local_args, called_via)));
|
||||
|
||||
// return Err(EUnwrapped::UnwrappedSubExpr { sub_arg, sub_pat, sub_new: new_apply })
|
||||
// } else {
|
||||
// *arg = sub_new;
|
||||
|
||||
// let new_apply = arena.alloc(Loc::at(loc_expr.region, Apply(function, local_args, called_via)));
|
||||
|
||||
// return unwrap_suffixed_expression(arena, apply_task_await(arena, loc_expr.region, sub_arg, sub_pat, new_apply), called_from_def);
|
||||
// }
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
}
|
||||
}
|
||||
|
||||
// try to unwrap the function
|
||||
|
||||
// function is a Var
|
||||
if let Loc { value: Var{module_name, ident,suffixed},region } = function {
|
||||
match suffixed {
|
||||
0 => {
|
||||
// nothing to unwrap, move on to the return expression
|
||||
return Ok(arena.alloc(Loc::at(loc_expr.region, Expr::Apply(function, local_args, called_via))));
|
||||
},
|
||||
1 => {
|
||||
if called_from_def {
|
||||
let new_apply = Loc::at(
|
||||
loc_expr.region,
|
||||
Expr::Apply(
|
||||
arena.alloc(Loc::at(
|
||||
loc_expr.region,
|
||||
Var {
|
||||
module_name,
|
||||
ident,
|
||||
suffixed: 0,
|
||||
},
|
||||
)),
|
||||
local_args,
|
||||
called_via,
|
||||
),
|
||||
);
|
||||
|
||||
return Err(EUnwrapped::UnwrappedExpr(arena.alloc(new_apply)));
|
||||
} else {
|
||||
|
||||
/*
|
||||
## Example of suffixed function within an Apply
|
||||
x = (foo! "bar") "hello"
|
||||
^^^^^^^^^^^^ is our suffixed function
|
||||
baz x
|
||||
|
||||
Task.await (foo "bar") \#!a1 ->
|
||||
x = #!a1 "hello"
|
||||
baz x
|
||||
*/
|
||||
|
||||
let (answer_var, answer_pat) = next_suffixed_answer_pattern(arena);
|
||||
let arg = arena.alloc(Loc::at(loc_expr.region, Apply(
|
||||
arena.alloc(Loc::at(
|
||||
*region,
|
||||
Var {
|
||||
module_name,
|
||||
ident,
|
||||
suffixed: 0,
|
||||
},
|
||||
)),
|
||||
local_args,
|
||||
called_via,
|
||||
)));
|
||||
|
||||
let pat = Loc::at(loc_expr.region, answer_pat);
|
||||
|
||||
let new = arena.alloc(Loc::at(loc_expr.region, answer_var));
|
||||
|
||||
return Err(EUnwrapped::UnwrappedSubExpr { arg, pat, new });
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
internal_error!("TODO support multiple suffixes");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// handle other cases
|
||||
match unwrap_suffixed_expression(arena, function, false) {
|
||||
// function is another expression
|
||||
match unwrap_suffixed_expression(arena, function, None) {
|
||||
Ok(new_function) => {
|
||||
return Ok(arena.alloc(Loc::at(loc_expr.region, Expr::Apply(new_function, local_args, called_via))));
|
||||
},
|
||||
Err(EUnwrapped::UnwrappedExpr(..)) => {
|
||||
todo!();
|
||||
let new_apply = arena.alloc(Loc::at(loc_expr.region, Expr::Apply(new_function, local_args, called_via)));
|
||||
return Ok(new_apply);
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedSubExpr { arg, pat, new }) => {
|
||||
Err(EUnwrapped::UnwrappedDefExpr(..)) => {
|
||||
internal_error!("is this even possible? we aren't a Def");
|
||||
// if called_from_def {
|
||||
// let new_apply = arena.alloc(Loc::at(loc_expr.region, Expr::Apply(new_function, local_args, called_via)));
|
||||
// return Err(EUnwrapped::UnwrappedExpr(new_apply));
|
||||
// } else {
|
||||
// let (answer_var, answer_pat) = next_suffixed_answer_pattern(arena);
|
||||
|
||||
return Err(EUnwrapped::UnwrappedSubExpr {
|
||||
arg,
|
||||
pat,
|
||||
new: arena.alloc(Loc::at(loc_expr.region, Expr::Apply(new, local_args, called_via)))
|
||||
});
|
||||
// let sub_pat = Loc::at(loc_expr.region, answer_pat);
|
||||
|
||||
// let function_answer = arena.alloc(Loc::at(loc_expr.region, answer_var));
|
||||
|
||||
// let new_apply= arena.alloc(Loc::at(loc_expr.region, Expr::Apply(function_answer, local_args, called_via)));
|
||||
|
||||
// return unwrap_suffixed_expression(
|
||||
// arena,
|
||||
// apply_task_await(arena, loc_expr.region, new_function, sub_pat, new_apply),
|
||||
// false
|
||||
// );
|
||||
|
||||
// // return Err(EUnwrapped::UnwrappedSubExpr {
|
||||
// // sub_arg: arena.alloc(Loc::at(loc_expr.region, Expr::Apply(new_function, local_args, called_via))),
|
||||
// // sub_pat,
|
||||
// // sub_new: arena.alloc(Loc::at(new_function.region, answer_var))
|
||||
// // });
|
||||
// }
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedSubExpr { sub_arg, sub_pat, sub_new: new_function }) => {
|
||||
|
||||
let new_apply = arena.alloc(Loc::at(loc_expr.region, Expr::Apply(new_function, local_args, called_via)));
|
||||
|
||||
return Err(EUnwrapped::UnwrappedSubExpr { sub_arg, sub_pat, sub_new: new_apply});
|
||||
|
||||
// let new_apply = arena.alloc(Loc::at(loc_expr.region, Expr::Apply(sub_new, local_args, called_via)));
|
||||
|
||||
// return unwrap_suffixed_expression(arena,
|
||||
// apply_task_await(arena, loc_expr.region, sub_arg, sub_pat, new_apply), false);
|
||||
|
||||
}
|
||||
Err(err) => Err(err)
|
||||
}
|
||||
}
|
||||
_ => internal_error!("unreachable, expected an Apply node to be passed into unwrap_suffixed_expression_apply_help"),
|
||||
|
@ -340,7 +356,7 @@ pub fn unwrap_suffixed_expression_apply_help<'a>(
|
|||
pub fn unwrap_suffixed_expression_if_then_else_help<'a>(
|
||||
_arena: &'a Bump,
|
||||
loc_expr: &'a Loc<Expr<'a>>,
|
||||
_called_from_def: bool, // TODO do we need this? pretty sure we do
|
||||
_maybe_def_pat: Option<&'a Loc<Pattern<'a>>>,
|
||||
) -> Result<&'a Loc<Expr<'a>>, EUnwrapped<'a>> {
|
||||
Ok(loc_expr)
|
||||
|
||||
|
@ -471,7 +487,7 @@ pub fn unwrap_suffixed_expression_if_then_else_help<'a>(
|
|||
pub fn unwrap_suffixed_expression_when_help<'a>(
|
||||
_arena: &'a Bump,
|
||||
loc_expr: &'a Loc<Expr<'a>>,
|
||||
_called_from_def: bool, // TODO do we need this? pretty sure we do
|
||||
_maybe_def_pat: Option<&'a Loc<Pattern<'a>>>,
|
||||
) -> Result<&'a Loc<Expr<'a>>, EUnwrapped<'a>> {
|
||||
Ok(loc_expr)
|
||||
}
|
||||
|
@ -479,30 +495,28 @@ pub fn unwrap_suffixed_expression_when_help<'a>(
|
|||
pub fn unwrap_suffixed_expression_defs_help<'a>(
|
||||
arena: &'a Bump,
|
||||
loc_expr: &'a Loc<Expr<'a>>,
|
||||
_called_from_def: bool, // TODO do we need this? pretty sure we do
|
||||
maybe_def_pat: Option<&'a Loc<Pattern<'a>>>,
|
||||
) -> Result<&'a Loc<Expr<'a>>, EUnwrapped<'a>> {
|
||||
match loc_expr.value {
|
||||
Expr::Defs(defs, loc_ret) => {
|
||||
|
||||
let mut local_defs = defs.clone();
|
||||
|
||||
// try an unwrap each def, if none can be unwrapped, then try to unwrap the loc_ret
|
||||
for (tag_index, type_or_value_def) in defs.defs().enumerate() {
|
||||
use ValueDef::*;
|
||||
|
||||
let maybe_pattern_expr = match type_or_value_def.err() {
|
||||
let maybe_suffixed_value_def = match type_or_value_def.err() {
|
||||
None | Some(Annotation(..)) | Some(Dbg{..}) | Some(Expect{..}) | Some(ExpectFx{..}) | Some(Stmt(..))=> None,
|
||||
Some(AnnotatedBody { body_pattern, body_expr, .. }) => Some((body_pattern, body_expr)),
|
||||
Some(Body (def_pattern, def_expr, .. )) => Some((def_pattern, def_expr)),
|
||||
};
|
||||
|
||||
match maybe_pattern_expr {
|
||||
match maybe_suffixed_value_def {
|
||||
None => {
|
||||
// nothing to unwrap, move on to the next def
|
||||
// not a def that we can unwrap, move on to the next
|
||||
continue;
|
||||
},
|
||||
Some((def_pattern, def_expr)) => {
|
||||
match unwrap_suffixed_expression(arena, def_expr, true) {
|
||||
match unwrap_suffixed_expression(arena, def_expr, Some(*def_pattern)) {
|
||||
Ok(new_def_expr) => {
|
||||
|
||||
let new_value_def = match type_or_value_def.err() {
|
||||
|
@ -511,129 +525,92 @@ pub fn unwrap_suffixed_expression_defs_help<'a>(
|
|||
Some(Body ( .. )) => ValueDef::Body(def_pattern, new_def_expr),
|
||||
};
|
||||
|
||||
// do nothing, move on to check the next def
|
||||
let mut local_defs = defs.clone();
|
||||
local_defs.replace_with_value_def(tag_index, new_value_def, loc_expr.region);
|
||||
|
||||
let new_defs = arena.alloc(Loc::at(def_expr.region, Defs(arena.alloc(local_defs), loc_ret)));
|
||||
return unwrap_suffixed_expression(arena, new_defs, maybe_def_pat);
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedExpr(new)) => {
|
||||
let split_defs = defs.split_defs_around(tag_index);
|
||||
Err(EUnwrapped::UnwrappedDefExpr(unwrapped_expr)) => {
|
||||
let local_defs = defs.clone();
|
||||
let split_defs = local_defs.split_defs_around(tag_index);
|
||||
let before_empty = split_defs.before.is_empty();
|
||||
let after_empty = split_defs.after.is_empty();
|
||||
if before_empty && after_empty {
|
||||
// NIL before, NIL after -> SINGLE
|
||||
return unwrap_suffixed_expression(
|
||||
arena,
|
||||
apply_task_await(
|
||||
arena,
|
||||
def_expr.region,
|
||||
new,
|
||||
**def_pattern,
|
||||
loc_ret,
|
||||
),
|
||||
false,
|
||||
);
|
||||
let replaced_def = apply_task_await(arena,def_expr.region,unwrapped_expr,*def_pattern,loc_ret);
|
||||
return unwrap_suffixed_expression(arena,replaced_def,maybe_def_pat);
|
||||
} else if before_empty {
|
||||
// NIL before, SOME after -> FIRST
|
||||
return unwrap_suffixed_expression(
|
||||
arena,
|
||||
apply_task_await(arena, loc_expr.region, new, **def_pattern, arena.alloc(Loc::at(def_expr.region, Defs(arena.alloc(split_defs.after), loc_ret)))),
|
||||
false,
|
||||
);
|
||||
let new_defs = arena.alloc(Loc::at(def_expr.region, Defs(arena.alloc(split_defs.after), loc_ret)));
|
||||
let replaced_def = apply_task_await(arena, loc_expr.region, unwrapped_expr, *def_pattern, new_defs);
|
||||
return unwrap_suffixed_expression(arena,replaced_def,maybe_def_pat);
|
||||
} else {
|
||||
// SOME before, NIL after -> LAST
|
||||
debug_assert!(after_empty);
|
||||
/*
|
||||
## Example with multiple defs, last suffixed
|
||||
msg = "hello"
|
||||
x = foo! msg <-- e.g. we've just unwrapped this def
|
||||
bar x
|
||||
|
||||
msg = "hello"
|
||||
Task.await (foo msg) \x -> bar x
|
||||
*/
|
||||
let new_loc_ret = apply_task_await(
|
||||
arena,
|
||||
loc_expr.region,
|
||||
new,
|
||||
**def_pattern,
|
||||
loc_ret,
|
||||
);
|
||||
return unwrap_suffixed_expression(
|
||||
arena,
|
||||
arena.alloc(Loc::at(loc_expr.region,
|
||||
Defs(arena.alloc(split_defs.before), new_loc_ret))),
|
||||
false,
|
||||
);
|
||||
let new_loc_ret = apply_task_await(arena,loc_expr.region,unwrapped_expr,*def_pattern,loc_ret);
|
||||
let new_defs = arena.alloc(Loc::at(loc_expr.region,Defs(arena.alloc(split_defs.before), new_loc_ret)));
|
||||
return unwrap_suffixed_expression(arena,new_defs,maybe_def_pat);
|
||||
}
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedSubExpr { arg, pat, new }) => {
|
||||
let new_value_def = ValueDef::Body(def_pattern, new);
|
||||
let mut new_defs = defs.clone();
|
||||
new_defs.replace_with_value_def(
|
||||
tag_index,
|
||||
new_value_def,
|
||||
def_expr.region,
|
||||
);
|
||||
return unwrap_suffixed_expression(
|
||||
arena,
|
||||
apply_task_await(
|
||||
arena,
|
||||
def_expr.region,
|
||||
arg,
|
||||
pat,
|
||||
arena.alloc(Loc::at(
|
||||
def_expr.region,
|
||||
Defs(arena.alloc(new_defs), loc_ret),
|
||||
)),
|
||||
),
|
||||
false,
|
||||
);
|
||||
Err(EUnwrapped::UnwrappedSubExpr { sub_arg, sub_pat, sub_new }) => {
|
||||
let mut local_defs = defs.clone();
|
||||
let new_body_def = ValueDef::Body(def_pattern, sub_new);
|
||||
local_defs.replace_with_value_def(tag_index,new_body_def,def_expr.region);
|
||||
let new_defs_expr = arena.alloc(Loc::at(def_expr.region,Defs(arena.alloc(local_defs), loc_ret)));
|
||||
let replaced_def = apply_task_await(arena,def_expr.region,sub_arg,sub_pat,new_defs_expr);
|
||||
return unwrap_suffixed_expression(arena,replaced_def,maybe_def_pat);
|
||||
}
|
||||
Err(err) => return Err(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// try to unwrap the loc_ret
|
||||
match unwrap_suffixed_expression(arena, loc_ret, false) {
|
||||
match unwrap_suffixed_expression(arena, loc_ret, None) {
|
||||
Ok(new_loc_ret) => {
|
||||
Ok(arena.alloc(Loc::at(loc_expr.region, Defs(arena.alloc(local_defs), new_loc_ret))))
|
||||
},
|
||||
Err(EUnwrapped::UnwrappedExpr(new_loc_ret)) => {
|
||||
// the loc_ret was unwrapped, replace and return new expression
|
||||
Err(EUnwrapped::UnwrappedExpr(arena.alloc(Loc::at(loc_expr.region, Defs(defs, new_loc_ret)))))
|
||||
Ok(arena.alloc(Loc::at(loc_expr.region, Defs(arena.alloc(defs), new_loc_ret))))
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedSubExpr { arg, pat, new }) => {
|
||||
Err(EUnwrapped::UnwrappedDefExpr(new_loc_ret)) => {
|
||||
// the loc_ret was unwrapped, replace and return new expression
|
||||
internal_error!("is this even possible? we are in the loc_ret of a Def");
|
||||
// Err(EUnwrapped::UnwrappedDefExpr(arena.alloc(Loc::at(loc_expr.region, Defs(defs, new_loc_ret)))))
|
||||
}
|
||||
Err(EUnwrapped::UnwrappedSubExpr { sub_arg, sub_pat, sub_new }) => {
|
||||
// the loc_ret was unwrapped
|
||||
let new_loc_ret = apply_task_await(arena, loc_ret.region, arg, pat, new);
|
||||
Err(EUnwrapped::UnwrappedExpr(arena.alloc(Loc::at(loc_expr.region, Defs(defs, new_loc_ret)))))
|
||||
},
|
||||
internal_error!("is this even possible? we are in the loc_ret of a Def");
|
||||
// let new_loc_ret = apply_task_await(arena, loc_ret.region, sub_arg, sub_pat, sub_new);
|
||||
// Err(EUnwrapped::UnwrappedDefExpr(arena.alloc(Loc::at(loc_expr.region, Defs(defs, new_loc_ret)))))
|
||||
}
|
||||
Err(err) => Err(err)
|
||||
}
|
||||
},
|
||||
_ => internal_error!("unreachable, expected a Defs node to be passed into unwrap_suffixed_expression_defs_help"),
|
||||
}
|
||||
}
|
||||
|
||||
fn apply_task_await<'a>(
|
||||
/// Helper for `Task.await (loc_arg) \loc_pat -> loc_new`
|
||||
pub fn apply_task_await<'a>(
|
||||
arena: &'a Bump,
|
||||
region: Region,
|
||||
arg_loc_expr: &'a Loc<Expr<'a>>,
|
||||
loc_pat: Loc<Pattern<'a>>,
|
||||
new: &'a Loc<Expr<'a>>,
|
||||
loc_arg: &'a Loc<Expr<'a>>,
|
||||
loc_pat: &'a Loc<Pattern<'a>>,
|
||||
loc_new: &'a Loc<Expr<'a>>,
|
||||
) -> &'a Loc<Expr<'a>> {
|
||||
let mut task_await_apply_args: Vec<&'a Loc<Expr<'a>>> = Vec::new_in(arena);
|
||||
|
||||
// apply the unwrapped suffixed expression
|
||||
task_await_apply_args.push(arg_loc_expr);
|
||||
task_await_apply_args.push(loc_arg);
|
||||
|
||||
// apply the closure
|
||||
let mut closure_pattern = Vec::new_in(arena);
|
||||
closure_pattern.push(loc_pat);
|
||||
closure_pattern.push(*loc_pat);
|
||||
task_await_apply_args.push(arena.alloc(Loc::at(
|
||||
region,
|
||||
Closure(arena.alloc_slice_copy(closure_pattern.as_slice()), new),
|
||||
Closure(arena.alloc_slice_copy(closure_pattern.as_slice()), loc_new),
|
||||
)));
|
||||
|
||||
// e.g. `Task.await (arg_loc_expr) \pattern -> new`
|
||||
arena.alloc(Loc::at(
|
||||
region,
|
||||
Apply(
|
||||
|
@ -650,252 +627,3 @@ fn apply_task_await<'a>(
|
|||
),
|
||||
))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod unwrap_suffixed {
|
||||
|
||||
use crate::desugar::desugar_defs_node_values;
|
||||
use bumpalo::Bump;
|
||||
use roc_parse::test_helpers::parse_defs_with;
|
||||
use roc_test_utils::assert_multiline_str_eq;
|
||||
|
||||
#[test]
|
||||
fn multi_defs_stmts() {
|
||||
/*
|
||||
line! "Ahoy"
|
||||
{} = "There" |> Stdout.line!
|
||||
|
||||
Task.ok {}
|
||||
|
||||
# desugared
|
||||
Task.await (line "Ahoy") \{} ->
|
||||
Task.await ("There" |> Stdout.line) \{} ->
|
||||
Task.ok {}
|
||||
*/
|
||||
|
||||
let arena = &Bump::new();
|
||||
|
||||
let src = r#"
|
||||
main =
|
||||
line! "Ahoy"
|
||||
{} = "There" |> Stdout.line!
|
||||
|
||||
Task.ok {}
|
||||
"#;
|
||||
|
||||
let mut defs = parse_defs_with(arena, src).unwrap();
|
||||
|
||||
desugar_defs_node_values(arena, &mut defs, src, &mut None, "test.roc", true);
|
||||
|
||||
let expected = r#"Defs { tags: [Index(2147483648)], regions: [@0-36], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @0-36 Apply(@0-36 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@24-36 Apply(@24-36 Var { module_name: "", ident: "line", suffixed: 0 }, [@30-36 Str(PlainLine("Ahoy"))], Space), @0-36 Closure([@24-36 RecordDestructure([])], @58-81 Apply(@58-81 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@58-81 Apply(@58-81 Var { module_name: "Stdout", ident: "line", suffixed: 0 }, [@58-65 Str(PlainLine("There"))], BinOp(Pizza)), @58-81 Closure([@53-55 RecordDestructure([])], @115-125 Apply(@115-122 Var { module_name: "Task", ident: "ok", suffixed: 0 }, [@123-125 Record([])], Space))], BangSuffix))], BangSuffix))] }"#;
|
||||
|
||||
assert_multiline_str_eq!(format!("{:?}", &defs).as_str(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn simple_pizza() {
|
||||
/*
|
||||
"hello" |> line!
|
||||
|
||||
Task.ok {}
|
||||
|
||||
# desugared
|
||||
Task.await ("hello" |> line) \{} -> Task.ok {}
|
||||
*/
|
||||
|
||||
let arena = &Bump::new();
|
||||
|
||||
let src = r#"
|
||||
main =
|
||||
"hello" |> line!
|
||||
|
||||
Task.ok {}
|
||||
"#;
|
||||
|
||||
let mut defs = parse_defs_with(arena, src).unwrap();
|
||||
|
||||
desugar_defs_node_values(arena, &mut defs, src, &mut None, "test.roc", true);
|
||||
|
||||
let expected = r#"Defs { tags: [Index(2147483648)], regions: [@0-40], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @24-40 Apply(@24-40 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@24-40 Apply(@24-40 Var { module_name: "", ident: "line", suffixed: 0 }, [@24-31 Str(PlainLine("hello"))], BinOp(Pizza)), @24-40 Closure([@24-40 RecordDestructure([])], @58-68 Apply(@58-65 Var { module_name: "Task", ident: "ok", suffixed: 0 }, [@66-68 Record([])], Space))], BangSuffix))] }"#;
|
||||
|
||||
assert_multiline_str_eq!(format!("{:?}", &defs).as_str(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn single_suffix() {
|
||||
/*
|
||||
## Example with single suffix
|
||||
x = foo!
|
||||
bar x
|
||||
|
||||
# desugared
|
||||
Task.await (foo) \x -> bar x
|
||||
*/
|
||||
|
||||
let arena = &Bump::new();
|
||||
|
||||
let src = r#"
|
||||
main =
|
||||
x = foo!
|
||||
bar x
|
||||
"#;
|
||||
|
||||
let mut defs = parse_defs_with(arena, src).unwrap();
|
||||
|
||||
desugar_defs_node_values(arena, &mut defs, src, &mut None, "test.roc", true);
|
||||
|
||||
let expected = r#"Defs { tags: [Index(2147483648)], regions: [@0-54], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @28-32 Apply(@28-32 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@28-32 Var { module_name: "", ident: "foo", suffixed: 0 }, @28-32 Closure([@24-25 Identifier { ident: "x", suffixed: 0 }], @49-54 Apply(@49-52 Var { module_name: "", ident: "bar", suffixed: 0 }, [@53-54 Var { module_name: "", ident: "x", suffixed: 0 }], Space))], BangSuffix))] }"#;
|
||||
|
||||
assert_multiline_str_eq!(format!("{:?}", &defs).as_str(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_suffix() {
|
||||
/*
|
||||
## Example with multiple suffix
|
||||
foo!!
|
||||
bar
|
||||
|
||||
# step 1
|
||||
Task.await (foo) \#!a1 ->
|
||||
(#!a1)!
|
||||
bar
|
||||
|
||||
# desugared
|
||||
Task.await (foo) \#!a1 ->
|
||||
Task.await #!a1 \{} -> bar
|
||||
*/
|
||||
|
||||
let arena = &Bump::new();
|
||||
|
||||
let src = r#"
|
||||
main =
|
||||
foo!!
|
||||
bar
|
||||
"#;
|
||||
|
||||
let mut defs = parse_defs_with(arena, src).unwrap();
|
||||
|
||||
desugar_defs_node_values(arena, &mut defs, src, &mut None, "test.roc", true);
|
||||
|
||||
// TODO fix this... the value_defs is incorrect here in a harmless way because
|
||||
// roc_parse::ast::Defs::split_defs_around is not completely correct
|
||||
// so we have the value_def in the vec in Defs, but there is no tag pointing to it
|
||||
let expected = r##"Defs { tags: [Index(2147483648)], regions: [@0-29], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @24-29 Apply(@24-29 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@24-29 Var { module_name: "", ident: "foo", suffixed: 0 }, @24-29 Closure([@24-29 Identifier { ident: "#!a0", suffixed: 0 }], @24-29 Apply(@24-29 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@24-29 Var { module_name: "", ident: "#!a0", suffixed: 0 }, @24-29 Closure([@24-29 RecordDestructure([])], @46-49 Var { module_name: "", ident: "bar", suffixed: 0 })], BangSuffix))], BangSuffix))] }"##;
|
||||
|
||||
assert_multiline_str_eq!(format!("{:?}", &defs).as_str(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn apply_function_suffixed_sub_expr() {
|
||||
/*
|
||||
## Example of suffixed function within an Apply
|
||||
x = (foo! "bar") "hello"
|
||||
^^^^^^^^^^^^ is our suffixed function
|
||||
baz x
|
||||
|
||||
# desugared
|
||||
Task.await (foo "bar") \#!a1 ->
|
||||
x = #!a1 "hello"
|
||||
bar x
|
||||
*/
|
||||
|
||||
let arena = &Bump::new();
|
||||
|
||||
let src = r#"
|
||||
main =
|
||||
x = (foo! "bar") "hello"
|
||||
baz x
|
||||
"#;
|
||||
|
||||
let mut defs = parse_defs_with(arena, src).unwrap();
|
||||
|
||||
desugar_defs_node_values(arena, &mut defs, src, &mut None, "test.roc", true);
|
||||
|
||||
// TODO fix this... the value_defs is incorrect here in a harmless way because
|
||||
// roc_parse::ast::Defs::split_defs_around is not completely correct
|
||||
// so we have the value_def in the vec in Defs, but there is no tag pointing to it
|
||||
let expected = r##"Defs { tags: [Index(2147483648)], regions: [@0-70], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @28-48 Apply(@28-48 Var { module_name: "Task", ident: "await", suffixed: 0 }, [Apply(@29-33 Var { module_name: "", ident: "foo", suffixed: 0 }, [@34-39 Str(PlainLine("bar"))], Space), @28-48 Closure([Identifier { ident: "#!a0", suffixed: 0 }], @28-48 Defs(Defs { tags: [Index(2147483650)], regions: [@28-48], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@24-25 Identifier { ident: "x", suffixed: 0 }, @28-48 Apply(@29-39 ParensAround(Apply(@29-33 Var { module_name: "", ident: "foo", suffixed: 1 }, [@34-39 Str(PlainLine("bar"))], Space)), [@41-48 Str(PlainLine("hello"))], Space)), Body(@24-25 Identifier { ident: "x", suffixed: 0 }, @28-48 Apply(@29-39 ParensAround(Var { module_name: "", ident: "#!a0", suffixed: 0 }), [@41-48 Str(PlainLine("hello"))], Space)), Body(@24-25 Identifier { ident: "x", suffixed: 0 }, @28-48 Apply(@29-39 ParensAround(Var { module_name: "", ident: "#!a0", suffixed: 0 }), [@41-48 Str(PlainLine("hello"))], Space))] }, @65-70 Apply(@65-68 Var { module_name: "", ident: "baz", suffixed: 0 }, [@69-70 Var { module_name: "", ident: "x", suffixed: 0 }], Space)))], BangSuffix))] }"##;
|
||||
|
||||
assert_multiline_str_eq!(format!("{:?}", &defs).as_str(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_def_first_suffixed() {
|
||||
/*
|
||||
## Example with multiple defs, first suffixed
|
||||
msg = "hello"
|
||||
x = foo! msg
|
||||
bar x
|
||||
|
||||
# desugared
|
||||
msg = "hello"
|
||||
Task.await (foo msg) \x -> bar x
|
||||
*/
|
||||
|
||||
let arena = &Bump::new();
|
||||
|
||||
let src = r#"
|
||||
main =
|
||||
msg = "hello"
|
||||
x = foo! msg
|
||||
bar x
|
||||
"#;
|
||||
|
||||
let mut defs = parse_defs_with(arena, src).unwrap();
|
||||
|
||||
desugar_defs_node_values(arena, &mut defs, src, &mut None, "test.roc", true);
|
||||
|
||||
// TODO fix this... the value_defs is incorrect here in a harmless way because
|
||||
// roc_parse::ast::Defs::split_defs_around is not completely correct
|
||||
// so we have the value_def in the vec in Defs, but there is no tag pointing to it
|
||||
let expected = r#"Defs { tags: [Index(2147483648)], regions: [@0-88], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @24-88 Defs(Defs { tags: [Index(2147483650)], regions: [@24-88, @54-66], space_before: [Slice(start = 0, length = 0), Slice(start = 0, length = 1)], space_after: [Slice(start = 0, length = 0), Slice(start = 1, length = 0)], spaces: [Newline], type_defs: [], value_defs: [Body(@24-27 Identifier { ident: "msg", suffixed: 0 }, @30-37 Str(PlainLine("hello"))), Body(@54-55 Identifier { ident: "x", suffixed: 0 }, @58-66 Apply(@58-62 Var { module_name: "", ident: "foo", suffixed: 1 }, [@63-66 Var { module_name: "", ident: "msg", suffixed: 0 }], Space)), Body(@24-27 Identifier { ident: "msg", suffixed: 0 }, @30-37 Str(PlainLine("hello")))] }, @24-88 Apply(@24-88 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@58-66 Apply(@58-66 Var { module_name: "", ident: "foo", suffixed: 0 }, [@63-66 Var { module_name: "", ident: "msg", suffixed: 0 }], Space), @24-88 Closure([@54-55 Identifier { ident: "x", suffixed: 0 }], @83-88 Apply(@83-86 Var { module_name: "", ident: "bar", suffixed: 0 }, [@87-88 Var { module_name: "", ident: "x", suffixed: 0 }], Space))], BangSuffix)))] }"#;
|
||||
|
||||
assert_multiline_str_eq!(format!("{:?}", &defs).as_str(), expected);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn closure_with_annotations() {
|
||||
/*
|
||||
x : Str -> Task _ _
|
||||
x = \msg ->
|
||||
|
||||
y : Task {} _
|
||||
y = line! msg
|
||||
y
|
||||
|
||||
x "foo"
|
||||
|
||||
# desugared
|
||||
x = \msg ->
|
||||
Task.await (line msg) \y -> y
|
||||
|
||||
x "foo"
|
||||
*/
|
||||
|
||||
let arena = &Bump::new();
|
||||
|
||||
let src = r#"
|
||||
main =
|
||||
x : Str -> Task _ _
|
||||
x = \msg ->
|
||||
|
||||
y : Task {} _
|
||||
y = line! msg
|
||||
y
|
||||
|
||||
x "foo"
|
||||
"#;
|
||||
|
||||
let mut defs = parse_defs_with(arena, src).unwrap();
|
||||
|
||||
desugar_defs_node_values(arena, &mut defs, src, &mut None, "test.roc", true);
|
||||
|
||||
// TODO fix this... the value_defs is incorrect here in a harmless way because
|
||||
// roc_parse::ast::Defs::split_defs_around is not completely correct
|
||||
// so we have the value_def in the vec in Defs, but there is no tag pointing to it
|
||||
let expected = r#"Defs { tags: [Index(2147483648)], regions: [@0-187], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @24-187 Defs(Defs { tags: [Index(2147483650)], regions: [@24-187], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Annotation(@24-25 Identifier { ident: "x", suffixed: 0 }, @28-43 Function([@28-31 Apply("", "Str", [])], @35-43 Apply("", "Task", [@40-41 Inferred, @42-43 Inferred]))), AnnotatedBody { ann_pattern: @24-25 Identifier { ident: "x", suffixed: 0 }, ann_type: @28-43 Function([@28-31 Apply("", "Str", [])], @35-43 Apply("", "Task", [@40-41 Inferred, @42-43 Inferred])), comment: None, body_pattern: @60-61 Identifier { ident: "x", suffixed: 0 }, body_expr: @64-162 Closure([@65-68 Identifier { ident: "msg", suffixed: 0 }], @93-162 Defs(Defs { tags: [Index(2147483649)], regions: [@93-140], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Annotation(@93-94 Identifier { ident: "y", suffixed: 0 }, @97-106 Apply("", "Task", [@102-104 Record { fields: [], ext: None }, @105-106 Inferred])), AnnotatedBody { ann_pattern: @93-94 Identifier { ident: "y", suffixed: 0 }, ann_type: @97-106 Apply("", "Task", [@102-104 Record { fields: [], ext: None }, @105-106 Inferred]), comment: None, body_pattern: @127-128 Identifier { ident: "y", suffixed: 0 }, body_expr: @131-140 Apply(@131-136 Var { module_name: "", ident: "line", suffixed: 1 }, [@137-140 Var { module_name: "", ident: "msg", suffixed: 0 }], Space) }] }, @161-162 Var { module_name: "", ident: "y", suffixed: 0 })) }, AnnotatedBody { ann_pattern: @24-25 Identifier { ident: "x", suffixed: 0 }, ann_type: @28-43 Function([@28-31 Apply("", "Str", [])], @35-43 Apply("", "Task", [@40-41 Inferred, @42-43 Inferred])), comment: None, body_pattern: @60-61 Identifier { ident: "x", suffixed: 0 }, body_expr: @64-162 Closure([@65-68 Identifier { ident: "msg", suffixed: 0 }], @131-140 Apply(@131-140 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@131-140 Apply(@131-140 Var { module_name: "", ident: "line", suffixed: 0 }, [@137-140 Var { module_name: "", ident: "msg", suffixed: 0 }], Space), @131-140 Closure([@127-128 Identifier { ident: "y", suffixed: 0 }], @161-162 Var { module_name: "", ident: "y", suffixed: 0 })], BangSuffix)) }] }, @180-187 Apply(@180-181 Var { module_name: "", ident: "x", suffixed: 0 }, [@182-187 Str(PlainLine("foo"))], Space)))] }"#;
|
||||
|
||||
assert_multiline_str_eq!(format!("{:?}", &defs).as_str(), expected);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue