From 2d4fde18735f68ff6a09e166256d6c5a9a565730 Mon Sep 17 00:00:00 2001 From: pcjordan Date: Thu, 25 Apr 2024 20:44:38 +0200 Subject: [PATCH 1/5] warn then asserting None --- opshin/type_inference.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/opshin/type_inference.py b/opshin/type_inference.py index f9b0939b..192ed2db 100644 --- a/opshin/type_inference.py +++ b/opshin/type_inference.py @@ -838,6 +838,25 @@ def visit_Attribute(self, node: Attribute) -> TypedAttribute: def visit_Assert(self, node: Assert) -> TypedAssert: ta = copy(node) ta.test = self.visit(node.test) + warn_assert_msg = ( + f" (see assert with message '{node.msg.value}')" + if node.msg is not None + else "" + ) + if isinstance(ta.test.args[0], Constant) and ta.test.args[0].value is None: + OPSHIN_LOGGER.warning( + "Asserting `None'" + + warn_assert_msg + + " is equivalent to asserting False, which always fails." + ) + if isinstance(ta.test.args[0], Call) and isinstance( + ta.test.args[0].typ.typ, UnitType + ): + OPSHIN_LOGGER.warning( + "Asserting `None'" + + warn_assert_msg + + " is likely to stem from a procedure already doing internal assertions and returning `None'. Asserting this is equivalent to asserting False, which always fails (likely unintended)." + ) assert ( ta.test.typ == BoolInstanceType ), "Assertions must result in a boolean type" From 971a20caf836e69ea502d906c298b6fafee8e0ee Mon Sep 17 00:00:00 2001 From: pcjordan Date: Fri, 26 Apr 2024 10:43:19 +0200 Subject: [PATCH 2/5] fix warn msg --- opshin/type_inference.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/opshin/type_inference.py b/opshin/type_inference.py index 192ed2db..827d152c 100644 --- a/opshin/type_inference.py +++ b/opshin/type_inference.py @@ -838,11 +838,10 @@ def visit_Attribute(self, node: Attribute) -> TypedAttribute: def visit_Assert(self, node: Assert) -> TypedAssert: ta = copy(node) ta.test = self.visit(node.test) - warn_assert_msg = ( - f" (see assert with message '{node.msg.value}')" - if node.msg is not None - else "" - ) + try: + warn_assert_msg = f" (see assert with message '{node.msg.value}')" + except Exception: + warn_assert_msg = "" if isinstance(ta.test.args[0], Constant) and ta.test.args[0].value is None: OPSHIN_LOGGER.warning( "Asserting `None'" From 3344020f0b857369a25b86ee07508512d439d99b Mon Sep 17 00:00:00 2001 From: pcjordan Date: Tue, 9 Jul 2024 10:55:35 +0200 Subject: [PATCH 3/5] fix too broad Exception, add examples --- examples/smart_contracts/assert_foo_none.py | 10 ++++++++++ examples/smart_contracts/assert_none.py | 6 ++++++ opshin/type_inference.py | 4 ++-- 3 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 examples/smart_contracts/assert_foo_none.py create mode 100644 examples/smart_contracts/assert_none.py diff --git a/examples/smart_contracts/assert_foo_none.py b/examples/smart_contracts/assert_foo_none.py new file mode 100644 index 00000000..405884fb --- /dev/null +++ b/examples/smart_contracts/assert_foo_none.py @@ -0,0 +1,10 @@ +#!opshin +from opshin.prelude import * + + +def foo() -> None: + return None + + +def validator(datum: int, redeemer: int, context: ScriptContext) -> None: + assert foo(), f"Always fails!" diff --git a/examples/smart_contracts/assert_none.py b/examples/smart_contracts/assert_none.py new file mode 100644 index 00000000..d5e2049f --- /dev/null +++ b/examples/smart_contracts/assert_none.py @@ -0,0 +1,6 @@ +#!opshin +from opshin.prelude import * + + +def validator(datum: int, redeemer: int, context: ScriptContext) -> None: + assert None, f"Always fails!" diff --git a/opshin/type_inference.py b/opshin/type_inference.py index 827d152c..450927bf 100644 --- a/opshin/type_inference.py +++ b/opshin/type_inference.py @@ -839,8 +839,8 @@ def visit_Assert(self, node: Assert) -> TypedAssert: ta = copy(node) ta.test = self.visit(node.test) try: - warn_assert_msg = f" (see assert with message '{node.msg.value}')" - except Exception: + warn_assert_msg = f" (see assert with message '{node.msg.values[0].value}')" + except AttributeError as err: warn_assert_msg = "" if isinstance(ta.test.args[0], Constant) and ta.test.args[0].value is None: OPSHIN_LOGGER.warning( From d5649110011d64ce80258070111826820cce4338 Mon Sep 17 00:00:00 2001 From: pcjordan Date: Tue, 9 Jul 2024 11:02:12 +0200 Subject: [PATCH 4/5] fix --- opshin/type_inference.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/opshin/type_inference.py b/opshin/type_inference.py index 450927bf..704d5683 100644 --- a/opshin/type_inference.py +++ b/opshin/type_inference.py @@ -840,7 +840,7 @@ def visit_Assert(self, node: Assert) -> TypedAssert: ta.test = self.visit(node.test) try: warn_assert_msg = f" (see assert with message '{node.msg.values[0].value}')" - except AttributeError as err: + except AttributeError: warn_assert_msg = "" if isinstance(ta.test.args[0], Constant) and ta.test.args[0].value is None: OPSHIN_LOGGER.warning( From 30fabaf95a97fc22cfd41cb898c50d49cb7f2530 Mon Sep 17 00:00:00 2001 From: pcjordan Date: Tue, 9 Jul 2024 18:06:26 +0200 Subject: [PATCH 5/5] fix --- opshin/type_inference.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/opshin/type_inference.py b/opshin/type_inference.py index 704d5683..b1a29348 100644 --- a/opshin/type_inference.py +++ b/opshin/type_inference.py @@ -848,9 +848,7 @@ def visit_Assert(self, node: Assert) -> TypedAssert: + warn_assert_msg + " is equivalent to asserting False, which always fails." ) - if isinstance(ta.test.args[0], Call) and isinstance( - ta.test.args[0].typ.typ, UnitType - ): + elif isinstance(ta.test.args[0].typ.typ, UnitType): OPSHIN_LOGGER.warning( "Asserting `None'" + warn_assert_msg