Skip to content

Make translucentSuperType handle match types #12153

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

Merged
merged 3 commits into from
Apr 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1888,7 +1888,15 @@ object Types {
case st => st
}

/** Same as superType, except that opaque types are treated as transparent aliases */
/** Same as superType, except for two differences:
* - opaque types are treated as transparent aliases
* - applied type are matchtype-reduced if possible
*
* Note: the reason to reduce match type aliases here and not in `superType`
* is that `superType` is context-independent and cached, whereas matchtype
* reduction depends on context and should not be cached (at least not without
* the very specific cache invalidation condition for matchtypes).
*/
def translucentSuperType(using Context): Type = superType
}

Expand Down Expand Up @@ -4011,7 +4019,7 @@ object Types {
case tycon: TypeRef if tycon.symbol.isOpaqueAlias =>
tycon.translucentSuperType.applyIfParameterized(args)
case _ =>
superType
tryNormalize.orElse(superType)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do this in translucentSuperType and not superType? At the very least it means the documentation of the base translucentSuperType in https://github.com/lampepfl/dotty/blob/f618dd66a93a962c99c22eb586dbc93d7d410988/compiler/src/dotty/tools/dotc/core/Types.scala#L1891-L1892 is now incorrect.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed the comment, which now also explains why we can't do it in superType.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can see we did not normalize match types when computing the signatures and erasures of inherited members. That means we did not detect logical overrides, and that means we did not generate bridges where they would have been necessary.

Match types that can be reduced directly in signatures were reduced. For instance in

  type M[X] = X match
    case Int => String
    case _ => X

  def test(x: M[Int]): Int = ???

the signature of test is String => Int.

}

inline def map(inline op: Type => Type)(using Context) =
Expand Down
16 changes: 16 additions & 0 deletions tests/pos/i7894.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
case class Box[T](t: T)

type Boxed[T <: Tuple] <: Tuple = T match {
case EmptyTuple => EmptyTuple
case h *: t => Box[h] *: Boxed[t]
}

trait Cmp[T <: Tuple] { def cmp(t: T, b: Boxed[T]): Boolean }

object UnitCmp extends Cmp[EmptyTuple] {
def cmp(t: EmptyTuple, b: EmptyTuple): Boolean = true
}

object UnitCmp2 extends Cmp[EmptyTuple] {
def cmp(t: EmptyTuple, b: Boxed[EmptyTuple]): Boolean = true
}
10 changes: 10 additions & 0 deletions tests/pos/i8666.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
class Foo[A, B]()

type FooSnd[X] = X match
case Foo[_, b] => b

trait Bar[A]:
def bar(h: FooSnd[A]): Int

val foo: Bar[Foo[String, Int]] = new Bar[Foo[String, Int]]:
def bar(h: FooSnd[Foo[String, Int]]) = h