Skip to content

Commit b43a9a5

Browse files
cknittclaude
andcommitted
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>
1 parent b391eff commit b43a9a5

3 files changed

Lines changed: 75 additions & 95 deletions

File tree

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 71 additions & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -127,20 +127,28 @@ builtin payload type. `@notUndefined` on an abstract type leaves it unknown.
127127
`untagged_builtin_alias_test.res`.
128128
- Shaped by #6121, #6208, #6219, #6279, #6368, #6394, #6550, #6670.
129129

130-
### 5. Declared instance types
130+
### 5. Declared runtime shapes
131131

132-
`@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).
138146

139147
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):
144152

145153
```rescript
146154
@instanceOf("Animal") type animal
@@ -151,14 +159,16 @@ The classifier and every other reader take the shape of the payload type as
151159
written, or of the first type it abbreviates that states one, so the
152160
outermost annotation decides. A shape is resolved in the environment the
153161
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
155163
the abbreviated type's, as the declarations of a module alias do, adds no
156164
superclass.
157165

158166
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
162172
recursive module being defined, and a locally abstract type `(type a)` are
163173
abstract only where they are declared: they stand for another type, so they
164174
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
171181
| Declaration | Error | Fixture |
172182
| --- | --- | --- |
173183
| `@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` |
174185
| `@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` |
176187
| `@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` |
177188
| `@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` |
180191
| one annotation twice | Duplicate @instanceOf annotation; a type can have only one. | `instance_of_duplicate.res` |
181192

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+
182212
The class path is a dot-separated list of identifiers whose first one is not
183213
a JavaScript keyword. Paths that name no class to recognize values by are
184214
rejected as well:
@@ -216,16 +246,26 @@ written (Known bugs).
216246
- Implementation: `types.mli:type_shape`,
217247
`variant_runtime.mli:value_shape`; `variant_layout.ml:declaration_shape`,
218248
`resolve_shapes`, `value_shape`, `check_manifest_annotation`;
219-
`ast_untagged_variants.ml:process_runtime_annotation`, `unsupported_class`,
220-
`check_runtime_annotation`;
249+
`typedecl.ml:check_manifest_annotations`; `typeopt.ml:undefinedness`;
250+
`ast_untagged_variants.ml:process_runtime_annotation`, `has_not_undefined`,
251+
`unsupported_class`, `check_runtime_annotation`;
252+
`includecore.ml:compare_runtime_annotations`;
221253
`variant_runtime.ml:Instance`; `js_exp_make.ml:class_constructor`.
222254
- Tests: `spec_test` rules 4 and 5;
223255
`tests/tests/src/untagged_runtime_annotations_test.res`,
224256
`untagged_runtime_annotations_shadow_test.res`,
225-
`untagged_file_blob_test.res`; the fixtures above,
226-
`UntaggedAtMostOneInstance.res`, `UntaggedTupleAndArray.res`.
227-
- Shaped by #6296, #6383, #6396, #7788, #8009, #8950, #8948, #8949.
228-
Related: #7147.
257+
`untagged_file_blob_test.res`, `not_undefined_interface_test.res`,
258+
`not_undefined_abbreviation_test.res`; the fixtures above,
259+
`UntaggedAtMostOneInstance.res`, `UntaggedTupleAndArray.res`,
260+
`not_undefined_functor_argument.res`, `not_undefined_functor_parameter.res`;
261+
`tests/build_tests/super_errors_multi/fixtures/`
262+
`Cross_runtime_annotation_hidden_superclass`,
263+
`Cross_runtime_annotation_hidden_base`,
264+
`Cross_runtime_annotation_claimed_by_interface`,
265+
`Cross_runtime_annotation_functor_argument`,
266+
`Cross_not_undefined_option_implementation`.
267+
- Shaped by #6296, #6383, #6396, #7458, #7464, #7788, #8009, #8950, #8948,
268+
#8949, #8964. Related: #7147.
229269

230270
### 6. Well-formedness
231271

@@ -424,7 +464,7 @@ as `Primitive_option.some(v)` otherwise. An untagged variant cannot hold
424464
| `string`, `int`, `float`, `bool`, `bigint`, a function, a tuple, a record without `@unboxed` | no |
425465
| an inline record with two or more fields | no |
426466
| 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 |
428468
| an abstract type without it, a type variable, `option<_>` | yes |
429469
| a type whose declaration is not on the load path | yes |
430470

@@ -457,51 +497,7 @@ hold `undefined`.
457497
`tests/build_tests/option_transitive_dependency`.
458498
- Shaped by #6222, #6233, #8719, #8954.
459499

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.
489-
490-
- Implementation: `typedecl.ml:transl_declaration`,
491-
`check_not_undefined_manifest`; `typeopt.ml:undefinedness`;
492-
`ast_untagged_variants.ml:has_not_undefined`;
493-
`variant_layout.ml:declaration_shape`;
494-
`typeopt.ml:type_cannot_contain_undefined`;
495-
`includecore.ml:compare_runtime_annotations`.
496-
- Tests: `spec_test` rule 13; fixtures `invalid_attribute_not_undefined.res`,
497-
`not_undefined_functor_argument.res`, `not_undefined_functor_parameter.res`;
498-
`tests/tests/src/not_undefined_interface_test.res`,
499-
`not_undefined_abbreviation_test.res`;
500-
`tests/build_tests/super_errors_multi/fixtures/Cross_not_undefined_option_implementation`;
501-
`tests/build_tests/not_undefined_attribute`.
502-
- Shaped by #7458, #7464, #8964.
503-
504-
### 14. `@tag` and interfaces
500+
### 13. `@tag` and interfaces
505501

506502
In a tagged variant, `@tag("f")` names the field that holds the tag of a case
507503
with a payload (`TAG` by default), and `@as` on such a case sets the tag's
@@ -518,29 +514,14 @@ not checked, so it agrees with any payload of the implementation
518514
payload does not change its representation (rule 2) and is not compared.
519515
Type declarations in messages print the `@as` of nullary and tagged cases.
520516

521-
A type for which an interface states a class or `typeof` (rule 5), through
522-
its own annotation or the type it abbreviates, must state the same one in the
523-
implementation, superclasses included: "Their runtime representations
524-
differ". A builtin type states its kind, so `@typeOf("number") type t` in an
525-
interface accepts `type t = int` (`untagged_builtin_alias_test.res`). An
526-
interface that states none hides it. The same holds for functor
527-
arguments, `with type` constraints and module type ascriptions. Within one
528-
variant a class is identified by its path, so two types of the same class are
529-
one case (rule 6); across a signature the two declarations must also state
530-
the same superclasses.
531-
532517
- Implementation: `variant_layout.ml:layout_from_type_variant`,
533518
`ast_untagged_variants.ml:process_tag_name`, `check_tag_field_conflicts`;
534519
`includecore.ml:compare_variants`, `type_declarations`.
535-
- Tests: `spec_test` rule 14; fixtures `UntaggedTagFieldNameConflict.res`,
520+
- Tests: `spec_test` rule 13; fixtures `UntaggedTagFieldNameConflict.res`,
536521
`variant_tag_overlaps_with_field.res`, `UntaggedInvalidVariantTagAnnotation.res`,
537522
`UntaggedImplIntf.res`; `tests/build_tests/super_errors_multi/fixtures/`
538523
`Iface_unboxed_variant_mismatch`, `Iface_tag_name_mismatch`,
539-
`Iface_variant_representation_mismatch`,
540-
`Cross_runtime_annotation_hidden_superclass`,
541-
`Cross_runtime_annotation_hidden_base`,
542-
`Cross_runtime_annotation_claimed_by_interface`,
543-
`Cross_runtime_annotation_functor_argument`; `variant_payload_tag_mismatch.res`.
524+
`Iface_variant_representation_mismatch`; `variant_payload_tag_mismatch.res`.
544525
- Shaped by #6498, #6513, #6669, #7875, #8956.
545526

546527
## Known bugs
@@ -569,15 +550,14 @@ had to answer, and the answers of rule 5:
569550
down its chain of private abbreviations. A superclass case excludes its subclass cases in
570551
any order, and an upcast value lands in the subclass case (rule 8). The
571552
relation must be visible where the variant is declared: an interface that
572-
hides it is rejected (rule 14).
553+
hides it is rejected (rule 5).
573554
3. **Overlaps across kinds.** `Object`, `Function`, `Array` and the wrappers
574555
of primitives are rejected as classes. A declared kind counts as the
575556
builtin kind (rule 6). Unrelated classes are tested in declaration order.
576557
4. **The unchecked branch** stays the contract (rule 10).
577558
5. **Realms.** Recognition is local to a realm (rule 10).
578-
6. **Interfaces and functors** compare declared classes and kinds, and the
579-
payload kinds of untagged cases (rule 14), and `@notUndefined` against
580-
the implementation (rule 13).
559+
6. **Interfaces and functors** compare declared shapes (rule 5) and the
560+
payload kinds of untagged cases (rule 13).
581561
7. **Implied `@notUndefined`.** A declared class or kind implies it, and
582562
rule 5 rejects annotations on abbreviations of types that can hold
583563
`undefined`.

‎tests/tests/src/untagged_variants_spec_test.mjs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -354,11 +354,11 @@ Mocha.describe("Untagged_variants_spec_test", () => {
354354
Test_utils.eq("File \"untagged_variants_spec_test.res\", line 336, characters 7-14", someWithUnboxedOption(undefined) === undefined, false);
355355
Test_utils.eq("File \"untagged_variants_spec_test.res\", line 337, characters 7-14", someWithUnboxedString(undefined) === undefined, true);
356356
});
357-
Mocha.test("rule 13: @notUndefined unboxes option of a variant holding the type", () => {
357+
Mocha.test("rule 5: @notUndefined unboxes option of a variant holding the type", () => {
358358
Test_utils.eq("File \"untagged_variants_spec_test.res\", line 341, characters 7-14", someWithDefined(undefined) === undefined, true);
359359
Test_utils.eq("File \"untagged_variants_spec_test.res\", line 342, characters 7-14", someWithDefined(5), 5);
360360
});
361-
Mocha.test("rule 14: @tag and @as on a tagged variant", () => {
361+
Mocha.test("rule 13: @tag and @as on a tagged variant", () => {
362362
Test_utils.eq("File \"untagged_variants_spec_test.res\", line 346, characters 7-14", id({
363363
kind: "TA",
364364
x: 1

‎tests/tests/src/untagged_variants_spec_test.res‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -337,12 +337,12 @@ describe(__MODULE__, () => {
337337
eq(__LOC__, someWithUnboxedString(UString(raw(Nullable.undefined))) == None, true)
338338
})
339339

340-
test("rule 13: @notUndefined unboxes option of a variant holding the type", () => {
340+
test("rule 5: @notUndefined unboxes option of a variant holding the type", () => {
341341
eq(__LOC__, someWithDefined(Defined(raw(Nullable.undefined))) == None, true)
342342
eq(__LOC__, raw(someWithDefined(Defined(raw(5)))), 5)
343343
})
344344

345-
test("rule 14: @tag and @as on a tagged variant", () => {
345+
test("rule 13: @tag and @as on a tagged variant", () => {
346346
eq(__LOC__, raw(id(TA({x: 1}))), {"kind": "TA", "x": 1})
347347
eq(__LOC__, raw(id(TB({y: 1}))), {"kind": "bee", "y": 1})
348348
eq(__LOC__, raw(id(TC)), "TC")

0 commit comments

Comments
 (0)