Skip to content

Commit 92b2561

Browse files
authored
Merge pull request #2639 from abeln/new-select
Fix #2566: Ycheck that `New` node is always enclosed in a `Select`
2 parents 49baa86 + a79cf5f commit 92b2561

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

compiler/src/dotty/tools/dotc/transform/TreeChecker.scala

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ class TreeChecker extends Phase with SymTransformer {
122122
val squahsedPhase = ctx.squashed(prevPhase)
123123
ctx.echo(s"checking ${ctx.compilationUnit} after phase ${squahsedPhase}")
124124

125+
assertSelectWrapsNew(ctx.compilationUnit.tpdTree)(ctx)
126+
125127
val checkingCtx = ctx
126128
.fresh
127129
.setMode(Mode.ImplicitsEnabled)
@@ -449,6 +451,26 @@ class TreeChecker extends Phase with SymTransformer {
449451
tree
450452
}
451453
}
454+
455+
/**
456+
* Checks that `New` nodes are always wrapped inside `Select` nodes.
457+
*/
458+
def assertSelectWrapsNew(tree: tpd.Tree)(implicit ctx: Context): Unit = {
459+
(new TreeAccumulator[tpd.Tree] {
460+
override def apply(parent: tpd.Tree, tree: tpd.Tree)(implicit ctx: Context): tpd.Tree = {
461+
tree match {
462+
case tree: tpd.New if !parent.isInstanceOf[tpd.Select] =>
463+
assert(assertion = false, i"`New` node must be wrapped in a `Select`:\n parent = ${parent.show}\n child = ${tree.show}")
464+
case _: tpd.Annotated =>
465+
// Don't check inside annotations, since they're allowed to contain
466+
// somewhat invalid trees.
467+
case _ =>
468+
foldOver(tree, tree) // replace the parent when folding over the children
469+
}
470+
parent // return the old parent so that my siblings see it
471+
}
472+
})(tpd.EmptyTree, tree)
473+
}
452474
}
453475

454476
object TreeChecker {

0 commit comments

Comments
 (0)