From cff09340327ddcfd3d39aca69dfb719e7a501b5f Mon Sep 17 00:00:00 2001 From: Jason Zaugg Date: Sun, 20 Jan 2013 01:12:58 +0100 Subject: Ill-scoped reference checking in TreeCheckers Find trees which have an info referring to an out-of-scope type parameter or local symbol, as could happen in the test for SI-6981, in which tree transplanting did not substitute symbols in symbol infos. The enclosed, pending test for that bug that will now fail under -Ycheck:extmethods -Xfatal-warnings. [Now checking: extmethods] [check: extmethods] The symbol, tpe or info of tree `(@scala.annotation.tailrec def loop(x: A): Unit = loop(x)) : (x: A)Unit` refers to a out-of-scope symbol, type A in class Foo. tree.symbol.ownerChain: method loop, method bippy$extension, object Foo, object O, package , package [check: extmethods] The symbol, tpe or info of tree `(val x: A = _) : A` refers to a out-of-scope symbol, type A in class Foo. tree.symbol.ownerChain: value x, method loop, method bippy$extension, object Foo, object O, package , package [check: extmethods] The symbol, tpe or info of tree `(loop(x)) : (x: A)Unit` refers to a out-of-scope symbol, type A in class Foo. tree.symbol.ownerChain: method loop, method bippy$extension, object Foo, object O, package , package [check: extmethods] The symbol, tpe or info of tree `(loop) : (x: A)Unit` refers to a out-of-scope symbol, type A in class Foo. tree.symbol.ownerChain: method loop, method bippy$extension, object Foo, object O, package , package [check: extmethods] The symbol, tpe or info of tree `(x) : A` refers to a out-of-scope symbol, type A in class Foo. tree.symbol.ownerChain: value x, method loop, method bippy$extension, object Foo, object O, package , package [check: extmethods] The symbol, tpe or info of tree `( val x2: O.Foo[A] = (x1.asInstanceOf[O.Foo[A]]: O.Foo[A])) : O.Foo[A]` refers to a out-of-scope symbol, type A in class Foo. tree.symbol.ownerChain: value x2, method equals$extension, object Foo, object O, package , package [check: extmethods] The symbol, tpe or info of tree `( val Foo$1: O.Foo[A] = x$1.asInstanceOf[O.Foo[A]]) : O.Foo[A]` refers to a out-of-scope symbol, type A in class Foo. tree.symbol.ownerChain: value Foo$1, method equals$extension, object Foo, object O, package , package [check: extmethods] The symbol, tpe or info of tree `(Foo$1) : O.Foo[A]` refers to a out-of-scope symbol, type A in class Foo. tree.symbol.ownerChain: value Foo$1, method equals$extension, object Foo, object O, package , package error: TreeCheckers detected non-compliant trees in t6891.scala one error found --- .../scala/tools/nsc/typechecker/TreeCheckers.scala | 45 +++++++++++++++++++++- 1 file changed, 43 insertions(+), 2 deletions(-) (limited to 'src/compiler') diff --git a/src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala b/src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala index 48a5a36b00..c5c3c560ea 100644 --- a/src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/TreeCheckers.scala @@ -117,7 +117,8 @@ abstract class TreeCheckers extends Analyzer { try p.source.path + ":" + p.line catch { case _: UnsupportedOperationException => p.toString } - def errorFn(msg: Any): Unit = println("[check: %s] %s".format(phase.prev, msg)) + private var hasError: Boolean = false + def errorFn(msg: Any): Unit = {hasError = true; println("[check: %s] %s".format(phase.prev, msg))} def errorFn(pos: Position, msg: Any): Unit = errorFn(posstr(pos) + ": " + msg) def informFn(msg: Any) { if (settings.verbose.value || settings.debug.value) @@ -151,6 +152,7 @@ abstract class TreeCheckers extends Analyzer { result } def runWithUnit[T](unit: CompilationUnit)(body: => Unit): Unit = { + hasError = false val unit0 = currentUnit currentRun.currentUnit = unit body @@ -169,6 +171,7 @@ abstract class TreeCheckers extends Analyzer { checker.precheck.traverse(unit.body) checker.typed(unit.body) checker.postcheck.traverse(unit.body) + if (hasError) unit.warning(NoPosition, "TreeCheckers detected non-compliant trees in " + unit) } } @@ -217,8 +220,11 @@ abstract class TreeCheckers extends Analyzer { case _ => () } - object precheck extends Traverser { + object precheck extends TreeStackTraverser { override def traverse(tree: Tree) { + checkSymbolRefsRespectScope(tree) + checkReturnReferencesDirectlyEnclosingDef(tree) + val sym = tree.symbol def accessed = sym.accessed def fail(msg: String) = errorFn(tree.pos, msg + classstr(tree) + " / " + tree) @@ -289,6 +295,41 @@ abstract class TreeCheckers extends Analyzer { } super.traverse(tree) } + + private def checkSymbolRefsRespectScope(tree: Tree) { + def symbolOf(t: Tree): Symbol = Option(tree.symbol).getOrElse(NoSymbol) + def definedSymbolOf(t: Tree): Symbol = if (t.isDef) symbolOf(t) else NoSymbol + val info = Option(symbolOf(tree).info).getOrElse(NoType) + val referencedSymbols: List[Symbol] = { + val directRef = tree match { + case _: RefTree => symbolOf(tree).toOption + case _ => None + } + def referencedSyms(tp: Type) = (tp collect { + case TypeRef(_, sym, _) => sym + }).toList + val indirectRefs = referencedSyms(info) + (indirectRefs ++ directRef).distinct + } + for { + sym <- referencedSymbols + if (sym.isTypeParameter || sym.isLocal) && !(tree.symbol hasTransOwner sym.owner) + } errorFn(s"The symbol, tpe or info of tree `(${tree}) : ${info}` refers to a out-of-scope symbol, ${sym.fullLocationString}. tree.symbol.ownerChain: ${tree.symbol.ownerChain.mkString(", ")}") + } + + private def checkReturnReferencesDirectlyEnclosingDef(tree: Tree) { + tree match { + case _: Return => + path.collectFirst { + case dd: DefDef => dd + } match { + case None => errorFn(s"Return node ($tree) must be enclosed in a DefDef") + case Some(dd) => + if (tree.symbol != dd.symbol) errorFn(s"Return symbol (${tree.symbol}} does not reference directly enclosing DefDef (${dd.symbol})") + } + case _ => + } + } } object postcheck extends Traverser { -- cgit v1.2.3