fix tests, make clippy happy

This commit is contained in:
Luke Boswell 2024-04-14 12:39:39 +10:00
parent f9fb59f0c2
commit 93ef5e42dc
No known key found for this signature in database
GPG Key ID: F6DB3C9DB47377B0
8 changed files with 203 additions and 266 deletions

View File

@ -95,65 +95,63 @@ pub fn unwrap_suffixed_expression<'a>(
) -> Result<&'a Loc<Expr<'a>>, EUnwrapped<'a>> {
let unwrapped_expression = {
match loc_expr.value {
Expr::Var { suffixed, .. } if suffixed == 0 => Ok(loc_expr),
Expr::Var {
module_name,
ident,
suffixed,
} if suffixed == 1 => {
let unwrapped_var = arena.alloc(Loc::at(
loc_expr.region,
Expr::Var {
module_name,
ident,
suffixed: suffixed.saturating_sub(1),
},
));
} => {
match suffixed {
0 => Ok(loc_expr),
1 => {
let unwrapped_var = arena.alloc(Loc::at(
loc_expr.region,
Expr::Var {
module_name,
ident,
suffixed: suffixed.saturating_sub(1),
},
));
init_unwrapped_err(arena, unwrapped_var, maybe_def_pat)
}
Expr::Var {
module_name,
ident,
suffixed,
} if suffixed > 1 => {
let unwrapped_var = arena.alloc(Loc::at(
loc_expr.region,
Expr::Var {
module_name,
ident,
suffixed: 0,
},
));
// we generate an intermediate pattern `#!a0` etc
// so we dont unwrap the definition pattern
let (mut answer_var, answer_pat) = next_suffixed_answer_pattern(arena);
// we transfer the suffix from the Var to the intermediate answer Var
// as that will need to be unwrapped in a future call
if let Expr::Var {
module_name: "",
ident: answer_ident,
suffixed: 0,
} = answer_var
{
answer_var = Expr::Var {
module_name: "",
ident: answer_ident,
suffixed: suffixed.saturating_sub(1),
init_unwrapped_err(arena, unwrapped_var, maybe_def_pat)
}
} else {
internal_error!("expected a suffixed Var to be generated");
}
_ => {
let unwrapped_var = arena.alloc(Loc::at(
loc_expr.region,
Expr::Var {
module_name,
ident,
suffixed: 0,
},
));
Err(EUnwrapped::UnwrappedSubExpr {
sub_arg: unwrapped_var,
sub_pat: arena.alloc(Loc::at(unwrapped_var.region, answer_pat)),
sub_new: arena.alloc(Loc::at(unwrapped_var.region, answer_var)),
})
// we generate an intermediate pattern `#!a0` etc
// so we dont unwrap the definition pattern
let (mut answer_var, answer_pat) = next_suffixed_answer_pattern(arena);
// we transfer the suffix from the Var to the intermediate answer Var
// as that will need to be unwrapped in a future call
if let Expr::Var {
module_name: "",
ident: answer_ident,
suffixed: 0,
} = answer_var
{
answer_var = Expr::Var {
module_name: "",
ident: answer_ident,
suffixed: suffixed.saturating_sub(1),
}
} else {
internal_error!("expected a suffixed Var to be generated");
}
Err(EUnwrapped::UnwrappedSubExpr {
sub_arg: unwrapped_var,
sub_pat: arena.alloc(Loc::at(unwrapped_var.region, answer_pat)),
sub_new: arena.alloc(Loc::at(unwrapped_var.region, answer_var)),
})
}
}
}
Expr::Defs(..) => unwrap_suffixed_expression_defs_help(arena, loc_expr, maybe_def_pat),

View File

@ -71,7 +71,7 @@ mod suffixed_tests {
ok {}
"#,
r#"Defs { tags: [Index(2147483648)], regions: [@0-28], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @24-28 Apply(@24-28 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@24-28 Var { module_name: "", ident: "foo", suffixed: 0 }, @24-28 Closure([@24-28 RecordDestructure([])], @54-59 Apply(@54-56 Var { module_name: "", ident: "ok", suffixed: 0 }, [@57-59 Record([])], Space))], BangSuffix))] }"#,
r#"Defs { tags: [Index(2147483648)], regions: [@0-24], space_before: [Slice(start = 0, length = 0)], space_after: [Slice(start = 0, length = 0)], spaces: [], type_defs: [], value_defs: [Body(@0-4 Identifier { ident: "main", suffixed: 0 }, @20-24 Apply(@20-24 Var { module_name: "Task", ident: "await", suffixed: 0 }, [@20-24 Var { module_name: "", ident: "foo", suffixed: 0 }, @20-24 Closure([@20-24 RecordDestructure([])], @42-47 Apply(@42-44 Var { module_name: "", ident: "ok", suffixed: 0 }, [@45-47 Record([])], Space))], BangSuffix))] }"#,
);
}

View File

@ -4767,33 +4767,38 @@ mod test_reporting {
"
);
test_report!(
def_missing_final_expression,
indoc!(
r"
f : Foo.foo
"
),
@r#"
MISSING FINAL EXPRESSION in tmp/def_missing_final_expression/Test.roc
// TODO investigate this test. It was disabled in https://github.com/roc-lang/roc/pull/6634
// as the way Defs without final expressions are handled. The changes probably shouldn't have
// changed this error report. The exact same test_syntax test for this has not changed, so
// we know the parser is parsing thesame thing. Therefore the way the AST is desugared must be
// the cause of the change in error report.
// test_report!(
// def_missing_final_expression,
// indoc!(
// r"
// f : Foo.foo
// "
// ),
// @r#"
// ── MISSING FINAL EXPRESSION in tmp/def_missing_final_expression/Test.roc ───────
I am partway through parsing a definition, but I got stuck here:
// I am partway through parsing a definition, but I got stuck here:
1 app "test" provides [main] to "./platform"
2
3 main =
4 f : Foo.foo
^
// 1│ app "test" provides [main] to "./platform"
// 2│
// 3│ main =
// 4│ f : Foo.foo
// ^
This definition is missing a final expression. A nested definition
must be followed by either another definition, or an expression
// This definition is missing a final expression. A nested definition
// must be followed by either another definition, or an expression
x = 4
y = 2
// x = 4
// y = 2
x + y
"#
);
// x + y
// "#
// );
test_report!(
expression_indentation_end,

View File

@ -1,5 +1,4 @@
main =
a! "Bar"
x =
B.b! "Foo"
x = B.b! "Foo"
c! x

View File

@ -73,45 +73,20 @@ Defs {
ident: "x",
suffixed: 0,
},
@26-39 Defs(
Defs {
tags: [
Index(2147483648),
],
regions: [
@29-39,
],
space_before: [
Slice(start = 1, length = 0),
],
space_after: [
Slice(start = 1, length = 0),
],
spaces: [
Newline,
],
type_defs: [],
value_defs: [
Stmt(
@29-39 Apply(
@29-33 Var {
module_name: "B",
ident: "b",
suffixed: 1,
},
[
@34-39 Str(
PlainLine(
"Foo",
),
),
],
Space,
),
),
],
@26-39 Apply(
@29-33 Var {
module_name: "B",
ident: "b",
suffixed: 1,
},
EmptyDefsFinal,
[
@34-39 Str(
PlainLine(
"Foo",
),
),
],
Space,
),
),
Stmt(

View File

@ -10,7 +10,6 @@ app "desugar-bang"
main =
# is this a valid statement?
"Foo" |> A.x!
# what about this?
"Bar"
|> B.y!

View File

@ -122,15 +122,19 @@ Full {
Defs {
tags: [
Index(2147483648),
Index(2147483649),
],
regions: [
@199-212,
@241-284,
],
space_before: [
Slice(start = 0, length = 0),
Slice(start = 0, length = 0),
],
space_after: [
Slice(start = 0, length = 0),
Slice(start = 0, length = 0),
],
spaces: [],
type_defs: [],
@ -154,98 +158,78 @@ Full {
},
),
),
],
},
@241-299 SpaceBefore(
Defs(
Defs {
tags: [
Index(2147483648),
],
regions: [
@241-284,
],
space_before: [
Slice(start = 0, length = 0),
],
space_after: [
Slice(start = 0, length = 0),
],
spaces: [],
type_defs: [],
value_defs: [
Stmt(
@241-284 BinOps(
[
(
@241-246 Str(
PlainLine(
"Bar",
),
Stmt(
@241-284 SpaceBefore(
BinOps(
[
(
@241-246 Str(
PlainLine(
"Bar",
),
@247-249 Pizza,
),
],
@250-284 Apply(
@250-254 Var {
module_name: "B",
ident: "y",
suffixed: 1,
},
[
@264-284 SpaceBefore(
Record(
[
@266-282 RequiredValue(
@266-272 "config",
[],
@274-282 Str(
PlainLine(
"config",
),
@247-249 Pizza,
),
],
@250-284 Apply(
@250-254 Var {
module_name: "B",
ident: "y",
suffixed: 1,
},
[
@264-284 SpaceBefore(
Record(
[
@266-282 RequiredValue(
@266-272 "config",
[],
@274-282 Str(
PlainLine(
"config",
),
),
],
),
[
Newline,
),
],
),
],
Space,
),
[
Newline,
],
),
],
Space,
),
),
],
},
@290-299 SpaceBefore(
Apply(
@290-293 Var {
module_name: "C",
ident: "z",
suffixed: 0,
},
[
@294-299 Str(
PlainLine(
"Bar",
),
Newline,
Newline,
LineComment(
" what about this?",
),
],
Space,
),
[
Newline,
Newline,
],
),
],
},
@290-299 SpaceBefore(
Apply(
@290-293 Var {
module_name: "C",
ident: "z",
suffixed: 0,
},
[
@294-299 Str(
PlainLine(
"Bar",
),
),
],
Space,
),
[
Newline,
Newline,
LineComment(
" what about this?",
),
],
),
),

View File

@ -99,85 +99,62 @@ Full {
ident: "main",
suffixed: 0,
},
@100-202 SpaceBefore(
Defs(
Defs {
tags: [
Index(2147483648),
],
regions: [
@145-202,
],
space_before: [
Slice(start = 0, length = 0),
],
space_after: [
Slice(start = 0, length = 0),
],
spaces: [],
type_defs: [],
value_defs: [
Stmt(
@145-202 BinOps(
[
(
@100-114 SpaceAfter(
Str(
PlainLine(
"jq --version",
),
),
[
Newline,
],
),
@123-125 Pizza,
),
(
@126-133 SpaceAfter(
Var {
module_name: "Cmd",
ident: "new",
suffixed: 0,
},
[
Newline,
],
),
@142-144 Pizza,
),
(
@145-155 SpaceAfter(
Var {
module_name: "Cmd",
ident: "status",
suffixed: 0,
},
[
Newline,
],
),
@164-166 Pizza,
),
],
@167-202 Apply(
@167-179 Var {
module_name: "Task",
ident: "mapErr",
suffixed: 1,
},
[
@180-202 Tag(
"UnableToCheckJQVersion",
),
],
Space,
@88-202 SpaceBefore(
BinOps(
[
(
@100-114 SpaceAfter(
Str(
PlainLine(
"jq --version",
),
),
[
Newline,
],
),
@123-125 Pizza,
),
(
@126-133 SpaceAfter(
Var {
module_name: "Cmd",
ident: "new",
suffixed: 0,
},
[
Newline,
],
),
@142-144 Pizza,
),
(
@145-155 SpaceAfter(
Var {
module_name: "Cmd",
ident: "status",
suffixed: 0,
},
[
Newline,
],
),
@164-166 Pizza,
),
],
@167-202 Apply(
@167-179 Var {
module_name: "Task",
ident: "mapErr",
suffixed: 1,
},
[
@180-202 Tag(
"UnableToCheckJQVersion",
),
],
},
EmptyDefsFinal,
Space,
),
),
[
Newline,