Propogate original annotation region down in AnnotationSource

This makes it easier for error reporting to find the relevant
annotations that were part of a type error, and display that in the
error message presented to a user.
This commit is contained in:
ayazhafiz 2021-11-20 23:46:37 -05:00
parent 7a0ecbd262
commit 62873fed81
3 changed files with 29 additions and 6 deletions

View file

@ -448,7 +448,7 @@ pub fn constrain_expr(
branch_cons.push(cond_var_is_bool_con); branch_cons.push(cond_var_is_bool_con);
match expected { match expected {
FromAnnotation(name, arity, _, tipe) => { FromAnnotation(name, arity, ann_source, tipe) => {
let num_branches = branches.len() + 1; let num_branches = branches.len() + 1;
for (index, (loc_cond, loc_body)) in branches.iter().enumerate() { for (index, (loc_cond, loc_body)) in branches.iter().enumerate() {
let cond_con = constrain_expr( let cond_con = constrain_expr(
@ -468,6 +468,7 @@ pub fn constrain_expr(
AnnotationSource::TypedIfBranch { AnnotationSource::TypedIfBranch {
index: Index::zero_based(index), index: Index::zero_based(index),
num_branches, num_branches,
region: ann_source.region(),
}, },
tipe.clone(), tipe.clone(),
), ),
@ -487,6 +488,7 @@ pub fn constrain_expr(
AnnotationSource::TypedIfBranch { AnnotationSource::TypedIfBranch {
index: Index::zero_based(branches.len()), index: Index::zero_based(branches.len()),
num_branches, num_branches,
region: ann_source.region(),
}, },
tipe.clone(), tipe.clone(),
), ),
@ -577,7 +579,7 @@ pub fn constrain_expr(
constraints.push(expr_con); constraints.push(expr_con);
match &expected { match &expected {
FromAnnotation(name, arity, _, _typ) => { FromAnnotation(name, arity, ann_source, _typ) => {
// NOTE deviation from elm. // NOTE deviation from elm.
// //
// in elm, `_typ` is used, but because we have this `expr_var` too // in elm, `_typ` is used, but because we have this `expr_var` too
@ -604,6 +606,7 @@ pub fn constrain_expr(
*arity, *arity,
TypedWhenBranch { TypedWhenBranch {
index: Index::zero_based(index), index: Index::zero_based(index),
region: ann_source.region(),
}, },
typ.clone(), typ.clone(),
), ),

View file

@ -1109,9 +1109,28 @@ pub enum PReason {
#[derive(Debug, Clone, Copy, PartialEq, Eq)] #[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum AnnotationSource { pub enum AnnotationSource {
TypedIfBranch { index: Index, num_branches: usize }, TypedIfBranch {
TypedWhenBranch { index: Index }, index: Index,
TypedBody { region: Region }, num_branches: usize,
region: Region,
},
TypedWhenBranch {
index: Index,
region: Region,
},
TypedBody {
region: Region,
},
}
impl AnnotationSource {
pub fn region(&self) -> Region {
match self {
&Self::TypedIfBranch { region, .. }
| &Self::TypedWhenBranch { region, .. }
| &Self::TypedBody { region, .. } => region,
}
}
} }
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]

View file

@ -319,6 +319,7 @@ fn to_expr_report<'b>(
TypedIfBranch { TypedIfBranch {
index, index,
num_branches, num_branches,
region: _,
} if num_branches == 2 => alloc.concat(vec![ } if num_branches == 2 => alloc.concat(vec![
alloc.keyword(if index == Index::FIRST { alloc.keyword(if index == Index::FIRST {
"then" "then"
@ -335,7 +336,7 @@ fn to_expr_report<'b>(
alloc.keyword("if"), alloc.keyword("if"),
alloc.text(" expression:"), alloc.text(" expression:"),
]), ]),
TypedWhenBranch { index } => alloc.concat(vec![ TypedWhenBranch { index, region: _ } => alloc.concat(vec![
alloc.string(index.ordinal()), alloc.string(index.ordinal()),
alloc.reflow(" branch of this "), alloc.reflow(" branch of this "),
alloc.keyword("when"), alloc.keyword("when"),