Analyze match expression default cases

This commit is contained in:
Sasha Koshka 2024-03-25 11:29:24 -04:00
parent d8f82d3646
commit 6916b3b7b1
2 changed files with 26 additions and 2 deletions

View File

@ -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,
`

View File

@ -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()