Skip to content

2.12 compiler fails to infer type for complex nested Map involving F-bounded type #12387

Open
@wboult

Description

@wboult

reproduction steps

using Scala 2.12.13,

The following code seems to never compile, presumably the compiler gets into some kind of trouble while inferring the type of the map being constructed:

case class DocumentId(tpe: String, value: String)

sealed trait KeyType extends Product with Serializable
case class StringKey(k: String) extends KeyType
case class DateKey(k: String) extends KeyType

object Example {
  // ---- No explicit type provided ----
  val expected = Map(
    DocumentId("doc1", "1") -> Set(
      Map(
        StringKey("businessname") -> "eagle invest",
        DateKey("start_date") -> java.sql.Date.valueOf("2017-01-01")
      )
    ),
    DocumentId("doc2", "1") -> Set(
      Map(StringKey("address") -> "London, UK"),
      Map(StringKey("address") -> "London")
    )
  )
}

// compiler falls over trying to infer Map[DocumentId, Set[Map[Any, Any]]]

When I add an explicit type, the code compiles fine:

case class DocumentId(tpe: String, value: String)

sealed trait KeyType extends Product with Serializable
case class StringKey(k: String) extends KeyType
case class DateKey(k: String) extends KeyType

object Example {
  // ---- Explicit type provided ----
  val expected: Map[DocumentId, Set[Map[Any, Any]]] = Map(
    DocumentId("doc1", "1") -> Set(
      Map(
        StringKey("businessname") -> "eagle invest",
        DateKey("start_date") -> java.sql.Date.valueOf("2017-01-01")
      )
    ),
    DocumentId("doc2", "1") -> Set(
      Map(StringKey("address") -> "London, UK"),
      Map(StringKey("address") -> "London")
    )
  )
}

The same code compiles fine without any explicit type in both 2.11 and 2.13 interestingly.
Examples on Scastie:

problem

compiler gets stuck and eventually dies

Metadata

Metadata

Assignees

No one assigned

    Labels

    f-boundsfixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)infer

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions