-
Notifications
You must be signed in to change notification settings - Fork 470
Add support for functions in untagged variants. #6279
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
"subdirs": true | ||
} | ||
], | ||
"uncurried": false, | ||
"package-specs": { | ||
"module": "es6", | ||
"in-source": true | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
type untaggedError = OnlyOneUnknown | AtMostOneObject | AtMostOneArray | AtMostOneString | AtMostOneNumber | DuplicateLiteral of string | ||
type untaggedError = OnlyOneUnknown | AtMostOneObject | AtMostOneArray | AtMostOneFunction | AtMostOneString | AtMostOneNumber | DuplicateLiteral of string | ||
type error = | ||
| InvalidVariantAsAnnotation | ||
| Duplicated_bs_as | ||
|
@@ -22,14 +22,15 @@ let report_error ppf = | |
| OnlyOneUnknown -> "An unknown case must be the only case with payloads." | ||
| AtMostOneObject -> "At most one case can be an object type." | ||
| AtMostOneArray -> "At most one case can be an array type." | ||
| AtMostOneFunction -> "At most one case can be a function type." | ||
| AtMostOneString -> "At most one case can be a string type." | ||
| AtMostOneNumber -> "At most one case can be a number type (int or float)." | ||
| DuplicateLiteral s -> "Duplicate literal " ^ s ^ "." | ||
) | ||
|
||
(* Type of the runtime representation of an untagged block (case with payoad) *) | ||
type block_type = | ||
| IntType | StringType | FloatType | ArrayType | ObjectType | UnknownType | ||
| IntType | StringType | FloatType | ArrayType | FunctionType | ObjectType | UnknownType | ||
|
||
(* | ||
Type of the runtime representation of a tag. | ||
|
@@ -116,6 +117,10 @@ let get_block_type ~env (cstr: Types.constructor_declaration) : block_type optio | |
Some FloatType | ||
| true, Cstr_tuple [{desc = Tconstr (path, _, _)}] when Path.same path Predef.path_array -> | ||
Some ArrayType | ||
| true, Cstr_tuple [{desc = Tconstr _} as t] when Ast_uncurried.typeIsUncurriedFun t -> | ||
Some FunctionType | ||
| true, Cstr_tuple [{desc = Tarrow _} ] -> | ||
Some FunctionType | ||
| true, Cstr_tuple [{desc = Tconstr (path, _, _)}] when Path. same path Predef.path_string -> | ||
Some StringType | ||
| true, Cstr_tuple [{desc = Tconstr _} as t] when type_is_builtin_object t -> | ||
|
@@ -162,6 +167,7 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * tag) list) ~(blocks : | |
let string_literals = ref StringSet.empty in | ||
let nonstring_literals = ref StringSet.empty in | ||
let arrayTypes = ref 0 in | ||
let functionTypes = ref 0 in | ||
let objectTypes = ref 0 in | ||
let stringTypes = ref 0 in | ||
let numberTypes = ref 0 in | ||
|
@@ -181,6 +187,8 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * tag) list) ~(blocks : | |
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneObject)); | ||
if !arrayTypes > 1 | ||
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneArray)); | ||
if !functionTypes > 1 | ||
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneFunction)); | ||
if !stringTypes > 1 | ||
then raise (Error (loc, InvalidUntaggedVariantDefinition AtMostOneString)); | ||
if !numberTypes > 1 | ||
|
@@ -214,6 +222,9 @@ let checkInvariant ~isUntaggedDef ~(consts : (Location.t * tag) list) ~(blocks : | |
| Some ArrayType -> | ||
incr arrayTypes; | ||
invariant loc | ||
| Some FunctionType -> | ||
incr functionTypes; | ||
invariant loc | ||
| Some (IntType | FloatType) -> | ||
incr numberTypes; | ||
invariant loc | ||
|
@@ -266,6 +277,8 @@ module DynamicChecks = struct | |
let nil = Null |> tag_type | ||
let undefined = Undefined |> tag_type | ||
let object_ = Untagged ObjectType |> tag_type | ||
|
||
let function_ = Untagged FunctionType |> tag_type | ||
let string = Untagged StringType |> tag_type | ||
let number = Untagged IntType |> tag_type | ||
|
||
|
@@ -298,6 +311,8 @@ module DynamicChecks = struct | |
typeof e != number | ||
| ArrayType -> | ||
not (is_array e) | ||
| FunctionType -> | ||
typeof e != function_ | ||
| ObjectType when literals_overlaps_with_object () = false -> | ||
typeof e != object_ | ||
| ObjectType (* overlap *) -> | ||
|
@@ -341,9 +356,8 @@ module DynamicChecks = struct | |
let add_runtime_type_check ~tag_type ~(block_cases: block_type list) x y = | ||
let has_array() = Ext_list.exists block_cases (fun t -> t = ArrayType) in | ||
match tag_type with | ||
| Untagged IntType | ||
| Untagged StringType | ||
| Untagged FloatType -> typeof y == x | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This seems OK as functions can't overlap with int, string, float, object, array. |
||
| Untagged (IntType | StringType | FloatType | FunctionType) -> | ||
typeof y == x | ||
| Untagged ObjectType -> | ||
if has_array() then | ||
typeof y == x &&& not (is_array y) | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Functions cannot overlap with literals so this case should be OK.