Analyzer lets unions of the same type be assigned to eachother

This commit is contained in:
Sasha Koshka 2024-03-06 14:00:37 -05:00
parent 716306dacb
commit a1dd782a88
2 changed files with 14 additions and 7 deletions

View File

@ -91,7 +91,7 @@ func (this *Tree) canAssign (
}
// then, check for union assignment
if destination, ok := this.isUnion(destination); ok {
if _, ok := this.isUnion(destination); ok {
return this.canAssignUnion(pos, destination, source)
}
@ -295,12 +295,19 @@ func (this *Tree) canAssignInterface (
// analyzed source type, and determines whether source can be assigned to
// destination.
func (this *Tree) canAssignUnion (
pos errors.Position,
destination *entity.TypeUnion,
source entity.Type,
pos errors.Position,
destination entity.Type,
source entity.Type,
) error {
hash := source.Hash()
if _, ok := destination.AllowedMap[hash]; ok { return nil }
sourceHash := source.Hash()
destinationHash := destination.Hash()
union, _ := this.isUnion(destination)
// check if types are identical
if sourceHash == destinationHash { return nil }
// check if type is included within the union
if _, ok := union.AllowedMap[sourceHash]; ok { return nil }
return errors.Errorf (
pos, "%v is not included within union %v",
source, destination)

View File

@ -273,7 +273,7 @@ U: (| Int F64)
func TestAssignmentUnionErrNotListed (test *testing.T) {
testStringErr (test,
"I64 is not included within union (| Int F64)", 5, 8,
"I64 is not included within union U", 5, 8,
`
U: (| Int F64)
[main] = {