Skip to content

Fix #5948: Tighten rule in hasMatchingMember #5950

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 2 commits into from
Feb 20, 2019
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
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/TypeComparer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1132,7 +1132,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
/*>|>*/ trace(i"hasMatchingMember($tp1 . $name :? ${tp2.refinedInfo}), mbr: ${tp1.member(name).info}", subtyping) /*<|<*/ {
val rinfo2 = tp2.refinedInfo

// If the member is an abstract type, compare the member itself
// If the member is an abstract type and the prefix is a path, compare the member itself
// instead of its bounds. This case is needed situations like:
//
// class C { type T }
Expand All @@ -1148,7 +1148,7 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
def matchAbstractTypeMember(info1: Type) = info1 match {
case TypeBounds(lo, hi) if lo ne hi =>
tp2.refinedInfo match {
case rinfo2: TypeBounds =>
case rinfo2: TypeBounds if tp1.isStable =>
val ref1 = tp1.widenExpr.select(name)
isSubType(rinfo2.lo, ref1) && isSubType(ref1, rinfo2.hi)
case _ =>
Expand Down
20 changes: 20 additions & 0 deletions tests/neg/i5948.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
abstract class Foo {
type A
var elem: A
}

object Test {
def setFirstInList[T](list: List[Foo { type A = T }]) = {
list(0).elem = list(1).elem
}

def main(args: Array[String]): Unit = {
val fooInt = new Foo { type A = Int; var elem = 1 }
val fooString = new Foo { type A = String; var elem = "" }

val list: List[Foo] = List(fooInt, fooString)
setFirstInList[Foo#A](list) // error
setFirstInList(list) // error
println(fooInt.elem + 1)
}
}
14 changes: 14 additions & 0 deletions tests/neg/i5948a.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
trait Bar {
type A
}

object Test {
def test0: Unit = {
val b1: Bar = new Bar {}
val b2: Bar { type A = Bar#A } = b1 // error
}
def test1: Unit = {
val b1: List[Bar] = List(new Bar {})
val b2: List[Bar { type A = Bar#A }] = b1 // error
}
}