Implement better comparisons between alias and body

This commit is contained in:
Joshua Warner 2024-12-11 21:22:59 -08:00
parent 8b3abafa08
commit 07f7252573
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
5 changed files with 87 additions and 5 deletions

View file

@ -3201,11 +3201,7 @@ fn stmts_to_defs<'a>(
) = (td, stmts.get(i + 1).map(|s| (s.before, s.item.value)))
{
if (spaces_middle.len() <= 1 && !ends_with_spaces_conservative(&ann_type.value))
|| header
.vars
.first()
.map(|var| var.value.equivalent(&loc_pattern.value))
.unwrap_or(false)
|| header_to_pat(arena, header).equivalent(&loc_pattern.value)
{
// This is a case like
// UserId x : [UserId Int]
@ -3322,6 +3318,17 @@ fn stmts_to_defs<'a>(
Ok((defs, last_expr))
}
fn header_to_pat<'a>(arena: &'a Bump, header: TypeHeader<'a>) -> Pattern<'a> {
if header.vars.is_empty() {
Pattern::Tag(header.name.value)
} else {
Pattern::Apply(
arena.alloc(Loc::at(header.name.region, Pattern::Tag(header.name.value))),
header.vars,
)
}
}
fn ends_with_spaces_conservative(ty: &TypeAnnotation<'_>) -> bool {
match ty {
TypeAnnotation::Function(_, _, res) => ends_with_spaces_conservative(&res.value),

View file

@ -0,0 +1,4 @@
B @A : w
#
@A = e
i

View file

@ -0,0 +1,66 @@
@0-14 SpaceAfter(
Defs(
Defs {
tags: [
EitherIndex(0),
EitherIndex(2147483648),
],
regions: [
@0-5,
@8-12,
],
space_before: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 2 },
],
space_after: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
Slice<roc_parse::ast::CommentOrNewline> { start: 2, length: 0 },
],
spaces: [
Newline,
LineComment(
"",
),
],
type_defs: [
Alias {
header: TypeHeader {
name: @0-1 "B",
vars: [
@1-3 OpaqueRef(
"@A",
),
],
},
ann: @4-5 BoundVariable(
"w",
),
},
],
value_defs: [
Body(
@8-10 OpaqueRef(
"@A",
),
@11-12 Var {
module_name: "",
ident: "e",
},
),
],
},
@13-14 SpaceBefore(
Var {
module_name: "",
ident: "i",
},
[
Newline,
],
),
),
[
Newline,
],
)

View file

@ -0,0 +1,4 @@
B@A:w
#
@A=e
i

View file

@ -528,6 +528,7 @@ mod test_snapshots {
pass/opaque_comment_after_head.expr,
pass/opaque_destructure_first_item_in_body.expr,
pass/opaque_has_abilities.expr,
pass/opaque_in_ann_apply_arg.expr,
pass/opaque_reference_expr.expr,
pass/opaque_reference_expr_with_arguments.expr,
pass/opaque_reference_pattern.expr,