Skip to content

Commit d681a78

Browse files
committed
suppress checking of for, try and partial functions
1 parent fd1b286 commit d681a78

File tree

12 files changed

+69
-16
lines changed

12 files changed

+69
-16
lines changed

src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -616,16 +616,20 @@ object desugar {
616616
*
617617
* { cases }
618618
* ==>
619-
* x$1 => x$1 match { cases }
619+
* x$1 => (x$1 @unchecked) match { cases }
620620
*
621621
* If `nparams` != 1, expand instead to
622622
*
623-
* (x$1, ..., x$n) => (x$0, ..., x${n-1}) match { cases }
623+
* (x$1, ..., x$n) => (x$0, ..., x${n-1} @unchecked) match { cases }
624624
*/
625-
def makeCaseLambda(cases: List[CaseDef], nparams: Int = 1)(implicit ctx: Context) = {
625+
def makeCaseLambda(cases: List[CaseDef], nparams: Int = 1, unchecked: Boolean = true)(implicit ctx: Context) = {
626626
val params = (1 to nparams).toList.map(makeSyntheticParameter(_))
627627
val selector = makeTuple(params.map(p => Ident(p.name)))
628-
Function(params, Match(selector, cases))
628+
629+
if (unchecked)
630+
Function(params, Match(Annotated(New(ref(defn.UncheckedAnnotType)), selector), cases))
631+
else
632+
Function(params, Match(selector, cases))
629633
}
630634

631635
/** Map n-ary function `(p1, ..., pn) => body` where n != 1 to unary function as follows:
@@ -753,7 +757,7 @@ object desugar {
753757
case VarPattern(named, tpt) =>
754758
Function(derivedValDef(named, tpt, EmptyTree, Modifiers(Param)) :: Nil, body)
755759
case _ =>
756-
makeCaseLambda(CaseDef(pat, EmptyTree, body) :: Nil)
760+
makeCaseLambda(CaseDef(pat, EmptyTree, body) :: Nil, unchecked = false)
757761
}
758762

759763
/** If `pat` is not an Identifier, a Typed(Ident, _), or a Bind, wrap
@@ -799,7 +803,7 @@ object desugar {
799803
val cases = List(
800804
CaseDef(pat, EmptyTree, Literal(Constant(true))),
801805
CaseDef(Ident(nme.WILDCARD), EmptyTree, Literal(Constant(false))))
802-
Apply(Select(rhs, nme.withFilter), Match(EmptyTree, cases))
806+
Apply(Select(rhs, nme.withFilter), makeCaseLambda(cases))
803807
}
804808

805809
/** Is pattern `pat` irrefutable when matched against `rhs`?

src/dotty/tools/dotc/transform/ExpandSAMs.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,8 @@ class ExpandSAMs extends MiniPhaseTransform { thisTransformer =>
7474
Bind(defaultSym, Underscore(selector.tpe.widen)),
7575
EmptyTree,
7676
Literal(Constant(false)))
77-
cpy.Match(applyRhs)(paramRef, cases.map(translateCase) :+ defaultCase)
77+
val annotated = Annotated(New(ref(defn.UncheckedAnnotType)), paramRef)
78+
cpy.Match(applyRhs)(annotated, cases.map(translateCase) :+ defaultCase)
7879
case _ =>
7980
tru
8081
}

src/dotty/tools/dotc/transform/patmat/Space.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,7 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
258258
case UnApply(_, _, pats) =>
259259
if (pat.tpe.classSymbol.is(CaseClass))
260260
Kon(pat.tpe.stripAnnots, pats.map(pat => project(pat, roundUp)))
261-
else if (roundUp) Typ(pat.tpe, false)
261+
else if (roundUp) Typ(pat.tpe.stripAnnots, false)
262262
else Empty
263263
case Typed(pat @ UnApply(_, _, _), _) => project(pat)
264264
case Typed(expr, _) => Typ(expr.tpe.stripAnnots, true)
@@ -392,9 +392,9 @@ class SpaceEngine(implicit ctx: Context) extends SpaceLogic {
392392
case _ => tp.show.stripSuffix("$")
393393
}
394394

395-
val text = tp match {
395+
val text = tp.stripAnnots match {
396396
case tp: OrType => showType(tp.tp1) + " | " + showType(tp.tp2)
397-
case _ => refine(tp)
397+
case tp => refine(tp)
398398
}
399399

400400
if (text.isEmpty) enclosingCls.show.stripSuffix("$")

src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ trait TypeAssigner {
470470
tree.withType(sym.nonMemberTermRef)
471471

472472
def assignType(tree: untpd.Annotated, annot: Tree, arg: Tree)(implicit ctx: Context) =
473-
tree.withType(AnnotatedType(arg.tpe, Annotation(annot)))
473+
tree.withType(AnnotatedType(arg.tpe.widen, Annotation(annot)))
474474

475475
def assignType(tree: untpd.PackageDef, pid: Tree)(implicit ctx: Context) =
476476
tree.withType(pid.symbol.valRef)

src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
719719
tree.selector match {
720720
case EmptyTree =>
721721
val (protoFormals, _) = decomposeProtoFunction(pt, 1)
722-
typed(desugar.makeCaseLambda(tree.cases, protoFormals.length) withPos tree.pos, pt)
722+
val unchecked = pt <:< defn.PartialFunctionType
723+
typed(desugar.makeCaseLambda(tree.cases, protoFormals.length, unchecked) withPos tree.pos, pt)
723724
case _ =>
724725
val sel1 = typedExpr(tree.selector)
725726
val selType = widenForMatchSelector(

test/test/transform/PatmatExhaustivityTest.scala

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@ import dotty.tools.dotc.reporting.ConsoleReporter
1010

1111
class PatmatExhaustivityTest {
1212
val testsDir = "./tests/patmat"
13-
val options = "-Ystop-after:splitter" // no need for code generation
14-
// patmatexhaust-huge.scala crash compiler
13+
// stop-after: patmatexhaust-huge.scala crash compiler
14+
val options = List("-Ystop-after:splitter")
1515

1616
private def compileFile(file: File) = {
1717
val stringBuffer = new StringWriter()
1818
val reporter = new ConsoleReporter(writer = new PrintWriter(stringBuffer))
1919

2020
try {
21-
Main.process(Array(file.getPath, options), reporter, null)
21+
Main.process((file.getPath::options).toArray, reporter, null)
2222
} catch {
2323
case e: Throwable =>
2424
println(s"Compile $file exception:")
@@ -45,7 +45,7 @@ class PatmatExhaustivityTest {
4545
.map(_.jfile.getPath)
4646

4747
try {
48-
Main.process((options::files).toArray, reporter, null)
48+
Main.process((options ++ files).toArray, reporter, null)
4949
} catch {
5050
case e: Throwable =>
5151
println(s"Compile $file exception:")

tests/patmat/for.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
def foo[A, B](l: List[(A, B)]): List[A] = {
3+
for ((a, b) <- l) yield a
4+
}
5+
}

tests/patmat/i947.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
./tests/patmat/i947.scala:10: warning: unreachable code
2+
case ys: List[d18383] => false
3+
^
4+
one warning found

tests/patmat/i947.scala

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
object Test {
2+
3+
class c {
4+
5+
private var x: Int = 0
6+
7+
override def equals(other: Any) = other match {
8+
case o: c => x == o.x
9+
case xs: List[c] => false
10+
case ys: List[d18383] => false
11+
case _ => false
12+
}
13+
14+
15+
}
16+
}

tests/patmat/partial-function.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
sealed abstract class TA
2+
sealed abstract class TB extends TA
3+
case object B extends TB
4+
case object B2 extends TB
5+
6+
case class CC(i: Int, tb: TB)
7+
8+
object Test {
9+
def foo: PartialFunction[CC, Unit] = {
10+
case CC(_, B) => ()
11+
}
12+
}

tests/patmat/t1056.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
type T = PartialFunction[String,String]
3+
def g(h: T) = ()
4+
g({case s: String => s})
5+
}

tests/patmat/try.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
object Test {
2+
try 2/0 catch {
3+
case e: Exception =>
4+
}
5+
}

0 commit comments

Comments
 (0)