Skip to content

10.1_release -> master #5855

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 4 commits into from
Nov 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,11 @@ These are only breaking changes for unformatted code.
- Process `@set` annotation for field update as generating an uncurried function https://github.com/rescript-lang/rescript-compiler/pull/5846
- Treat uncurried application of primitives like curried application, which produces better output https://github.com/rescript-lang/rescript-compiler/pull/5851

# 10.1.0-rc.6
# 10.1.0

#### :bug: Bug Fix

- Fix issue where no error was reported when ? was used for non-optional fields. https://github.com/rescript-lang/rescript-compiler/pull/5853
- Fix issue where optional fields in inline records were not supported and would cause type errors https://github.com/rescript-lang/rescript-compiler/pull/5827

# 10.1.0-rc.5
Expand Down
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 @@ -74,6 +74,7 @@ type error =
| Labels_omitted of string list
| Empty_record_literal
| Uncurried_arity_mismatch of type_expr * int * int
| 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 @@ -309,6 +310,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 @@ -1151,15 +1165,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 @@ -1878,15 +1885,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 @@ -3876,6 +3876,11 @@ let report_error env ppf = function
type_expr typ;
fprintf ppf "@ @[It is applied with @{<error>%d@} argument%s but it requires @{<info>%d@}.@]@]"
args (if args = 0 then "" else "s") arity
| 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 @@ -110,6 +110,7 @@ type error =
| Labels_omitted of string list
| Empty_record_literal
| Uncurried_arity_mismatch of type_expr * int * int
| 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 @@ -40696,6 +40696,7 @@ type error =
| Labels_omitted of string list
| Empty_record_literal
| Uncurried_arity_mismatch of type_expr * int * int
| 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 @@ -40804,6 +40805,7 @@ type error =
| Labels_omitted of string list
| Empty_record_literal
| Uncurried_arity_mismatch of type_expr * int * int
| 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 @@ -41039,6 +41041,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 @@ -41881,15 +41896,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 @@ -42608,15 +42616,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 @@ -44606,6 +44607,11 @@ let report_error env ppf = function
type_expr typ;
fprintf ppf "@ @[It is applied with @{<error>%d@} argument%s but it requires @{<info>%d@}.@]@]"
args (if args = 0 then "" else "s") arity
| 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 @@ -40696,6 +40696,7 @@ type error =
| Labels_omitted of string list
| Empty_record_literal
| Uncurried_arity_mismatch of type_expr * int * int
| 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 @@ -40804,6 +40805,7 @@ type error =
| Labels_omitted of string list
| Empty_record_literal
| Uncurried_arity_mismatch of type_expr * int * int
| 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 @@ -41039,6 +41041,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 @@ -41881,15 +41896,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 @@ -42608,15 +42616,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 @@ -44606,6 +44607,11 @@ let report_error env ppf = function
type_expr typ;
fprintf ppf "@ @[It is applied with @{<error>%d@} argument%s but it requires @{<info>%d@}.@]@]"
args (if args = 0 then "" else "s") arity
| 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 @@ -95690,6 +95690,7 @@ type error =
| Labels_omitted of string list
| Empty_record_literal
| Uncurried_arity_mismatch of type_expr * int * int
| 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 @@ -95798,6 +95799,7 @@ type error =
| Labels_omitted of string list
| Empty_record_literal
| Uncurried_arity_mismatch of type_expr * int * int
| 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 @@ -96033,6 +96035,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 @@ -96875,15 +96890,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 @@ -97602,15 +97610,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 @@ -99600,6 +99601,11 @@ let report_error env ppf = function
type_expr typ;
fprintf ppf "@ @[It is applied with @{<error>%d@} argument%s but it requires @{<info>%d@}.@]@]"
args (if args = 0 then "" else "s") arity
| 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