Skip to content

Commit

Permalink
Merge pull request #7000 from smores56/try-suffix-ast-node
Browse files Browse the repository at this point in the history
Combine ! and ? into single TrySuffix AST node
  • Loading branch information
smores56 committed Aug 15, 2024
2 parents 2423ce2 + fdacdbe commit a14a110
Show file tree
Hide file tree
Showing 45 changed files with 921 additions and 188 deletions.
2 changes: 1 addition & 1 deletion crates/cli/tests/cli/parse-args.roc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ main =
file,
count: numParam { name: "count" },
doubled: numParam { name: "doubled" }
|> cliMap \d -> d * 2,
|> cliMap \d -> d * 2,
}

args = ["parse-args", "file.txt", "5", "7"]
Expand Down
2 changes: 1 addition & 1 deletion crates/cli/tests/cli_run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ mod cli_run {
}

#[test]
#[cfg_attr(any(target_os = "windows", target_os = "linux"), ignore = "Segfault")]
#[cfg_attr(target_os = "windows", ignore = "Segfault")]
fn false_interpreter() {
test_roc_app(
"examples/cli/false-interpreter",
Expand Down
32 changes: 26 additions & 6 deletions crates/compiler/can/src/desugar.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#![allow(clippy::manual_map)]

use crate::suffixed::{apply_task_await, unwrap_suffixed_expression, EUnwrapped};
use crate::suffixed::{apply_try_function, unwrap_suffixed_expression, EUnwrapped};
use bumpalo::collections::Vec;
use bumpalo::Bump;
use roc_error_macros::internal_error;
Expand Down Expand Up @@ -220,11 +220,20 @@ pub fn desugar_value_def_suffixed<'a>(arena: &'a Bump, value_def: ValueDef<'a>)
sub_arg,
sub_pat,
sub_new,
target,
}) => desugar_value_def_suffixed(
arena,
Body(
loc_pattern,
apply_task_await(arena, loc_expr.region, sub_arg, sub_pat, sub_new, None),
apply_try_function(
arena,
loc_expr.region,
sub_arg,
sub_pat,
sub_new,
None,
target,
),
),
),
Err(..) => Body(
Expand Down Expand Up @@ -254,20 +263,22 @@ pub fn desugar_value_def_suffixed<'a>(arena: &'a Bump, value_def: ValueDef<'a>)
sub_arg,
sub_pat,
sub_new,
target,
}) => desugar_value_def_suffixed(
arena,
AnnotatedBody {
ann_pattern,
ann_type,
lines_between,
body_pattern,
body_expr: apply_task_await(
body_expr: apply_try_function(
arena,
body_expr.region,
sub_arg,
sub_pat,
sub_new,
Some((ann_pattern, ann_type)),
target,
),
},
),
Expand Down Expand Up @@ -371,14 +382,23 @@ pub fn desugar_expr<'a>(

arena.alloc(Loc { region, value })
}
// desugar the sub_expression, but leave the TaskAwaitBang as this will
// desugar the sub_expression, but leave the TrySuffix as this will
// be unwrapped later in desugar_value_def_suffixed
TaskAwaitBang(sub_expr) => {
TrySuffix {
expr: sub_expr,
target,
} => {
let intermediate = arena.alloc(Loc::at(loc_expr.region, **sub_expr));
let new_sub_loc_expr = desugar_expr(arena, intermediate, src, line_info, module_path);
let new_sub_expr = arena.alloc(new_sub_loc_expr.value);

arena.alloc(Loc::at(loc_expr.region, TaskAwaitBang(new_sub_expr)))
arena.alloc(Loc::at(
loc_expr.region,
TrySuffix {
expr: new_sub_expr,
target: *target,
},
))
}
RecordAccess(sub_expr, paths) => {
let region = loc_expr.region;
Expand Down
15 changes: 9 additions & 6 deletions crates/compiler/can/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -648,9 +648,7 @@ pub fn canonicalize_expr<'a>(

(answer, Output::default())
}
ast::Expr::Record(fields) => {
canonicalize_record(env, var_store, scope, region, *fields)
}
ast::Expr::Record(fields) => canonicalize_record(env, var_store, scope, region, *fields),
ast::Expr::RecordUpdate {
fields,
update: loc_update,
Expand Down Expand Up @@ -1119,7 +1117,9 @@ pub fn canonicalize_expr<'a>(
output,
)
}
ast::Expr::TaskAwaitBang(..) => internal_error!("a Expr::TaskAwaitBang expression was not completely removed in desugar_value_def_suffixed"),
ast::Expr::TrySuffix { .. } => internal_error!(
"a Expr::TrySuffix expression was not completely removed in desugar_value_def_suffixed"
),
ast::Expr::Tag(tag) => {
let variant_var = var_store.fresh();
let ext_var = var_store.fresh();
Expand Down Expand Up @@ -1371,7 +1371,10 @@ pub fn canonicalize_expr<'a>(
use roc_problem::can::RuntimeError::*;

let sub_region = Region::span_across(&loc_name.region, &loc_value.region);
let problem = OptionalFieldInRecordBuilder {record: region, field: sub_region };
let problem = OptionalFieldInRecordBuilder {
record: region,
field: sub_region,
};
env.problem(Problem::RuntimeError(problem.clone()));

(RuntimeError(problem), Output::default())
Expand Down Expand Up @@ -2515,7 +2518,7 @@ pub fn is_valid_interpolation(expr: &ast::Expr<'_>) -> bool {
ast::Expr::TupleAccess(sub_expr, _)
| ast::Expr::ParensAround(sub_expr)
| ast::Expr::RecordAccess(sub_expr, _)
| ast::Expr::TaskAwaitBang(sub_expr) => is_valid_interpolation(sub_expr),
| ast::Expr::TrySuffix { expr: sub_expr, .. } => is_valid_interpolation(sub_expr),
ast::Expr::Apply(loc_expr, args, _called_via) => {
is_valid_interpolation(&loc_expr.value)
&& args
Expand Down
Loading

0 comments on commit a14a110

Please sign in to comment.