diff --git a/analyzer/control-flow_test.go b/analyzer/control-flow_test.go index dd9ff34..d2437a2 100644 --- a/analyzer/control-flow_test.go +++ b/analyzer/control-flow_test.go @@ -33,6 +33,16 @@ U: (| Int F64) `) } +func TestMatchDefault (test *testing.T) { +testString (test, +` +U: (| Int F64) +[isInt u:U]:Bool = match u + | u:Int [return true] + * false +`) +} + func TestMatchUnionUnderComplete (test *testing.T) { testString (test, ` diff --git a/analyzer/expression.go b/analyzer/expression.go index 8d87007..a617053 100644 --- a/analyzer/expression.go +++ b/analyzer/expression.go @@ -759,7 +759,21 @@ func (this *Tree) analyzeMatch ( match.CaseMap[hash] = cas } - if into != nil && len(match.Cases) < len(rawUnion.Allowed) { + if match.Default != nil { + this.pushScope(match.Default) + expression, err := this.analyzeExpression ( + into, mode, + match.Default.Expression) + this.popScope() + + if err != nil { return nil, err } + match.Default.Expression = expression + } + + allCasesCovered := + len(match.Cases) == len(rawUnion.Allowed) || + match.Default != nil + if into != nil && !allCasesCovered { return nil, errors.Errorf ( match.Position(), "match does not cover all types within %v", @@ -772,7 +786,7 @@ func (this *Tree) analyzeMatch ( } func (this *Tree) assembleMatchMap (match *entity.Match) (*entity.Match, error) { - match.CaseMap = make(map[entity.Hash] *entity.Case) + match.CaseMap = make(map[entity.Hash] *entity.MatchCase) match.CaseOrder = make([]entity.Hash, len(match.Cases)) for index, cas := range match.Cases { hash := cas.Declaration.Ty.Hash()