@@ -16,14 +16,6 @@ class CheckPhantomCast extends MiniPhaseTransform { thisTransformer =>
16
16
17
17
override def phaseName = " checkPhantomCast"
18
18
19
- override def checkPostCondition (tree : tpd.Tree )(implicit ctx : Context ): Unit = {
20
- tree match {
21
- case TypeApply (fun, targs) if fun.symbol eq defn.Any_asInstanceOf => assert(! containsPhantom(targs.head.tpe))
22
- case Bind (_, Typed (_, tpt)) => assert(! containsPhantom(tpt.tpe))
23
- case _ =>
24
- }
25
- }
26
-
27
19
override def transformTypeApply (tree : tpd.TypeApply )(implicit ctx : Context , info : TransformerInfo ): tpd.Tree = {
28
20
if (tree.fun.symbol eq defn.Any_asInstanceOf )
29
21
checkNoPhantoms(tree.args.head)
@@ -39,12 +31,13 @@ class CheckPhantomCast extends MiniPhaseTransform { thisTransformer =>
39
31
}
40
32
41
33
private def checkNoPhantoms (tpTree : tpd.Tree )(implicit ctx : Context ): Unit = {
42
- if (containsPhantom(tpTree.tpe))
43
- ctx.error(" Cannot cast type containing a phantom type" , tpTree.pos)
34
+ val checker = new TypeTraverser () {
35
+ override def traverse (tp : Type ): Unit = {
36
+ if (tp.isPhantom) ctx.error(" Cannot cast type containing a phantom type" , tpTree.pos)
37
+ else traverseChildren(tp)
38
+ }
39
+ }
40
+ checker.traverse(tpTree.tpe)
44
41
}
45
42
46
- private def containsPhantom (tp : Type )(implicit ctx : Context ): Boolean = new TypeAccumulator [Boolean ] {
47
- override def apply (x : Boolean , tp : Type ): Boolean = x || tp.isPhantom || foldOver(false , tp)
48
- }.apply(x = false , tp)
49
-
50
43
}
0 commit comments