-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't capture wildcards if in closure or by-name (#16732)
Fixes scala/bug#9419
- Loading branch information
Showing
5 changed files
with
71 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
trait Magic[S]: | ||
def init: S | ||
def step(s: S): String | ||
|
||
object IntMagic extends Magic[Int]: | ||
def init = 0 | ||
def step(s: Int): String = (s - 1).toString | ||
|
||
object StrMagic extends Magic[String]: | ||
def init = "hi" | ||
def step(s: String): String = s.reverse | ||
|
||
object Main: | ||
def onestep[T](m: () => Magic[T]): String = m().step(m().init) | ||
def unostep[T](m: => Magic[T]): String = m.step(m.init) | ||
|
||
val iter: Iterator[Magic[?]] = Iterator.tabulate(Int.MaxValue)(i => if i % 2 == 0 then IntMagic else StrMagic) | ||
|
||
// was: class java.lang.String cannot be cast to class java.lang.Integer | ||
def main(args: Array[String]): Unit = | ||
onestep(() => iter.next()) // error | ||
unostep(iter.next()) // error | ||
val m = iter.next() | ||
unostep(m) // ok, because m is a value |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
// from failure in the community project | ||
// jackson-module-scala | ||
// in ScalaAnnotationIntrospectorModule.scala:139:12 | ||
|
||
import scala.language.implicitConversions | ||
|
||
trait EnrichedType[X]: | ||
def value: X | ||
|
||
trait ClassW extends EnrichedType[Class[_]]: | ||
def extendsScalaClass = false | ||
|
||
class Test: | ||
implicit def mkClassW(c: => Class[_]): ClassW = new ClassW: | ||
lazy val value = c | ||
|
||
def test1(c1: Class[_]) = c1.extendsScalaClass // ok: c1 is a value | ||
def test2(c2: Class[_]) = mkClassW(c2).extendsScalaClass // ok: c2 is a value | ||
// c1 in test1 goes throw adapting to find the extension method and gains the wildcard capture cast then | ||
// c2 in test2 goes straight to typedArg, as it's already an arg, so it never gets wildcard captured |