Skip to content

Commit 667b281

Browse files
cknittclaude
andcommitted
Close the gaps the re-review of runtime annotations found
Why: An annotation could refine a functor parameter's or a locally abstract type, which stands for any type: `@instanceOf("Date") type d = X.t` in a functor applied to `option<int>` made `Some(None)` collapse to `None`. An abbreviation through an alias passed as a phantom subclass, an interface hiding a payload's class was rejected although master accepts it, and two `@instanceOf` attributes were reported as a conflict between the two kinds. Annotations on the type of a functor parameter, of a recursive module being defined, or on a locally abstract type are rejected. The phantom-hierarchy exception reads the abbreviation's arguments after expansion. A class can be declared over a `@typeOf("object")` type. An interface case with an unknown payload agrees with any payload of the implementation, since it is the only payload case and is never checked. Diagnostic added (a type that stands for another one): Example: module Make = (X: {type t}) => { @instanceof("Date") type date = X.t } After (diagnostic): @instanceof cannot describe the type of a functor parameter, of a recursive module being defined, or a locally abstract type: such a type stands for another one, whose values can be anything. Diagnostic added (public subclass through an alias): Example: @instanceof("Animal") type animal type phantom<'a> = animal @instanceof("Dog") type dog = phantom<int> After (diagnostic): A subclass must be a private abbreviation: write `= private ...` so that instances of Animal cannot be used as instances of Dog without a coercion. Diagnostic removed (an interface hiding a payload's class): Example: // A.resi type t @unboxed type v = | @as("n") N | P(t) // A.res type t = Date.t @unboxed type v = | @as("n") N | P(t) Before (diagnostic): The internal representations for case P are not equal 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 d06f82d commit 667b281

20 files changed

Lines changed: 218 additions & 42 deletions

‎analysis/src/completion_decorators.ml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ You will need this decorator whenever you want to use a JSX component in ReScrip
303303
[
304304
{|The `@instanceOf` decorator states that the values of an abstract type are instances of a global JavaScript class. Such a type can be the payload of an untagged variant case, which is then recognized with `instanceof`, and is never `undefined`, like with `@notUndefined`.
305305

306-
A private abbreviation of another annotated type declares a subclass, so that the subclass case is checked first.
306+
A private abbreviation of another annotated type declares a subclass. A case of the superclass then excludes the instances of a subclass that is a case too, whatever the order of the cases.
307307

308308
Example usage:
309309
```rescript

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,20 +144,27 @@ superclass.
144144

145145
An annotation on an abbreviation must agree with the abbreviated type. It can
146146
refine an abstract type that is not builtin and has no annotation, or repeat
147-
the annotation of the abbreviated type. Only a private abbreviation can
148-
declare a subclass of the class annotated there, or a public one that is a
149-
strict instance of the superclass type, as in the phantom type hierarchy
147+
the annotation of the abbreviated type. The type of a functor parameter, of a
148+
recursive module being defined, and a locally abstract type `(type a)` are
149+
abstract only where they are declared: they stand for another type, so they
150+
cannot be refined. Only a private abbreviation can declare a subclass of the
151+
class annotated there, or of a `@typeOf("object")` type, or a public one that
152+
is a strict instance of the superclass type after expansion, as in the
153+
phantom type hierarchy
150154
`@instanceOf("Element") type elementLike<'a> = nodeLike<_element<'a>>`. A
151-
public `type dog = animal` would make every animal a dog without a coercion.
155+
public `type dog = animal` would make every animal a dog without a coercion,
156+
and so would `type dog = phantom<int>` with `type phantom<'a> = animal`.
152157

153158
| Declaration | Error | Fixture |
154159
| --- | --- | --- |
155160
| `@typeOf("string") type t = option<string>`, `type t<'a> = 'a`, an abbreviation of `unit`, a builtin type, 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` |
156161
| `@typeOf("number") type t = sym` with `@typeOf("symbol") type sym` | This annotation, @typeOf("number"), contradicts the abbreviated type's annotation, @typeOf("symbol"). | `runtime_annotation_contradicts_manifest.res` |
157-
| `@instanceOf("Dog") type dog = animal` | A subclass must be a private abbreviation ... | `instance_of_public_subclass.res` |
162+
| `@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` |
163+
| `@instanceOf("Dog") type dog = animal`, or `= phantom<int>` with `type phantom<'a> = animal` | A subclass must be a private abbreviation ... | `instance_of_public_subclass.res`, `instance_of_public_subclass_through_alias.res` |
158164
| `@instanceOf("Animal") type t = private dog` | Animal appears twice in this chain of abbreviations ... | `instance_of_class_twice_in_chain.res` |
159165
| `@instanceOf` on a record or variant | @instanceOf can only be used on abstract types and type abbreviations ... | `runtime_annotation_on_record.res` |
160166
| both annotations | A type can have either @instanceOf or @typeOf, not both. | `runtime_annotation_conflict.res` |
167+
| one annotation twice | Duplicate @instanceOf annotation; a type can have only one. | `instance_of_duplicate.res` |
161168

162169
The class path is a dot-separated list of identifiers whose first one is not
163170
a JavaScript keyword. Paths that name no class to recognize values by are
@@ -443,8 +450,10 @@ error.
443450

444451
An interface and its implementation must agree on `@unboxed`, on `@tag`, on
445452
the literal of every nullary case, and on the payload kind of every untagged
446-
case. They are not compared on the `@as` of cases with a payload (see Known
447-
bugs).
453+
case whose kind the interface knows. An unknown payload in the interface is
454+
its only payload case and is not checked, so it agrees with any payload of
455+
the implementation (`untagged_hidden_payload_test.res`). They are not
456+
compared on the `@as` of cases with a payload (see Known bugs).
448457

449458
A type for which an interface states a class or `typeof` (rule 5), through
450459
its own annotation or the type it abbreviates, must state the same one in the

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ type error =
3434
| UnsupportedInstanceOf of string * unsupported_class
3535
| InvalidTypeOf of string
3636
| ConflictingRuntimeAnnotations
37+
| DuplicateRuntimeAnnotation of string
38+
| RuntimeAnnotationOnTypeParameter of string
3739
| RuntimeAnnotationOnRepresentedType of string
3840
| RuntimeAnnotationOnUnrelatedType of string
3941
| RuntimeAnnotationContradictsManifest of string * string
@@ -124,6 +126,14 @@ let report_error ppf =
124126
"@typeOf expects one of \"string\", \"number\", \"bigint\", \"boolean\", \
125127
\"symbol\", \"function\" or \"object\"%s."
126128
(if name = "" then "" else ", not \"" ^ name ^ "\"")
129+
| DuplicateRuntimeAnnotation attribute ->
130+
fprintf ppf "Duplicate @%s annotation; a type can have only one." attribute
131+
| RuntimeAnnotationOnTypeParameter attribute ->
132+
fprintf ppf
133+
"@%s cannot describe the type of a functor parameter, of a recursive \
134+
module being defined, or a locally abstract type: such a type stands \
135+
for another one, whose values can be anything."
136+
attribute
127137
| ConflictingRuntimeAnnotations ->
128138
fprintf ppf "A type can have either @instanceOf or @typeOf, not both."
129139
| RuntimeAnnotationOnRepresentedType attribute ->
@@ -345,7 +355,13 @@ let process_runtime_annotation ?(loc = Location.none)
345355
match Ext_list.filter_map attrs annotation_of with
346356
| [] -> None
347357
| [annotation] -> Some annotation
348-
| _ :: _ :: _ -> raise (Error (loc, ConflictingRuntimeAnnotations))
358+
| first :: second :: _ ->
359+
raise
360+
(Error
361+
( loc,
362+
if runtime_attribute_name first = runtime_attribute_name second then
363+
DuplicateRuntimeAnnotation (runtime_attribute_name first)
364+
else ConflictingRuntimeAnnotations ))
349365

350366
(* Checks the runtime annotations of a declaration whose kind is
351367
[is_abstract_or_abbreviation]. *)

‎compiler/ml/includecore.ml‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -281,9 +281,11 @@ and compare_variants ~loc env params1 params2 ~layout1 ~layout2 n
281281
( Variant_runtime.constructor_at layout1 (n - 1),
282282
Variant_runtime.constructor_at layout2 (n - 1) )
283283
with
284+
(* An unknown payload in the second declaration is the only
285+
payload case and has no check, so any first one agrees *)
284286
| ( Block (Untagged {block_type = block_type1}),
285287
Block (Untagged {block_type = block_type2}) )
286-
when block_type1 <> block_type2 ->
288+
when block_type1 <> block_type2 && block_type2 <> UnknownType ->
287289
[Variant_representation cd1.cd_id]
288290
| _ -> r)
289291
in

‎compiler/ml/variant_layout.ml‎

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,35 +101,49 @@ let check_manifest_annotation ~loc ~env (decl : Types.type_declaration) =
101101
let error err = raise (Error (loc, err)) in
102102
match (outer, recognized_as ~env manifest) with
103103
| _, None -> (
104-
let is_abstract path =
104+
(* A functor parameter's type and a locally abstract type are abstract
105+
only here: they stand for whatever type they are instantiated with *)
106+
let refinable path =
105107
match Env.find_type path env with
106-
| {type_kind = Type_abstract; type_manifest = None} -> true
108+
| {type_kind = Type_abstract; type_manifest = None; type_newtype_level}
109+
->
110+
if type_newtype_level <> None || Env.is_functor_arg path env then
111+
error
112+
(RuntimeAnnotationOnTypeParameter (runtime_attribute_name outer))
113+
else true
107114
| _ -> false
108115
| exception Not_found -> false
109116
in
110117
match (Ctype.expand_head_opt env manifest).desc with
111118
| Tconstr (path, _, _)
112119
when Predef.type_is_builtin_path_but_option path = Predef.NA
113-
&& is_abstract path ->
120+
&& refinable path ->
114121
()
115122
| _ ->
116123
error (RuntimeAnnotationOnUnrelatedType (runtime_attribute_name outer)))
117124
| Type_of block_type, Some inner when block_type = inner -> ()
118125
| Instance_of name, Some (InstanceType (Class {name = inner}))
119126
when name = inner ->
120127
()
121-
| Instance_of name, Some (InstanceType (Class {name = ancestor; ancestors}))
122-
->
128+
| Instance_of name, Some ((InstanceType (Class _) | ObjectType) as inner) ->
123129
(* A public abbreviation is the same type as the superclass, unless it
124130
is a strict instance of it, as in the phantom type hierarchy
125-
[type element<'a> = node<_element<'a>>] *)
131+
[type element<'a> = node<_element<'a>>]. The arguments are read after
132+
expansion, where the superclass type is reached. *)
126133
let same_type =
127-
match (Btype.repr manifest).desc with
134+
match (Ctype.expand_head_opt env manifest).desc with
128135
| Tconstr (_, args, _) -> List.for_all Btype.is_Tvar args
129136
| _ -> true
130137
in
138+
let superclass, ancestors =
139+
match inner with
140+
| InstanceType (Class {name = ancestor; ancestors}) ->
141+
(ancestor, ancestor :: ancestors)
142+
(* Every class instance is an object *)
143+
| _ -> ("Object", [])
144+
in
131145
if decl.type_private = Public && same_type then
132-
error (SubclassOfPublicAbbreviation (name, ancestor))
146+
error (SubclassOfPublicAbbreviation (name, superclass))
133147
else if List.mem name ancestors then error (ClassTwiceInChain name)
134148
| (Instance_of _ | Type_of _), Some inner ->
135149
error

‎tests/analysis_tests/tests/src/expected/CompletionAttributes.res.txt‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ Resolved opens 1 Stdlib
325325
"detail": "",
326326
"documentation": {
327327
"kind": "markdown",
328-
"value": "The `@instanceOf` decorator states that the values of an abstract type are instances of a global JavaScript class. Such a type can be the payload of an untagged variant case, which is then recognized with `instanceof`, and is never `undefined`, like with `@notUndefined`.\n\nA private abbreviation of another annotated type declares a subclass, so that the subclass case is checked first.\n\nExample usage:\n```rescript\n@instanceOf(\"Blob\")\ntype blob\n\n@instanceOf(\"File\")\ntype file = private blob\n```"
328+
"value": "The `@instanceOf` decorator states that the values of an abstract type are instances of a global JavaScript class. Such a type can be the payload of an untagged variant case, which is then recognized with `instanceof`, and is never `undefined`, like with `@notUndefined`.\n\nA private abbreviation of another annotated type declares a subclass. A case of the superclass then excludes the instances of a subclass that is a case too, whatever the order of the cases.\n\nExample usage:\n```rescript\n@instanceOf(\"Blob\")\ntype blob\n\n@instanceOf(\"File\")\ntype file = private blob\n```"
329329
},
330330
"insertText": "instanceOf(\"$0\")",
331331
"insertTextFormat": 2,
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/instance_of_duplicate.res:1:43-48
4+
5+
1 │ @instanceOf("Date") @instanceOf("RegExp") type t
6+
2 │
7+
8+
Duplicate @instanceOf annotation; a type can have only one.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/instance_of_functor_parameter.res:6:23-37
4+
5+
4 │ },
6+
5 │ ) => {
7+
6 │ @instanceOf("Date") type date = X.t
8+
7 │ }
9+
8 │
10+
11+
@instanceOf cannot describe the type of a functor parameter, of a recursive module being defined, or a locally abstract type: such a type stands for another one, whose values can be anything.
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/instance_of_public_subclass_through_alias.res:3:20-42
4+
5+
1 │ @instanceOf("Animal") type animal
6+
2 │ type phantom<'a> = animal
7+
3 │ @instanceOf("Dog") type dog = phantom<int>
8+
4 │
9+
10+
A subclass must be a private abbreviation: write `= private ...` so that instances of Animal cannot be used as instances of Dog without a coercion.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/type_of_locally_abstract_type.res:3:23-32
4+
5+
1 │ let f = (type a, x: a) => {
6+
2 │ module M = {
7+
3 │ @typeOf("object") type o = a
8+
4 │ }
9+
5 │ x
10+
11+
@typeOf cannot describe the type of a functor parameter, of a recursive module being defined, or a locally abstract type: such a type stands for another one, whose values can be anything.

0 commit comments

Comments
 (0)