Pārlūkot izejas kodu

Made some better error messages for array assignments

Taddeus Kroes 12 gadi atpakaļ
vecāks
revīzija
f496ce57f6
3 mainītis faili ar 9 papildinājumiem un 3 dzēšanām
  1. 0 2
      README.md
  2. 8 1
      phases/typecheck.ml
  3. 1 0
      test/old/array_assign.cvc

+ 0 - 2
README.md

@@ -8,8 +8,6 @@ Issues & TODO
 -------------
 
 - Documentation for each phase, in ocamldoc format.
-- Erronous array initialisation needs more insightfull error messages and array
-  assignment needs better better type checking.
 - Typechecking now gives an error when integers are not in the 32-bit range, as
   the reference compiler implements it. However, 64-bit should imo also be
   supported (using Nativeint instead of Int32).

+ 8 - 1
phases/typecheck.ml

@@ -214,7 +214,8 @@ let rec typecheck node =
     (* Array pointers cannot be re-assigned, because array dimension reduction
      * makes assumptions about dimensions of an array *)
     | VarLet (dec, None, _, _) when is_array dec ->
-        raise (NodeError (node, "cannot re-assign array pointer"))
+        raise (NodeError (node, "cannot assign value to array pointer " ^
+                                "after initialisation"))
 
     (* Assigned values must match variable declaration *)
     | VarLet (dec, None, value, ann) ->
@@ -234,6 +235,12 @@ let rec typecheck node =
 
         VarLet (dec, Some dims, value, ann)
 
+    (* ArrayConst initialisations are transformed during desugaring, so any
+     * occurrences that are left are illegal *)
+    | ArrayConst _ ->
+        raise (NodeError (node, "array constants can only be used in array " ^
+                                "initialisation"))
+
     | _ -> transform_children typecheck node
 
 let phase = function

+ 1 - 0
test/old/array_assign.cvc

@@ -2,4 +2,5 @@ void foo() {
     int[4] a;
 
     a = [1, 2, 3];
+    a[0] = 1;
 }