scala / bug

Scala 2 bug reports only. Please, no questions — proper bug reports only.
https://scala-lang.org
232 stars 21 forks source link

Path dependent type is lost when aliases type parameter and is used in hof #9004

Open scabug opened 9 years ago

scabug commented 9 years ago
object Main extends App {
  trait A[RR] { type R <: RR; def r: R }
  def f1(a: A[_]): a.R = a.r
  def f(a: A[_]): Option[a.R] = Some(1).map(_ => a.r)

  trait AA[RR] { type R = RR; def r: R }
  def f1(a: AA[_]): a.R = a.r
  def f(a: AA[_]): Option[a.R] = Some(1).map(_ => a.r)
 /**
[error]  found   : Int => Any
[error]  required: Int => a.R
[error]     (which expands to)  Int => _$4
[error]   def f(a: AA[_]): Option[a.R] = Some(1).map(_ => a.r)
**/
}
scabug commented 9 years ago

Imported From: https://issues.scala-lang.org/browse/SI-9004?orig=1 Reporter: @OlegYch Affected Versions: 2.10.4, 2.11.4 See #6904

scabug commented 9 years ago

@retronym said: Here's another encoding which works:

def f[R](a: AA[R]): Option[a.R] = Some(1).map(_ => a.r)
scabug commented 9 years ago

@retronym said:

object Main {
  trait AA[RR] { type R = RR; def r: R }

  def test1(a: AA[B] forSome { type B }) = {
    val f = () => a.r
    // The tree a.r is given the type `a.R` which normalizes
    // to B', where B' is a distinct symbol ("captured existential skolem")
    // to substitute for the reference to an existential skolem of B.
    //
    // inference of the result type of the function computes the
    // packed type of tree `a.r` to make sure that terms and types
    // local to the body of the function don't leak into its result
    // type. The captured existential skolem is considered to be local
    // so it is abstracted to its upper bound, Any.
    f: (() => a.R)

    // The workaround is to annotate the function type, rather than
    // relying in inference.
    val g: (() => a.R) () => a.r

    ()
  }
  // typer debug trace: http://rawgit.com/retronym/d5aeaf8e0a4a2e6eef4b/raw/out.html
}
scabug commented 9 years ago

@retronym said: I think I might have a solution: https://github.com/retronym/scala/tree/ticket/9004

I'll see if that change is at odds with the test suite. Assuming it is sound, we'll have to hide it behind -Xsource:2.12.

scabug commented 9 years ago

@retronym said: :*(

Two failing tests on that branch:

test/partest \
  /Users/jason/code/scala3/test/files/pos/t8023.scala \
  /Users/jason/code/scala3/test/files/neg/t5378.scala
scabug commented 9 years ago

@retronym said (edited on Jan 27, 2015 2:52:42 AM UTC): Another test case that crashes under my proposed fix:

object Test {
  def foo1[A1] = new { private type A2 = A1 ; def bar: A2 = ??? }
  // def foo2[A1] = new {         type A2 = A1 ; def bar: A2 = ??? }

  def f1: Int = foo1[Int].bar
  // def f2: Int = foo2[Int].bar
}
scabug commented 9 years ago

@retronym said:

6904 seems related to the problem with dealiasing in packedType.