Skip to content

Commit aff4439

Browse files
cknittclaude
andauthored
Make typed arrays distinct types (#9050)
* Make typed arrays private abbreviations of TypedArray.t Why: Every typed array type was a public abbreviation of `TypedArray.t<_>`, so the type checker took `Uint8Array.t` for `Int8Array.t`, while an untagged variant recognized them by their classes: `I8(uint8Array)` type-checked and then matched the `U8` case (#8955). Each typed array type is `private TypedArray.t<_>`: a distinct type that upcasts to `TypedArray.t<_>` with `:>`. The `TypedArray.Impl` functor gives each typed array module the functions of `TypedArray` for its own type, with their documentation, so `Int8Array.get` works on an `Int8Array.t` and `Int8Array.slice` returns one. The functor holds only externals, so including it emits no code; `Stdlib_TypedArray` gains an empty `Impl` function. Typing rules changed (typed arrays): Example: @unboxed type ta = I8(Int8Array.t) | U8(Uint8Array.t) let x = I8(Uint8Array.fromArray([1])) Before (rule): `Int8Array.t` and `Uint8Array.t` are both `TypedArray.t<int>`, so `I8(uint8Array)` type-checks and is matched as `U8` at run time After (rule): they are distinct types; `I8(uint8Array)` is a type error Diagnostic added (one typed array for another): Example: let x = I8(Uint8Array.fromArray([1])) Before (diagnostic): none After (diagnostic): This has type: Uint8Array.t But it's expected to have type: Int8Array.t 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 a private abbreviation for a class on an abstract type Why: A class declared with @instanceof on a public abbreviation of an abstract type made every value of that type an instance, and two such abbreviations could declare the same values instances of two classes while the type checker treated them as one type, as the typed arrays did. `Typedecl.check_manifest_annotation` rejects @instanceof on a public abbreviation of a type the compiler does not see; a subclass already had to be private. Typing rules changed (a class on an abbreviation): Example: type node @instanceof("Element") type element = node Before (rule): a class on a public abbreviation of an abstract type is accepted After (rule): the abbreviation must be private, `@instanceOf("Element") type element = private node` Diagnostic added (a class on a public abbreviation): Example: type node @instanceof("Element") type element = node Before (diagnostic): none After (diagnostic): A class must be declared on an abstract type or a private abbreviation: write `= private ...`, so that values of the abbreviated type are not instances of Element without a coercion, and another abbreviation cannot declare them instances of another class. 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> * Migrate the deprecated TypedArray functions on a TypedArray.t Why: The migration test applied the deprecated `TypedArray.*ToEnd` functions to a `Uint8Array.t`, which is now a type of its own; those functions exist only on `TypedArray`. The test's typed array is upcast to `TypedArray.t<int>`. 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> --------- Signed-off-by: Christoph Knittel <ck@cca.io> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent 27b5460 commit aff4439

30 files changed

Lines changed: 3119 additions & 3626 deletions
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
- Typed arrays are distinct types: `Int8Array.t` and the others are private abbreviations of `TypedArray.t<_>`, so an untagged variant can no longer take one typed array for another. Use each module's own functions, such as `Int8Array.get`, or upcast with `(view :> TypedArray.t<int>)` to call the `TypedArray` functions.
2+
- `@instanceOf` that introduces a class on an abbreviation of an abstract type requires a private abbreviation: write `@instanceOf("Element") type element = private node`.

‎compiler/ml/UntaggedVariants.md‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -181,13 +181,24 @@ type hierarchy (`type dogLike<'a> = private animalLike<_dog<'a>>`). A public
181181
would `type dog = animal<int>` or `type dog = phantom<int>` with
182182
`type phantom<'a> = animal`.
183183

184+
A class introduced on an abbreviation of a type the compiler does not see is
185+
private too: `@instanceOf("Element") type element = private node`. A public
186+
abbreviation would make every `node` an `element`, and a second one could
187+
declare the same values instances of another class while the type checker
188+
treats both as one type. The stdlib's typed arrays are such private
189+
abbreviations of `TypedArray.t<_>`, so `Int8Array.t` and `Uint8Array.t` are
190+
different types, upcast to `TypedArray.t<int>` with `:>`. Each typed array
191+
module has the functions of `TypedArray` for its own type, through the
192+
`TypedArray.Impl` functor.
193+
184194
| Declaration | Error | Fixture |
185195
| --- | --- | --- |
186196
| `@typeOf("string") type t = option<string>`, `type t<'a> = 'a`, an abbreviation of `unit`, `list<_>` or a variant | @typeOf on a type abbreviation must describe an abstract type, ... | `runtime_annotation_on_option.res`, `runtime_annotation_on_type_variable.res` |
187197
| `@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` |
188198
| `@typeOf("number") type t = sym` with `@typeOf("symbol") type sym`, `@typeOf("string") type t = int`, or an abbreviation of a function type, a record or a tuple | This annotation, @typeOf("number"), contradicts the abbreviated type, whose values are recognized by @typeOf("symbol"). An array or tuple is recognized by Array.isArray. | `runtime_annotation_contradicts_manifest.res`, `runtime_annotation_contradicts_builtin.res`, `runtime_annotation_contradicts_function.res`, `runtime_annotation_contradicts_tuple.res` |
189199
| `@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` |
190200
| `@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` |
201+
| `@instanceOf("Element") type element = node` with `type node` | A class must be declared on an abstract type or a private abbreviation ... | `instance_of_public_abbreviation.res`; `typed_array_cases_distinct.res` (typed arrays are distinct types) |
191202
| `@instanceOf("Animal") type t = private dog` | Animal appears twice in this chain of abbreviations ... | `instance_of_class_twice_in_chain.res` |
192203
| `@instanceOf("MyList") type t = private array<int>` | @instanceOf("MyList") cannot describe the values of an array type: ... subclasses of Array are not supported. | `instance_of_array_subclass.res` |
193204
| 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` |
@@ -271,7 +282,7 @@ written (Known bugs).
271282
`Cross_runtime_annotation_functor_argument`,
272283
`Cross_not_undefined_option_implementation`.
273284
- Shaped by #6296, #6383, #6396, #7458, #7464, #7788, #8009, #8950, #8948,
274-
#8949, #8964. Related: #7147.
285+
#8949, #8955, #8964. Related: #7147.
275286

276287
### 6. Well-formedness
277288

‎compiler/ml/ast_untagged_variants.ml‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ type error =
3535
| RuntimeAnnotationContradictsManifest of string * string
3636
| SubclassOfArray of string
3737
| SubclassOfPublicAbbreviation of string * string
38+
| ClassOnPublicAbbreviation of string
3839
| ClassTwiceInChain of string
3940
exception Error of Location.t * error
4041

@@ -169,6 +170,13 @@ let report_error ppf =
169170
are recognized by Array.isArray, and subclasses of Array are not \
170171
supported."
171172
name
173+
| ClassOnPublicAbbreviation name ->
174+
fprintf ppf
175+
"A class must be declared on an abstract type or a private abbreviation: \
176+
write `= private ...`, so that values of the abbreviated type are not \
177+
instances of %s without a coercion, and another abbreviation cannot \
178+
declare them instances of another class."
179+
name
172180
| SubclassOfPublicAbbreviation (name, ancestor) ->
173181
fprintf ppf
174182
"A subclass must be a private abbreviation: write `= private ...` so \

‎compiler/ml/typedecl.ml‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,14 @@ let check_manifest_annotation ~loc ~env (decl : Types.type_declaration) =
367367
| Can_be_undefined -> unrelated ())
368368
| (Instance_of _ | Type_of _), None -> (
369369
match Typeopt.abstractness env manifest with
370-
| Opaque -> ()
370+
| Opaque -> (
371+
(* Two public abbreviations of one type would declare its values
372+
instances of two classes, and the type checker would not tell
373+
them apart *)
374+
match outer with
375+
| Instance_of name when decl.type_private = Public ->
376+
error (ClassOnPublicAbbreviation name)
377+
| Not_undefined | Instance_of _ | Type_of _ -> ())
371378
| Stands_for_another -> stands_for_another ()
372379
| Not_abstract -> unrelated ())
373380
| Type_of block_type, Some inner when block_type = inner -> ()

‎packages/@rescript/runtime/Stdlib_BigInt64Array.res‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** The `BigInt64Array` typed array represents an array of 64-bit signed integers in platform byte order. See [BigInt64Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt64Array)
22
*/
3-
@editor.completeFrom(Stdlib.TypedArray) @instanceOf("BigInt64Array")
4-
type t = Stdlib_TypedArray.t<bigint>
3+
@instanceOf("BigInt64Array")
4+
type t = private Stdlib_TypedArray.t<bigint>
5+
6+
include Stdlib_TypedArray.Impl({
7+
type t = t
8+
type elt = bigint
9+
})
510

611
module Constants = {
712
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)

‎packages/@rescript/runtime/Stdlib_BigUint64Array.res‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** The `BigUint64Array` typed array represents an array of 64-bit unsigned integers in platform byte order. See [BigUint64Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigUint64Array)
22
*/
3-
@editor.completeFrom(Stdlib.TypedArray) @instanceOf("BigUint64Array")
4-
type t = Stdlib_TypedArray.t<bigint>
3+
@instanceOf("BigUint64Array")
4+
type t = private Stdlib_TypedArray.t<bigint>
5+
6+
include Stdlib_TypedArray.Impl({
7+
type t = t
8+
type elt = bigint
9+
})
510

611
module Constants = {
712
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)

‎packages/@rescript/runtime/Stdlib_Float32Array.res‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** The `Float32Array` typed array represents an array of 32-bit floating point numbers in platform byte order. See [Float32Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float32Array)
22
*/
3-
@editor.completeFrom(Stdlib.TypedArray) @instanceOf("Float32Array")
4-
type t = Stdlib_TypedArray.t<float>
3+
@instanceOf("Float32Array")
4+
type t = private Stdlib_TypedArray.t<float>
5+
6+
include Stdlib_TypedArray.Impl({
7+
type t = t
8+
type elt = float
9+
})
510

611
module Constants = {
712
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)

‎packages/@rescript/runtime/Stdlib_Float64Array.res‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** The `Float64Array` typed array represents an array of 64-bit floating point numbers in platform byte order. See [Float64Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Float64Array)
22
*/
3-
@editor.completeFrom(Stdlib.TypedArray) @instanceOf("Float64Array")
4-
type t = Stdlib_TypedArray.t<float>
3+
@instanceOf("Float64Array")
4+
type t = private Stdlib_TypedArray.t<float>
5+
6+
include Stdlib_TypedArray.Impl({
7+
type t = t
8+
type elt = float
9+
})
510

611
module Constants = {
712
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)

‎packages/@rescript/runtime/Stdlib_Int16Array.res‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** The `Int16Array` typed array represents an array of twos-complement 16-bit signed integers in platform byte order. See [Int16Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int16Array)
22
*/
3-
@editor.completeFrom(Stdlib.TypedArray) @instanceOf("Int16Array")
4-
type t = Stdlib_TypedArray.t<int>
3+
@instanceOf("Int16Array")
4+
type t = private Stdlib_TypedArray.t<int>
5+
6+
include Stdlib_TypedArray.Impl({
7+
type t = t
8+
type elt = int
9+
})
510

611
module Constants = {
712
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)

‎packages/@rescript/runtime/Stdlib_Int32Array.res‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
/** The `Int32Array` typed array represents an array of twos-complemenet 32-bit signed integers in platform byte order. See [Int32Array on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Int32Array)
22
*/
3-
@editor.completeFrom(Stdlib.TypedArray) @instanceOf("Int32Array")
4-
type t = Stdlib_TypedArray.t<int>
3+
@instanceOf("Int32Array")
4+
type t = private Stdlib_TypedArray.t<int>
5+
6+
include Stdlib_TypedArray.Impl({
7+
type t = t
8+
type elt = int
9+
})
510

611
module Constants = {
712
/**`bytesPerElement` returns the element size. See [BYTES_PER_ELEMENT on MDN](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray/BYTES_PER_ELEMENT)

0 commit comments

Comments
 (0)