accept a newline and body when patterns are different

This commit is contained in:
Folkert 2020-07-05 18:34:28 +02:00
parent 1b4c5cac25
commit 58dfeba043
3 changed files with 32 additions and 1 deletions

View file

@ -151,7 +151,7 @@ pub fn canonicalize_defs<'a>(
match iter.peek() { match iter.peek() {
Some(Located { Some(Located {
value: Body(body_pattern, body_expr), value: Body(body_pattern, body_expr),
.. region: body_region,
}) => { }) => {
if pattern.value.equivalent(&body_pattern.value) { if pattern.value.equivalent(&body_pattern.value) {
iter.next(); iter.next();
@ -165,6 +165,10 @@ pub fn canonicalize_defs<'a>(
&mut scope, &mut scope,
pattern_type, pattern_type,
) )
} else if loc_def.region.lines_between(body_region) > 1 {
// there is a line of whitespace between the annotation and the body
// treat annotation and body separately
to_pending_def(env, var_store, &loc_def.value, &mut scope, pattern_type)
} else { } else {
// the pattern of the annotation does not match the pattern of the body directly below it // the pattern of the annotation does not match the pattern of the body directly below it
env.problems.push(Problem::SignatureDefMismatch { env.problems.push(Problem::SignatureDefMismatch {

View file

@ -78,6 +78,17 @@ impl Region {
Self::zero() Self::zero()
} }
} }
pub fn lines_between(&self, other: &Region) -> u32 {
if self.end_line <= other.start_line {
other.start_line - self.end_line
} else if self.start_line >= other.end_line {
self.start_line - other.end_line
} else {
// intersection
0
}
}
} }
#[test] #[test]

View file

@ -2839,6 +2839,22 @@ mod test_reporting {
) )
} }
#[test]
fn annotation_newline_body_is_fine() {
report_problem_as(
indoc!(
r#"
bar : Int
foo = \x -> x
foo bar
"#
),
indoc!(""),
)
}
#[test] #[test]
fn invalid_alias_rigid_var_pattern() { fn invalid_alias_rigid_var_pattern() {
report_problem_as( report_problem_as(