Skip to content

Commit 7187197

Browse files
nicolasstuckidwijnand
authored andcommitted
Fix #1023: Test that TypeTrees are not in an unexpected tree
1 parent e54a934 commit 7187197

File tree

1 file changed

+100
-0
lines changed

1 file changed

+100
-0
lines changed

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

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,7 @@ class TreeChecker extends Phase with SymTransformer {
306306
sym.isEffectivelyErased && sym.is(Private) && !sym.initial.is(Private)
307307

308308
override def typed(tree: untpd.Tree, pt: Type = WildcardType)(using Context): Tree = {
309+
checkTreeNode(tree)
309310
val tpdTree = super.typed(tree, pt)
310311
Typer.assertPositioned(tree)
311312
if (ctx.erasedTypes)
@@ -646,4 +647,103 @@ object TreeChecker {
646647
tp
647648
}
648649
}.apply(tp0)
650+
651+
private val PrivateErased = allOf(Private, Erased)
652+
653+
/** Check that the tree only contains legal children trees */
654+
def checkTreeNode(tree: untpd.Tree)(implicit ctx: Context): Unit = {
655+
def assertNotTypeTree(t: untpd.Tree): Unit = assert(!t.isInstanceOf[untpd.TypeTree], tree)
656+
657+
// TODO add many more sanity checks
658+
tree match {
659+
// case Ident(name) =>
660+
case Select(qualifier, name) =>
661+
// FIXME this assertion fails only in tests/run/scala2trait-lazyval.scala
662+
// assertNotTypeTree(qual)
663+
// case This(qual) =>
664+
//
665+
// case Super(qual, mix) =>
666+
case Apply(fun, args) =>
667+
assertNotTypeTree(fun)
668+
for (arg <- args) {
669+
assertNotTypeTree(arg)
670+
}
671+
case TypeApply(fun, args) =>
672+
assertNotTypeTree(fun)
673+
// case Literal(const) =>
674+
//
675+
// case New(tpt) =>
676+
case Typed(expr, tpt) =>
677+
assertNotTypeTree(expr)
678+
// case NamedArg(name, arg) =>
679+
case Assign(lhs, rhs) =>
680+
assertNotTypeTree(lhs)
681+
assertNotTypeTree(rhs)
682+
case Block(stats, expr) =>
683+
for (stat <- stats) {
684+
assertNotTypeTree(stat)
685+
}
686+
assertNotTypeTree(expr)
687+
case If(cond, thenp, elsep) =>
688+
assertNotTypeTree(cond)
689+
assertNotTypeTree(thenp)
690+
assertNotTypeTree(elsep)
691+
// case Closure(env, meth, tpt) =>
692+
case Match(selector, cases) =>
693+
assertNotTypeTree(selector)
694+
case CaseDef(pat, guard, body) =>
695+
assertNotTypeTree(guard)
696+
assertNotTypeTree(body)
697+
case Return(expr, from) =>
698+
assertNotTypeTree(expr)
699+
case Try(block, handler, finalizer) =>
700+
assertNotTypeTree(block)
701+
assertNotTypeTree(finalizer)
702+
case SeqLiteral(elems, elemtpt) =>
703+
for (elem <- elems) {
704+
assertNotTypeTree(elem)
705+
}
706+
case Inlined(call, bindings, expansion) =>
707+
assertNotTypeTree(call)
708+
// case TypeTree() =>
709+
//
710+
// case SingletonTypeTree(ref) =>
711+
//
712+
// case AndTypeTree(left, right) =>
713+
//
714+
// case OrTypeTree(left, right) =>
715+
//
716+
// case RefinedTypeTree(tpt, refinements) =>
717+
//
718+
// case AppliedTypeTree(tpt, args) =>
719+
//
720+
// case LambdaTypeTree(tparams, body) =>
721+
//
722+
// case ByNameTypeTree(result) =>
723+
//
724+
// case TypeBoundsTree(lo, hi) =>
725+
//
726+
// case Bind(name, body) =>
727+
//
728+
// case Alternative(trees) =>
729+
//
730+
// case UnApply(fun, implicits, patterns) =>
731+
case tree @ ValDef(name, tpt, _) =>
732+
assertNotTypeTree(tree.rhs)
733+
case tree @ DefDef(name, tparams, vparamss, tpt, _) =>
734+
assertNotTypeTree(tree.rhs)
735+
// case TypeDef(name, rhs) =>
736+
//
737+
// case tree @ Template(constr, parents, self, _) =>
738+
//
739+
// case Import(expr, selectors) =>
740+
//
741+
// case PackageDef(pid, stats) =>
742+
//
743+
// case Annotated(arg, annot) =>
744+
//
745+
// case Thicket(ts) =>
746+
case _ =>
747+
}
748+
}
649749
}

0 commit comments

Comments
 (0)