@@ -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
@@ -129,8 +133,10 @@ let find_constructor =
129133let find_all_constructors =
130134 find_component Env. lookup_all_constructors (fun lid ->
131135 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
134140
135141let find_value ?deprecated_context env loc lid =
136142 Env. check_value_name (Longident. last lid) loc;
@@ -170,8 +176,9 @@ let unbound_constructor_error ?from_type env lid =
170176 Unbound_constructor lid)
171177
172178let unbound_label_error ?from_type env lid =
179+ let lid_with_loc = lid in
173180 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} )
175182
176183(* Support for first-class modules. *)
177184
@@ -938,10 +945,17 @@ let report_error env ppf = function
938945 = Bar@}.@]@]"
939946 Printtyp. longident lid Printtyp. longident lid Printtyp. longident lid;
940947 spellcheck ppf fold_constructors env lid
941- | Unbound_label ( lid , from_type ) ->
948+ | Unbound_label {loc; field_name; from_type} ->
942949 (* modified *)
943950 (match from_type with
944951 | 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+ };
945959 (* TODO: Extend for nullable/null? *)
946960 Format. fprintf ppf
947961 " @[<v>You're trying to access the record field @{<info>%a@}, but the \
@@ -953,14 +967,15 @@ let report_error env ppf = function
953967 @{<info>xx->Option.map(field => field.%a)@}@]@,\
954968 @[- Or use @{<info>Option.getOr@} with a default: \
955969 @{<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
957972 | Some {desc = Tconstr (p , _ , _ )} when Path. same p Predef. path_array ->
958973 Format. fprintf ppf
959974 " @[<v>You're trying to access the record field @{<info>%a@}, but the \
960975 value you're trying to access it on is an @{<info>array@}.@ You need \
961976 to access an individual element of the array if you want to access an \
962977 individual record field.@]"
963- Printtyp. longident lid
978+ Printtyp. longident field_name
964979 | Some ({desc = Tconstr (_p , _ , _ )} as t1 ) ->
965980 Format. fprintf ppf
966981 " @[<v>You're trying to access the record field @{<info>%a@}, but the \
@@ -969,7 +984,7 @@ let report_error env ppf = function
969984 %a@,\n \
970985 @,\
971986 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
973988 | None | Some _ ->
974989 Format. fprintf ppf
975990 " @[<v>@{<info>%a@} refers to a record field, but no corresponding \
@@ -980,8 +995,9 @@ let report_error env ppf = function
980995 @{<info>TheModule.%a@}@]@,\
981996 @[- Or specifying the record type explicitly:@ @{<info>let theValue: \
982997 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
9851001 | Unbound_modtype lid ->
9861002 fprintf ppf " Unbound module type %a" longident lid;
9871003 spellcheck ppf fold_modtypes env lid
0 commit comments