Fix PNC + ? suffix interaction

This commit is contained in:
Sam Mohr 2025-01-25 05:41:59 -08:00
parent 36aa6f0883
commit 1aff18d47c
No known key found for this signature in database
GPG key ID: EA41D161A3C1BC99
2 changed files with 46 additions and 31 deletions

View file

@ -93,6 +93,29 @@ fn new_op_call_expr<'a>(
),
);
}
TrySuffix(PncApply(loc_fn_expr, loc_args)) => {
let function = desugar_expr(env, scope, loc_fn_expr);
let mut desugared_args = Vec::with_capacity_in(loc_args.len(), env.arena);
desugared_args.push(desugar_expr(env, scope, left));
for loc_arg in loc_args.iter() {
desugared_args.push(desugar_expr(env, scope, loc_arg));
}
return Loc::at(
region,
LowLevelTry(
env.arena.alloc(Loc::at(
region,
Expr::PncApply(
function,
Collection::with_items(desugared_args.into_bump_slice()),
),
)),
ResultTryKind::OperatorSuffix,
),
);
}
TrySuffix(fn_expr) => {
let loc_fn = env.arena.alloc(Loc::at(right.region, **fn_expr));
let function = desugar_expr(env, scope, loc_fn);
@ -112,37 +135,6 @@ fn new_op_call_expr<'a>(
),
);
}
PncApply(
&Loc {
value: TrySuffix(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() + 1, env.arena);
desugared_args.push(desugar_expr(env, scope, left));
for loc_arg in loc_args.items {
desugared_args.push(desugar_expr(env, scope, loc_arg));
}
return Loc::at(
region,
LowLevelTry(
env.arena.alloc(Loc::at(
region,
Expr::Apply(
function,
desugared_args.into_bump_slice(),
CalledVia::Try,
),
)),
ResultTryKind::OperatorSuffix,
),
);
}
Apply(
&Loc {
value: TrySuffix(fn_expr),

View file

@ -971,6 +971,29 @@ mod test_can {
assert_str_value(&cond_args[0].1.value, "123");
}
#[test]
fn try_desugar_pipe_suffix_pnc() {
let src = indoc!(
r#"
"123" |> Str.to_u64()?
"#
);
let arena = Bump::new();
let out = can_expr_with(&arena, test_home(), src);
assert_eq!(out.problems, Vec::new());
// Assert that we desugar to:
//
// Try(Str.to_u64 "123")
let cond_expr = assert_try_expr(&out.loc_expr.value);
let cond_args = assert_func_call(cond_expr, "to_u64", CalledVia::Space, &out.interns);
assert_eq!(cond_args.len(), 1);
assert_str_value(&cond_args[0].1.value, "123");
}
#[test]
fn try_desugar_works_elsewhere() {
let src = indoc!(