Skip to content

Commit

Permalink
dialects: (builtin) integer bitwidths must not be negative (#3799)
Browse files Browse the repository at this point in the history
  • Loading branch information
superlopuh authored Jan 29, 2025
1 parent 2c234ab commit 6cdfb16
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
6 changes: 6 additions & 0 deletions tests/dialects/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ def test_FloatType_formats():
Float128Type().format


def test_IntegerType_verifier():
IntegerType(32)
with pytest.raises(VerifyException):
IntegerType(-1)


def test_IntegerType_formats():
assert IntegerType(1).format == "<b"
assert IntegerType(2).format == "<b"
Expand Down
6 changes: 6 additions & 0 deletions xdsl/dialects/builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,12 @@ def __init__(
signedness = SignednessAttr(signedness)
super().__init__([data, signedness])

def verify(self):
if self.width.data < 0:
raise VerifyException(
f"integer type bitwidth should be nonnegative (got {self.width.data})"
)

def value_range(self) -> tuple[int, int]:
return self.signedness.data.value_range(self.width.data)

Expand Down

0 comments on commit 6cdfb16

Please sign in to comment.