improve formatting of records in pattern exhaustiveness errors

This commit is contained in:
Folkert 2020-04-23 21:49:24 +02:00 committed by Richard Feldman
parent 8284575a47
commit 3e36bea700
5 changed files with 81 additions and 30 deletions

View file

@ -1,5 +1,5 @@
use roc_collections::all::{Index, MutMap};
use roc_module::ident::TagName;
use roc_module::ident::{Lowercase, TagName};
use roc_region::all::{Located, Region};
use self::Pattern::*;
@ -7,6 +7,14 @@ use self::Pattern::*;
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub struct Union {
pub alternatives: Vec<Ctor>,
pub render_as: RenderAs,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
pub enum RenderAs {
Tag,
Record(Vec<Lowercase>),
Guard,
}
#[derive(Clone, Debug, PartialEq, Eq, Hash, Copy)]
@ -52,7 +60,20 @@ fn simplify<'a>(pattern: &crate::expr::Pattern<'a>) -> Pattern {
Identifier(_) => Anything,
RecordDestructure(destructures, _) => {
let tag_id = TagId(0);
let mut patterns = std::vec::Vec::with_capacity(destructures.len());
let mut field_names = std::vec::Vec::with_capacity(destructures.len());
for destruct in destructures {
field_names.push(destruct.label.clone());
match &destruct.guard {
None => patterns.push(Anything),
Some(guard) => patterns.push(simplify(guard)),
}
}
let union = Union {
render_as: RenderAs::Record(field_names),
alternatives: vec![Ctor {
name: TagName::Global("#Record".into()),
tag_id,
@ -60,15 +81,6 @@ fn simplify<'a>(pattern: &crate::expr::Pattern<'a>) -> Pattern {
}],
};
let mut patterns = std::vec::Vec::with_capacity(destructures.len());
for destruct in destructures {
match &destruct.guard {
None => patterns.push(Anything),
Some(guard) => patterns.push(simplify(guard)),
}
}
Ctor(union, tag_id, patterns)
}
@ -306,6 +318,7 @@ fn to_nonredundant_rows<'a>(
let tag_id = TagId(0);
let union = Union {
render_as: RenderAs::Guard,
alternatives: vec![Ctor {
tag_id,
name: TagName::Global("#Guard".into()),