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