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 0ddbc12
Browse filesBrowse the repository at this point in the historyBrowse files
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>
- 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.
Copy file name to clipboardExpand all lines: compiler/ml/UntaggedVariants.md
+7-9Lines changed: 7 additions & 9 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -148,19 +148,18 @@ the annotation of the abbreviated type. The type of a functor parameter, of a
148
148
recursive module being defined, and a locally abstract type `(type a)` are
149
149
abstract only where they are declared: they stand for another type, so they
150
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
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`.
157
156
158
157
| Declaration | Error | Fixture |
159
158
| --- | --- | --- |
160
159
|`@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`|
161
160
|`@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`|
162
161
|`@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`|
164
163
|`@instanceOf("Animal") type t = private dog`| Animal appears twice in this chain of abbreviations ... |`instance_of_class_twice_in_chain.res`|
165
164
|`@instanceOf` on a record or variant |@instanceOf can only be used on abstract types and type abbreviations ... |`runtime_annotation_on_record.res`|
166
165
| 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:
530
529
1.**Unchecked claims.** An annotation is trusted, as the type of an external
531
530
is, but checked against the type it abbreviates (rule 5).
532
531
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
535
533
any order, and an upcast value lands in the subclass case (rule 8). The
536
534
relation must be visible where the variant is declared: an interface that
0 commit comments