Skip to content

Commit d06f82d

Browse files
committed
Store the resolved runtime shape on type declarations
Why: Every reader re-derived what `@instanceOf`, `@typeOf` and `@notUndefined` state by walking a type's expansion chain in the reader's environment. Interface inclusion therefore evaluated an interface's `@instanceOf("Dog") type dog = private animal` against the implementation's annotated `animal`, while clients saw an unannotated `animal`: a superclass hidden that way was lost in clients. A declaration now carries `type_shape`: `Unstated`, `Never_undefined`, or `Recognized_as` a class with its superclasses or a `typeof` kind, resolved when the type is declared, in the environment it is declared in. The classifier, `option` unboxing and inclusion read it, walking only to the first declaration that states a shape; no attribute is parsed after declaration, and the deduplication of annotations repeated through module aliases is gone. Diagnostic added (a superclass hidden by its interface): Example: // Classes.res @instanceof("Animal") type animal @instanceof("Dog") type dog = private animal // Classes.resi type animal @instanceof("Dog") type dog = private animal After (diagnostic): Their runtime representations differ: the first declaration states instances of Dog, a subclass of Animal, the second declaration states instances of Dog. Review: Why a field rather than a normalized attribute? The facts are typed data, not text, and a field cannot be printed or forwarded by tools that handle attributes. 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> # Conflicts: # compiler/ext/config.ml
1 parent 70073c6 commit d06f82d

22 files changed

Lines changed: 194 additions & 90 deletions

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ is the number kind, and `@typeOf("symbol")` is a kind of its own, with no
103103
builtin payload type. `@notUndefined` on an abstract type leaves it unknown.
104104

105105
- Implementation: `variant_layout.ml:get_block_type_from_typ`,
106-
`get_block_type`, `declared_block_type`.
106+
`get_block_type`, `recognized_as`.
107107
- Tests: `spec_test` rules 4 (`kinds`, `tuples`, `aliasDict`, `instances`,
108108
`unknown`); fixtures `UntaggedConstructorMoreThanOneArg.res`,
109109
`UntaggedConstructorAttributeMoreThanOneArg.res`, `UntaggedNonUnary1.res`,
@@ -123,18 +123,24 @@ global class at the path `C`, such as `"Date"` or `"Intl.Locale"`.
123123
`"object"`. A type has at most one of the two, and only abstract types and
124124
type abbreviations, public or private, can have them.
125125

126-
The classifier reads the annotations of the payload type as written and of
127-
each type it abbreviates; the outermost one decides. The classes annotated
128-
further down a chain of abbreviations are the superclasses of the outermost
129-
one (rule 8):
126+
When a type is declared, its annotation is resolved into its *shape*, which
127+
is stored on the declaration: the class with its superclasses, or the
128+
`typeof` kind (`@notUndefined` gives the shape "never undefined", rule 13).
129+
The classes annotated further down a chain of abbreviations are the
130+
superclasses (rule 8):
130131

131132
```rescript
132133
@instanceOf("Animal") type animal
133134
@instanceOf("Dog") type dog = private animal
134135
```
135136

136-
A module alias repeats the declarations of the aliased module, so an
137-
annotation equal to the one just read is not counted again.
137+
The classifier and every other reader take the shape of the payload type as
138+
written, or of the first type it abbreviates that states one, so the
139+
outermost annotation decides. A shape is resolved in the environment the
140+
type is declared in, so an interface's shape is what its users see, even
141+
where the implementation knows more (rule 14). An annotation that repeats
142+
the abbreviated type's, as the declarations of a module alias do, adds no
143+
superclass.
138144

139145
An annotation on an abbreviation must agree with the abbreviated type. It can
140146
refine an abstract type that is not builtin and has no annotation, or repeat
@@ -187,8 +193,9 @@ The typed-array types are abbreviations of one type, `TypedArray.t<_>`, and
187193
are told apart only by their annotations, which are read on the name as
188194
written (Known bugs).
189195

190-
- Implementation: `variant_layout.ml:runtime_annotations`,
191-
`declared_block_type`, `check_manifest_annotation`;
196+
- Implementation: `types.mli:type_shape`,
197+
`variant_runtime.mli:value_shape`; `variant_layout.ml:declaration_shape`,
198+
`resolve_shapes`, `value_shape`, `check_manifest_annotation`;
192199
`ast_untagged_variants.ml:process_runtime_annotation`, `unsupported_class`,
193200
`check_runtime_annotation`;
194201
`variant_runtime.ml:Instance`; `js_exp_make.ml:class_constructor`.
@@ -419,7 +426,9 @@ declaration it is an error, "@notUndefined can only be used on abstract
419426
types". Interface inclusion does not compare it. `@instanceOf` and `@typeOf`
420427
(rule 5) imply it.
421428

422-
- Implementation: `typedecl.ml:transl_declaration`, `is_not_undefined_attr`;
429+
- Implementation: `typedecl.ml:transl_declaration`;
430+
`ast_untagged_variants.ml:has_not_undefined`;
431+
`variant_layout.ml:declaration_shape`;
423432
`typeopt.ml:type_cannot_contain_undefined`.
424433
- Tests: `spec_test` rule 13; fixture `invalid_attribute_not_undefined.res`;
425434
`tests/build_tests/not_undefined_attribute`.
@@ -452,6 +461,7 @@ arguments, `with type` constraints and module type ascriptions.
452461
`Iface_unboxed_variant_mismatch`, `Iface_tag_name_mismatch`,
453462
`Iface_variant_representation_mismatch`,
454463
`Cross_runtime_annotation_hidden_superclass`,
464+
`Cross_runtime_annotation_hidden_base`,
455465
`Cross_runtime_annotation_claimed_by_interface`,
456466
`Cross_runtime_annotation_functor_argument`.
457467
- Shaped by #6498, #6513, #6669, #7875.

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,9 @@ let describe_declared_block_type = function
316316
"values whose typeof is \"" ^ typeof_name block_type ^ "\""
317317
| None -> "no runtime class or typeof"
318318

319+
let has_not_undefined (attrs : Parsetree.attributes) =
320+
Ext_list.exists attrs (fun ({txt}, _) -> txt = "notUndefined")
321+
319322
(* The [@instanceOf] or [@typeOf] annotation in [attrs], validated. Only
320323
[Typedecl] passes [~loc]; later readers see attributes it accepted. *)
321324
let process_runtime_annotation ?(loc = Location.none)

‎compiler/ml/ctype.ml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -928,6 +928,7 @@ let new_declaration newtype manifest =
928928
type_attributes = [];
929929
type_immediate = false;
930930
type_representation = Boxed;
931+
type_shape = Unstated;
931932
type_inlined_types = [];
932933
}
933934

@@ -4312,6 +4313,7 @@ let nondep_type_decl env mid id is_covariant decl =
43124313
type_attributes = decl.type_attributes;
43134314
type_immediate = decl.type_immediate;
43144315
type_representation = decl.type_representation;
4316+
type_shape = decl.type_shape;
43154317
type_inlined_types = decl.type_inlined_types;
43164318
})
43174319

‎compiler/ml/datarepr.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ let constructor_args priv cd_args cd_res path rep =
9191
type_attributes = [];
9292
type_immediate = false;
9393
type_representation;
94+
type_shape = Unstated;
9495
type_inlined_types = [];
9596
}
9697
in

‎compiler/ml/includecore.ml‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -334,10 +334,19 @@ and compare_records env params1_ params2_ n_
334334
(* Values are recognized by the class or [typeof] the second declaration
335335
states, so the first must state the same. Stating nothing hides it. *)
336336
let compare_runtime_annotations env decl1 decl2 =
337-
match Variant_layout.declaration_block_type ~env decl2 with
338-
| None -> []
339-
| Some _ as declared2 ->
340-
let declared1 = Variant_layout.declaration_block_type ~env decl1 in
337+
(* The second declaration's shape was resolved in its own environment, so
338+
it is what users of the second declaration see *)
339+
match decl2.type_shape with
340+
| Unstated | Never_undefined -> []
341+
| Recognized_as block_type2 ->
342+
let declared2 = Some block_type2 in
343+
let declared1 =
344+
match decl1.type_shape with
345+
| Recognized_as block_type1 -> Some block_type1
346+
| Never_undefined -> None
347+
| Unstated ->
348+
Option.bind decl1.type_manifest (Variant_layout.recognized_as ~env)
349+
in
341350
if declared1 = declared2 then []
342351
else
343352
[

‎compiler/ml/predef.ml‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ let decl_abstr =
215215
type_attributes = [];
216216
type_immediate = false;
217217
type_representation = Boxed;
218+
type_shape = Unstated;
218219
type_inlined_types = [];
219220
}
220221

@@ -352,6 +353,7 @@ let common_initial_env add_type add_extension empty_env =
352353
};
353354
];
354355
type_representation = Types.Unboxed;
356+
type_shape = Unstated;
355357
}
356358
and decl_promise =
357359
let tvar = newgenvar () in

‎compiler/ml/printtyp.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1079,6 +1079,7 @@ let dummy =
10791079
type_attributes = [];
10801080
type_immediate = false;
10811081
type_representation = Boxed;
1082+
type_shape = Unstated;
10821083
type_inlined_types = [];
10831084
}
10841085

‎compiler/ml/subst.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,7 @@ let type_declaration s decl =
283283
type_attributes = attrs s decl.type_attributes;
284284
type_immediate = decl.type_immediate;
285285
type_representation = decl.type_representation;
286+
type_shape = decl.type_shape;
286287
type_inlined_types = decl.type_inlined_types;
287288
})
288289

‎compiler/ml/typecore.ml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3751,6 +3751,7 @@ and type_newtype ~loc ~env ~name (type_body : Env.t -> Typedtree.expression) =
37513751
type_attributes = [];
37523752
type_immediate = false;
37533753
type_representation = Boxed;
3754+
type_shape = Unstated;
37543755
type_inlined_types = [];
37553756
}
37563757
in

‎compiler/ml/typedecl.ml‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ let enter_type rec_flag env sdecl id =
9797
type_attributes = sdecl.ptype_attributes;
9898
type_immediate = false;
9999
type_representation = Boxed;
100+
type_shape = Unstated;
100101
type_inlined_types = [];
101102
}
102103
in
@@ -362,11 +363,6 @@ let make_constructor env type_path type_params sargs sret_type =
362363
widen z;
363364
(targs, Some tret_type, args, Some ret_type, params)
364365

365-
let is_not_undefined_attr (attr : attribute) =
366-
match attr with
367-
| {Location.txt = "notUndefined"; _}, _ -> true
368-
| _ -> false
369-
370366
(* Check that all the variables found in [ty] are in [univ].
371367
Because [ty] is the argument to an abstract type, the representation
372368
of that abstract type could be any subexpression of [ty], in particular
@@ -376,7 +372,7 @@ let is_not_undefined_attr (attr : attribute) =
376372
let transl_declaration ~type_record_as_object env sdecl id =
377373
(* Check for @notUndefined attribute *)
378374
let has_not_undefined =
379-
List.exists is_not_undefined_attr sdecl.ptype_attributes
375+
Ast_untagged_variants.has_not_undefined sdecl.ptype_attributes
380376
in
381377
(if has_not_undefined then
382378
match (sdecl.ptype_kind, sdecl.ptype_manifest) with
@@ -715,6 +711,7 @@ let transl_declaration ~type_record_as_object env sdecl id =
715711
type_attributes = sdecl.ptype_attributes;
716712
type_immediate = false;
717713
type_representation = representation;
714+
type_shape = Unstated;
718715
type_inlined_types = [];
719716
}
720717
in
@@ -1543,6 +1540,9 @@ let transl_type_decl env rec_flag sdecl_list =
15431540
current_slot := None;
15441541
(* Check for duplicates *)
15451542
check_duplicates sdecl_list;
1543+
(* What each declaration states about its values at run time, resolved in
1544+
the environment it is declared in *)
1545+
let decls = Variant_layout.resolve_shapes ~env decls in
15461546
(* Build the final env. *)
15471547
let newenv =
15481548
List.fold_right
@@ -2015,6 +2015,7 @@ let transl_with_constraint env id row_path orig_decl sdecl =
20152015
type_attributes = sdecl.ptype_attributes;
20162016
type_immediate = false;
20172017
type_representation;
2018+
type_shape = Unstated;
20182019
type_inlined_types = [];
20192020
}
20202021
in
@@ -2024,6 +2025,9 @@ let transl_with_constraint env id row_path orig_decl sdecl =
20242025
(match Ctype.closed_type_decl decl with
20252026
| None -> ()
20262027
| Some ty -> raise (Error (sdecl.ptype_loc, Unbound_type_var (ty, decl))));
2028+
let decl =
2029+
{decl with type_shape = Variant_layout.declaration_shape ~env decl}
2030+
in
20272031
Variant_layout.check_manifest_annotation ~loc:sdecl.ptype_loc ~env decl;
20282032
let decl = name_recursion sdecl id decl in
20292033
let type_variance =
@@ -2067,6 +2071,7 @@ let abstract_type_decl arity =
20672071
type_attributes = [];
20682072
type_immediate = false;
20692073
type_representation = Boxed;
2074+
type_shape = Unstated;
20702075
type_inlined_types = [];
20712076
}
20722077
in

0 commit comments

Comments
 (0)