Skip to content

Type infers widens union of applied abstract covariant types F[A] | F[B] into Any instead of F[A | B] #12264

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

Closed
japgolly opened this issue Apr 29, 2021 · 4 comments · Fixed by #12272
Assignees
Milestone

Comments

@japgolly
Copy link
Contributor

Compiler version

3.0.0-RC3

Minimized code

object Html {
  final opaque type Tag[+N] = String
  def apply[N](name: String): Tag[N] = name
}

object HtmlTags {
  final def br: Html.Tag[Int] = Html("br")
  final def p = Html[Long]("p")
}

object Test {
  type Expect = Html.Tag[Any]

  val x = List[Expect](HtmlTags.br, HtmlTags.p) // ok

  val y = List(HtmlTags.br, HtmlTags.p)
  y: List[Expect] // error
}

Output

[error] 17 |  y: List[Expect] // error
[error]    |  ^
[error]    |  Found:    (Test.y : List[Any])
[error]    |  Required: List[Test.Expect]

Expectation

Success.

Without opaque types

If we replace the opaque type with a case class and type alias, everything compiles successfully.

case class Html[+N](name: String)
object Html {
  type Tag[+N] = Html[N]
}

// object Html {
//   final opaque type Tag[+N] = String
//   def apply[N](name: String): Tag[N] = name
// }

// Code below is unchanged

object HtmlTags {
  final def br: Html.Tag[Int] = Html("br")
  final def p = Html[Long]("p")
}

object Test {
  type Expect = Html.Tag[Any]

  val x = List[Expect](HtmlTags.br, HtmlTags.p) // ok

  val y = List(HtmlTags.br, HtmlTags.p)
  y: List[Expect] // ok now
}
@b-studios
Copy link
Contributor

For completeness: type inference also works, if we replace the opaque type with a sealed trait:

object Html {
  sealed trait Tag[+N] // <<<<<
  def apply[N](name: String): Tag[N] = ???
}

@smarter
Copy link
Member

smarter commented Apr 29, 2021

This isn't specific to opaque types, it also happens with regular abstract types:

object Test {
  type Tag[+N]
  def br: Tag[Int] = ???
  def p: Tag[Long] = ???
  val y = List(br, p) // infers List[Any] instead of List[Tag[AnyVal]]
}

@smarter smarter changed the title Type inference should reduce covariant opaque types F[A] + F[B] to F[LUB[A,B]] Type infers widens union of applied abstract covariant types F[A] | F[B] into Any instead of F[A | B] Apr 29, 2021
@smarter
Copy link
Member

smarter commented Apr 29, 2021

The current logic for widening union is based around finding base classes, which means abstract types don't get preserved: https://github.com/lampepfl/dotty/blob/4e8ca785d5e92a0ff2fafb19deb4e6b75b74d8fc/compiler/src/dotty/tools/dotc/core/TypeOps.scala#L183-L199

@odersky do you think this logic could be adapted to allow Tag[Int] | Tag[Long] to be widened to Tag[Int | Long] where Tag is a covariant abstract type?

@smarter smarter assigned odersky and unassigned smarter Apr 29, 2021
@odersky
Copy link
Contributor

odersky commented Apr 29, 2021

Yes, I think that can work.

odersky added a commit to dotty-staging/dotty that referenced this issue Apr 29, 2021
Merge applied type arguments also for abstract and opaque type constructors

Fixes scala#12264
odersky added a commit to dotty-staging/dotty that referenced this issue Apr 29, 2021
Merge applied type arguments also for abstract and opaque type constructors

Fixes scala#12264
michelou pushed a commit to michelou/scala3 that referenced this issue May 1, 2021
Merge applied type arguments also for abstract and opaque type constructors

Fixes scala#12264
liufengyun pushed a commit to dotty-staging/dotty that referenced this issue May 3, 2021
Merge applied type arguments also for abstract and opaque type constructors

Fixes scala#12264
@Kordyjan Kordyjan added this to the 3.0.1 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants