@@ -43,7 +43,11 @@ type error =
43
43
| Method_mismatch of string * type_expr * type_expr
44
44
| Unbound_value of Longident .t * Location .t
45
45
| Unbound_constructor of Longident .t
46
- | Unbound_label of Longident .t * type_expr option
46
+ | Unbound_label of {
47
+ loc : Location .t ;
48
+ field_name : Longident .t ;
49
+ from_type : type_expr option ;
50
+ }
47
51
| Unbound_module of Longident .t
48
52
| Unbound_modtype of Longident .t
49
53
| Ill_typed_functor_application of Longident .t
@@ -128,8 +132,10 @@ let find_constructor =
128
132
let find_all_constructors =
129
133
find_component Env. lookup_all_constructors (fun lid ->
130
134
Unbound_constructor lid)
131
- let find_all_labels =
132
- find_component Env. lookup_all_labels (fun lid -> Unbound_label (lid, None ))
135
+ let find_all_labels env loc =
136
+ find_component Env. lookup_all_labels
137
+ (fun lid -> Unbound_label {loc; field_name = lid; from_type = None })
138
+ env loc
133
139
134
140
let find_value env loc lid =
135
141
Env. check_value_name (Longident. last lid) loc;
@@ -168,8 +174,9 @@ let unbound_constructor_error ?from_type env lid =
168
174
Unbound_constructor lid)
169
175
170
176
let unbound_label_error ?from_type env lid =
177
+ let lid_with_loc = lid in
171
178
narrow_unbound_lid_error env lid.loc lid.txt (fun lid ->
172
- Unbound_label ( lid, from_type) )
179
+ Unbound_label {loc = lid_with_loc.loc; field_name = lid; from_type} )
173
180
174
181
(* Support for first-class modules. *)
175
182
@@ -936,10 +943,17 @@ let report_error env ppf = function
936
943
= Bar@}.@]@]"
937
944
Printtyp. longident lid Printtyp. longident lid Printtyp. longident lid;
938
945
spellcheck ppf fold_constructors env lid
939
- | Unbound_label ( lid , from_type ) ->
946
+ | Unbound_label {loc; field_name; from_type} ->
940
947
(* modified *)
941
948
(match from_type with
942
949
| Some {desc = Tconstr (p , _ , _ )} when Path. same p Predef. path_option ->
950
+ Cmt_utils. add_possible_action
951
+ {
952
+ loc;
953
+ action = UnwrapOptionMapRecordField {field_name};
954
+ description =
955
+ " Unwrap the option first before accessing the record field" ;
956
+ };
943
957
(* TODO: Extend for nullable/null? *)
944
958
Format. fprintf ppf
945
959
" @[<v>You're trying to access the record field @{<info>%a@}, but the \
@@ -951,14 +965,15 @@ let report_error env ppf = function
951
965
@{<info>xx->Option.map(field => field.%a)@}@]@,\
952
966
@[- Or use @{<info>Option.getOr@} with a default: \
953
967
@{<info>xx->Option.getOr(defaultRecord).%a@}@]@]"
954
- Printtyp. longident lid Printtyp. longident lid Printtyp. longident lid
968
+ Printtyp. longident field_name Printtyp. longident field_name
969
+ Printtyp. longident field_name
955
970
| Some {desc = Tconstr (p , _ , _ )} when Path. same p Predef. path_array ->
956
971
Format. fprintf ppf
957
972
" @[<v>You're trying to access the record field @{<info>%a@}, but the \
958
973
value you're trying to access it on is an @{<info>array@}.@ You need \
959
974
to access an individual element of the array if you want to access an \
960
975
individual record field.@]"
961
- Printtyp. longident lid
976
+ Printtyp. longident field_name
962
977
| Some ({desc = Tconstr (_p , _ , _ )} as t1 ) ->
963
978
Format. fprintf ppf
964
979
" @[<v>You're trying to access the record field @{<info>%a@}, but the \
@@ -967,7 +982,7 @@ let report_error env ppf = function
967
982
%a@,\n \
968
983
@,\
969
984
Only records have fields that can be accessed with dot notation.@]"
970
- Printtyp. longident lid Error_message_utils. type_expr t1
985
+ Printtyp. longident field_name Error_message_utils. type_expr t1
971
986
| None | Some _ ->
972
987
Format. fprintf ppf
973
988
" @[<v>@{<info>%a@} refers to a record field, but no corresponding \
@@ -978,8 +993,9 @@ let report_error env ppf = function
978
993
@{<info>TheModule.%a@}@]@,\
979
994
@[- Or specifying the record type explicitly:@ @{<info>let theValue: \
980
995
TheModule.theType = {%a: VALUE}@}@]@]"
981
- Printtyp. longident lid Printtyp. longident lid Printtyp. longident lid);
982
- spellcheck ppf fold_labels env lid
996
+ Printtyp. longident field_name Printtyp. longident field_name
997
+ Printtyp. longident field_name);
998
+ spellcheck ppf fold_labels env field_name
983
999
| Unbound_modtype lid ->
984
1000
fprintf ppf " Unbound module type %a" longident lid;
985
1001
spellcheck ppf fold_modtypes env lid
0 commit comments