-
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.
Make unchecked cases non-
@unchecked
and non-unreachable
- Loading branch information
Showing
12 changed files
with
187 additions
and
27 deletions.
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
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,28 @@ | ||
-- Error: tests/neg/i16451.scala:20:9 ---------------------------------------------------------------------------------- | ||
20 | case x: Wrapper[Color.Red.type] => Some(x) // error | ||
| ^ | ||
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color] | ||
-- Error: tests/neg/i16451.scala:21:9 ---------------------------------------------------------------------------------- | ||
21 | case x: Wrapper[Color.Green.type] => None // error | ||
| ^ | ||
|the type test for Wrapper[(Color.Green : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color] | ||
-- Error: tests/neg/i16451.scala:28:9 ---------------------------------------------------------------------------------- | ||
28 | case x: Wrapper[Color.Red.type] => Some(x) // error | ||
| ^ | ||
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Any | ||
-- Error: tests/neg/i16451.scala:32:9 ---------------------------------------------------------------------------------- | ||
32 | case x: Wrapper[Color.Red.type] => Some(x) // error | ||
| ^ | ||
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color] | ||
-- Error: tests/neg/i16451.scala:36:9 ---------------------------------------------------------------------------------- | ||
36 | case x: Wrapper[Color.Red.type] => Some(x) // error | ||
| ^ | ||
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from A1 | ||
-- Error: tests/neg/i16451.scala:41:11 --------------------------------------------------------------------------------- | ||
41 | case x: Wrapper[Color.Red.type] => x // error | ||
| ^ | ||
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color] | ||
-- Error: tests/neg/i16451.scala:46:11 --------------------------------------------------------------------------------- | ||
46 | case x: Wrapper[Color.Red.type] => x // error | ||
| ^ | ||
|the type test for Wrapper[(Color.Red : Color)] cannot be checked at runtime because its type arguments can't be determined from Wrapper[Color] |
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,51 @@ | ||
// scalac: -Werror | ||
enum Color: | ||
case Red, Green | ||
//sealed trait Color | ||
//object Color: | ||
// case object Red extends Color | ||
// case object Green extends Color | ||
|
||
case class Wrapper[A](value: A) | ||
//object Wrapper: | ||
//import scala.reflect.TypeTest | ||
//given TypeTest[Wrapper[Color], Wrapper[Color.Red.type]] with | ||
// def unapply(x: Wrapper[Color]): Option[x.type & Wrapper[Color.Red.type]] = | ||
// if x.value == Color.Red then | ||
// Some(x.asInstanceOf[x.type & Wrapper[Color.Red.type]]) | ||
// else None | ||
|
||
object Test: | ||
def test_correct(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match | ||
case x: Wrapper[Color.Red.type] => Some(x) // error | ||
case x: Wrapper[Color.Green.type] => None // error | ||
|
||
def test_different(x: Wrapper[Color]): Option[Wrapper[Color]] = x match | ||
case x @ Wrapper(_: Color.Red.type) => Some(x) | ||
case x @ Wrapper(_: Color.Green.type) => None | ||
|
||
def test_any(x: Any): Option[Wrapper[Color.Red.type]] = x match | ||
case x: Wrapper[Color.Red.type] => Some(x) // error | ||
case _ => None | ||
|
||
def test_wrong(x: Wrapper[Color]): Option[Wrapper[Color.Red.type]] = x match | ||
case x: Wrapper[Color.Red.type] => Some(x) // error | ||
case _ => None | ||
|
||
def t2[A1 <: Wrapper[Color]](x: A1): Option[Wrapper[Color.Red.type]] = x match | ||
case x: Wrapper[Color.Red.type] => Some(x) // error | ||
case _ => None | ||
|
||
def test_wrong_seq(xs: Seq[Wrapper[Color]]): Seq[Wrapper[Color.Red.type]] = | ||
xs.collect { | ||
case x: Wrapper[Color.Red.type] => x // error | ||
} | ||
|
||
def test_wrong_seq2(xs: Seq[Wrapper[Color]]): Seq[Wrapper[Color.Red.type]] = | ||
xs.collect { x => x match | ||
case x: Wrapper[Color.Red.type] => x // error | ||
} | ||
|
||
def main(args: Array[String]): Unit = | ||
println(test_wrong_seq(Seq(Wrapper(Color.Red), Wrapper(Color.Green)))) | ||
// outputs: List(Wrapper(Red), Wrapper(Green)) |
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 @@ | ||
4: Pattern Match |
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,34 @@ | ||
// scalac: -Werror | ||
abstract class Namer: | ||
private enum CanForward: | ||
case Yes | ||
case No(whyNot: String) | ||
case Skip // for members that have never forwarders | ||
|
||
class Mbr | ||
private def canForward(mbr: Mbr): CanForward = CanForward.Yes | ||
|
||
private def applyOrElse[A1 <: CanForward, B1 >: String](x: A1, default: A1 => B1): B1 = x match | ||
case CanForward.No(whyNot @ _) => whyNot | ||
case _ => "" | ||
|
||
def addForwardersNamed(mbrs: List[Mbr]) = | ||
val reason = mbrs.map(canForward).collect { | ||
case CanForward.No(whyNot) => whyNot | ||
}.headOption.getOrElse("") | ||
|
||
class ClassCompleter: | ||
def addForwardersNamed(mbrs: List[Mbr]) = | ||
val reason = mbrs.map(canForward).collect { | ||
case CanForward.No(whyNot) => whyNot | ||
}.headOption.getOrElse("") | ||
|
||
private def exportForwarders = | ||
def addForwardersNamed(mbrs: List[Mbr]) = | ||
val reason = mbrs.map(canForward).collect { | ||
case CanForward.No(whyNot) => whyNot | ||
}.headOption.getOrElse("") | ||
if mbrs.size == 4 then | ||
val reason = mbrs.map(canForward).collect { | ||
case CanForward.No(whyNot) => whyNot | ||
}.headOption.getOrElse("") |
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,15 @@ | ||
// scalac: -Werror | ||
object DiffUtil: | ||
private sealed trait Patch | ||
private final case class Unmodified(str: String) extends Patch | ||
private final case class Modified(original: String, str: String) extends Patch | ||
private final case class Deleted(str: String) extends Patch | ||
private final case class Inserted(str: String) extends Patch | ||
|
||
private def test(diff: Array[Patch]) = | ||
diff.collect { | ||
case Unmodified(str) => str | ||
case Inserted(str) => s"+$str" | ||
case Modified(orig, str) => s"{$orig,$str}" | ||
case Deleted(str) => s"-$str" | ||
}.mkString |
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,22 @@ | ||
// scalac: -Werror | ||
|
||
import java.lang.reflect.* | ||
import scala.annotation.tailrec | ||
|
||
class Test: | ||
@tailrec private def unwrapThrowable(x: Throwable): Throwable = x match { | ||
case _: InvocationTargetException | // thrown by reflectively invoked method or constructor | ||
_: ExceptionInInitializerError | // thrown when running a static initializer (e.g. a scala module constructor) | ||
_: UndeclaredThrowableException | // invocation on a proxy instance if its invocation handler's `invoke` throws an exception | ||
_: ClassNotFoundException | // no definition for a class instantiated by name | ||
_: NoClassDefFoundError // the definition existed when the executing class was compiled, but can no longer be found | ||
if x.getCause != null => | ||
unwrapThrowable(x.getCause) | ||
case _ => x | ||
} | ||
|
||
private def unwrapHandler[T](pf: PartialFunction[Throwable, T]): PartialFunction[Throwable, T] = | ||
pf.compose({ case ex => unwrapThrowable(ex) }) | ||
|
||
def test = | ||
unwrapHandler({ case ex => throw ex }) |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
scala.List.apply[scala.PartialFunction[scala.Int, scala.Predef.String]](((x$1: scala.Int) => (x$1: @scala.unchecked) match { | ||
scala.List.apply[scala.PartialFunction[scala.Int, scala.Predef.String]](((x$1: scala.Int) => x$1 match { | ||
case 1 => | ||
"x" | ||
})) |