Skip to content

Commit 714b8a8

Browse files
committed
@notundefined attribute for abstract types
1 parent bece9c7 commit 714b8a8

File tree

9 files changed

+96
-13
lines changed

9 files changed

+96
-13
lines changed

compiler/ml/typeopt.ml

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,14 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
5050
| For_sure_yes -> true
5151
| For_sure_no -> false
5252
| NA -> (
53-
let untagged = ref false in
54-
match
55-
let decl = Env.find_type p env in
56-
let () =
57-
if Ast_untagged_variants.has_untagged decl.type_attributes then
58-
untagged := true
59-
in
60-
decl.type_kind
61-
with
53+
let decl = Env.find_type p env in
54+
match decl.type_kind with
6255
| exception _ -> false
63-
| Type_abstract | Type_open -> false
56+
| Type_abstract ->
57+
List.exists
58+
(fun ({txt}, _) -> txt = "notUndefined")
59+
decl.type_attributes
60+
| Type_open -> false
6461
| Type_record _ -> true
6562
| Type_variant
6663
( [
@@ -74,10 +71,13 @@ let rec type_cannot_contain_undefined (typ : Types.type_expr) (env : Env.t) =
7471
| [{cd_id = {name = "()"}; cd_args = Cstr_tuple []}] ) ->
7572
false (* conservative *)
7673
| Type_variant cdecls ->
74+
let untagged =
75+
Ast_untagged_variants.has_untagged decl.type_attributes
76+
in
7777
Ext_list.for_all cdecls (fun cd ->
7878
if Ast_untagged_variants.has_undefined_literal cd.cd_attributes then
7979
false
80-
else if !untagged then
80+
else if untagged then
8181
match cd.cd_args with
8282
| Cstr_tuple [t] ->
8383
Ast_untagged_variants.type_is_builtin_object t

runtime/Jsx.res

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
// Define this as a private empty record so that the compiler does not
2626
// unnecessarily add `Primitive_option.some` calls for optional props.
27-
type element = private {}
27+
@notUndefined
28+
type element
2829

2930
@val external null: element = "null"
3031

runtime/Stdlib_Date.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@notUndefined
12
type t
23

34
type msSinceEpoch = float

runtime/Stdlib_Date.resi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
/**
66
A type representing a JavaScript date.
77
*/
8+
@notUndefined
89
type t
910

1011
/**

runtime/Stdlib_RegExp.res

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
@notUndefined
12
type t
23

34
module Result = {

runtime/Stdlib_RegExp.resi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ See [`RegExp`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference
77
/**
88
Type representing an instantiated `RegExp`.
99
*/
10+
@notUndefined
1011
type t
1112

1213
module Result: {

tests/analysis_tests/tests/src/expected/Completion.res.txt

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
4+
5+
let x6 = {
6+
x: 42
7+
};
8+
9+
let x7 = [
10+
1,
11+
2,
12+
3
13+
];
14+
15+
let x8 = () => {};
16+
17+
let x10 = null;
18+
19+
let x11 = Primitive_option.some(undefined);
20+
21+
let x20 = null;
22+
23+
let x21 = new Date();
24+
25+
let x22 = /test/;
26+
27+
let x1 = "hello";
28+
29+
let x2 = 1;
30+
31+
let x3 = {
32+
TAG: "Ok",
33+
_0: "hi"
34+
};
35+
36+
let x4 = "polyvar";
37+
38+
let x5 = {
39+
x: 42
40+
};
41+
42+
let x12 = "test";
43+
44+
export {
45+
x1,
46+
x2,
47+
x3,
48+
x4,
49+
x5,
50+
x6,
51+
x7,
52+
x8,
53+
x10,
54+
x11,
55+
x12,
56+
x20,
57+
x21,
58+
x22,
59+
}
60+
/* x20 Not a pure module */
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type test = {x: int}
2+
3+
let x1 = Some("hello")
4+
let x2 = Some(1)
5+
let x3 = Some(Ok("hi"))
6+
let x4 = Some(#polyvar)
7+
let x5 = Some({x: 42})
8+
let x6 = Some({"x": 42})
9+
let x7 = Some([1, 2, 3])
10+
let x8 = Some(() => ())
11+
12+
let x10 = Some(Nullable.null)
13+
let x11 = Some(Nullable.undefined)
14+
let x12 = Some(Nullable.Value("test"))
15+
16+
let x20 = Some(Jsx.null)
17+
let x21 = Some(Date.make())
18+
let x22 = Some(/test/)

0 commit comments

Comments
 (0)