Skip to content

Commit 90f59ab

Browse files
committed
@notundefined attribute for abstract types
1 parent bece9c7 commit 90f59ab

File tree

9 files changed

+75
-13
lines changed

9 files changed

+75
-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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Generated by ReScript, PLEASE EDIT WITH CARE
2+
3+
import * as Primitive_option from "rescript/lib/es6/Primitive_option.js";
4+
5+
let x4 = [
6+
1,
7+
2,
8+
3
9+
];
10+
11+
let x5 = () => {};
12+
13+
let x10 = null;
14+
15+
let x11 = Primitive_option.some(undefined);
16+
17+
let x20 = null;
18+
19+
let x21 = new Date();
20+
21+
let x22 = /test/;
22+
23+
let x1 = "hello";
24+
25+
let x2 = 1;
26+
27+
let x3 = "polyvar";
28+
29+
let x12 = "test";
30+
31+
export {
32+
x1,
33+
x2,
34+
x3,
35+
x4,
36+
x5,
37+
x10,
38+
x11,
39+
x12,
40+
x20,
41+
x21,
42+
x22,
43+
}
44+
/* x20 Not a pure module */
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
let x1 = Some("hello")
2+
let x2 = Some(1)
3+
let x3 = Some(#polyvar)
4+
let x4 = Some([1, 2, 3])
5+
let x5 = Some(() => ())
6+
7+
let x10 = Some(Nullable.null)
8+
let x11 = Some(Nullable.undefined)
9+
let x12 = Some(Nullable.Value("test"))
10+
11+
let x20 = Some(Jsx.null)
12+
let x21 = Some(Date.make())
13+
let x22 = Some(/test/)

0 commit comments

Comments
 (0)