hdl.dsl: warn on suspicious statements like m.If(~True):.

This pattern usually produces an extremely hard to notice bug that
will usually break a design when it is triggered, but will also be
hidden unless the pathological value of a boolean switch is used.

Fixes #159.
This commit is contained in:
whitequark 2019-08-03 14:00:29 +00:00
parent ab5426ce74
commit ace2b5ff0a
2 changed files with 33 additions and 0 deletions

View file

@ -268,6 +268,26 @@ class DSLTestCase(FHDLTestCase):
)
""")
def test_If_signed_suspicious(self):
m = Module()
with self.assertWarns(SyntaxWarning,
msg="Signed values in If/Elif conditions usually result from inverting Python "
"booleans with ~, which leads to unexpected results: ~True is -2, which is "
"truthful. (Silence this warning with `m.If(x)` → `m.If(x.bool())`.)"):
with m.If(~True):
pass
def test_Elif_signed_suspicious(self):
m = Module()
with m.If(0):
pass
with self.assertWarns(SyntaxWarning,
msg="Signed values in If/Elif conditions usually result from inverting Python "
"booleans with ~, which leads to unexpected results: ~True is -2, which is "
"truthful. (Silence this warning with `m.If(x)` → `m.If(x.bool())`.)"):
with m.Elif(~True):
pass
def test_Switch(self):
m = Module()
with m.Switch(self.w1):