diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-08-16 14:18:20 +0200 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2013-08-16 14:53:50 +0200 |
commit | 3df1d77fc984b976efa68098206e801cf3b83a9e (patch) | |
tree | b0db025e00cc4748532c1dbdfe105cd1dc77010d | |
parent | f17fb5eaa545490c761acd4f6979a619f919ac86 (diff) | |
download | scala-3df1d77fc984b976efa68098206e801cf3b83a9e.tar.gz scala-3df1d77fc984b976efa68098206e801cf3b83a9e.tar.bz2 scala-3df1d77fc984b976efa68098206e801cf3b83a9e.zip |
SI-7756 Uncripple refchecks in case bodies
In 65340ed4ad2e, parts of RefChecks were disabled when
we traversed into the results of the new pattern matcher.
Similar logic existed for the old pattern matcher, but in
that case the Match / CaseDef nodes still existed in the tree.
The new approach was too broad: important checks no longer
scrutinized the body of cases.
This commit turns the checks back on when it finds the remnants
of a case body, which appears as an application to a label def.
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/RefChecks.scala | 35 | ||||
-rw-r--r-- | test/files/neg/t7756a.check | 7 | ||||
-rw-r--r-- | test/files/neg/t7756a.scala | 11 | ||||
-rw-r--r-- | test/files/neg/t7756b.check | 6 | ||||
-rw-r--r-- | test/files/neg/t7756b.flags | 1 | ||||
-rw-r--r-- | test/files/neg/t7756b.scala | 5 |
6 files changed, 57 insertions, 8 deletions
diff --git a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala index 1b6963b598..30b923e2a1 100644 --- a/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala +++ b/src/compiler/scala/tools/nsc/typechecker/RefChecks.scala @@ -113,6 +113,11 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans var localTyper: analyzer.Typer = typer var currentApplication: Tree = EmptyTree var inPattern: Boolean = false + @inline final def savingInPattern[A](body: => A): A = { + val saved = inPattern + try body finally inPattern = saved + } + var checkedCombinations = Set[List[Type]]() // only one overloaded alternative is allowed to define default arguments @@ -1667,19 +1672,33 @@ abstract class RefChecks extends InfoTransform with scala.reflect.internal.trans case _ => tree } + // skip refchecks in patterns.... result = result match { case CaseDef(pat, guard, body) => - inPattern = true - val pat1 = transform(pat) - inPattern = false + val pat1 = savingInPattern { + inPattern = true + transform(pat) + } treeCopy.CaseDef(tree, pat1, transform(guard), transform(body)) case LabelDef(_, _, _) if treeInfo.hasSynthCaseSymbol(result) => - val old = inPattern - inPattern = true - val res = deriveLabelDef(result)(transform) - inPattern = old - res + savingInPattern { + inPattern = true + deriveLabelDef(result)(transform) + } + case Apply(fun, args) if fun.symbol.isLabel && treeInfo.isSynthCaseSymbol(fun.symbol) => + savingInPattern { + // SI-7756 If we were in a translated pattern, we can now switch out of pattern mode, as the label apply signals + // that we are in the user-supplied code in the case body. + // + // Relies on the translation of: + // (null: Any) match { case x: List[_] => x; x.reverse; case _ => }' + // to: + // <synthetic> val x2: List[_] = (x1.asInstanceOf[List[_]]: List[_]); + // matchEnd4({ x2; x2.reverse}) // case body is an argument to a label apply. + inPattern = false + super.transform(result) + } case _ => super.transform(result) } diff --git a/test/files/neg/t7756a.check b/test/files/neg/t7756a.check new file mode 100644 index 0000000000..8d42717e47 --- /dev/null +++ b/test/files/neg/t7756a.check @@ -0,0 +1,7 @@ +t7756a.scala:7: error: type arguments [Object] do not conform to trait TA's type parameter bounds [X <: CharSequence] + locally(null: TA[Object]) + ^ +t7756a.scala:7: error: type arguments [Object] do not conform to trait TA's type parameter bounds [X <: CharSequence] + locally(null: TA[Object]) + ^ +two errors found diff --git a/test/files/neg/t7756a.scala b/test/files/neg/t7756a.scala new file mode 100644 index 0000000000..4453e84963 --- /dev/null +++ b/test/files/neg/t7756a.scala @@ -0,0 +1,11 @@ +object Test { + def test: Unit = { + trait TA[X <: CharSequence] + 0 match { + case _ => + // the bounds violation isn't reported. RefChecks seems to be too broadly disabled under virtpatmat: see 65340ed4ad2e + locally(null: TA[Object]) + () + } + } +} diff --git a/test/files/neg/t7756b.check b/test/files/neg/t7756b.check new file mode 100644 index 0000000000..2817a7e230 --- /dev/null +++ b/test/files/neg/t7756b.check @@ -0,0 +1,6 @@ +t7756b.scala:3: warning: comparing values of types Int and String using `==' will always yield false + case _ => 0 == "" + ^ +error: No warnings can be incurred under -Xfatal-warnings. +one warning found +one error found diff --git a/test/files/neg/t7756b.flags b/test/files/neg/t7756b.flags new file mode 100644 index 0000000000..85d8eb2ba2 --- /dev/null +++ b/test/files/neg/t7756b.flags @@ -0,0 +1 @@ +-Xfatal-warnings diff --git a/test/files/neg/t7756b.scala b/test/files/neg/t7756b.scala new file mode 100644 index 0000000000..a2de29c8e7 --- /dev/null +++ b/test/files/neg/t7756b.scala @@ -0,0 +1,5 @@ +object Test { + 0 match { + case _ => 0 == "" + } +} |