Skip to content
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

Support {} for empty record literals and types. #5658

Merged
merged 4 commits into from
Sep 13, 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
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@

# 10.1.0-alpha.2

#### :rocket: New Feature

- Add support for empty record literal `{}` for records where all fields are optional https://github.com/rescript-lang/rescript-compiler/pull/5658
- Add support for empty record type (e.g. `type empty = {}`) https://github.com/rescript-lang/rescript-compiler/pull/5658

#### :bug: Bug Fix

- Fix printing of type declarations in error message where they would be considered recursive by default
Expand Down
2 changes: 1 addition & 1 deletion jscomp/core/js_dump.ml
Original file line number Diff line number Diff line change
Expand Up @@ -718,7 +718,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
Js_op.Lit (Ext_ident.convert x))))
(*name convention of Record is slight different from modules*)
| Caml_block (el, mutable_flag, _, Blk_record { fields; record_repr }) -> (
if Ext_array.for_alli fields (fun i v -> string_of_int i = v) then
if Array.length fields <> 0 && Ext_array.for_alli fields (fun i v -> string_of_int i = v) then
expression_desc cxt ~level f (Array (el, mutable_flag))
else
match record_repr with
Expand Down
41 changes: 30 additions & 11 deletions jscomp/ml/typecore.ml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ type error =
| Unknown_literal of string * char
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -298,7 +299,7 @@ let extract_option_type env ty =

let extract_concrete_record env ty =
match extract_concrete_typedecl env ty with
(p0, p, {type_kind=Type_record (fields, _)}) -> (p0, p, fields)
(p0, p, {type_kind=Type_record (fields, repr)}) -> (p0, p, fields, repr)
| _ -> raise Not_found

let extract_concrete_variant env ty =
Expand Down Expand Up @@ -1145,7 +1146,7 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
assert (lid_sp_list <> []);
let opath, record_ty =
try
let (p0, p,_) = extract_concrete_record !env expected_ty in
let (p0, p, _, _) = extract_concrete_record !env expected_ty in
Some (p0, p), expected_ty
with Not_found -> None, newvar ()
in
Expand Down Expand Up @@ -2147,14 +2148,13 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
exp_env = env }
end
| Pexp_record(lid_sexp_list, None) ->
assert (lid_sexp_list <> []);
let ty_record, opath =
let ty_record, opath, fields, repr_opt =
match extract_concrete_record env ty_expected with
| (p0, p,_) ->
| (p0, p, fields, repr) ->
(* XXX level may be wrong *)
ty_expected, Some (p0, p)
ty_expected, Some (p0, p), fields, Some repr
| exception Not_found ->
newvar (), None
newvar (), None, [], None

in
let lbl_exp_list =
Expand All @@ -2166,7 +2166,24 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
in
unify_exp_types loc env ty_record (instance env ty_expected);
check_duplicates loc env lbl_exp_list;
let (_, { lbl_all = label_descriptions; lbl_repres = representation}, _) = List.hd lbl_exp_list in
let label_descriptions, representation = match lbl_exp_list, repr_opt with
| (_, { lbl_all = label_descriptions; lbl_repres = representation}, _) :: _, _ -> label_descriptions, representation
| [], Some (Record_optional_labels optional_labels as representation) when lid_sexp_list = [] ->
let filter_missing (ld : Types.label_declaration) =
let name = Ident.name ld.ld_id in
if List.mem name optional_labels then
None
else
Some name in
let labels_missing = fields |> List.filter_map filter_missing in
if labels_missing <> [] then
raise(Error(loc, env, Labels_missing labels_missing));
[||], representation
| [], _ ->
if fields = [] then
[||], Record_optional_labels []
else
raise(Error(loc, env, Empty_record_literal)) in
let labels_missing = ref [] in
let label_definitions =
let matching_label lbl =
Expand Down Expand Up @@ -2205,7 +2222,7 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
let ty_record, opath =
let get_path ty =
try
let (p0, p,_) = extract_concrete_record env ty in
let (p0, p, _, _) = extract_concrete_record env ty in
(* XXX level may be wrong *)
Some (p0, p)
with Not_found -> None
Expand Down Expand Up @@ -2803,7 +2820,7 @@ and type_label_access env srecord lid =
let ty_exp = record.exp_type in
let opath =
try
let (p0, p,_) = extract_concrete_record env ty_exp in
let (p0, p, _, _) = extract_concrete_record env ty_exp in
Some(p0, p)
with Not_found -> None
in
Expand Down Expand Up @@ -3805,9 +3822,11 @@ let report_error env ppf = function
| Illegal_letrec_pat ->
fprintf ppf
"Only variables are allowed as left-hand side of `let rec'"
| Labels_omitted labels ->
| Labels_omitted labels ->
fprintf ppf "For labeled funciton, labels %s were omitted in the application of this function."
(String.concat ", " labels)
| Empty_record_literal ->
fprintf ppf "Empty record literal {} should be type annotated or used in a record context."

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 @@ -108,6 +108,7 @@ type error =
| Unknown_literal of string * char
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down
1 change: 0 additions & 1 deletion jscomp/ml/typedecl.ml
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,6 @@ let make_params env params =
List.map make_param params

let transl_labels env closed lbls =
assert (lbls <> []);
if !Config.bs_only then
match !Builtin_attributes.check_duplicated_labels lbls with
| None -> ()
Expand Down
18 changes: 18 additions & 0 deletions jscomp/test/EmptyRecord.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
'use strict';


function construct(b) {
if (b) {
return {
n: 0
};
} else {
return {};
}
}

var er = {};

exports.construct = construct;
exports.er = er;
/* No side effect */
10 changes: 10 additions & 0 deletions jscomp/test/EmptyRecord.res
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
type allOptRec = {n?: int, s?:string}

let construct = (b) => b ? {n:0} : {}

// let z = {}
// Error: Empty record literal {} should be type annotated or used in a record context.

type emptyrec = {}

let er : emptyrec = {}
3 changes: 2 additions & 1 deletion jscomp/test/build.ninja

Large diffs are not rendered by default.

45 changes: 32 additions & 13 deletions lib/4.06.1/unstable/js_compiler.ml
Original file line number Diff line number Diff line change
Expand Up @@ -36048,7 +36048,6 @@ let make_params env params =
List.map make_param params

let transl_labels env closed lbls =
assert (lbls <> []);
if !Config.bs_only then
match !Builtin_attributes.check_duplicated_labels lbls with
| None -> ()
Expand Down Expand Up @@ -38915,6 +38914,7 @@ type error =
| Unknown_literal of string * char
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -39021,6 +39021,7 @@ type error =
| Unknown_literal of string * char
| Illegal_letrec_pat
| Labels_omitted of string list
| Empty_record_literal
exception Error of Location.t * Env.t * error
exception Error_forward of Location.error

Expand Down Expand Up @@ -39247,7 +39248,7 @@ let extract_option_type env ty =

let extract_concrete_record env ty =
match extract_concrete_typedecl env ty with
(p0, p, {type_kind=Type_record (fields, _)}) -> (p0, p, fields)
(p0, p, {type_kind=Type_record (fields, repr)}) -> (p0, p, fields, repr)
| _ -> raise Not_found

let extract_concrete_variant env ty =
Expand Down Expand Up @@ -40094,7 +40095,7 @@ and type_pat_aux ~constrs ~labels ~no_existentials ~mode ~explode ~env
assert (lid_sp_list <> []);
let opath, record_ty =
try
let (p0, p,_) = extract_concrete_record !env expected_ty in
let (p0, p, _, _) = extract_concrete_record !env expected_ty in
Some (p0, p), expected_ty
with Not_found -> None, newvar ()
in
Expand Down Expand Up @@ -41096,14 +41097,13 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
exp_env = env }
end
| Pexp_record(lid_sexp_list, None) ->
assert (lid_sexp_list <> []);
let ty_record, opath =
let ty_record, opath, fields, repr_opt =
match extract_concrete_record env ty_expected with
| (p0, p,_) ->
| (p0, p, fields, repr) ->
(* XXX level may be wrong *)
ty_expected, Some (p0, p)
ty_expected, Some (p0, p), fields, Some repr
| exception Not_found ->
newvar (), None
newvar (), None, [], None

in
let lbl_exp_list =
Expand All @@ -41115,7 +41115,24 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
in
unify_exp_types loc env ty_record (instance env ty_expected);
check_duplicates loc env lbl_exp_list;
let (_, { lbl_all = label_descriptions; lbl_repres = representation}, _) = List.hd lbl_exp_list in
let label_descriptions, representation = match lbl_exp_list, repr_opt with
| (_, { lbl_all = label_descriptions; lbl_repres = representation}, _) :: _, _ -> label_descriptions, representation
| [], Some (Record_optional_labels optional_labels as representation) when lid_sexp_list = [] ->
let filter_missing (ld : Types.label_declaration) =
let name = Ident.name ld.ld_id in
if List.mem name optional_labels then
None
else
Some name in
let labels_missing = fields |> List.filter_map filter_missing in
if labels_missing <> [] then
raise(Error(loc, env, Labels_missing labels_missing));
[||], representation
| [], _ ->
if fields = [] then
[||], Record_optional_labels []
else
raise(Error(loc, env, Empty_record_literal)) in
let labels_missing = ref [] in
let label_definitions =
let matching_label lbl =
Expand Down Expand Up @@ -41154,7 +41171,7 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
let ty_record, opath =
let get_path ty =
try
let (p0, p,_) = extract_concrete_record env ty in
let (p0, p, _, _) = extract_concrete_record env ty in
(* XXX level may be wrong *)
Some (p0, p)
with Not_found -> None
Expand Down Expand Up @@ -41752,7 +41769,7 @@ and type_label_access env srecord lid =
let ty_exp = record.exp_type in
let opath =
try
let (p0, p,_) = extract_concrete_record env ty_exp in
let (p0, p, _, _) = extract_concrete_record env ty_exp in
Some(p0, p)
with Not_found -> None
in
Expand Down Expand Up @@ -42754,9 +42771,11 @@ let report_error env ppf = function
| Illegal_letrec_pat ->
fprintf ppf
"Only variables are allowed as left-hand side of `let rec'"
| Labels_omitted labels ->
| Labels_omitted labels ->
fprintf ppf "For labeled funciton, labels %s were omitted in the application of this function."
(String.concat ", " labels)
| Empty_record_literal ->
fprintf ppf "Empty record literal {} should be type annotated or used in a record context."

let super_report_error_no_wrap_printing_env = report_error

Expand Down Expand Up @@ -79689,7 +79708,7 @@ and expression_desc cxt ~(level : int) f x : cxt =
Js_op.Lit (Ext_ident.convert x))))
(*name convention of Record is slight different from modules*)
| Caml_block (el, mutable_flag, _, Blk_record { fields; record_repr }) -> (
if Ext_array.for_alli fields (fun i v -> string_of_int i = v) then
if Array.length fields <> 0 && Ext_array.for_alli fields (fun i v -> string_of_int i = v) then
expression_desc cxt ~level f (Array (el, mutable_flag))
else
match record_repr with
Expand Down
Loading