Refine GADT casts with GADT approximation of singleton types #15533
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Fixes #14776.
Fixes #15531.
#12159 refines GADT casts with singleton types to retain the identity of casted stable references.
This causes #14776, where we find that the checker fails when checking some if expressions with GADT constraints. Briefly, the reason why the code snippet in the #14776 crashes:
T2
, the else-body, after GADT casting, is typed ase.data.type & T1
.T2
ande.data.type & T1
, which isT2
, with GADT knowledge.T2 | (e.data.type & T1)
mismatches with the assigned typeT2
.It turns out that, the target type in the cast of the then-body,
e.data.type & T1
, does not contain enough typing information to recover the (simplified) union type of the entire if expression. To fix this issue, we intersect the GADT approximation of the singleton type to preserve as much GADT information as possible. Specifically, now we caste.data: T
toe.data.type & T2 & T1
.This PR also fixes a bug (see #15531) where the typer leaks the internal type parameters in GADT casts, which will break the pickler.