From 6e977342111c9b0359c7952dbd5bed944ca85739 Mon Sep 17 00:00:00 2001 From: Folkert Date: Sun, 15 Mar 2020 14:41:51 +0100 Subject: [PATCH] ensure Bit/Enum patterns are considered exhaustive --- compiler/mono/src/pattern.rs | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/compiler/mono/src/pattern.rs b/compiler/mono/src/pattern.rs index 638587a044..f8f4183fa2 100644 --- a/compiler/mono/src/pattern.rs +++ b/compiler/mono/src/pattern.rs @@ -39,9 +39,35 @@ fn simplify<'a>(pattern: &crate::expr::Pattern<'a>) -> Pattern { FloatLiteral(v) => Literal(Literal::Float(*v)), StrLiteral(v) => Literal(Literal::Str(v.clone())), - // TODO make sure these are exhaustive - BitLiteral(b) => Literal(Literal::Bit(*b)), - EnumLiteral { tag_id, .. } => Literal(Literal::Byte(*tag_id)), + // To make sure these are exhaustive, we have to "fake" a union here + // TODO: use the hash or some other integer to discriminate between constructors + BitLiteral(b) => { + let union = Union { + alternatives: vec![ + Ctor { + name: TagName::Global("False".into()), + arity: 0, + }, + Ctor { + name: TagName::Global("True".into()), + arity: 0, + }, + ], + }; + + Ctor(union, TagName::Global(format!("{}", b).into()), vec![]) + } + EnumLiteral { tag_id, enum_size } => { + let alternatives = (0..*enum_size) + .map(|id| Ctor { + name: TagName::Global(format!("{}", id).into()), + arity: 0, + }) + .collect(); + + let union = Union { alternatives }; + Ctor(union, TagName::Global(format!("{}", tag_id).into()), vec![]) + } Underscore => Anything, Identifier(_) => Anything,