Reimplement constraint merging for correctness #13292
Merged
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.
The previous implementation simply combined the content of both
constraints into one, but this is not enough since it meant that bounds
were not propagated and so transitivity was violated. For example, when
merging a constraint containing
?S <: ?T
and one containing?T <: ?R
,the result did not verify
?S <:< ?R
(see the unit tests added inConstraintsTest).
The new implementation simply starts with one set of constraints and
then adds the constraints from the other set one by one using
<:<
which takes care of propagating bounds. This is likely to be more
expensive than the previous implementation but it turns out that
TyperState#mergeConstraintWith
is a rare operation (it isonly called 43 times when compiling scala3-compiler), so
the difference shouldn't be significant.
This also incidentally fixes #12730 because the previous logic for
checking if merging succeeded was flawed.