Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hint on type error on int operators #2307

Merged
merged 5 commits into from Mar 11, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions Changes
Expand Up @@ -69,6 +69,10 @@ Working version
(Jules Aguillon, review by Nicolás Ojeda Bär , Florian Angeletti,
Gabriel Scherer and Armaël Guéneau)

- GPR#2307: Hint on type error on int's operators
(Jules Aguillon, with help from Armaël Guéneau,
review by Gabriel Scherer and Florian Angeletti)

### Bug fixes:

- MPR#7937, GPR#2287: fix uncaught Unify exception when looking for type
Expand Down
3 changes: 2 additions & 1 deletion testsuite/tests/tool-caml-tex/redirections.reference
Expand Up @@ -18,9 +18,10 @@
\camlexample{toplevel}
\caml\camlinput\?[@@@warning "+A"];;
\endcamlinput\endcaml
\caml\camlinput\?1 + \<2.\> ;;
\caml\camlinput\?1 \<+\> \<2.\> ;;
\endcamlinput\camlerror\:Error: This expression has type float but an expression was expected of type
\: int
\: Hint: Did you mean to use \textasciigrave\-+.\textquotesingle\-?
\endcamlerror\endcaml
\caml\camlinput\?let f \<x\> = () ;;
\endcamlinput\camlwarn\:Warning 27: unused variable x.
Expand Down
79 changes: 79 additions & 0 deletions testsuite/tests/typing-core-bugs/int_operator_hint.ml
@@ -0,0 +1,79 @@
(* TEST
* expect
*)

let _ = 0. + 0.
[%%expect{|
Line 1, characters 8-10:
1 | let _ = 0. + 0.
^^
Error: This expression has type float but an expression was expected of type
int
Line 1, characters 11-12:
1 | let _ = 0. + 0.
^
Hint: Did you mean to use `+.'?
|}]

let _ = 0l - 0l
[%%expect{|
Line 1, characters 8-10:
1 | let _ = 0l - 0l
^^
Error: This expression has type int32 but an expression was expected of type
int
Line 1, characters 11-12:
1 | let _ = 0l - 0l
^
Hint: Did you mean to use `Int32.sub'?
|}]

let _ = 0L * 0L
[%%expect{|
Line 1, characters 8-10:
1 | let _ = 0L * 0L
^^
Error: This expression has type int64 but an expression was expected of type
int
Line 1, characters 11-12:
1 | let _ = 0L * 0L
^
Hint: Did you mean to use `Int64.mul'?
|}]

let _ = 0n / 0n
[%%expect{|
Line 1, characters 8-10:
1 | let _ = 0n / 0n
^^
Error: This expression has type nativeint
but an expression was expected of type int
Line 1, characters 11-12:
1 | let _ = 0n / 0n
^
Hint: Did you mean to use `Nativeint.div'?
|}]

let _ = 0. mod 0.
[%%expect{|
Line 1, characters 8-10:
1 | let _ = 0. mod 0.
^^
Error: This expression has type float but an expression was expected of type
int
Line 1, characters 11-14:
1 | let _ = 0. mod 0.
^^^
Hint: Did you mean to use `Float.rem'?
|}]

(* disabled *)
let _ = 0 +. 0
[%%expect{|
Line 1, characters 8-9:
1 | let _ = 0 +. 0
^
Error: This expression has type int but an expression was expected of type
float
Hint: Did you mean `0.'?
|}]
1 change: 1 addition & 0 deletions testsuite/tests/typing-core-bugs/ocamltests
Expand Up @@ -3,3 +3,4 @@ unit_fun_hints.ml
type_expected_explanation.ml
repeated_did_you_mean.ml
const_int_hint.ml
int_operator_hint.ml
6 changes: 4 additions & 2 deletions tools/caml_tex.ml
Expand Up @@ -132,10 +132,12 @@ module Toplevel = struct
if startchar >= 0 then
locs := (startchar, endchar) :: !locs

(** Record the main location instead of printing it *)
(** Record locations in the main error and suberrors without printing them *)
let printer_register_locs =
{ Location.batch_mode_printer with
pp_main_loc = (fun _ _ _ loc -> register_loc loc) }
pp_main_loc = (fun _ _ _ loc -> register_loc loc);
pp_submsg_loc = (fun _ _ _ loc -> register_loc loc);
}
gasche marked this conversation as resolved.
Show resolved Hide resolved

(** Capture warnings and keep them in a list *)
let warnings = ref []
Expand Down