Skip to content
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

Stack overflow error with higher kinded contravariant type parameter #4889

Closed
scabug opened this issue Aug 8, 2011 · 3 comments
Closed

Stack overflow error with higher kinded contravariant type parameter #4889

scabug opened this issue Aug 8, 2011 · 3 comments

Comments

@scabug
Copy link

scabug commented Aug 8, 2011

The following will crash the compiler with a stack overflow,

trait Matrix[S, +Repr[s] <: Matrix[s, Repr]] {
  def foo[R[S] >: Repr[S]](implicit ma1: MatrixAdder[S, R]) {}
}

trait SparseMatrix[S] extends Matrix[S, SparseMatrix]

trait MatrixAdder[S, -R[_]] {
  def addTo(m: R[S])
}

implicit def adderImplicit[S, R[s] <: Matrix[s, R]] = new MatrixAdder[S, R] {
  def addTo(m: R[S]) = { }
}

val m1 = new SparseMatrix[Int] { }
m1.foo // crasher

The top of the stack trace is,

java.lang.StackOverflowError
	at scala.tools.nsc.symtab.Symbols$ClassSymbol.typeOfThis(Symbols.scala:2074)
	at scala.tools.nsc.symtab.Types$ThisType.underlying(Types.scala:1122)
	at scala.tools.nsc.symtab.Types$SimpleTypeProxy$class.boundSyms(Types.scala:189)
	at scala.tools.nsc.symtab.Types$SingletonType.boundSyms(Types.scala:1040)
	at scala.tools.nsc.symtab.Types$SubstMap.apply(Types.scala:3557)
	at scala.tools.nsc.symtab.Types$SubstSymMap.apply(Types.scala:3603)
	at scala.tools.nsc.symtab.Types$SubstSymMap.apply(Types.scala:3584)
...

The crash goes away, and the the program compiles, when the contravariance is removed from the R type parameter in trait MatrixAdder.

If the type parameter S is removed throughout, the crash goes away, but the program fails to compile due to adderImplicit not being found. Is this correct behavior?

Thanks.

@scabug
Copy link
Author

scabug commented Aug 8, 2011

Imported From: https://issues.scala-lang.org/browse/SI-4889?orig=1
Reporter: Kipton Barros (kbarros)
Affected Versions: 2.11.0-M8

@scabug
Copy link
Author

scabug commented Aug 15, 2011

@paulp said:
I think it should be incorrect by definition, that the following code fails to compile with the contravariant R, but succeeds with an invariant R.

trait MatrixAdder[-R] { def addTo(m: R) }
trait Matrix[+Repr <: Matrix[Repr]] {
  def foo[R >: Repr](implicit ma1: MatrixAdder[R]) {}
}
trait SparseMatrix extends Matrix[SparseMatrix] { }

object Test {
  implicit def adderImplicit[R <: Matrix[R]] = new MatrixAdder[R] {
    def addTo(m: R) = { }
  }

  val m1 = new SparseMatrix { }
  m1.foo // crasher
}

There are a ton of open and closed tickets on contravariance and implicits. Suffice to say that there are problems, and there is not yet universal agreement on how to fix them or who might do the fixing.

@scabug scabug added this to the Backlog milestone Apr 7, 2017
@scala scala deleted a comment from scabug Mar 3, 2018
@diesalbla
Copy link

diesalbla commented Dec 24, 2018

This still crashes the compiler with Scala 2.12.7, but it has been fixed in the current branch 2.13.x. Which is to say, that compiling the program in the bug description no longer crashes the compiler. However, the compiler rejects the program, because it can not resolve the implicit in the call to .foo.

I am not sure if I, per Paul Philipps comment, the compiler should accept and compile the program or not.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants