Navigation Menu

Skip to content

Commit

Permalink
SI-7226 Fix inference regression caused by TypeVar equality.
Browse files Browse the repository at this point in the history
TypeVars, being mutable creatures, mustn't have structural
equality/hashing, otherwise TypeRefs that differ only by
having distinct TypeVars as components get wrongly uniqued
together.

The reported bug showed the disaterous consequences: constraints
from the `C?[Int]` in the return type applied to the `?C[?A]` in
the parameter list.

This commit overrides `equals` and `hashCode` in `TypeVar`
to use reference equality. An alternative fix would be to drop
the `case`-ness of the class, as was the case before 0cde930
when this regressed.
  • Loading branch information
retronym committed Mar 9, 2013
1 parent 2ff7650 commit 7e52fb9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/reflect/scala/reflect/internal/Types.scala
Expand Up @@ -3052,6 +3052,12 @@ trait Types extends api.Types { self: SymbolTable =>
val origin: Type,
var constr: TypeConstraint
) extends Type {

// We don't want case class equality/hashing as TypeVar-s are mutable,
// and TypeRefs based on them get wrongly `uniqued` otherwise. See SI-7226.
override def hashCode(): Int = System.identityHashCode(this)
override def equals(other: Any): Boolean = this eq other.asInstanceOf[AnyRef]

def untouchable = false // by other typevars
override def params: List[Symbol] = Nil
override def typeArgs: List[Type] = Nil
Expand Down
26 changes: 26 additions & 0 deletions test/files/pos/t7226.scala
@@ -0,0 +1,26 @@
trait HK {
type Rep[X]

// okay
def unzip2[A, B](ps: Rep[List[(A, B)]])
unzip2(null.asInstanceOf[Rep[List[(Int, String)]]])

// okay
def unzipHK[A, B, C[_]](ps: Rep[C[(A, B)]])
unzipHK(null.asInstanceOf[Rep[List[(Int, String)]]])

def unzipHKRet0[A, C[_]](ps: C[A]): C[Int]
def ls: List[String]
unzipHKRet0(ls)

// fail
def unzipHKRet[A, C[_]](ps: Rep[C[A]]): Rep[C[Int]]
def rls: Rep[List[String]]
unzipHKRet(rls)
}

trait HK1 {
type Rep[A]
def unzip1[A, B, C[_]](ps: Rep[C[(A, B)]]): (Rep[C[A]], Rep[C[B]])
def doUnzip1[A, B](ps: Rep[List[(A, B)]]) = unzip1(ps)
}

0 comments on commit 7e52fb9

Please sign in to comment.