Skip to content

Print error message when ? is used for non-optional fields. #5853

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

We've found a bug for you!
/.../fixtures/fieldNotOptional.res:3:19

1 │ type r = {nonopt: int, opt?: string}
2 │
3 │ let v = {nonopt: ?3, opt: ?None}
4 │
5 │ let f = r =>

Field nonopt is not optional in type r. Use without ?
17 changes: 17 additions & 0 deletions jscomp/build_tests/super_errors/fixtures/fieldNotOptional.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type r = {nonopt: int, opt?: string}

let v = {nonopt: ?3, opt: ?None}

let f = r =>
switch r {
| {nonopt: ?_, opt: ?_} => true
}

type inline = A({nonopt: int, opt?: string})

let vi = A({nonopt: ?3, opt: ?None})

let fi = a =>
switch a {
| A ({nonopt: ?_, opt: ?_}) => true
}
37 changes: 21 additions & 16 deletions jscomp/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ type error =
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
| Field_not_optional of string * type_expr
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -308,6 +309,19 @@ let extract_concrete_variant env ty =
| (p0, p, {type_kind=Type_open}) -> (p0, p, [])
| _ -> raise Not_found

let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false

let check_optional_attr env ld attrs loc =
let check_redundant () =
if not (label_is_optional ld) then
raise (Error (loc, env, Field_not_optional (ld.lbl_name, ld.lbl_res)));
true in
Ext_list.exists attrs (fun ({txt}, _) ->
txt = "ns.optional" && check_redundant ())

(* unification inside type_pat*)
let unify_pat_types loc env ty ty' =
Expand Down Expand Up @@ -1150,15 +1164,8 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
Some (p0, p), expected_ty
with Not_found -> None, newvar ()
in
let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false in
let process_optional_label (ld, pat) =
let exp_optional_attr =
Ext_list.exists pat.ppat_attributes (fun ({txt },_) -> txt = "ns.optional")
in
let exp_optional_attr = check_optional_attr !env ld pat.ppat_attributes pat.ppat_loc in
let isFromPamatch = match pat.ppat_desc with
| Ppat_construct ({txt = Lident s}, _) ->
String.length s >= 2 && s.[0] = '#' && s.[1] = '$'
Expand Down Expand Up @@ -1877,15 +1884,8 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
unify_exp env (re exp) (instance env ty_expected);
exp
in
let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false in
let process_optional_label (id, ld, e) =
let exp_optional_attr =
Ext_list.exists e.pexp_attributes (fun ({txt },_) -> txt = "ns.optional")
in
let exp_optional_attr = check_optional_attr env ld e.pexp_attributes e.pexp_loc in
if label_is_optional ld && not exp_optional_attr then
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
let e = Ast_helper.Exp.construct ~loc:e.pexp_loc lid (Some e)
Expand Down Expand Up @@ -3797,6 +3797,11 @@ let report_error env ppf = function
(String.concat ", " labels)
| Empty_record_literal ->
fprintf ppf "Empty record literal {} should be type annotated or used in a record context."
| Field_not_optional (name, typ) ->
fprintf ppf
"Field @{<info>%s@} is not optional in type %a. Use without ?" name
type_expr typ


let super_report_error_no_wrap_printing_env = report_error

Expand Down
1 change: 1 addition & 0 deletions jscomp/ml/typecore.mli
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ type error =
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
| Field_not_optional of string * type_expr
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down
38 changes: 22 additions & 16 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40690,6 +40690,7 @@ type error =
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
| Field_not_optional of string * type_expr
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -40797,6 +40798,7 @@ type error =
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
| Field_not_optional of string * type_expr
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -41032,6 +41034,19 @@ let extract_concrete_variant env ty =
| (p0, p, {type_kind=Type_open}) -> (p0, p, [])
| _ -> raise Not_found

let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false

let check_optional_attr env ld attrs loc =
let check_redundant () =
if not (label_is_optional ld) then
raise (Error (loc, env, Field_not_optional (ld.lbl_name, ld.lbl_res)));
true in
Ext_list.exists attrs (fun ({txt}, _) ->
txt = "ns.optional" && check_redundant ())

(* unification inside type_pat*)
let unify_pat_types loc env ty ty' =
Expand Down Expand Up @@ -41874,15 +41889,8 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
Some (p0, p), expected_ty
with Not_found -> None, newvar ()
in
let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false in
let process_optional_label (ld, pat) =
let exp_optional_attr =
Ext_list.exists pat.ppat_attributes (fun ({txt },_) -> txt = "ns.optional")
in
let exp_optional_attr = check_optional_attr !env ld pat.ppat_attributes pat.ppat_loc in
let isFromPamatch = match pat.ppat_desc with
| Ppat_construct ({txt = Lident s}, _) ->
String.length s >= 2 && s.[0] = '#' && s.[1] = '$'
Expand Down Expand Up @@ -42601,15 +42609,8 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
unify_exp env (re exp) (instance env ty_expected);
exp
in
let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false in
let process_optional_label (id, ld, e) =
let exp_optional_attr =
Ext_list.exists e.pexp_attributes (fun ({txt },_) -> txt = "ns.optional")
in
let exp_optional_attr = check_optional_attr env ld e.pexp_attributes e.pexp_loc in
if label_is_optional ld && not exp_optional_attr then
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
let e = Ast_helper.Exp.construct ~loc:e.pexp_loc lid (Some e)
Expand Down Expand Up @@ -44521,6 +44522,11 @@ let report_error env ppf = function
(String.concat ", " labels)
| Empty_record_literal ->
fprintf ppf "Empty record literal {} should be type annotated or used in a record context."
| Field_not_optional (name, typ) ->
fprintf ppf
"Field @{<info>%s@} is not optional in type %a. Use without ?" name
type_expr typ


let super_report_error_no_wrap_printing_env = report_error

Expand Down
38 changes: 22 additions & 16 deletions lib/4.06.1/unstable/js_playground_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40690,6 +40690,7 @@ type error =
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
| Field_not_optional of string * type_expr
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -40797,6 +40798,7 @@ type error =
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
| Field_not_optional of string * type_expr
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -41032,6 +41034,19 @@ let extract_concrete_variant env ty =
| (p0, p, {type_kind=Type_open}) -> (p0, p, [])
| _ -> raise Not_found

let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false

let check_optional_attr env ld attrs loc =
let check_redundant () =
if not (label_is_optional ld) then
raise (Error (loc, env, Field_not_optional (ld.lbl_name, ld.lbl_res)));
true in
Ext_list.exists attrs (fun ({txt}, _) ->
txt = "ns.optional" && check_redundant ())

(* unification inside type_pat*)
let unify_pat_types loc env ty ty' =
Expand Down Expand Up @@ -41874,15 +41889,8 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
Some (p0, p), expected_ty
with Not_found -> None, newvar ()
in
let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false in
let process_optional_label (ld, pat) =
let exp_optional_attr =
Ext_list.exists pat.ppat_attributes (fun ({txt },_) -> txt = "ns.optional")
in
let exp_optional_attr = check_optional_attr !env ld pat.ppat_attributes pat.ppat_loc in
let isFromPamatch = match pat.ppat_desc with
| Ppat_construct ({txt = Lident s}, _) ->
String.length s >= 2 && s.[0] = '#' && s.[1] = '$'
Expand Down Expand Up @@ -42601,15 +42609,8 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
unify_exp env (re exp) (instance env ty_expected);
exp
in
let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false in
let process_optional_label (id, ld, e) =
let exp_optional_attr =
Ext_list.exists e.pexp_attributes (fun ({txt },_) -> txt = "ns.optional")
in
let exp_optional_attr = check_optional_attr env ld e.pexp_attributes e.pexp_loc in
if label_is_optional ld && not exp_optional_attr then
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
let e = Ast_helper.Exp.construct ~loc:e.pexp_loc lid (Some e)
Expand Down Expand Up @@ -44521,6 +44522,11 @@ let report_error env ppf = function
(String.concat ", " labels)
| Empty_record_literal ->
fprintf ppf "Empty record literal {} should be type annotated or used in a record context."
| Field_not_optional (name, typ) ->
fprintf ppf
"Field @{<info>%s@} is not optional in type %a. Use without ?" name
type_expr typ


let super_report_error_no_wrap_printing_env = report_error

Expand Down
38 changes: 22 additions & 16 deletions lib/4.06.1/whole_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -216862,6 +216862,7 @@ type error =
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
| Field_not_optional of string * type_expr
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -216969,6 +216970,7 @@ type error =
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
| Field_not_optional of string * type_expr
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -217204,6 +217206,19 @@ let extract_concrete_variant env ty =
| (p0, p, {type_kind=Type_open}) -> (p0, p, [])
| _ -> raise Not_found

let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false

let check_optional_attr env ld attrs loc =
let check_redundant () =
if not (label_is_optional ld) then
raise (Error (loc, env, Field_not_optional (ld.lbl_name, ld.lbl_res)));
true in
Ext_list.exists attrs (fun ({txt}, _) ->
txt = "ns.optional" && check_redundant ())

(* unification inside type_pat*)
let unify_pat_types loc env ty ty' =
Expand Down Expand Up @@ -218046,15 +218061,8 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
Some (p0, p), expected_ty
with Not_found -> None, newvar ()
in
let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false in
let process_optional_label (ld, pat) =
let exp_optional_attr =
Ext_list.exists pat.ppat_attributes (fun ({txt },_) -> txt = "ns.optional")
in
let exp_optional_attr = check_optional_attr !env ld pat.ppat_attributes pat.ppat_loc in
let isFromPamatch = match pat.ppat_desc with
| Ppat_construct ({txt = Lident s}, _) ->
String.length s >= 2 && s.[0] = '#' && s.[1] = '$'
Expand Down Expand Up @@ -218773,15 +218781,8 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
unify_exp env (re exp) (instance env ty_expected);
exp
in
let label_is_optional ld =
match ld.lbl_repres with
| Record_optional_labels lbls -> Ext_list.mem_string lbls ld.lbl_name
| Record_inlined {optional_labels} -> Ext_list.mem_string optional_labels ld.lbl_name
| _ -> false in
let process_optional_label (id, ld, e) =
let exp_optional_attr =
Ext_list.exists e.pexp_attributes (fun ({txt },_) -> txt = "ns.optional")
in
let exp_optional_attr = check_optional_attr env ld e.pexp_attributes e.pexp_loc in
if label_is_optional ld && not exp_optional_attr then
let lid = mknoloc (Longident.(Ldot (Lident "*predef*", "Some"))) in
let e = Ast_helper.Exp.construct ~loc:e.pexp_loc lid (Some e)
Expand Down Expand Up @@ -220693,6 +220694,11 @@ let report_error env ppf = function
(String.concat ", " labels)
| Empty_record_literal ->
fprintf ppf "Empty record literal {} should be type annotated or used in a record context."
| Field_not_optional (name, typ) ->
fprintf ppf
"Field @{<info>%s@} is not optional in type %a. Use without ?" name
type_expr typ


let super_report_error_no_wrap_printing_env = report_error

Expand Down