Skip to content

Commit 2fb657c

Browse files
committed
hint about when appropriate
1 parent 9d93ede commit 2fb657c

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

compiler/ml/parmatch.ml

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2013,7 +2013,7 @@ let ppat_of_type env ty =
20132013
(Conv.mkpat Parsetree.Ppat_any, Hashtbl.create 0, Hashtbl.create 0)
20142014
| pats -> Conv.conv (orify_many pats)
20152015

2016-
let do_check_partial ?pred exhaust loc casel pss =
2016+
let do_check_partial ?partial_match_warning_hint ?pred exhaust loc casel pss =
20172017
match pss with
20182018
| [] ->
20192019
(*
@@ -2071,6 +2071,11 @@ let do_check_partial ?pred exhaust loc casel pss =
20712071
Matching over values of extensible variant types (the \
20722072
*extension* above)\n\
20732073
must include a wild card pattern in order to be exhaustive.";
2074+
(match partial_match_warning_hint with
2075+
| None -> ()
2076+
| Some h when String.length h > 0 ->
2077+
Buffer.add_string buf ("\n\n " ^ h)
2078+
| Some _ -> ());
20742079
Buffer.contents buf
20752080
with _ -> ""
20762081
in
@@ -2083,8 +2088,8 @@ let do_check_partial_normal loc casel pss =
20832088
do_check_partial exhaust loc casel pss
20842089
*)
20852090

2086-
let do_check_partial_gadt pred loc casel pss =
2087-
do_check_partial ~pred exhaust_gadt loc casel pss
2091+
let do_check_partial_gadt ?partial_match_warning_hint pred loc casel pss =
2092+
do_check_partial ?partial_match_warning_hint ~pred exhaust_gadt loc casel pss
20882093

20892094
(*****************)
20902095
(* Fragile check *)
@@ -2265,9 +2270,9 @@ let check_partial_param do_check_partial do_check_fragile loc casel =
22652270
do_check_partial_normal
22662271
do_check_fragile_normal*)
22672272

2268-
let check_partial_gadt pred loc casel =
2273+
let check_partial_gadt ?partial_match_warning_hint pred loc casel =
22692274
check_partial_param
2270-
(do_check_partial_gadt pred)
2275+
(do_check_partial_gadt ?partial_match_warning_hint pred)
22712276
do_check_fragile_gadt loc casel
22722277

22732278
(*************************************)

compiler/ml/parmatch.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ val ppat_of_type :
6969

7070
val pressure_variants : Env.t -> pattern list -> unit
7171
val check_partial_gadt :
72+
?partial_match_warning_hint:string ->
7273
((string, constructor_description) Hashtbl.t ->
7374
(string, label_description) Hashtbl.t ->
7475
Parsetree.pattern ->

compiler/ml/typecore.ml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,13 +1722,14 @@ let partial_pred ~lev ?mode ?explode env expected_ty constrs labels p =
17221722
set_state state env;
17231723
None
17241724
1725-
let check_partial ?(lev = get_current_level ()) env expected_ty loc cases =
1725+
let check_partial ?(lev = get_current_level ()) ?partial_match_warning_hint env
1726+
expected_ty loc cases =
17261727
let explode =
17271728
match cases with
17281729
| [_] -> 5
17291730
| _ -> 0
17301731
in
1731-
Parmatch.check_partial_gadt
1732+
Parmatch.check_partial_gadt ?partial_match_warning_hint
17321733
(partial_pred ~lev ~explode env expected_ty)
17331734
loc cases
17341735
@@ -4202,7 +4203,24 @@ and type_let ~context ?(check = fun s -> Warnings.Unused_var s)
42024203
List.iter2
42034204
(fun pat (attrs, exp) ->
42044205
Builtin_attributes.warning_scope ~ppwarning:false attrs (fun () ->
4205-
ignore (check_partial env pat.pat_type pat.pat_loc [case pat exp])))
4206+
let partial_match_warning_hint =
4207+
if Experimental_features.is_enabled Experimental_features.LetUnwrap
4208+
then
4209+
let ty = repr (Ctype.expand_head env pat.pat_type) in
4210+
match ty.desc with
4211+
| Tconstr (path, _, _)
4212+
when Path.same path Predef.path_option
4213+
|| Path.same path Predef.path_result ->
4214+
Some
4215+
"Hint: You can use `let?` to automatically unwrap this \
4216+
expression."
4217+
| _ -> None
4218+
else None
4219+
in
4220+
ignore
4221+
(check_partial ?partial_match_warning_hint env pat.pat_type
4222+
pat.pat_loc
4223+
[case pat exp])))
42064224
pat_list
42074225
(List.map2 (fun (attrs, _) e -> (attrs, e)) spatl exp_list);
42084226
end_def ();

compiler/ml/typecore.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ val type_expression :
3535
Typedtree.expression
3636
val check_partial :
3737
?lev:int ->
38+
?partial_match_warning_hint:string ->
3839
Env.t ->
3940
type_expr ->
4041
Location.t ->
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
2+
Warning number 8
3+
/.../fixtures/let_without_unwrap_but_elgible.res:6:7-11
4+
5+
4 │
6+
5 │ let f = {
7+
6 │ let Ok(_) = x
8+
7 │ Ok(1)
9+
8 │ }
10+
11+
You forgot to handle a possible case here, for example:
12+
| Error(_)
13+
14+
Hint: You can use `let?` to automatically unwrap this expression.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
@@config({flags: ["-enable-experimental", "LetUnwrap"]})
2+
3+
let x = Ok(1)
4+
5+
let f = {
6+
let Ok(_) = x
7+
Ok(1)
8+
}

0 commit comments

Comments
 (0)