From 0efa9a3484291bef96ab565b467672c55fd1950f Mon Sep 17 00:00:00 2001 From: whitequark Date: Sat, 3 Aug 2019 18:52:24 +0000 Subject: [PATCH] hdl.dsl: reword m.If(~True) warning to be more clear. Before this commit, it only suggested one thing (silencing it) and that's wrong almost all of the time, so suggest the right thing instead. --- nmigen/hdl/dsl.py | 5 +++-- nmigen/test/test_hdl_dsl.py | 6 ++++-- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/nmigen/hdl/dsl.py b/nmigen/hdl/dsl.py index e090b09..9a7d598 100644 --- a/nmigen/hdl/dsl.py +++ b/nmigen/hdl/dsl.py @@ -171,8 +171,9 @@ class Module(_ModuleBuilderRoot, Elaboratable): if sign: warnings.warn("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())`.)", + "-2, which is truthful. Replace `~flag` with `not flag`. (If this is " + "a false positive, silence this warning with " + "`m.If(x)` → `m.If(x.bool())`.)", SyntaxWarning, stacklevel=4) return cond diff --git a/nmigen/test/test_hdl_dsl.py b/nmigen/test/test_hdl_dsl.py index 0ff7fef..1da5a6a 100644 --- a/nmigen/test/test_hdl_dsl.py +++ b/nmigen/test/test_hdl_dsl.py @@ -273,7 +273,8 @@ class DSLTestCase(FHDLTestCase): 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())`.)"): + "truthful. Replace `~flag` with `not flag`. (If this is a false positive, " + "silence this warning with `m.If(x)` → `m.If(x.bool())`.)"): with m.If(~True): pass @@ -284,7 +285,8 @@ class DSLTestCase(FHDLTestCase): 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())`.)"): + "truthful. Replace `~flag` with `not flag`. (If this is a false positive, " + "silence this warning with `m.If(x)` → `m.If(x.bool())`.)"): with m.Elif(~True): pass -- 2.30.2