-
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
Better mismatch errors for type unions and intersections #18737
Comments
I agree it would be a HUGE improvement if TypeMismatch errors could be diagnosed more precisely. But it's also a huge undertaking. TypeComparer goes through a maze of many possible paths to prove a subtype relation that in the end does not hold. For instance, I append the current output with -explain, which just records the possibilities that were tried and is as such not very helpful except for experts. How to condense that without giving false or misleading info? It's certainly a challenging task.
|
Note also that if actual and expected types are syntactically similar, we already do color-based diffing. The problem arises when they are not syntactically similar. |
This is a partial fix of scala#18737. We still can't explain the differences concisely, but at least we shorten the comparison traces by showing only steps that contributed to the overall failure and by avoiding repetitions.
Although the type resolution mechanism has to explore multiple paths to determine soundness, wouldn't it be possible to improve the error at a later stage after the mismatch is detected? If there's a mismatch of union or intersection types, it seems a simple diff could produce a good error message as zio-clippy does. Variance might make a generic logic more difficult to implement since I image zio-clippy assumes ZIO's specific variance behavior. Another path that could be interesting to explore is allowing libraries to define custom type mismatch error reporting. I'd say that would be even more convenient since libraries could provide more advanced error messages considering their APIs and common user mistakes. I tried to implement that via implicits and macros but I couldn't create a mechanism that doesn't interfere with the type inference of Kyo computations. |
I did a partial fix in #18742, which improves the situation in that it makes -explain traces more concise and a little bit less scary. For the rest, if someone else has a good idea how to make concrete progress and has time to explore this, please have a go at this. |
This is a partial fix for #18737. We still can't explain the differences concisely, but at least we shorten the comparison traces by showing only steps that contributed to the overall failure and by avoiding repetitions.
Compiler version
3.3.0
Minimized example
A common encoding for libraries is to use intersection and union types to express requirements a computation might have. For example, ZIO uses a type intersection for the environment channel and an union for the error channel, Tapir uses a similar approach to express capabilities, and Kyo uses an intersection to represent the set of pending algebraic effects.
Output Error/Warning message
Why this Error/Warning was not helpful
Users normally have issues to understand where the mismatch is since the types can end up accumulating several components.
Suggested improvement
Provide a message that highlights where the differences are. Ideally, it should also consider variance. For example, a contravariant type parameter can receive an intersection that has a subset of its components without issues.
zio-clippy is a good source of inspiration although it's specific to ZIO. It improves error messages by parsing and transforming them.
cc/ @hmemcpy
The text was updated successfully, but these errors were encountered: