Skip to content

Commit

Permalink
Merge pull request #7128 from roc-lang/fix-7103
Browse files Browse the repository at this point in the history
Ignore final try suffix for annotated top-level defs
  • Loading branch information
lukewilliamboswell authored Oct 2, 2024
2 parents 15dc028 + afb247d commit 2c62f77
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 3 deletions.
16 changes: 14 additions & 2 deletions crates/compiler/can/src/desugar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,8 +248,7 @@ pub fn desugar_value_def_suffixed<'a>(arena: &'a Bump, value_def: ValueDef<'a>)
body_pattern,
body_expr,
} => {
// note called_from_def is passed as `false` as this is a top_level_def
match unwrap_suffixed_expression(arena, body_expr, None) {
match unwrap_suffixed_expression(arena, body_expr, Some(ann_pattern)) {
Ok(new_expr) => AnnotatedBody {
ann_pattern,
ann_type,
Expand Down Expand Up @@ -280,6 +279,19 @@ pub fn desugar_value_def_suffixed<'a>(arena: &'a Bump, value_def: ValueDef<'a>)
),
},
),
// When the last expression is suffixed, it will try to unwrap the def, but because we
// have an annotated def we can simply ignore the try and return it as is without
// creating an intermediate identifier
Err(EUnwrapped::UnwrappedDefExpr { loc_expr, .. }) => desugar_value_def_suffixed(
arena,
AnnotatedBody {
ann_pattern,
ann_type,
lines_between,
body_pattern,
body_expr: loc_expr,
},
),
Err(..) => AnnotatedBody {
ann_pattern,
ann_type,
Expand Down
9 changes: 8 additions & 1 deletion crates/compiler/can/src/suffixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,14 @@ pub fn unwrap_suffixed_expression_apply_help<'a>(

let new_apply = arena.alloc(Loc::at(loc_expr.region, Expr::Apply(unwrapped_function, local_args, called_via)));

return init_unwrapped_err(arena, new_apply, maybe_def_pat, target);
match maybe_def_pat {
Some(..) => {
return Err(EUnwrapped::UnwrappedDefExpr { loc_expr: new_apply, target });
}
None => {
return init_unwrapped_err(arena, new_apply, maybe_def_pat, target);
}
}
}

// function is another expression
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
---
source: crates/compiler/can/tests/test_suffixed.rs
expression: snapshot
---
Defs {
tags: [
Index(2147483648),
Index(2147483649),
],
regions: [
@0-33,
@35-45,
],
space_before: [
Slice(start = 0, length = 0),
Slice(start = 0, length = 2),
],
space_after: [
Slice(start = 0, length = 0),
Slice(start = 2, length = 1),
],
spaces: [
Newline,
Newline,
Newline,
],
type_defs: [],
value_defs: [
AnnotatedBody {
ann_pattern: @0-3 Identifier {
ident: "run",
},
ann_type: @6-15 Apply(
"",
"Task",
[
@11-13 Record {
fields: [],
ext: None,
},
@14-15 Inferred,
],
),
lines_between: [
Newline,
],
body_pattern: @16-19 Identifier {
ident: "run",
},
body_expr: @22-33 Apply(
@22-33 Var {
module_name: "",
ident: "line",
},
[
@28-33 Str(
PlainLine(
"foo",
),
),
],
Space,
),
},
Body(
@35-39 Identifier {
ident: "main",
},
@42-45 Var {
module_name: "",
ident: "run",
},
),
],
}
12 changes: 12 additions & 0 deletions crates/compiler/can/tests/test_suffixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -632,6 +632,18 @@ mod suffixed_tests {
"##
);
}

#[test]
fn issue_7103() {
run_test!(
r##"
run : Task {} _
run = line! "foo"
main = run
"##
);
}
}

#[cfg(test)]
Expand Down

0 comments on commit 2c62f77

Please sign in to comment.