Forráskód Böngészése

Add some tests for return type static analysis.

Joeri Exelmans 5 éve
szülő
commit
ae7057602a

+ 5 - 4
src/sccd/syntax/statement.py

@@ -37,9 +37,10 @@ class ReturnBehavior:
         # Only remaining case: ALWAYS & SOME_BRANCHES.
         # Now the types must match:
         if one.type != two.type:
-            raise StaticTypeError("Branches have different return types: %s and %s" % (str(one.type), str(two.type)))
+            raise StaticTypeError("Return types differ: One branch returns %s, the other %s" % (str(one.type), str(two.type)))
         return ReturnBehavior(ReturnBehavior.When.SOME_BRANCHES, one.type)
 
+    # If a statement with known ReturnBehavior is followed by another statement with known ReturnBehavior, what is the ReturnBehavior of their sequence? Also, raises if their sequence is illegal.
     @staticmethod
     def sequence(earlier: 'ReturnBehavior', later: 'ReturnBehavior') -> 'ReturnBehavior':
         if earlier.when == ReturnBehavior.When.NEVER:
@@ -49,12 +50,12 @@ class ReturnBehavior:
                 return earlier
             if later.when == ReturnBehavior.When.SOME_BRANCHES:
                 if earlier.type != later.type:
-                    raise StaticTypeError("Earlier statement may return %s, later statement may return %s" % (str(earlier.type), str(later.type)))
+                    raise StaticTypeError("Return types differ: Earlier statement may return %s, later statement may return %s" % (str(earlier.type), str(later.type)))
                 return earlier
             if earlier.type != later.type:
-                raise StaticTypeError("Earlier statement may return %s, later statement returns %s" % (str(earlier.type), str(later.type)))
+                raise StaticTypeError("Return types differ: Earlier statement may return %s, later statement returns %s" % (str(earlier.type), str(later.type)))
             return later
-        raise StaticTypeError("Earlier statement returns %s, cannot be followed by another statement" % str(earlier.type))
+        raise StaticTypeError("Earlier statement always returns %s, cannot be followed by another statement" % str(earlier.type))
 
 # A statement is NOT an expression.
 class Statement(ABC):

+ 1 - 7
test/test_files/features/functions/fail_return_branches.xml

@@ -1,11 +1,6 @@
 <?xml version="1.0" ?>
 <test>
   <statechart>
-    <semantics
-      big_step_maximality="take_many"
-      concurrency="single"
-      input_event_lifeline="first_combo_step"/>
-
     <datamodel>
       <func id="illegal_func(i: int)">
         if (i > 1)
@@ -15,7 +10,6 @@
       </func>
     </datamodel>
 
-    <root>
-    </root>
+    <root/>
   </statechart>
 </test>

+ 13 - 0
test/test_files/features/functions/fail_return_branches2.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" ?>
+<test>
+  <statechart>
+    <datamodel>
+      <func id="illegal_func(i: int)">
+        if (i &gt; 10) return 1;
+        return "hello";
+      </func>
+    </datamodel>
+
+    <root/>
+  </statechart>
+</test>

+ 12 - 0
test/test_files/features/functions/fail_return_branches3.xml

@@ -0,0 +1,12 @@
+<?xml version="1.0" ?>
+<test>
+  <statechart>
+    <datamodel>
+      <func id="illegal_func(i: int)">
+        if (i &gt; 10) return 1;
+      </func>
+    </datamodel>
+
+    <root/>
+  </statechart>
+</test>

+ 1 - 7
test/test_files/features/functions/fail_return_sequence.xml

@@ -1,11 +1,6 @@
 <?xml version="1.0" ?>
 <test>
   <statechart>
-    <semantics
-      big_step_maximality="take_many"
-      concurrency="single"
-      input_event_lifeline="first_combo_step"/>
-
     <datamodel>
       <func id="illegal_func">
         return 1;
@@ -13,7 +8,6 @@
       </func>
     </datamodel>
 
-    <root>
-    </root>
+    <root/>
   </statechart>
 </test>

+ 13 - 0
test/test_files/features/functions/fail_return_branches5.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" ?>
+<test>
+  <statechart>
+    <datamodel>
+      <func id="illegal_func(i: int)">
+        if (i &gt; 10) return 1;
+        if (i &lt; 5) return "hello";
+      </func>
+    </datamodel>
+
+    <root/>
+  </statechart>
+</test>