Implement lifting for abilities

This commit is contained in:
Joshua Warner 2025-01-01 09:35:01 -05:00
parent 4da6bb0be6
commit 7fd97dec72
No known key found for this signature in database
GPG key ID: 89AD497003F93FDD
5 changed files with 90 additions and 10 deletions

View file

@ -155,16 +155,30 @@ pub fn tydef_lift_spaces<'a, 'b: 'a>(arena: &'a Bump, def: TypeDef<'b>) -> Space
}
}
TypeDef::Ability {
header: _,
loc_implements: _,
members: _,
header,
loc_implements,
members,
} => {
// TODO: if the fuzzer ever generates examples where it's important to lift spaces from the members,
// we'll need to implement this. I'm not sure that's possible, though.
Spaces {
before: &[],
item: def,
after: &[],
let new_members = arena.alloc_slice_copy(members);
if let Some(last) = new_members.last_mut() {
let typ = ann_lift_spaces_after(arena, &last.typ.value);
last.typ.value = typ.item;
Spaces {
before: &[],
item: TypeDef::Ability {
header,
loc_implements,
members: new_members,
},
after: typ.after,
}
} else {
Spaces {
before: &[],
item: def,
after: &[],
}
}
}
}
@ -1142,7 +1156,6 @@ impl<'a> Formattable for AbilityMember<'a> {
buf: &mut Buf,
_parens: Parens,
_newlines: Newlines,
indent: u16,
) {
let Spaces { before, item, .. } = self.name.value.extract_spaces();

View file

@ -0,0 +1,4 @@
S implements i : a #
#
0

View file

@ -0,0 +1,58 @@
@0-24 SpaceAfter(
Defs(
Defs {
tags: [
EitherIndex(0),
],
regions: [
@0-22,
],
space_before: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
space_after: [
Slice<roc_parse::ast::CommentOrNewline> { start: 0, length: 0 },
],
spaces: [],
type_defs: [
Ability {
header: TypeHeader {
name: @0-1 "S",
vars: [],
},
loc_implements: @2-12 Implements,
members: [
AbilityMember {
name: @13-14 "i",
typ: @16-17 SpaceAfter(
BoundVariable(
"a",
),
[
LineComment(
"",
),
LineComment(
"",
),
],
),
},
],
},
],
value_defs: [],
},
@23-24 SpaceBefore(
Num(
"0",
),
[
Newline,
],
),
),
[
Newline,
],
)

View file

@ -0,0 +1,4 @@
S implements i:(a#
#
)
0

View file

@ -458,6 +458,7 @@ mod test_snapshots {
pass/if_in_record_field_opt_pat.expr,
pass/if_newline_then_negate_else_recordupdater.expr,
pass/if_then_weird_indent.expr,
pass/implements_annotation_comment.expr,
pass/implements_in_pat_after_comment.expr,
pass/implements_newline_in_fn_ty.expr,
pass/implements_newlines_comments.expr,