diff --git a/compiler/parse/src/expr.rs b/compiler/parse/src/expr.rs index 11453dbb25..480b3fb302 100644 --- a/compiler/parse/src/expr.rs +++ b/compiler/parse/src/expr.rs @@ -614,7 +614,7 @@ fn annotated_body<'a>(min_indent: u16) -> impl Parser<'a, AnnotationOrAnnotatedB fn annotation_or_alias<'a>( arena: &'a Bump, pattern: &Pattern<'a>, - region: Region, + pattern_region: Region, loc_ann: Located>, ) -> Def<'a> { use crate::ast::Pattern::*; @@ -625,21 +625,21 @@ fn annotation_or_alias<'a>( GlobalTag(name) => Def::Alias { name: Located { value: name, - region, + region: pattern_region, }, vars: &[], ann: loc_ann, }, Apply( Located { - region, + region: pattern_region, value: Pattern::GlobalTag(name), }, loc_vars, ) => Def::Alias { name: Located { value: name, - region: *region, + region: *pattern_region, }, vars: loc_vars, ann: loc_ann, @@ -648,14 +648,14 @@ fn annotation_or_alias<'a>( Def::NotYetImplemented("TODO gracefully handle invalid Apply in type annotation") } SpaceAfter(value, spaces_before) => Def::SpaceAfter( - arena.alloc(annotation_or_alias(arena, value, region, loc_ann)), + arena.alloc(annotation_or_alias(arena, value, pattern_region, loc_ann)), spaces_before, ), SpaceBefore(value, spaces_before) => Def::SpaceBefore( - arena.alloc(annotation_or_alias(arena, value, region, loc_ann)), + arena.alloc(annotation_or_alias(arena, value, pattern_region, loc_ann)), spaces_before, ), - Nested(value) => annotation_or_alias(arena, value, region, loc_ann), + Nested(value) => annotation_or_alias(arena, value, pattern_region, loc_ann), PrivateTag(_) => { Def::NotYetImplemented("TODO gracefully handle trying to use a private tag as an annotation.") @@ -676,7 +676,7 @@ fn annotation_or_alias<'a>( // This is a regular Annotation Def::Annotation( Located { - region, + region: pattern_region, value: Pattern::Identifier(ident), }, loc_ann, @@ -686,7 +686,7 @@ fn annotation_or_alias<'a>( // This is a record destructure Annotation Def::Annotation( Located { - region, + region: pattern_region, value: Pattern::RecordDestructure(loc_patterns), }, loc_ann, @@ -768,6 +768,8 @@ fn parse_def_expr<'a>( region: loc_first_body.region, } }; + let def_region = + Region::span_across(&loc_first_pattern.region, &loc_first_body.region); let first_def: Def<'a> = // TODO is there some way to eliminate this .clone() here? @@ -775,7 +777,7 @@ fn parse_def_expr<'a>( let loc_first_def = Located { value: first_def, - region: loc_first_pattern.region, + region: def_region, }; // for formatting reasons, we must insert the first def first! @@ -866,15 +868,21 @@ fn parse_def_signature<'a>( .map( move |(((loc_first_annotation, opt_body), (mut defs, loc_ret)), state)| { let loc_first_def: Located> = match opt_body { - None => Located { - value: annotation_or_alias( - arena, - &loc_first_pattern.value, - loc_first_pattern.region, - loc_first_annotation, - ), - region: loc_first_pattern.region, - }, + None => { + let region = Region::span_across( + &loc_first_pattern.region, + &loc_first_annotation.region, + ); + Located { + value: annotation_or_alias( + arena, + &loc_first_pattern.value, + loc_first_pattern.region, + loc_first_annotation, + ), + region, + } + } Some((opt_comment, (body_pattern, body_expr))) => { let region = Region::span_across(&loc_first_pattern.region, &body_expr.region); diff --git a/compiler/parse/tests/test_parse.rs b/compiler/parse/tests/test_parse.rs index 9ac546d639..3f07260220 100644 --- a/compiler/parse/tests/test_parse.rs +++ b/compiler/parse/tests/test_parse.rs @@ -1486,7 +1486,7 @@ mod test_parse { arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))), arena.alloc(Located::new(1, 1, 2, 3, Num("5"))), ); - let loc_def = &*arena.alloc(Located::new(1, 1, 0, 1, def)); + let loc_def = &*arena.alloc(Located::new(1, 1, 0, 3, def)); let defs = &[loc_def]; let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice()); let loc_ret = Located::new(3, 3, 0, 2, ret); @@ -1516,7 +1516,7 @@ mod test_parse { arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))), arena.alloc(Located::new(1, 1, 4, 5, Num("5"))), ); - let loc_def = &*arena.alloc(Located::new(1, 1, 0, 1, def)); + let loc_def = &*arena.alloc(Located::new(1, 1, 0, 5, def)); let defs = &[loc_def]; let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice()); let loc_ret = Located::new(3, 3, 0, 2, ret); @@ -1547,7 +1547,7 @@ mod test_parse { arena.alloc(Located::new(1, 1, 0, 1, Identifier("x"))), arena.alloc(Located::new(1, 1, 4, 5, Num("5"))), ); - let loc_def1 = &*arena.alloc(Located::new(1, 1, 0, 1, def1)); + let loc_def1 = &*arena.alloc(Located::new(1, 1, 0, 5, def1)); let def2 = Def::SpaceBefore( &*arena.alloc(Def::Body( arena.alloc(Located::new(2, 2, 0, 1, Identifier("y"))), @@ -1591,7 +1591,7 @@ mod test_parse { arena.alloc(Located::new(1, 1, 1, 8, RecordDestructure(&fields))), arena.alloc(Located::new(1, 1, 11, 12, Num("5"))), ); - let loc_def1 = &*arena.alloc(Located::new(1, 1, 1, 8, def1)); + let loc_def1 = &*arena.alloc(Located::new(1, 1, 1, 12, def1)); let def2 = Def::SpaceBefore( &*arena.alloc(Def::Body( arena.alloc(Located::new(2, 2, 0, 1, Identifier("y"))), @@ -1679,7 +1679,7 @@ mod test_parse { Located::new(0, 0, 6, 33, as_ann), ); - let loc_ann = &*arena.alloc(Located::new(0, 0, 0, 3, signature)); + let loc_ann = &*arena.alloc(Located::new(0, 0, 0, 33, signature)); let defs = &[loc_ann]; let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice()); let loc_ret = Located::new(2, 2, 0, 2, ret); @@ -1715,7 +1715,7 @@ mod test_parse { ann: Located::new(0, 0, 11, 26, applied_alias), }; - let loc_ann = &*arena.alloc(Located::new(0, 0, 0, 4, signature)); + let loc_ann = &*arena.alloc(Located::new(0, 0, 0, 26, signature)); let defs = &[loc_ann]; let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines.into_bump_slice()); let loc_ret = Located::new(2, 2, 0, 2, ret); @@ -2320,7 +2320,7 @@ mod test_parse { arena.alloc(Located::new(0, 0, 0, 1, Identifier("x"))), arena.alloc(Located::new(1, 1, 4, 5, Expr::SpaceBefore(num, &[Newline]))), ); - let loc_def = &*arena.alloc(Located::new(0, 0, 0, 1, def)); + let loc_def = &*arena.alloc(Located::new(0, 1, 0, 5, def)); let defs = &[loc_def]; let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines); let loc_ret = Located::new(3, 3, 0, 2, ret); @@ -2349,7 +2349,7 @@ mod test_parse { arena.alloc(Located::new(6, 6, 0, 1, Identifier("x"))), arena.alloc(Located::new(6, 6, 4, 5, Num("5"))), ); - let loc_def = &*arena.alloc(Located::new(6, 6, 0, 1, def)); + let loc_def = &*arena.alloc(Located::new(6, 6, 0, 5, def)); let defs = &[loc_def]; let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines); let loc_ret = Located::new(8, 8, 0, 2, ret); @@ -2392,7 +2392,7 @@ mod test_parse { arena.alloc(Located::new(4, 4, 0, 1, Identifier("x"))), arena.alloc(Located::new(4, 4, 4, 5, Num("5"))), ); - let loc_def = &*arena.alloc(Located::new(4, 4, 0, 1, def)); + let loc_def = &*arena.alloc(Located::new(4, 4, 0, 5, def)); let defs = &[loc_def]; let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines); let loc_ret = Located::new(6, 6, 0, 2, ret); @@ -2431,7 +2431,7 @@ mod test_parse { arena.alloc(Located::new(4, 4, 0, 1, Identifier("x"))), arena.alloc(Located::new(4, 4, 4, 5, Num("5"))), ); - let loc_def = &*arena.alloc(Located::new(4, 4, 0, 1, def)); + let loc_def = &*arena.alloc(Located::new(4, 4, 0, 5, def)); let defs = &[loc_def]; let ret = Expr::SpaceBefore(arena.alloc(Num("42")), newlines); let loc_ret = Located::new(6, 6, 0, 2, ret);