Skip to content

Commit ee7c849

Browse files
authored
If both the type and mode of an ident are wrong, complain about the type. (#19)
1 parent 44bade0 commit ee7c849

File tree

2 files changed

+23
-6
lines changed

2 files changed

+23
-6
lines changed

testsuite/tests/typing-local/local.ml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,18 @@ Error: This local value escapes its region
7777
Hint: Cannot return local value without an explicit "local_" annotation
7878
|}]
7979

80+
(* If both type and mode are wrong, complain about type *)
81+
let f () =
82+
let local_ r = ref 42 in
83+
print_endline r
84+
[%%expect{|
85+
Line 3, characters 16-17:
86+
3 | print_endline r
87+
^
88+
Error: This expression has type int ref
89+
but an expression was expected of type string
90+
|}]
91+
8092
(*
8193
* Type equalities of function types
8294
*)

typing/typecore.ml

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3308,9 +3308,14 @@ and type_expect_
33083308
unify_exp env (re exp) (instance ty_expected));
33093309
exp
33103310
in
3311+
let ruem ~mode ~expected_mode exp =
3312+
let exp = rue exp in
3313+
submode ~env ~loc:exp.exp_loc mode expected_mode;
3314+
exp
3315+
in
33113316
match sexp.pexp_desc with
33123317
| Pexp_ident lid ->
3313-
let path, desc, kind = type_ident env expected_mode ~recarg lid in
3318+
let path, mode, desc, kind = type_ident env ~recarg lid in
33143319
let exp_desc =
33153320
match desc.val_kind with
33163321
| Val_ivar (_, cl_num) ->
@@ -3330,7 +3335,7 @@ and type_expect_
33303335
| _ ->
33313336
Texp_ident(path, lid, desc, kind)
33323337
in
3333-
rue {
3338+
ruem ~mode ~expected_mode {
33343339
exp_desc; exp_loc = loc; exp_extra = [];
33353340
exp_type = desc.val_type;
33363341
exp_mode = expected_mode.mode;
@@ -4680,9 +4685,8 @@ and type_expect_
46804685
exp_attributes = sexp.pexp_attributes;
46814686
exp_env = env }
46824687

4683-
and type_ident env expected_mode ?(recarg=Rejected) lid =
4688+
and type_ident env ?(recarg=Rejected) lid =
46844689
let (path, desc, mode) = Env.lookup_value ~loc:lid.loc lid.txt env in
4685-
submode ~env ~loc:lid.loc mode expected_mode;
46864690
let is_recarg =
46874691
match (repr desc.val_type).desc with
46884692
| Tconstr(p, _, _) -> Path.is_constructor_typath p
@@ -4708,12 +4712,13 @@ and type_ident env expected_mode ?(recarg=Rejected) lid =
47084712
ty, Id_prim mode
47094713
| _ ->
47104714
instance desc.val_type, Id_value in
4711-
path, { desc with val_type }, kind
4715+
path, mode, { desc with val_type }, kind
47124716

47134717
and type_binding_op_ident env s =
47144718
let loc = s.loc in
47154719
let lid = Location.mkloc (Longident.Lident s.txt) loc in
4716-
let path, desc, kind = type_ident env mode_global lid in
4720+
let path, mode, desc, kind = type_ident env lid in
4721+
submode ~env ~loc:lid.loc mode mode_global;
47174722
let path =
47184723
match desc.val_kind with
47194724
| Val_ivar _ ->

0 commit comments

Comments
 (0)