Jelajahi Sumber

Fix for loop without condition or next

Signed-off-by: Sean Young <sean@mess.org>
Sean Young 4 tahun lalu
induk
melakukan
cee73beb26
2 mengubah file dengan 22 tambahan dan 3 penghapusan
  1. 2 2
      src/codegen/statements.rs
  2. 20 1
      tests/substrate_tests/loops.rs

+ 2 - 2
src/codegen/statements.rs

@@ -283,9 +283,9 @@ pub fn statement(
             loops.leave_scope();
 
             if body_reachable {
-                if !next.is_empty() {
-                    cfg.set_basic_block(next_block);
+                cfg.set_basic_block(next_block);
 
+                if !next.is_empty() {
                     for stmt in next {
                         statement(
                             stmt,

+ 20 - 1
tests/substrate_tests/loops.rs

@@ -1,4 +1,5 @@
-use crate::{first_error, parse_and_resolve};
+use crate::{build_solidity, first_error, parse_and_resolve};
+use parity_scale_codec::Encode;
 use solang::Target;
 
 #[test]
@@ -19,3 +20,21 @@ fn test_infinite_loop() {
 
     assert_eq!(first_error(ns.diagnostics), "unreachable statement");
 }
+
+#[test]
+fn for_loop_no_cond_or_next() {
+    let mut runtime = build_solidity(
+        r##"
+        contract test {
+            function foo(bool x) public {
+                for (;;) {
+                    if (x)
+                        break;
+                }
+            }
+        }"##,
+    );
+
+    runtime.constructor(0, Vec::new());
+    runtime.function("foo", true.encode());
+}