bug icon indicating copy to clipboard operation
bug copied to clipboard

Invalid return type when using default method parameters on non-stable instances

Open scabug opened this issue 9 years ago • 6 comments

The following code fails to compile.

The failure happens when a method has a default parameter, has a return type depending on one of its parameters; when the method itself is not called from a stable identifier.

See code below, the failure pattern is the same for all Scala version affected.

trait Container[S <: Seq[Int] with Singleton] {
  val s: S
}

trait Builder {
  def withoutDefault(s0: Seq[Int]): Container[s0.type] = new Container[s0.type] {
    val s: s0.type = s0
  }
  def withDefault(s0: Seq[Int], i: Int = 0): Container[s0.type] = withoutDefault(s0)
}

object Builder {
  val instance1 = new Builder { }
  def instance2 = new Builder { }
}

object Builder1 {
  def withoutDefault(s0: Seq[Int]): Container[s0.type] = new Container[s0.type] {
    val s: s0.type = s0
  }
  def withDefault(s0: Seq[Int], i: Int = 0): Container[s0.type] = withoutDefault(s0)
}

object Test {
  val seq = Seq(1, 2, 3, 4)
  val works1: Container[seq.type] = Builder1.withoutDefault(seq)
  val works2: Container[seq.type] = Builder1.withDefault(seq, 0)
  val works3: Container[seq.type] = Builder1.withDefault(seq)

  val works4: Container[seq.type] = Builder.instance1.withoutDefault(seq)
  val works5: Container[seq.type] = Builder.instance1.withDefault(seq, 0)
  val works6: Container[seq.type] = Builder.instance1.withDefault(seq)

  val works7: Container[seq.type] = Builder.instance2.withoutDefault(seq)
  val works8: Container[seq.type] = Builder.instance2.withDefault(seq, 0)
  val fails: Container[seq.type] = Builder.instance2.withDefault(seq)
}

scabug avatar Jan 07 '16 11:01 scabug

Imported From: https://issues.scala-lang.org/browse/SI-9611?orig=1 Reporter: Denis Rosset (denisrosset) Affected Versions: 2.10.6, 2.11.8, 2.12.0-RC1

scabug avatar Jan 07 '16 11:01 scabug

@SethTisue said: what's the compiler say?

scabug avatar Sep 29 '16 18:09 scabug

Denis Rosset (denisrosset) said:

[error] /home/denis/Documents/test/Test.scala:36: type mismatch;
[error]  found   : Container[x$1.type]
[error]  required: Container[Test.seq.type]
[error]   val fails: Container[seq.type] = Builder.instance2.withDefault(seq)

scabug avatar Sep 30 '16 04:09 scabug

Denis Rosset (denisrosset) said: BTW, this is on 2.10.6 and 2.11.8, and also on 2.12.0-RC1.

scabug avatar Sep 30 '16 04:09 scabug

compiles on Scala 3.6.3

SethTisue avatar Jan 31 '25 16:01 SethTisue

There was a similar issue with temp vars for either rassoc block or stabilizers. (Or maybe it was something I broke while poking one of those features.)

som-snytt avatar Jan 31 '25 17:01 som-snytt