Parcourir la source

indexOf: check for not supported types

Vladislav Folts il y a 10 ans
Parent
commit
70311d25bf
3 fichiers modifiés avec 16 ajouts et 3 suppressions
  1. BIN
      bin/compiled.zip
  2. 4 1
      src/eberon/EberonDynamicArray.ob
  3. 12 2
      test/test_unit_eberon.js

BIN
bin/compiled.zip


+ 4 - 1
src/eberon/EberonDynamicArray.ob

@@ -200,7 +200,10 @@ END MethodCallRemove.make;
 PROCEDURE MethodCallIndexOf.make(args: JsArray.Type; cx: LanguageContext.PType): Code.PExpression;
 BEGIN
     argCode <- Procedure.makeArgumentsCode(cx);
-    void <- Procedure.checkSingleArgument(args, SELF, cx.types, argCode);
+    argType <- Procedure.checkSingleArgument(args, SELF, cx.types, argCode).type();
+    IF (argType IS Types.PRecord) OR (argType IS Types.PArray) THEN
+        Errors.raise("cannot search for element of type '" + argType.description() + "'");
+    END;
     RETURN Code.makeSimpleExpression("(" + argCode.result() + ")", Types.basic.integer)
 END MethodCallIndexOf.make;
 

+ 12 - 2
test/test_unit_eberon.js

@@ -968,11 +968,21 @@ exports.suite = {
         ),
         "indexOf": testWithContext(
             context(grammar.expression, 
-                    "VAR intArray: ARRAY * OF INTEGER; boolArray: ARRAY * OF BOOLEAN;"),
+                    "TYPE "
+                    + "T = RECORD END;"
+                    + "VAR "
+                    + "r: T;"
+                    + "intArray: ARRAY * OF INTEGER;"
+                    + "boolArray: ARRAY * OF BOOLEAN;"
+                    + "recordArray: ARRAY * OF T;"
+                    + "arrayOfArray: ARRAY *,* OF INTEGER;"
+                    ),
             pass("intArray.indexOf(0)",
                  "boolArray.indexOf(FALSE) = -1"
                 ),
-            fail(["intArray.indexOf(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"]
+            fail(["intArray.indexOf(TRUE)", "type mismatch for argument 1: 'BOOLEAN' cannot be converted to 'INTEGER'"],
+                 ["recordArray.indexOf(r)", "cannot search for element of type 'T'"],
+                 ["arrayOfArray.indexOf(intArray)", "cannot search for element of type 'ARRAY * OF INTEGER'"]
                 )
         )
     }