-
Notifications
You must be signed in to change notification settings - Fork 1.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Refine criterion when to widen types #17180
Conversation
@odersky Happy to. @Decel let me know if you want to sync in a call or on the LAMP slack. Also, what do you think of my approach in #17173? I don't quite get the mechanics of Ranges in ApproximatingTypeMap's, so I wondered if it can be cleaned up further. But generally, it solves the problem within matchCases alone. (The other pickler and tupleElementTypes changes are both Ytest-pickler related.) |
One thing where #17180 falls short is error diagnostics. When we do not widen, we essentially get a "Stuck" error saying scrutinee is not a subtype and is not disjoint either. But before we got a "variable cannot be uniquely instantiated" error, which is much better. After all, scrutinee is a subtype of pattern in this case, for some instantiation of type variables. We could recover the original error diagnostics by doing the widening in So my proposal would be:
That could lead to much better error messages. And diagnostics is really important for match types, since the algorithms are so complex. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the new neg tests are effectivly less informative than the old ones. So before fixing the check files we should try to improve the diagnostics to a point where they are equivalent.
I agree. I marked it draft to convey that even if it's green, it's not to be merged. |
Also fixes #16504 |
Let's look at it this this coming week (24th). |
So, we have managed to find a counterexample with @dwijnand: trait Show[-A >: Nothing]
type MT1[I <: Show[Nothing], N] = I match
case Show[a] => N match
case 0 => a
val a = summon[MT1[Show[Any], 0] =:= Any] |
Refine criterion when to widen types in match type reduction. We now do not widen if the compared-against type contains variant named type parameters. This is intended to fix the soundness problem in scala#17149. Fixes scala#17149 Fixes scala#15926 Todos: - [ ] Check & fix neg test failures - [ ] Add more tests - [ ] Also consider approximating abstract types to lower bounds. This is completely missing so far. There are neither tests nor an implementation. - [ ] Update the docs on match types to explain what goes on here.
During match type reduction, if we're going to allow approximating we must further restrict match types from widening to its bound. Failing to do so will record GADT constraint bounds that parameters will be approximated to, which is lossy and leads to a loss of type checking completion.. For instance, in a variant of i13633 with an added `<: (Boolean, Boolean)` bound to `PlusTri`, when reducing `PlusTri[a, b, O] match { case (x, y) => ... }`, widening the expansion of `PlusTri[a, b, O]` to its bound `(Boolean, Boolean)`, causes the recording the constraints bounds `x >: Boolean` and `y >: Boolean` and then reduction instantiates `x` and `y` to Boolean, losing the precision that comes from `a`, `b`, and `O`. This came up while implementing bounds checking to match type case bodies, and requires an important fix to PatternTypeConstrainer (the use of typeSymbol's) so the tests that require this are part of that effort.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, pending Martin's review
@odersky I can't request your review of your PR, but could you have a look and say if you're happy with this landing? Thank you. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, pending Martin's review
Refine criterion when to widen types in match type reduction.
We now do not widen if the compared-against type contains variant named type parameters. This is intended to fix the soundness problem in #17149. It also fixes the regression in #15926. @Decel was right in suggesting these two were related.
Fixes #17149
Fixes #15926
Todos: