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 // then, check for union assignment
if destination, ok := this.isUnion(destination); ok { if _, ok := this.isUnion(destination); ok {
return this.canAssignUnion(pos, destination, source) return this.canAssignUnion(pos, destination, source)
} }
@ -296,11 +296,18 @@ func (this *Tree) canAssignInterface (
// destination. // destination.
func (this *Tree) canAssignUnion ( func (this *Tree) canAssignUnion (
pos errors.Position, pos errors.Position,
destination *entity.TypeUnion, destination entity.Type,
source entity.Type, source entity.Type,
) error { ) error {
hash := source.Hash() sourceHash := source.Hash()
if _, ok := destination.AllowedMap[hash]; ok { return nil } 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 ( return errors.Errorf (
pos, "%v is not included within union %v", pos, "%v is not included within union %v",
source, destination) source, destination)

View File

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