Skip to content

Inferred type K in Map[K, V] does not match or roundtrip #18551

Open
@mcanlas

Description

@mcanlas

Compiler version

3.3.1

Minimized code

object TestApp extends App {
  enum PersonEnum:
    case First
    case Second
    case Third

  sealed trait PersonSealedTrait

  object PersonSealedTrait:
    case object First extends PersonSealedTrait
    case object Second extends PersonSealedTrait
    case object Third extends PersonSealedTrait

  // minimization seems to be:
  // a) the key side of the map needs to be two fields (a tuple2)
  // b) one member of the tuple must be a sealed trait; not at enum
  lazy val tupleKeyMap = Map(
    ("apple", PersonSealedTrait.First) -> "apple",
    ("notapple", PersonSealedTrait.Third) -> "zapple"
  )

  // possible bug or confusing intuition:
  //   - inferred type of map's key does not match type given to pattern in case match
  //   - scala3 compiler complains that union of first | third is required, but person sealed trait was given from `p`
  tupleKeyMap.map { case ((n, p), _) =>
    tupleKeyMap.apply(n -> p)
  }
}

Output

Found:    (p : TestApp.PersonSealedTrait)
Required: TestApp.PersonSealedTrait.First.type | TestApp.PersonSealedTrait.Third.type
    tupleKeyMap.apply(n -> p)

Expectation

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions