Skip to content

Commit 920e45a

Browse files
committed
Add support for empty record type.
1 parent 2bb4f28 commit 920e45a

10 files changed

+52
-24
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#### :rocket: New Feature
1616

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

1920
#### :bug: Bug Fix
2021

jscomp/ml/typecore.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2179,7 +2179,11 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
21792179
if labels_missing <> [] then
21802180
raise(Error(loc, env, Labels_missing labels_missing));
21812181
[||], representation
2182-
| [], _ -> raise(Error(loc, env, Empty_record_literal)) in
2182+
| [], _ ->
2183+
if fields = [] then
2184+
[||], Record_optional_labels []
2185+
else
2186+
raise(Error(loc, env, Empty_record_literal)) in
21832187
let labels_missing = ref [] in
21842188
let label_definitions =
21852189
let matching_label lbl =

jscomp/ml/typedecl.ml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,6 @@ let make_params env params =
206206
List.map make_param params
207207

208208
let transl_labels env closed lbls =
209-
assert (lbls <> []);
210209
if !Config.bs_only then
211210
match !Builtin_attributes.check_duplicated_labels lbls with
212211
| None -> ()

jscomp/test/EmptyRecord.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
4+
function construct(b) {
5+
if (b) {
6+
return {
7+
n: 0
8+
};
9+
} else {
10+
return {};
11+
}
12+
}
13+
14+
var er = {};
15+
16+
exports.construct = construct;
17+
exports.er = er;
18+
/* No side effect */

jscomp/test/EmptyRecord.res

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
type allOptRec = {n?: int, s?:string}
2+
3+
let construct = (b) => b ? {n:0} : {}
4+
5+
// let z = {}
6+
// Error: Empty record literal {} should be type annotated or used in a record context.
7+
8+
type emptyrec = {}
9+
10+
let er : emptyrec = {}

jscomp/test/build.ninja

Lines changed: 2 additions & 1 deletion
Large diffs are not rendered by default.

lib/4.06.1/unstable/js_compiler.ml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36048,7 +36048,6 @@ let make_params env params =
3604836048
List.map make_param params
3604936049

3605036050
let transl_labels env closed lbls =
36051-
assert (lbls <> []);
3605236051
if !Config.bs_only then
3605336052
match !Builtin_attributes.check_duplicated_labels lbls with
3605436053
| None -> ()
@@ -41129,7 +41128,11 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
4112941128
if labels_missing <> [] then
4113041129
raise(Error(loc, env, Labels_missing labels_missing));
4113141130
[||], representation
41132-
| [], _ -> raise(Error(loc, env, Empty_record_literal)) in
41131+
| [], _ ->
41132+
if fields = [] then
41133+
[||], Record_optional_labels []
41134+
else
41135+
raise(Error(loc, env, Empty_record_literal)) in
4113341136
let labels_missing = ref [] in
4113441137
let label_definitions =
4113541138
let matching_label lbl =

lib/4.06.1/unstable/js_playground_compiler.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36048,7 +36048,6 @@ let make_params env params =
3604836048
List.map make_param params
3604936049

3605036050
let transl_labels env closed lbls =
36051-
assert (lbls <> []);
3605236051
if !Config.bs_only then
3605336052
match !Builtin_attributes.check_duplicated_labels lbls with
3605436053
| None -> ()
@@ -41129,7 +41128,11 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
4112941128
if labels_missing <> [] then
4113041129
raise(Error(loc, env, Labels_missing labels_missing));
4113141130
[||], representation
41132-
| [], _ -> raise(Error(loc, env, Empty_record_literal)) in
41131+
| [], _ ->
41132+
if fields = [] then
41133+
[||], Record_optional_labels []
41134+
else
41135+
raise(Error(loc, env, Empty_record_literal)) in
4113341136
let labels_missing = ref [] in
4113441137
let label_definitions =
4113541138
let matching_label lbl =
@@ -287722,13 +287725,6 @@ and parseRecordOrObjectDecl p =
287722287725
:: parseCommaDelimitedRegion ~grammar:Grammar.FieldDeclarations
287723287726
~closing:Rbrace ~f:parseFieldDeclarationRegion p
287724287727
in
287725-
let () =
287726-
match fields with
287727-
| [] ->
287728-
Parser.err ~startPos p
287729-
(Diagnostics.message "A record needs at least one field")
287730-
| _ -> ()
287731-
in
287732287728
Parser.expect Rbrace p;
287733287729
Parser.eatBreadcrumb p;
287734287730
(None, Asttypes.Public, Parsetree.Ptype_record fields))

lib/4.06.1/whole_compiler.ml

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -212435,7 +212435,6 @@ let make_params env params =
212435212435
List.map make_param params
212436212436

212437212437
let transl_labels env closed lbls =
212438-
assert (lbls <> []);
212439212438
if true then
212440212439
match !Builtin_attributes.check_duplicated_labels lbls with
212441212440
| None -> ()
@@ -217516,7 +217515,11 @@ and type_expect_ ?in_function ?(recarg=Rejected) env sexp ty_expected =
217516217515
if labels_missing <> [] then
217517217516
raise(Error(loc, env, Labels_missing labels_missing));
217518217517
[||], representation
217519-
| [], _ -> raise(Error(loc, env, Empty_record_literal)) in
217518+
| [], _ ->
217519+
if fields = [] then
217520+
[||], Record_optional_labels []
217521+
else
217522+
raise(Error(loc, env, Empty_record_literal)) in
217520217523
let labels_missing = ref [] in
217521217524
let label_definitions =
217522217525
let matching_label lbl =
@@ -301246,13 +301249,6 @@ and parseRecordOrObjectDecl p =
301246301249
:: parseCommaDelimitedRegion ~grammar:Grammar.FieldDeclarations
301247301250
~closing:Rbrace ~f:parseFieldDeclarationRegion p
301248301251
in
301249-
let () =
301250-
match fields with
301251-
| [] ->
301252-
Parser.err ~startPos p
301253-
(Diagnostics.message "A record needs at least one field")
301254-
| _ -> ()
301255-
in
301256301252
Parser.expect Rbrace p;
301257301253
Parser.eatBreadcrumb p;
301258301254
(None, Asttypes.Public, Parsetree.Ptype_record fields))

0 commit comments

Comments
 (0)