Skip to content

Better mismatch errors for type unions and intersections #18737

Open
@fwbrasil

Description

@fwbrasil

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.

def test1(v: String & Integer & List[String]) = ()
def test2(v: String & Long) = test1(v)

def test3(v: String | Integer | List[String]) = ()
def test4(v: String | Long) = test3(v)

Output Error/Warning message

[error] -- [E007] Type Mismatch Error: test.scala:14:56
[error] 14 |  def test2(v: String & Long) = test1(v)
[error]    |                                                        ^
[error]    |                               Found:    (v : String & Long)
[error]    |                               Required: String & Integer & List[String]
[error]    |
[error]    | longer explanation available when compiling with `-explain`
[error] -- [E007] Type Mismatch Error: test.scala:17:56
[error] 17 |  def test4(v: String | Long) = test3(v)
[error]    |                                                        ^
[error]    |                               Found:    (v : String | Long)
[error]    |                               Required: String | Integer | List[String]
[error]    |
[error]    | longer explanation available when compiling with `-explain

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:reportingError reporting including formatting, implicit suggestions, etcarea:typerbetter-errorsIssues concerned with improving confusing/unhelpful diagnostic messagesitype:enhancement

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions