Просмотр исходного кода

Typeof: Do not raise warning when assigning an integer in a string variable

Chimrod 5 месяцев назад
Родитель
Сommit
4535058e48
2 измененных файлов с 12 добавлено и 4 удалено
  1. 9 3
      lib/syntax/type_of.ml
  2. 3 1
      test/type_of.ml

+ 9 - 3
lib/syntax/type_of.ml

@@ -454,9 +454,15 @@ module Instruction = struct
     let report' = Option.map snd variable.index |> Option.value ~default:[] in
 
     let report = List.rev_append report' report in
-    match right_expression.empty with
-    | true -> report
-    | false -> (
+    match
+      ( right_expression.empty,
+        Get_type.get_type (Lazy.force right_expression.result) )
+    with
+    | true, _
+    (* It’s allowed to assign an integer in any kind of variable *)
+    | _, Get_type.(Integer) ->
+        report
+    | false, _ -> (
         let var_type = Lazy.from_val (Get_type.ident variable) in
         let op1 = arg_of_repr var_type variable.pos in
         let op2 = arg_of_repr right_expression.result right_expression.pos in

+ 3 - 1
test/type_of.ml

@@ -26,6 +26,7 @@ let _test_instruction : string -> Qsp_syntax.Report.t list -> unit =
   Check._test_instruction
 
 let type_mismatch () = _test_instruction {|abc = 'ABC'|} (message Error)
+let assign_int_str () = _test_instruction {|$abc = 123|} []
 let type_mismatch2 () = _test_instruction {|abc[''] = $Var|} (message Warn)
 
 let type_conversion () =
@@ -75,7 +76,8 @@ let wrong_predicate () =
 let test =
   ( "Typechecking",
     [
-      Alcotest.test_case "Assign" `Quick type_mismatch;
+      Alcotest.test_case "Assign str to int" `Quick type_mismatch;
+      Alcotest.test_case "Assign int to str" `Quick assign_int_str;
       Alcotest.test_case "Assign array" `Quick type_mismatch2;
       Alcotest.test_case "Conversion" `Quick type_conversion;
       Alcotest.test_case "Conversion'" `Quick type_conversion';