Merge pull request #7128 from roc-lang/fix-7103

Ignore final try suffix for annotated top-level defs
This commit is contained in:
Luke Boswell 2024-10-02 21:02:17 +10:00 committed by GitHub
commit 2c62f773ef
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 109 additions and 3 deletions

View File

@ -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,
@ -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,

View File

@ -325,8 +325,15 @@ 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)));
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
match unwrap_suffixed_expression(arena, function, maybe_def_pat) {

View File

@ -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",
},
),
],
}

View File

@ -632,6 +632,18 @@ mod suffixed_tests {
"##
);
}
#[test]
fn issue_7103() {
run_test!(
r##"
run : Task {} _
run = line! "foo"
main = run
"##
);
}
}
#[cfg(test)]