You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit b43a9a5
Browse filesBrowse the repository at this point in the historyBrowse files
Merge the @notundefined rule into the rule on declared shapes
`@notUndefined`, `@typeOf` and `@instanceOf` declare levels of one stored
shape, checked on abbreviations by one entry point and across interfaces
by one comparer. Rule 5 now describes the three together, with their
errors and interface checks; rule 13 is gone and the rule on `@tag` and
interfaces becomes rule 13.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01CMkrYaktctdKc4M41WF36t
Signed-off-by: Christoph Knittel <ck@cca.io>
`@instanceOf("C")` on a type declares that its values are instances of the
133
-
global class at the path `C`, such as `"Date"` or `"Intl.Locale"`.
134
-
`@typeOf("k")` declares that their `typeof` is `k`, one of `"string"`,
135
-
`"number"`, `"bigint"`, `"boolean"`, `"symbol"`, `"function"` and
136
-
`"object"`. A type has at most one of the two, and only abstract types and
137
-
type abbreviations, public or private, can have them.
132
+
Three annotations declare what the values of a type are at run time, each
133
+
level implying the one before:
134
+
135
+
-`@notUndefined`: they are never `undefined`;
136
+
-`@typeOf("k")`: their `typeof` is `k`, one of `"string"`, `"number"`,
137
+
`"bigint"`, `"boolean"`, `"symbol"`, `"function"` and `"object"`;
138
+
-`@instanceOf("C")`: they are instances of the global class at the path `C`,
139
+
such as `"Date"` or `"Intl.Locale"`.
140
+
141
+
A type has at most one of `@typeOf` and `@instanceOf`, and only abstract types
142
+
and type abbreviations, public or private, can have any of the three.
143
+
`@notUndefined` affects only rule 12: `option` of the type, and of an untagged
144
+
variant with a payload of the type, is unboxed. It does not make the type a
145
+
recognized payload kind (rule 4).
138
146
139
147
When a type is declared, its annotation is resolved into its *shape*, which
140
-
is stored on the declaration: the class with its superclasses, or the
141
-
`typeof` kind (`@notUndefined` gives the shape "never undefined", rule 13).
142
-
The classes annotated further down a chain of abbreviations are the
143
-
superclasses (rule 8):
148
+
is stored on the declaration: "never undefined", the `typeof` kind, or the
149
+
class with its superclasses. The builtin types state their shapes in their
150
+
declarations (rule 4). The classes annotated further down a chain of
151
+
abbreviations are the superclasses (rule 8):
144
152
145
153
```rescript
146
154
@instanceOf("Animal") type animal
@@ -151,14 +159,16 @@ The classifier and every other reader take the shape of the payload type as
151
159
written, or of the first type it abbreviates that states one, so the
152
160
outermost annotation decides. A shape is resolved in the environment the
153
161
type is declared in, so an interface's shape is what its users see, even
154
-
where the implementation knows more (rule 14). An annotation that repeats
162
+
where the implementation knows more. An annotation that repeats
155
163
the abbreviated type's, as the declarations of a module alias do, adds no
156
164
superclass.
157
165
158
166
An annotation on an abbreviation must agree with the abbreviated type. It can
159
-
refine an abstract type that is not builtin and has no annotation, or repeat
160
-
what the abbreviated type is recognized by, as `@typeOf("number") type count =
161
-
int` does. The type of a functor parameter, of a
167
+
refine a type whose representation the compiler does not see, an abstract
168
+
type that is not builtin and has no annotation, or restate what the
169
+
abbreviated type is known to be, as `@typeOf("number") type count = int` and
170
+
`@notUndefined type count = int` do; a restating `@notUndefined` keeps the
171
+
kind of `int`. The type of a functor parameter, of a
162
172
recursive module being defined, and a locally abstract type `(type a)` are
163
173
abstract only where they are declared: they stand for another type, so they
164
174
cannot be refined. Only a private abbreviation can declare a subclass of the
@@ -171,14 +181,34 @@ would `type dog = animal<int>` or `type dog = phantom<int>` with
171
181
| Declaration | Error | Fixture |
172
182
| --- | --- | --- |
173
183
|`@typeOf("string") type t = option<string>`, `type t<'a> = 'a`, an abbreviation of `unit`, `list<_>`, a record or a variant |@typeOf on a type abbreviation must describe an abstract type, ... |`runtime_annotation_on_option.res`, `runtime_annotation_on_type_variable.res`|
184
+
|`@notUndefined type t = option<int>`|@notUndefined on a type abbreviation must describe an abstract type, ... |`not_undefined_on_option.res`, `tests/build_tests/not_undefined_attribute`|
174
185
|`@typeOf("number") type t = sym` with `@typeOf("symbol") type sym`, or `@typeOf("string") type t = int`| This annotation, @typeOf("number"), contradicts the abbreviated type, whose values are recognized by @typeOf("symbol"). |`runtime_annotation_contradicts_manifest.res`, `runtime_annotation_contradicts_builtin.res`|
175
-
|`@instanceOf("Date") type date = X.t` in a functor of `X`, or over `(type a)`|@instanceOf cannot describe the type of a functor parameter, ... |`instance_of_functor_parameter.res`, `type_of_locally_abstract_type.res`|
186
+
|`@instanceOf("Date") type date = X.t` in a functor of `X`, or over `(type a)`; `@notUndefined type t = X.t`|@instanceOf cannot describe the type of a functor parameter, ... |`instance_of_functor_parameter.res`, `type_of_locally_abstract_type.res`, `not_undefined_on_functor_parameter_type.res`|
176
187
|`@instanceOf("Dog") type dog = animal`, `= animal<int>`, or `= phantom<int>` with `type phantom<'a> = animal`| A subclass must be a private abbreviation ... |`instance_of_public_subclass.res`, `instance_of_public_subclass_instance.res`, `instance_of_public_subclass_through_alias.res`|
177
188
|`@instanceOf("Animal") type t = private dog`| Animal appears twice in this chain of abbreviations ... |`instance_of_class_twice_in_chain.res`|
178
-
|`@instanceOf`on a record or variant |@instanceOf can only be used on abstract types and type abbreviations ... |`runtime_annotation_on_record.res`|
179
-
| both annotations| A type can have either @instanceOf or @typeOf, not both. |`runtime_annotation_conflict.res`|
189
+
|any of the three on a record or variant |@instanceOf can only be used on abstract types and type abbreviations ... |`runtime_annotation_on_record.res`, `invalid_attribute_not_undefined.res`|
190
+
| both `@instanceOf` and `@typeOf`| A type can have either @instanceOf or @typeOf, not both. |`runtime_annotation_conflict.res`|
180
191
| one annotation twice | Duplicate @instanceOf annotation; a type can have only one. |`instance_of_duplicate.res`|
181
192
193
+
What an interface states about a type must hold for its implementation. A
194
+
class or `typeof`, stated through its own annotation or the type it
195
+
abbreviates, must be the one the implementation states, superclasses
196
+
included: "Their runtime representations differ". A builtin type states its
197
+
kind, so `@typeOf("number") type t` in an interface accepts `type t = int`
198
+
(`untagged_builtin_alias_test.res`). Under `@notUndefined`, the
199
+
implementation must be a type that cannot hold `undefined` either, or one
200
+
whose representation the compiler does not see, which has none to contradict
201
+
it: an abstract type, also through an abbreviation such as
202
+
`type t = Dom.element`, or a JavaScript object type; a functor parameter's or
203
+
a locally abstract type stands for another type and does not count.
204
+
`type t = option<int>` under `@notUndefined type t` is an error: "the second
205
+
declaration is @notUndefined, but the first declaration can hold undefined".
206
+
An interface that states nothing hides the shape. The same holds for functor
207
+
arguments, `with type` constraints and module type ascriptions. Within one
208
+
variant a class is identified by its path, so two types of the same class are
209
+
one case (rule 6); across a signature the two declarations must also state
210
+
the same superclasses.
211
+
182
212
The class path is a dot-separated list of identifiers whose first one is not
183
213
a JavaScript keyword. Paths that name no class to recognize values by are
@@ -424,7 +464,7 @@ as `Primitive_option.some(v)` otherwise. An untagged variant cannot hold
424
464
|`string`, `int`, `float`, `bool`, `bigint`, a function, a tuple, a record without `@unboxed`| no |
425
465
| an inline record with two or more fields | no |
426
466
| an `@unboxed` record, an inline record with one field | as the type of its field |
427
-
|an abstract type with `@notUndefined` (rule 13), a type declared with `@instanceOf` or `@typeOf` (rule 5), an `@unboxed` wrapper of either| no |
467
+
|a type declared with `@notUndefined`, `@instanceOf` or `@typeOf` (rule 5), an `@unboxed` wrapper of one| no |
428
468
| an abstract type without it, a type variable, `option<_>`| yes |
429
469
| a type whose declaration is not on the load path | yes |
430
470
@@ -457,51 +497,7 @@ hold `undefined`.
457
497
`tests/build_tests/option_transitive_dependency`.
458
498
- Shaped by #6222, #6233, #8719, #8954.
459
499
460
-
### 13. `@notUndefined`
461
-
462
-
`@notUndefined` on an abstract type declares that its values are never
463
-
`undefined`. It affects only rule 12: `option` of the type, and of an
464
-
untagged variant with a payload of the type, is unboxed. It does not make the
465
-
type a recognized payload kind (rule 4). `@instanceOf` and `@typeOf` (rule 5)
466
-
imply it.
467
-
468
-
On an abbreviation it follows the rule of rule 5: it can refine a type whose
469
-
representation the compiler does not see, such as an abstract type, or
470
-
restate that the abbreviated type cannot be `undefined`, as
471
-
`@notUndefined type count = int` does, which keeps the kind of `int`.
472
-
473
-
| Declaration | Error | Fixture |
474
-
| --- | --- | --- |
475
-
|`@notUndefined type t = {x: int}`, or on a variant |@notUndefined can only be used on abstract types and type abbreviations, ... |`invalid_attribute_not_undefined.res`|
476
-
|`@notUndefined type t = option<int>`|@notUndefined on a type abbreviation must describe an abstract type, ... |`not_undefined_on_option.res`, `tests/build_tests/not_undefined_attribute`|
477
-
|`@notUndefined type t = X.t` in a functor of `X`|@notUndefined cannot describe the type of a functor parameter, ... |`not_undefined_on_functor_parameter_type.res`|
478
-
479
-
When an interface declares a type `@notUndefined`, its implementation must be
480
-
a type that cannot hold `undefined` either, or a type whose representation
481
-
the compiler does not see, which has none to contradict it: an abstract type,
482
-
also through an abbreviation such as `type t = Dom.element`, or a JavaScript
483
-
object type. The type of a functor parameter or a locally abstract type
484
-
stands for another type and does not count as abstract. `type t = option<int>` under
485
-
`@notUndefined type t` is an error: "Their runtime representations
486
-
differ: the second declaration is @notUndefined, but the first declaration
487
-
can hold undefined". The same holds
488
-
for functor arguments and `with type` constraints.
0 commit comments