Repository navigation
Conversation
Why:
With the previous commit `WebAPI.focus((el :> WebAPI.element))` compiles
for an `el: ReactDOM.element`, but each call into another package's
bindings still needs the written coercion, also for every JSX prop.
Where the compiler knows the type a site expects, it can make the
coercion itself: the two types name one class, and the value is the
same.
Typing modified (an argument at a site that declares its type, `Typecore.unify_exp_at_argument`):
Before (rule):
Γ ⊢ e : τ τ = τ'
──────────────────── (Argument)
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
After (rule):
Γ ⊢ e : τ τ = τ'
──────────────────── (Argument)
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
Γ ⊢ e : τ τ ≠ τ' τ ≤class τ'
──────────────────── (Class argument)
Γ ⊢ e : τ' at a parameter, record field or constructor payload of type τ'
τ ≤class τ': τ is a class type of the class of τ' or of a subclass, or τ and τ' are equal up to two declarations of one class inside them
Diagnostic removed (another declaration of the parameter's class):
Example:
module WebAPI = {
@instanceof("Element") type element
@instanceof("HTMLInputElement") type input = private element
@send external focus: element => unit = "focus"
}
module ReactDOM = {
@instanceof("Element") type element
type props = {container: element}
}
let a = (el: ReactDOM.element) => WebAPI.focus(el)
let b = (input: WebAPI.input): ReactDOM.props => {container: input}
Before (diagnostic): each line compiled alone
This has type: ReactDOM.element
But this function argument is expecting: WebAPI.element
This has type: WebAPI.input
But the record field container is expected to have type: ReactDOM.element
After (diagnostic): none
After (js):
function a(el) {
el.focus();
}
function b(input) {
return {
container: input
};
}
Diagnostic modified (where `:>` stays written):
Example:
let c = (first, a: ReactDOM.element, b: WebAPI.element) => first ? a : b
let take = (_: option<ReactDOM.element>) => ()
let d = (input: option<WebAPI.input>) => take(input)
Before (diagnostic): each compiled alone, the second with the parent commit's compiler
This has type: WebAPI.element
But this ternary is expected to return: ReactDOM.element
This has type: option<WebAPI.input>
But this function argument is expecting: option<ReactDOM.element>
The incompatible parts:
WebAPI.input vs ReactDOM.element
Possible solutions:
- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (input :> option<ReactDOM.element>)
After (diagnostic): unchanged
The sites are the parameters of a function (labelled, optional and piped
arguments too), the fields of a record being built, which includes JSX
props, and constructor payloads: their expected type is declared. In
`first ? a : b` the expected type of `b` is the type of `a`, so accepting
it would make the type of the result, and with a subclass whether it
compiles, depend on the order of the branches; an assignment to a mutable
field keeps `:>` too. Inside another type only the same class is accepted
(`Same_class` steps of `subtype_rec`): `option<input>` for
`option<element>` is a subclass coercion that `:>` states. The argument
gets the expected type, as `(e :> t)` would give it.
Review: Does a program that compiled before get other types? No: the rule is only tried where the argument's type failed to unify with the expected one, which was an error.
Review: Are other errors printed differently? No: when the class check fails, it restores the type state it changed and the original clash is raised. The super_errors expectations are unchanged.
Co-authored-by: Jaap Frolich <jfrolich@gmail.com>
Session: https://os.submodule.nl/session/bks-6674cba8-4481-7b1d-882a-9a317373d7c1
Signed-off-by: Jaap Frolich <jfrolich@gmail.com>
Changelog🚀 New Feature
Semantic Diff1 commit · Full page Accept another declaration of a class at an argument without
|
| Before | After |
|---|---|
|
|
|
Diagnostic removed (another declaration of the parameter's class)
module WebAPI = {
@instanceOf("Element") type element
@instanceOf("HTMLInputElement") type input = private element
@send external focus: element => unit = "focus"
}
module ReactDOM = {
@instanceOf("Element") type element
type props = {container: element}
}
let a = (el: ReactDOM.element) => WebAPI.focus(el)
let b = (input: WebAPI.input): ReactDOM.props => {container: input}diagnostic · Before: each line compiled alone
-This has type: ReactDOM.element
-But this function argument is expecting: WebAPI.element
-
-This has type: WebAPI.input
-But the record field container is expected to have type: ReactDOM.element
+nonejs · After
function a(el) {
el.focus();
}
function b(input) {
return {
container: input
};
}Diagnostic modified (where :> stays written)
let c = (first, a: ReactDOM.element, b: WebAPI.element) => first ? a : b
let take = (_: option<ReactDOM.element>) => ()
let d = (input: option<WebAPI.input>) => take(input)diagnostic · Before: each compiled alone, the second with the parent commit's compiler
-This has type: WebAPI.element
-But this ternary is expected to return: ReactDOM.element
-
-This has type: option<WebAPI.input>
-But this function argument is expecting: option<ReactDOM.element>
-The incompatible parts:
- WebAPI.input vs ReactDOM.element
-Possible solutions:
-- These types are compatible at runtime. You can use the coercion operator to convert to the expected type: (input :> option<ReactDOM.element>)
+unchangedThe sites are the parameters of a function (labelled, optional and piped arguments too), the fields of a record being built, which includes JSX props, and constructor payloads: their expected type is declared. In first ? a : b the expected type of b is the type of a, so accepting it would make the type of the result, and with a subclass whether it compiles, depend on the order of the branches; an assignment to a mutable field keeps :> too. Inside another type only the same class is accepted (Same_class steps of subtype_rec): option<input> for option<element> is a subclass coercion that :> states. The argument gets the expected type, as (e :> t) would give it.
Review
- Does a program that compiled before get other types? No: the rule is only tried where the argument's type failed to unify with the expected one, which was an error.
- Are other errors printed differently? No: when the class check fails, it restores the type state it changed and the original clash is raised. The super_errors expectations are unchanged.
✅ Compiler benchmarksAllocated wordsIn total: 638,997,763 → 639,550,485 (+0.09%). Generated projects: 629,067,834 → 629,168,749 (+0.02%).
22 more changes
InstructionsIn total: 9,222,162,488 → 9,226,038,556 (+0.04%).
8 more changes
Output sizeIn total: 26,089,181 → 26,090,118 (±0.00%). None of the 13 benchmarks changed by 0.1% or more. Pull request 181b0bb compared with base 7964eea in this run. |
rescript
@rescript/belt
@rescript/darwin-arm64
@rescript/linux-arm64
@rescript/linux-x64
@rescript/runtime
@rescript/win32-x64
commit: |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## class-identity-explicit #9433 +/- ##
========================================================
Coverage 82.21% 82.22%
========================================================
Files 462 462
Lines 65628 65666 +38
========================================================
+ Hits 53957 53992 +35
- Misses 11671 11674 +3
🚀 New features to boost your workflow:
|
|
@codex review |
Codex Review Summary: ✅ CompletedThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 181b0bbbda
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| type_expect ~context ~class_coercion ?recarg env sarg ty_expected' | ||
| in | ||
| if class_coercion then unify_exp_at_argument ~context env texp ty_expected |
There was a problem hiding this comment.
Apply the class fallback to inline callback arguments
When a callback literal annotates its parameter with another declaration of the expected class, type_expect raises before reaching the fallback below. For example, using the added test modules, View.handle((_: Class_identity_dom.event) => (), Class_identity_dom.makeEvent()) is rejected, although binding that same callback to listener and passing listener succeeds. The Pexp_fun path calls type_function with the expected arrow type, so its parameter-pattern check requires ordinary unification and never uses class_coercion. Handle this early checking failure as well so the advertised coercion for function-valued arguments works for inline event handlers, not just previously bound functions.
Useful? React with 👍 / 👎.
When a function expects one package's declaration of a JavaScript class, a value using another declaration currently needs an explicit
:>even though its JavaScript representation is unchanged. This adds automatic class coercion at function arguments, record construction (including JSX props), and constructor payloads.This is the automatic-coercion part of #9413, stacked on the explicit class-coercion PR #9432. The fallback runs only after ordinary unification fails. At the outermost type it accepts the same class or a declared subclass; inside another type it accepts only declarations of the same class. Branch results, mutable-field assignments, and nested subclass coercions still require
:>. Failed checks restore the type state and retain the original diagnostic.Includes the automatic-coercion docs, completion text, runtime and diagnostic tests, and a separate changelog fragment. Function arguments include labelled, optional, and piped arguments.
Known limitation: a parameter's expected type can come from an earlier argument through a type variable. For
let same: ('a, 'a) => bool,same(element, input)can compile whilesame(input, element)does not.Validation: built the compiler from this branch; all five class-identity runtime tests and the full
super_errorssuite pass; generated regression JavaScript matches the fixtures; changelog and commit-record checks pass. The combined compiler implementation, docs, and tests match #9413. The local environment lacks dependencies for the complete toolchain build, so full CI remains required. Local runtime dependencies were prepared with the original PR's CI-built tooling; the changed fixtures and diagnostic suite were compiled with this branch's compiler.