@@ -43,7 +43,11 @@ type error =
4343 | Method_mismatch of string * type_expr * type_expr
4444 | Unbound_value of Longident .t * Location .t
4545 | 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+ }
4751 | Unbound_module of Longident .t
4852 | Unbound_modtype of Longident .t
4953 | Ill_typed_functor_application of Longident .t
@@ -128,8 +132,10 @@ let find_constructor =
128132let find_all_constructors =
129133 find_component Env. lookup_all_constructors (fun lid ->
130134 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
133139
134140let find_value env loc lid =
135141 Env. check_value_name (Longident. last lid) loc;
@@ -168,8 +174,9 @@ let unbound_constructor_error ?from_type env lid =
168174 Unbound_constructor lid)
169175
170176let unbound_label_error ?from_type env lid =
177+ let lid_with_loc = lid in
171178 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} )
173180
174181(* Support for first-class modules. *)
175182
@@ -936,10 +943,17 @@ let report_error env ppf = function
936943 = Bar@}.@]@]"
937944 Printtyp. longident lid Printtyp. longident lid Printtyp. longident lid;
938945 spellcheck ppf fold_constructors env lid
939- | Unbound_label ( lid , from_type ) ->
946+ | Unbound_label {loc; field_name; from_type} ->
940947 (* modified *)
941948 (match from_type with
942949 | 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+ };
943957 (* TODO: Extend for nullable/null? *)
944958 Format. fprintf ppf
945959 " @[<v>You're trying to access the record field @{<info>%a@}, but the \
@@ -951,14 +965,15 @@ let report_error env ppf = function
951965 @{<info>xx->Option.map(field => field.%a)@}@]@,\
952966 @[- Or use @{<info>Option.getOr@} with a default: \
953967 @{<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
955970 | Some {desc = Tconstr (p , _ , _ )} when Path. same p Predef. path_array ->
956971 Format. fprintf ppf
957972 " @[<v>You're trying to access the record field @{<info>%a@}, but the \
958973 value you're trying to access it on is an @{<info>array@}.@ You need \
959974 to access an individual element of the array if you want to access an \
960975 individual record field.@]"
961- Printtyp. longident lid
976+ Printtyp. longident field_name
962977 | Some ({desc = Tconstr (_p , _ , _ )} as t1 ) ->
963978 Format. fprintf ppf
964979 " @[<v>You're trying to access the record field @{<info>%a@}, but the \
@@ -967,7 +982,7 @@ let report_error env ppf = function
967982 %a@,\n \
968983 @,\
969984 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
971986 | None | Some _ ->
972987 Format. fprintf ppf
973988 " @[<v>@{<info>%a@} refers to a record field, but no corresponding \
@@ -978,8 +993,9 @@ let report_error env ppf = function
978993 @{<info>TheModule.%a@}@]@,\
979994 @[- Or specifying the record type explicitly:@ @{<info>let theValue: \
980995 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
983999 | Unbound_modtype lid ->
9841000 fprintf ppf " Unbound module type %a" longident lid;
9851001 spellcheck ppf fold_modtypes env lid
0 commit comments