From 58dfeba043f7764d31556cacb031b61ecac32312 Mon Sep 17 00:00:00 2001 From: Folkert Date: Sun, 5 Jul 2020 18:34:28 +0200 Subject: [PATCH] accept a newline and body when patterns are different --- compiler/can/src/def.rs | 6 +++++- compiler/region/src/all.rs | 11 +++++++++++ compiler/reporting/tests/test_reporting.rs | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) diff --git a/compiler/can/src/def.rs b/compiler/can/src/def.rs index 902f18bad3..fd3c0d010f 100644 --- a/compiler/can/src/def.rs +++ b/compiler/can/src/def.rs @@ -151,7 +151,7 @@ pub fn canonicalize_defs<'a>( match iter.peek() { Some(Located { value: Body(body_pattern, body_expr), - .. + region: body_region, }) => { if pattern.value.equivalent(&body_pattern.value) { iter.next(); @@ -165,6 +165,10 @@ pub fn canonicalize_defs<'a>( &mut scope, 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 { // the pattern of the annotation does not match the pattern of the body directly below it env.problems.push(Problem::SignatureDefMismatch { diff --git a/compiler/region/src/all.rs b/compiler/region/src/all.rs index 372f503ea0..08230b4729 100644 --- a/compiler/region/src/all.rs +++ b/compiler/region/src/all.rs @@ -78,6 +78,17 @@ impl Region { 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] diff --git a/compiler/reporting/tests/test_reporting.rs b/compiler/reporting/tests/test_reporting.rs index 63fc5c0731..58fd8a2fb7 100644 --- a/compiler/reporting/tests/test_reporting.rs +++ b/compiler/reporting/tests/test_reporting.rs @@ -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] fn invalid_alias_rigid_var_pattern() { report_problem_as(