Fix unsound injectivity assumption when inferring GADT bounds #13380
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.
Current compiler allows the compilation of the following unsound code (as added as a negative test):
The soundness problem arises from the implementation of
inFrozenGadtIf
:Note that the code will set
frozenGadt
tocond
regardless of its current value. In other words, if we freeze GADT constraints inop1
withinFrozenGadtIf(true)(op1)
and inop1
we callinFrozenGadtIf(false)(op2)
, the GADT constraints will be unfreezed inop2
.In the negative example here, when comparing
F[Option[A]] <: G[Option[Int]]
, we found that the injectivity does not hold, so we will freeze GADT and compareOption[A] <: Option[Int]
. However, since the concreteOption
constructor has injectivity, we will accidentally unfreeze GADT and infer the constraintA <: Int
, which is unsound.I am not sure whether it is a expected behavior that
inFrozenGadtIf(false)(op)
unfreezes GADT inop
. But after disabling such unfreezing by writingfrozenGadt ||= cond
ininFrozenGadtIf
, the unsound code will be rejected.@abgruszecki