Skip to content

Propagation of type parameter bounds to wildcard bind patterns loses prefix #8991

Open
@scabug

Description

@scabug
class Base {
  type N

  class Tree[-T >: N]

  def f(x: Any): Tree[N] = x match {
    case y: Tree[_] => y
  }
}
class Derived extends Base {
  def g(x: Any): Tree[N] = x match {
    case y: Tree[_] => y // fails in both scalac and dotc
  }
}

Here's a patch that makes this typecheck:

diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
index a182a2e..dffed83 100644
--- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala
+++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala
@@ -4923,8 +4923,10 @@ trait Typers extends Adaptations with Tags with TypersTracking with PatternTyper
                 val TypeBounds(lo1, hi1) = tbounds.subst(tparams, argtypes)
                 val lo = lub(List(lo0, lo1))
                 val hi = glb(List(hi0, hi1))
-                if (!(lo =:= lo0 && hi =:= hi0))
-                  asym setInfo logResult(s"Updating bounds of ${asym.fullLocationString} in $tree from '$abounds' to")(TypeBounds(lo, hi))
+                if (!(lo =:= lo0 && hi =:= hi0)) {
+                  val boundsSeenFrom = TypeBounds(lo, hi).asSeenFrom(tpt1.tpe.prefix, tparam.enclClass.owner.enclClass)
+                  asym setInfo logResult(s"Updating bounds of ${asym.fullLocationString} in $tree from '$abounds' to")(boundsSeenFrom)
+                }
               }
               if (asym != null && asym.isAbstractType) {
                 arg match {

Related discussion in dotty: scala/scala3#232

Metadata

Metadata

Assignees

No one assigned

    Labels

    fixed in Scala 3This issue does not exist in the Scala 3 compiler (https://github.com/lampepfl/dotty/)typer

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions