Skip to content

Fix #4880: always instantiate prefix tvar #4897

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
Aug 13, 2018
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
28 changes: 16 additions & 12 deletions compiler/src/dotty/tools/dotc/transform/patmat/Space.scala
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
private val nullType = ConstantType(Constant(null))
private val nullSpace = Typ(nullType)

override def intersectUnrelatedAtomicTypes(tp1: Type, tp2: Type) = {
override def intersectUnrelatedAtomicTypes(tp1: Type, tp2: Type): Space = {
val and = AndType(tp1, tp2)
// Precondition: !(tp1 <:< tp2) && !(tp2 <:< tp1)
// Then, no leaf of the and-type tree `and` is a subtype of `and`.
Expand Down Expand Up @@ -656,18 +656,18 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
// Fix subtype checking for child instantiation,
// such that `Foo(Test.this.foo) <:< Foo(Foo.this)`
// See tests/patmat/i3938.scala
def removeThisType(implicit ctx: Context) = new TypeMap {
// is in tvarBounds? Don't create new tvars if true
private var tvarBounds: Boolean = false
class RemoveThisMap extends TypeMap {
var prefixTVar: Type = null
def apply(tp: Type): Type = tp match {
case ThisType(tref: TypeRef) if !tref.symbol.isStaticOwner =>
if (tref.symbol.is(Module))
TermRef(this(tref.prefix), tref.symbol.sourceModule)
else if (tvarBounds)
else if (prefixTVar != null)
this(tref)
else {
tvarBounds = true
newTypeVar(TypeBounds.upper(this(tref)))
prefixTVar = WildcardType // prevent recursive call from assigning it
prefixTVar = newTypeVar(TypeBounds.upper(this(tref)))
prefixTVar
}
case tp => mapOver(tp)
}
Expand All @@ -681,14 +681,18 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
}
}

val force = new ForceDegree.Value(
tvar => !(ctx.typerState.constraint.entry(tvar.origin) eq tvar.origin.underlying),
minimizeAll = false
)

val removeThisType = new RemoveThisMap
val tvars = tp1.typeParams.map { tparam => newTypeVar(tparam.paramInfo.bounds) }
val protoTp1 = removeThisType.apply(tp1).appliedTo(tvars)

val force = new ForceDegree.Value(
tvar =>
!(ctx.typerState.constraint.entry(tvar.origin) `eq` tvar.origin.underlying) ||
(tvar `eq` removeThisType.prefixTVar),
minimizeAll = false,
allowBottom = false
)

// If parent contains a reference to an abstract type, then we should
// refine subtype checking to eliminate abstract types according to
// variance. As this logic is only needed in exhaustivity check,
Expand Down
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/dotc/typer/Inferencing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ object Inferencing {
val minimize =
force.minimizeAll ||
variance >= 0 && !(
force == ForceDegree.noBottom &&
!force.allowBottom &&
defn.isBottomType(ctx.typeComparer.approximation(tvar.origin, fromBelow = true)))
if (minimize) instantiate(tvar, fromBelow = true)
else toMaximize = true
Expand Down Expand Up @@ -466,9 +466,9 @@ trait Inferencing { this: Typer =>

/** An enumeration controlling the degree of forcing in "is-dully-defined" checks. */
@sharable object ForceDegree {
class Value(val appliesTo: TypeVar => Boolean, val minimizeAll: Boolean)
class Value(val appliesTo: TypeVar => Boolean, val minimizeAll: Boolean, val allowBottom: Boolean = true)
val none = new Value(_ => false, minimizeAll = false)
val all = new Value(_ => true, minimizeAll = false)
val noBottom = new Value(_ => true, minimizeAll = false)
val noBottom = new Value(_ => true, minimizeAll = false, allowBottom = false)
}

1 change: 1 addition & 0 deletions tests/patmat/i4880.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
10: Pattern Match Exhaustivity: _: ZipArchive, _: VirtualFile, _: PlainFile
13 changes: 13 additions & 0 deletions tests/patmat/i4880.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
sealed abstract class AbstractFile
class PlainFile(path: String) extends AbstractFile
class VirtualFile(name: String) extends AbstractFile
abstract class ZipArchive(path: String) extends AbstractFile {
sealed abstract class Entry(name: String) extends VirtualFile(name)
class DirEntry(path: String) extends Entry(path)
}

object Test {
def foo(file: AbstractFile) = file match {
case ze: ZipArchive#Entry =>
}
}
17 changes: 17 additions & 0 deletions tests/patmat/i4880a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
sealed abstract class AbstractFile
class PlainFile(path: String) extends AbstractFile
class VirtualFile(name: String) extends AbstractFile
abstract class ZipArchive(path: String) extends AbstractFile {
sealed abstract class Entry(name: String) extends VirtualFile(name)
class DirEntry(path: String) extends Entry(path)
}

object Test {
def foo(file: AbstractFile) = file match {
case a: PlainFile =>
case b: ZipArchive =>
case c1: ZipArchive#Entry =>
case c1: ZipArchive#DirEntry =>
case c: VirtualFile =>
}
}
16 changes: 16 additions & 0 deletions tests/patmat/i4880b.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
sealed abstract class AbstractFile
class PlainFile(path: String) extends AbstractFile
class VirtualFile(name: String) extends AbstractFile
abstract class ZipArchive(path: String) extends AbstractFile {
sealed abstract class Entry(name: String) extends VirtualFile(name)
class DirEntry(path: String) extends Entry(path)
}

object Test {
def foo(file: AbstractFile) = file match {
case a: PlainFile =>
case b: ZipArchive =>
case c1: ZipArchive#Entry =>
case c: VirtualFile =>
}
}