Skip to content

Fix #2566: Ycheck that New node is always enclosed in a Select #2639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 4, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions compiler/src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,8 @@ class TreeChecker extends Phase with SymTransformer {
val squahsedPhase = ctx.squashed(prevPhase)
ctx.echo(s"checking ${ctx.compilationUnit} after phase ${squahsedPhase}")

assertSelectWrapsNew(ctx.compilationUnit.tpdTree)(ctx)

val checkingCtx = ctx
.fresh
.setMode(Mode.ImplicitsEnabled)
Expand Down Expand Up @@ -449,6 +451,26 @@ class TreeChecker extends Phase with SymTransformer {
tree
}
}

/**
* Checks that `New` nodes are always wrapped inside `Select` nodes.
*/
def assertSelectWrapsNew(tree: tpd.Tree)(implicit ctx: Context): Unit = {
(new TreeAccumulator[tpd.Tree] {
override def apply(parent: tpd.Tree, tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = {
tree match {
case tree: tpd.New if !parent.isInstanceOf[tpd.Select] =>
assert(assertion = false, i"`New` node must be wrapped in a `Select`:\n parent = ${parent.show}\n child = ${tree.show}")
case _: tpd.Annotated =>
// Don't check inside annotations, since they're allowed to contain
// somewhat invalid trees.
case _ =>
foldOver(tree, tree) // replace the parent when folding over the children
}
parent // return the old parent so that my siblings see it
}
})(tpd.EmptyTree, tree)
}
}

object TreeChecker {
Expand Down