Skip to content

Commit

Permalink
Merge pull request #6868 from kdziamura/suffixed-stmt-desugaring
Browse files Browse the repository at this point in the history
  • Loading branch information
kdziamura committed Jul 24, 2024
2 parents a94994b + 7aa31a1 commit d5db313
Show file tree
Hide file tree
Showing 31 changed files with 1,277 additions and 371 deletions.
34 changes: 23 additions & 11 deletions crates/compiler/can/src/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use roc_module::ident::ModuleName;
use roc_parse::ast::Expr::{self, *};
use roc_parse::ast::{
AssignedField, Collection, ModuleImportParams, OldRecordBuilderField, Pattern, StrLiteral,
StrSegment, ValueDef, WhenBranch,
StrSegment, TypeAnnotation, ValueDef, WhenBranch,
};
use roc_region::all::{LineInfo, Loc, Region};

Expand Down Expand Up @@ -154,15 +154,26 @@ fn desugar_value_def<'a>(
IngestedFileImport(_) => *def,

Stmt(stmt_expr) => {
// desugar into a Body({}, stmt_expr)
let loc_pattern = arena.alloc(Loc::at(
stmt_expr.region,
Pattern::RecordDestructure(Collection::empty()),
));
Body(
loc_pattern,
desugar_expr(arena, stmt_expr, src, line_info, module_path),
)
// desugar `stmt_expr!` to
// _ : {}
// _ = stmt_expr!

let region = stmt_expr.region;
let new_pat = arena.alloc(Loc::at(region, Pattern::Underscore("#!stmt")));

ValueDef::AnnotatedBody {
ann_pattern: new_pat,
ann_type: arena.alloc(Loc::at(
region,
TypeAnnotation::Record {
fields: Collection::empty(),
ext: None,
},
)),
comment: None,
body_pattern: new_pat,
body_expr: desugar_expr(arena, stmt_expr, src, line_info, module_path),
}
}
}
}
Expand Down Expand Up @@ -211,7 +222,7 @@ pub fn desugar_value_def_suffixed<'a>(arena: &'a Bump, value_def: ValueDef<'a>)
arena,
Body(
loc_pattern,
apply_task_await(arena, loc_expr.region, sub_arg, sub_pat, sub_new),
apply_task_await(arena, loc_expr.region, sub_arg, sub_pat, sub_new, None),
),
),
Err(..) => Body(
Expand Down Expand Up @@ -254,6 +265,7 @@ pub fn desugar_value_def_suffixed<'a>(arena: &'a Bump, value_def: ValueDef<'a>)
sub_arg,
sub_pat,
sub_new,
Some((ann_pattern, ann_type)),
),
},
),
Expand Down
250 changes: 172 additions & 78 deletions crates/compiler/can/src/suffixed.rs

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Defs {
@15-22 Closure(
[
@17-18 Identifier {
ident: "#!a0",
ident: "#!0_arg",
},
],
@15-22 Apply(
Expand All @@ -51,7 +51,7 @@ Defs {
@15-22 Closure(
[
@20-21 Identifier {
ident: "#!a1",
ident: "#!1_arg",
},
],
@15-22 Defs(
Expand Down Expand Up @@ -83,11 +83,11 @@ Defs {
[
@17-18 Var {
module_name: "",
ident: "#!a0",
ident: "#!0_arg",
},
@20-21 Var {
module_name: "",
ident: "#!a1",
ident: "#!1_arg",
},
],
Space,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Defs {
@15-19 Closure(
[
@17-18 Identifier {
ident: "#!a0",
ident: "#!0_arg",
},
],
@15-19 Defs(
Expand Down Expand Up @@ -67,7 +67,7 @@ Defs {
[
@17-18 Var {
module_name: "",
ident: "#!a0",
ident: "#!0_arg",
},
],
Space,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Defs {
@15-33 Closure(
[
Identifier {
ident: "#!a0",
ident: "#!0_arg",
},
],
@15-33 Defs(
Expand Down Expand Up @@ -78,7 +78,7 @@ Defs {
@20-32 ParensAround(
Var {
module_name: "",
ident: "#!a0",
ident: "#!0_arg",
},
),
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ Defs {
@15-35 Closure(
[
Identifier {
ident: "#!a0",
ident: "#!0_arg",
},
],
@15-35 Defs(
Expand Down Expand Up @@ -73,7 +73,7 @@ Defs {
@16-26 ParensAround(
Var {
module_name: "",
ident: "#!a0",
ident: "#!0_arg",
},
),
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Defs {
@15-22 Closure(
[
@15-16 Identifier {
ident: "#!a0",
ident: "#!0_arg",
},
],
@15-22 Defs(
Expand Down Expand Up @@ -67,7 +67,7 @@ Defs {
[
@15-16 Var {
module_name: "",
ident: "#!a0",
ident: "#!0_arg",
},
],
BinOp(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,58 @@ Defs {
ident: "await",
},
[
@11-15 Var {
module_name: "",
ident: "foo",
},
@11-15 Defs(
Defs {
tags: [
Index(2147483648),
],
regions: [
@11-15,
],
space_before: [
Slice(start = 0, length = 0),
],
space_after: [
Slice(start = 0, length = 0),
],
spaces: [],
type_defs: [],
value_defs: [
AnnotatedBody {
ann_pattern: @11-15 Identifier {
ident: "#!0_stmt",
},
ann_type: @11-15 Apply(
"",
"Task",
[
@11-15 Record {
fields: [],
ext: None,
},
@11-15 Inferred,
],
),
comment: None,
body_pattern: @11-15 Identifier {
ident: "#!0_stmt",
},
body_expr: @11-15 Var {
module_name: "",
ident: "foo",
},
},
],
},
@11-15 Var {
module_name: "",
ident: "#!0_stmt",
},
),
@11-15 Closure(
[
@11-15 RecordDestructure(
[],
@11-15 Underscore(
"#!stmt",
),
],
@21-26 Apply(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Defs {
@16-35 Closure(
[
Identifier {
ident: "#!a0",
ident: "#!0_arg",
},
],
@16-35 Defs(
Expand Down Expand Up @@ -63,7 +63,7 @@ Defs {
@17-29 ParensAround(
Var {
module_name: "",
ident: "#!a0",
ident: "#!0_arg",
},
),
[
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,25 +55,69 @@ Defs {
ident: "await",
},
[
@31-42 Apply(
@31-42 Defs(
Defs {
tags: [
Index(2147483648),
],
regions: [
@31-42,
],
space_before: [
Slice(start = 0, length = 0),
],
space_after: [
Slice(start = 0, length = 0),
],
spaces: [],
type_defs: [],
value_defs: [
AnnotatedBody {
ann_pattern: @31-42 Identifier {
ident: "#!0_stmt",
},
ann_type: @31-42 Apply(
"",
"Task",
[
@31-42 Record {
fields: [],
ext: None,
},
@31-42 Inferred,
],
),
comment: None,
body_pattern: @31-42 Identifier {
ident: "#!0_stmt",
},
body_expr: @31-42 Apply(
@31-42 Var {
module_name: "",
ident: "line",
},
[
@31-34 Var {
module_name: "",
ident: "msg",
},
],
BinOp(
Pizza,
),
),
},
],
},
@31-42 Var {
module_name: "",
ident: "line",
ident: "#!0_stmt",
},
[
@31-34 Var {
module_name: "",
ident: "msg",
},
],
BinOp(
Pizza,
),
),
@31-42 Closure(
[
@31-42 RecordDestructure(
[],
@31-42 Underscore(
"#!stmt",
),
],
@52-57 Apply(
Expand Down
Loading

0 comments on commit d5db313

Please sign in to comment.