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

Dict as a builtin #6590

Merged
merged 5 commits into from
Jan 30, 2024
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

- Experimental support of tagged template literals, eg ```sql`select * from ${table}```. https://github.com/rescript-lang/rescript-compiler/pull/6250
- Experimental support for generic/custom JSX transforms. https://github.com/rescript-lang/rescript-compiler/pull/6565
- `dict` is now a builtin type. https://github.com/rescript-lang/rescript-compiler/pull/6590

#### :bug: Bug Fix

Expand Down
2 changes: 1 addition & 1 deletion jscomp/gentype/TranslateTypeExprFromTypes.ml
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ let translateConstr ~config ~paramsTranslation ~(path : Path.t) ~typeEnv =
| ( (["Js"; "Promise"; "t"] | ["Promise"; "t"] | ["promise"]),
[paramTranslation] ) ->
{paramTranslation with type_ = Promise paramTranslation.type_}
| (["Js"; "Dict"; "t"] | ["Dict"; "t"]), [paramTranslation] ->
| (["Js"; "Dict"; "t"] | ["Dict"; "t"] | ["dict"]), [paramTranslation] ->
{paramTranslation with type_ = Dict paramTranslation.type_}
| ["function$"], [arg; _arity] ->
{dependencies = arg.dependencies; type_ = arg.type_}
Expand Down
1 change: 1 addition & 0 deletions jscomp/ml/ast_untagged_variants.ml
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ let reportConstructorMoreThanOneArg ~loc ~name =

let type_is_builtin_object (t : Types.type_expr) =
match t.desc with
| Tconstr (Path.Pident ident, [_], _) when Ident.name ident = "dict" -> true
| Tconstr (path, _, _) ->
let name = Path.name path in
name = "Js.Dict.t" || name = "Js_dict.t"
Expand Down
12 changes: 11 additions & 1 deletion jscomp/ml/predef.ml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ and ident_array = ident_create "array"
and ident_list = ident_create "list"
and ident_option = ident_create "option"
and ident_result = ident_create "result"
and ident_dict = ident_create "dict"

and ident_int64 = ident_create "int64"
and ident_lazy_t = ident_create "lazy_t"
Expand Down Expand Up @@ -82,6 +83,7 @@ and path_array = Pident ident_array
and path_list = Pident ident_list
and path_option = Pident ident_option
and path_result = Pident ident_result
and path_dict = Pident ident_dict


and path_int64 = Pident ident_int64
Expand All @@ -105,6 +107,7 @@ and type_array t = newgenty (Tconstr(path_array, [t], ref Mnil))
and type_list t = newgenty (Tconstr(path_list, [t], ref Mnil))
and type_option t = newgenty (Tconstr(path_option, [t], ref Mnil))
and type_result t1 t2 = newgenty (Tconstr(path_result, [t1; t2], ref Mnil))
and type_dict t = newgenty (Tconstr(path_dict, [t], ref Mnil))

and type_int64 = newgenty (Tconstr(path_int64, [], ref Mnil))
and type_lazy_t t = newgenty (Tconstr(path_lazy_t, [t], ref Mnil))
Expand Down Expand Up @@ -226,6 +229,12 @@ let common_initial_env add_type add_extension empty_env =
Type_variant([cstr ident_ok [tvar1];
cstr ident_error [tvar2]]);
type_variance = [Variance.covariant; Variance.covariant]}
and decl_dict =
let tvar = newgenvar() in
{decl_abstr with
type_params = [tvar];
type_arity = 1;
type_variance = [Variance.covariant]}
and decl_uncurried =
let tvar1, tvar2 = newgenvar(), newgenvar() in
{decl_abstr with
Expand Down Expand Up @@ -292,6 +301,7 @@ let common_initial_env add_type add_extension empty_env =
add_type ident_lazy_t decl_lazy_t (
add_type ident_option decl_option (
add_type ident_result decl_result (
add_type ident_dict decl_dict (
add_type ident_list decl_list (
add_type ident_array decl_array (
add_type ident_exn decl_exn (
Expand All @@ -305,7 +315,7 @@ let common_initial_env add_type add_extension empty_env =
add_type ident_extension_constructor decl_abstr (
add_type ident_floatarray decl_abstr (
add_type ident_promise decl_promise (
empty_env))))))))))))))))))))))))))
empty_env)))))))))))))))))))))))))))

let build_initial_env add_type add_exception empty_env =
let common = common_initial_env add_type add_exception empty_env in
Expand Down
2 changes: 2 additions & 0 deletions jscomp/ml/predef.mli
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ val type_array: type_expr -> type_expr
val type_list: type_expr -> type_expr
val type_option: type_expr -> type_expr
val type_result: type_expr -> type_expr -> type_expr
val type_dict: type_expr -> type_expr

val type_int64: type_expr
val type_lazy_t: type_expr -> type_expr
Expand All @@ -47,6 +48,7 @@ val path_array: Path.t
val path_list: Path.t
val path_option: Path.t
val path_result: Path.t
val path_dict: Path.t

val path_int64: Path.t
val path_lazy_t: Path.t
Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/js_dict.res
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
/*** Provides a simple key-value dictionary abstraction over native JavaScript objects */

/** The dict type */
type t<'a>
type t<'a> = dict<'a>

/** The key type, an alias of string */
type key = string
Expand Down
2 changes: 1 addition & 1 deletion jscomp/others/js_dict.resi
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ Dictionary type (ie an '{ }' JS object). However it is restricted to hold a
single type; therefore values must have the same type. This Dictionary type is
mostly used with the Js_json.t type.
*/
type t<'a>
type t<'a> = dict<'a>

/**
The type for dictionary keys. This means that dictionaries *must* use `string`s as their keys.
Expand Down
3 changes: 1 addition & 2 deletions lib/es6/js_json.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@


import * as Caml_option from "./caml_option.js";

var Kind = {};

Expand Down Expand Up @@ -75,7 +74,7 @@ function decodeNumber(json) {

function decodeObject(json) {
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
return Caml_option.some(json);
return json;
}

}
Expand Down
3 changes: 1 addition & 2 deletions lib/js/js_json.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
'use strict';

var Caml_option = require("./caml_option.js");

var Kind = {};

Expand Down Expand Up @@ -75,7 +74,7 @@ function decodeNumber(json) {

function decodeObject(json) {
if (typeof json === "object" && !Array.isArray(json) && json !== null) {
return Caml_option.some(json);
return json;
}

}
Expand Down