diff --git a/src/bin/expect_tests/contract_tests.ml b/src/bin/expect_tests/contract_tests.ml index c2b26dc9a..cd835d61c 100644 --- a/src/bin/expect_tests/contract_tests.ml +++ b/src/bin/expect_tests/contract_tests.ml @@ -963,16 +963,6 @@ let%expect_test _ = * Open a gitlab issue: https://gitlab.com/ligolang/ligo/issues/new * Check the changelog by running 'ligo changelog' |}] -let%expect_test _ = - run_ligo_good [ "run-function" ; contract "failwith.ligo" ; "failer" ; "1" ] ; - [%expect {| - failwith("some_string") |}] - -let%expect_test _ = - run_ligo_good [ "run-function" ; contract "failwith.ligo" ; "failer" ; "1" ; "--format=json" ] ; - [%expect {| - {"status":"ok","content":"failwith(\"some_string\")"} |}] - let%expect_test _ = run_ligo_bad [ "compile-contract" ; contract "bad_address_format.religo" ; "main" ] ; [%expect {| @@ -1084,7 +1074,7 @@ let%expect_test _ = let%expect_test _ = run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_toplevel.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return\n let rhs#727 = #P in\n let p = rhs#727.0 in\n let s = rhs#727.1 in\n ( LIST_EMPTY() : (type_operator: list(operation)) , store ) ,\n NONE() : (type_operator: option(key_hash)) ,\n 300000000mutez ,\n \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} +ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, character 8. No free variable allowed in this lambda: variable 'store' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * string ))) : None return\n let rhs#693 = #P in\n let p = rhs#693.0 in\n let s = rhs#693.1 in\n ( LIST_EMPTY() : (type_operator: list(operation)) , store ) ,\n NONE() : (type_operator: option(key_hash)) ,\n 300000000mutez ,\n \"un\")","location":"in file \"create_contract_toplevel.mligo\", line 4, character 35 to line 8, character 8"} If you're not sure how to fix this error, you can @@ -1097,7 +1087,7 @@ ligo: in file "create_contract_toplevel.mligo", line 4, character 35 to line 8, run_ligo_bad [ "compile-contract" ; bad_contract "create_contract_var.mligo" ; "main" ] ; [%expect {| -ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return\n let rhs#730 = #P in\n let p = rhs#730.0 in\n let s = rhs#730.1 in\n ( LIST_EMPTY() : (type_operator: list(operation)) , a ) ,\n NONE() : (type_operator: option(key_hash)) ,\n 300000000mutez ,\n 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} +ligo: in file "create_contract_var.mligo", line 6, character 35 to line 10, character 5. No free variable allowed in this lambda: variable 'a' {"expression":"CREATE_CONTRACT(lambda (#P:Some(( nat * int ))) : None return\n let rhs#696 = #P in\n let p = rhs#696.0 in\n let s = rhs#696.1 in\n ( LIST_EMPTY() : (type_operator: list(operation)) , a ) ,\n NONE() : (type_operator: option(key_hash)) ,\n 300000000mutez ,\n 1)","location":"in file \"create_contract_var.mligo\", line 6, character 35 to line 10, character 5"} If you're not sure how to fix this error, you can diff --git a/src/bin/expect_tests/failwith_tests.ml b/src/bin/expect_tests/failwith_tests.ml index a66d462ee..8c5819ca4 100644 --- a/src/bin/expect_tests/failwith_tests.ml +++ b/src/bin/expect_tests/failwith_tests.ml @@ -8,11 +8,11 @@ let bad_contract basename = let%expect_test _ = run_ligo_good [ "run-function" ; contract "failwith.ligo" ; "failer" ; "1" ] ; [%expect {| - failwith("some_string") |}]; + failwith(42) |}]; run_ligo_good [ "run-function" ; contract "failwith.ligo" ; "failer" ; "1" ; "--format=json" ] ; [%expect {| - {"status":"ok","content":"failwith(\"some_string\")"} |}]; + {"status":"ok","content":"failwith(42)"} |}]; run_ligo_good [ "dry-run" ; contract "subtle_nontail_fail.mligo" ; "main" ; "()" ; "()" ] ; diff --git a/src/bin/expect_tests/typer_error_tests.ml b/src/bin/expect_tests/typer_error_tests.ml index d37ac5dd5..3bb9f2651 100644 --- a/src/bin/expect_tests/typer_error_tests.ml +++ b/src/bin/expect_tests/typer_error_tests.ml @@ -189,4 +189,17 @@ let%expect_test _ = * Visit our documentation: https://ligolang.org/docs/intro/introduction * Ask a question on our Discord: https://discord.gg/9rhYaEt * Open a gitlab issue: https://gitlab.com/ligolang/ligo/issues/new - * Check the changelog by running 'ligo changelog' |}]; + * Check the changelog by running 'ligo changelog' |}] + +let%expect_test _ = + run_ligo_bad [ "compile-contract" ; "../../test/contracts/negative/failwith_wrong_type.ligo" ; "main" ] ; + [%expect {| + ligo: in file "failwith_wrong_type.ligo", line 2, characters 19-46. Failwith with disallowed type: Expected arguments with one of the following combinations of types: failwith(string) or failwith(nat) or failwith(int) but got this combination instead: failwith((type_operator: list(int))) + + If you're not sure how to fix this error, you can + do one of the following: + + * Visit our documentation: https://ligolang.org/docs/intro/introduction + * Ask a question on our Discord: https://discord.gg/9rhYaEt + * Open a gitlab issue: https://gitlab.com/ligolang/ligo/issues/new + * Check the changelog by running 'ligo changelog' |}] diff --git a/src/passes/operators/operators.ml b/src/passes/operators/operators.ml index bd7cd3179..670f45c42 100644 --- a/src/passes/operators/operators.ml +++ b/src/passes/operators/operators.ml @@ -702,9 +702,21 @@ module Typer = struct [i ; j ; s] () let failwith_ = typer_1_opt "FAILWITH" @@ fun t opt -> - let%bind () = - Assert.assert_true @@ - (is_t_string t) in + let%bind _ = + if eq_1 t (t_string ()) + then ok () + else if eq_1 t (t_nat ()) + then ok () + else if eq_1 t (t_int ()) + then ok () + else + fail @@ Operator_errors.typeclass_error "Failwith with disallowed type" "failwith" + [ + [t_string()] ; + [t_nat()] ; + [t_int()] ; + ] + [t] () in let default = t_unit () in ok @@ Simple_utils.Option.unopt ~default opt diff --git a/src/test/contracts/failwith.ligo b/src/test/contracts/failwith.ligo index 73e9a4f06..48293b7e3 100644 --- a/src/test/contracts/failwith.ligo +++ b/src/test/contracts/failwith.ligo @@ -29,7 +29,7 @@ function foobar (const i : int) : int is } else case p of - Zero (n) -> failwith ("wooo") + Zero (n) -> failwith(42n) | Pos (n) -> skip end } with @@ -39,5 +39,5 @@ function foobar (const i : int) : int is end function failer (const p : int) : int is block { - if p = 1 then failwith ("some_string") else skip + if p = 1 then failwith (42) else skip } with p diff --git a/src/test/contracts/negative/failwith_wrong_type.ligo b/src/test/contracts/negative/failwith_wrong_type.ligo new file mode 100644 index 000000000..c5d6f7a68 --- /dev/null +++ b/src/test/contracts/negative/failwith_wrong_type.ligo @@ -0,0 +1,2 @@ + +const bad : unit = failwith((nil : list(int)))