Skip to content

Commit 0ddbc12

Browse files
cknittclaude
andcommitted
Require a private abbreviation for every subclass
Why: A public abbreviation could declare a subclass when its arguments were not all type variables, meant for phantom type hierarchies. The test was syntactic, so `@instanceOf("Dog") type dog = animal<int>` passed, and every `animal<int>` was then a dog without a coercion. A subclass is always a private abbreviation, phantom hierarchies included; upcasts use `:>`. Diagnostic added (public subclass of an instantiated class): Example: @instanceof("Animal") type animal<'a> @instanceof("Dog") type dog = animal<int> Before (diagnostic): none 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. 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 5e9bfb6 commit 0ddbc12

6 files changed

Lines changed: 25 additions & 22 deletions

File tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Require `= private` for every subclass declared with `@instanceOf`, also in phantom type hierarchies, and fold `null` into an untagged variant's object case as it is matched at run time.

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -148,19 +148,18 @@ the annotation of the abbreviated type. The type of a functor parameter, of a
148148
recursive module being defined, and a locally abstract type `(type a)` are
149149
abstract only where they are declared: they stand for another type, so they
150150
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
154-
`@instanceOf("Element") type elementLike<'a> = nodeLike<_element<'a>>`. A
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`.
151+
class annotated there, or of a `@typeOf("object")` type, also in a phantom
152+
type hierarchy (`type dogLike<'a> = private animalLike<_dog<'a>>`). A public
153+
`type dog = animal` would make every animal a dog without a coercion, and so
154+
would `type dog = animal<int>` or `type dog = phantom<int>` with
155+
`type phantom<'a> = animal`.
157156

158157
| Declaration | Error | Fixture |
159158
| --- | --- | --- |
160159
| `@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` |
161160
| `@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` |
162161
| `@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` |
162+
| `@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` |
164163
| `@instanceOf("Animal") type t = private dog` | Animal appears twice in this chain of abbreviations ... | `instance_of_class_twice_in_chain.res` |
165164
| `@instanceOf` on a record or variant | @instanceOf can only be used on abstract types and type abbreviations ... | `runtime_annotation_on_record.res` |
166165
| both annotations | A type can have either @instanceOf or @typeOf, not both. | `runtime_annotation_conflict.res` |
@@ -530,8 +529,7 @@ had to answer, and the answers of rule 5:
530529
1. **Unchecked claims.** An annotation is trusted, as the type of an external
531530
is, but checked against the type it abbreviates (rule 5).
532531
2. **Subclasses.** A class's superclasses are the classes annotated further
533-
down its chain of private abbreviations, or of strict instances in a
534-
phantom type hierarchy. A superclass case excludes its subclass cases in
532+
down its chain of private abbreviations. A superclass case excludes its subclass cases in
535533
any order, and an upcast value lands in the subclass case (rule 8). The
536534
relation must be visible where the variant is declared: an interface that
537535
hides it is rejected (rule 14).

‎compiler/ml/variant_layout.ml‎

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -140,23 +140,16 @@ let check_manifest_annotation ~loc ~env (decl : Types.type_declaration) =
140140
when name = inner ->
141141
()
142142
| Instance_of name, Some ((InstanceType (Class _) | ObjectType) as inner) ->
143-
(* A public abbreviation is the same type as the superclass, unless it
144-
is a strict instance of it, as in the phantom type hierarchy
145-
[type element<'a> = node<_element<'a>>]. The arguments are read after
146-
expansion, where the superclass type is reached. *)
147-
let same_type =
148-
match (Ctype.expand_head_opt env manifest).desc with
149-
| Tconstr (_, args, _) -> List.for_all Btype.is_Tvar args
150-
| _ -> true
151-
in
143+
(* A public abbreviation would make every value of the superclass type
144+
a value of the subclass, without a coercion *)
152145
let superclass, ancestors =
153146
match inner with
154147
| InstanceType (Class {name = ancestor; ancestors}) ->
155148
(ancestor, ancestor :: ancestors)
156149
(* Every class instance is an object *)
157150
| _ -> ("Object", [])
158151
in
159-
if decl.type_private = Public && same_type then
152+
if decl.type_private = Public then
160153
error (SubclassOfPublicAbbreviation (name, superclass))
161154
else if List.mem name ancestors then error (ClassTwiceInChain name)
162155
| (Instance_of _ | Type_of _), Some inner ->
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2+
We've found a bug for you!
3+
/.../fixtures/instance_of_public_subclass_instance.res:2:20-41
4+
5+
1 │ @instanceOf("Animal") type animal<'a>
6+
2 │ @instanceOf("Dog") type dog = animal<int>
7+
3 │
8+
9+
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: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@instanceOf("Animal") type animal<'a>
2+
@instanceOf("Dog") type dog = animal<int>

‎tests/tests/src/untagged_runtime_annotations_test.res‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,12 @@ let localeKind = locale =>
9494
| Tag(tag) => tag
9595
}
9696

97-
// A phantom type hierarchy declares subclasses with public abbreviations that
98-
// are strict instances of their superclass type
97+
// A phantom type hierarchy declares its subclasses as private abbreviations
98+
// too
9999
type nodeLike<'a>
100100
@instanceOf("UntaggedAnimal") type animalLike<'a> = nodeLike<'a>
101101
type _dog<'a>
102-
@instanceOf("UntaggedDog") type dogLike<'a> = animalLike<_dog<'a>>
102+
@instanceOf("UntaggedDog") type dogLike<'a> = private animalLike<_dog<'a>>
103103
type _base
104104
@new external makePhantomAnimal: unit => animalLike<_base> = "UntaggedAnimal"
105105
@new external makePhantomDog: unit => dogLike<_base> = "UntaggedDog"

0 commit comments

Comments
 (0)