Skip to content

Commit 2f4fb12

Browse files
authored
fix empty union (#368)
while one would just use `BottomType` for empty `Union` it is possible to write `Union({})` this fixes the error
1 parent 43091a5 commit 2f4fb12

File tree

2 files changed

+9
-0
lines changed

2 files changed

+9
-0
lines changed

src/kirin/lattice/abc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,12 @@ def top(cls) -> BoundedLatticeType: ...
9090
class UnionMeta(LatticeMeta):
9191
"""Meta class for union types. It simplifies the union if possible."""
9292

93+
def __init__(self, name, bases, attrs):
94+
super().__init__(name, bases, attrs)
95+
if not issubclass(base := bases[0], BoundedLattice):
96+
raise TypeError(f"Union must inherit from Lattice, got {bases[0]}")
97+
self._bottom = base.bottom()
98+
9399
def __call__(
94100
self,
95101
typ: Iterable[LatticeType] | LatticeType,
@@ -125,4 +131,6 @@ def __call__(
125131
if len(params) == 1:
126132
return params[0]
127133

134+
if len(params) == 0:
135+
return self._bottom
128136
return super(UnionMeta, self).__call__(*params)

test/dialects/test_pytypes.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Derived(Base):
2929

3030

3131
def test_union():
32+
assert Union({}) is BottomType()
3233
assert PyClass(int) | PyClass(float) == Union(PyClass(int), PyClass(float))
3334
assert Union(PyClass(int), PyClass(int)) == PyClass(int)
3435
assert Union(PyClass(int), PyClass(float)) == Union(PyClass(int), PyClass(float))

0 commit comments

Comments
 (0)