Skip to content

Commit 25acecb

Browse files
committed
Add phantom 'class' and 'abstract class' support.
1 parent c05cc31 commit 25acecb

File tree

2 files changed

+52
-41
lines changed

2 files changed

+52
-41
lines changed

src/dotty/tools/dotc/transform/PhantomDeclErasure.scala

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,13 @@ class PhantomDeclErasure extends MiniPhaseTransform with InfoTransformer {
3434

3535
tree match {
3636
case _: TypeTree =>
37-
case _: Trees.TypeDef[_] =>
38-
case tree: TypeDef => tree.symbol.asClass.classInfo.decls // TODO check decls
3937
case ValDef(_, tpt, _) => assertNotPhantom(tpt.typeOpt)
4038
case DefDef(_, _, _, tpt, _) => assertNotPhantom(tpt.typeOpt)
39+
case tree: Trees.TypeDef[_] =>
40+
// tree.symbol.asClass.classInfo.decls // TODO check decls
4141
case _ =>
4242
}
43+
4344
super.checkPostCondition(tree)
4445
}
4546

@@ -60,6 +61,9 @@ class PhantomDeclErasure extends MiniPhaseTransform with InfoTransformer {
6061
}
6162
!isPhantom
6263

64+
case tree @ TypeDef(_, _) =>
65+
!tree.tpe.derivesFrom(defn.PhantomAnyClass)
66+
6367
case _ =>
6468
true
6569
}
@@ -73,8 +77,8 @@ class PhantomDeclErasure extends MiniPhaseTransform with InfoTransformer {
7377
private def erasedPhantoms(tp: Type)(implicit ctx: Context): Type = {
7478
val flags = tp.typeSymbol.flags
7579
tp match {
76-
case tp: ClassInfo if !flags.is(Flags.Package) =>
77-
val newDecls = tp.decls.filteredScope(sym => !isPhantomMethodType(sym.info))
80+
case tp: ClassInfo if !flags.is(Flags.Package) && !tp.typeRef.derivesFrom(defn.PhantomAnyClass) =>
81+
val newDecls = tp.decls.filteredScope(sym => !isPhantomMethodType(sym.info) && !isPhantomClassType(sym.info))
7882
ClassInfo(tp.prefix, tp.cls, tp.classParents, newDecls, tp.selfInfo)
7983

8084
case _ => tp
@@ -85,4 +89,9 @@ class PhantomDeclErasure extends MiniPhaseTransform with InfoTransformer {
8589
case tpe: MethodicType => tpe.resultType.derivesFrom(defn.PhantomAnyClass) || isPhantomMethodType(tpe.resultType)
8690
case _ => false
8791
}
92+
93+
private def isPhantomClassType(tp: Type)(implicit ctx: Context): Boolean = tp match {
94+
case tp: ClassInfo => tp.cls.derivesFrom(defn.PhantomAnyClass)
95+
case _ => false
96+
}
8897
}

tests/run/phantom.scala

Lines changed: 39 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -12,31 +12,31 @@ object Test {
1212
def main(args: Array[String]): Unit = {
1313
fun1(phantasmic[PhantomAny])
1414
fun1(phantasmic[Blinky])
15-
fun1(phantasmic[Inky])
16-
fun1(phantasmic[Pinky])
15+
fun1(new Inky {})
16+
fun1(new Pinky)
1717
fun1(phantasmic[Clyde])
1818

1919
fun1(phantasmic[PhantomNothing])
2020
fun2(phantasmic[PhantomNothing])
2121

2222
fun3(phantasmic[PhantomAny], phantasmic[PhantomNothing])
2323
fun3(phantasmic[Blinky], phantasmic[PhantomNothing])
24-
fun3(phantasmic[Inky], phantasmic[PhantomNothing])
25-
fun3(phantasmic[Pinky], phantasmic[PhantomNothing])
24+
fun3(new Inky {}, phantasmic[PhantomNothing])
25+
fun3(new Pinky, phantasmic[PhantomNothing])
2626
fun3(phantasmic[Clyde], phantasmic[PhantomNothing])
2727
fun3(phantasmic[PhantomNothing], phantasmic[PhantomNothing])
2828

2929
fun4(1, 2)(phantasmic[PhantomAny], phantasmic[PhantomNothing])
3030
fun4(3, 4)(phantasmic[Blinky], phantasmic[PhantomNothing])
31-
fun4(5, 6)(phantasmic[Inky], phantasmic[PhantomNothing])
32-
fun4(7, 8)(phantasmic[Pinky], phantasmic[PhantomNothing])
31+
fun4(5, 6)(new Inky {}, phantasmic[PhantomNothing])
32+
fun4(7, 8)(new Pinky, phantasmic[PhantomNothing])
3333
fun4(9, 10)(phantasmic[Clyde], phantasmic[PhantomNothing])
3434
fun4(11, 12)(phantasmic[PhantomNothing], phantasmic[PhantomNothing])
3535

3636
fun5(phantasmic[PhantomAny], phantasmic[PhantomNothing])(13, 14)
3737
fun5(phantasmic[Blinky], phantasmic[PhantomNothing])(15, 16)
38-
fun5(phantasmic[Inky], phantasmic[PhantomNothing])(17, 18)
39-
fun5(phantasmic[Pinky], phantasmic[PhantomNothing])(19, 20)
38+
fun5(new Inky {}, phantasmic[PhantomNothing])(17, 18)
39+
fun5(new Pinky, phantasmic[PhantomNothing])(19, 20)
4040
fun5(phantasmic[Clyde], phantasmic[PhantomNothing])(21, 22)
4141
fun5(phantasmic[PhantomNothing], phantasmic[PhantomNothing])(23, 24)
4242

@@ -46,40 +46,40 @@ object Test {
4646
polyfun2(new Boo6().boo1)
4747
polyfun2(new Boo6().boo2)
4848
polyfun2(phantasmic[Blinky])
49-
polyfun2(phantasmic[Inky])
50-
polyfun2(phantasmic[Pinky])
49+
polyfun2(new Inky {})
50+
polyfun2(new Pinky)
5151
polyfun2(phantasmic[Clyde])
5252
polyfun2(phantasmic[PhantomNothing])
5353

5454
polyfun3(phantasmic[PhantomAny])
5555
polyfun3(new Boo6().boo1)
5656
polyfun3(new Boo6().boo2)
5757
polyfun3(phantasmic[Blinky])
58-
polyfun3(phantasmic[Inky])
59-
polyfun3(phantasmic[Pinky])
58+
polyfun3(new Inky {})
59+
polyfun3(new Pinky)
6060
polyfun3(phantasmic[Clyde])
6161
polyfun3(phantasmic[PhantomNothing])
6262

6363
polyfun4(phantasmic[PhantomAny])
6464
polyfun4(new Boo6().boo1)
6565
polyfun4(new Boo6().boo2)
6666
polyfun4(phantasmic[Blinky])
67-
polyfun4(phantasmic[Inky])
68-
polyfun4(phantasmic[Pinky])
67+
polyfun4(new Inky {})
68+
polyfun4(new Pinky)
6969
polyfun4(phantasmic[Clyde])
7070
polyfun4(phantasmic[PhantomNothing])
7171

7272
new Boo1[PhantomAny]().polyfun1(phantasmic[PhantomAny])
7373
new Boo1[PhantomAny]().polyfun1(phantasmic[Blinky])
74-
new Boo1[PhantomAny]().polyfun1(phantasmic[Inky])
74+
new Boo1[PhantomAny]().polyfun1(new Inky {})
7575
new Boo1[PhantomAny]().polyfun1(phantasmic[PhantomNothing])
7676
new Boo1[Blinky]().polyfun1(phantasmic[Clyde])
7777
new Boo1[PhantomNothing]().polyfun1(phantasmic[PhantomNothing])
7878

7979
new Boo2().polyfun1(phantasmic[PhantomAny])
8080
new Boo2().polyfun1(phantasmic[Blinky])
81-
new Boo2().polyfun1(phantasmic[Inky])
82-
new Boo2().polyfun1(phantasmic[Pinky])
81+
new Boo2().polyfun1(new Inky {})
82+
new Boo2().polyfun1(new Pinky)
8383
new Boo2().polyfun1(phantasmic[Clyde])
8484
new Boo2().polyfun1(phantasmic[PhantomNothing])
8585

@@ -88,7 +88,7 @@ object Test {
8888
}.polyfun1(phantasmic[PhantomAny])
8989
new Boo3(){
9090
type Boo = PhantomAny
91-
}.polyfun1(phantasmic[Pinky])
91+
}.polyfun1(new Pinky)
9292
new Boo3(){
9393
type Boo = Blinky
9494
}.polyfun1(phantasmic[Blinky])
@@ -101,8 +101,8 @@ object Test {
101101

102102
new Boo4(phantasmic[PhantomAny])
103103
new Boo4(phantasmic[Blinky])
104-
new Boo4(phantasmic[Inky])
105-
new Boo4(phantasmic[Pinky])
104+
new Boo4(new Inky {})
105+
new Boo4(new Pinky)
106106
new Boo4(phantasmic[Clyde])
107107
new Boo4(phantasmic[PhantomNothing])
108108

@@ -114,22 +114,22 @@ object Test {
114114

115115
fun(phantomFun2(phantasmic[PhantomAny]))
116116
fun(phantomFun2(phantasmic[Blinky]))
117-
fun(phantomFun2(phantasmic[Inky]))
118-
fun(phantomFun2(phantasmic[Pinky]))
117+
fun(phantomFun2(new Inky {}))
118+
fun(phantomFun2(new Pinky))
119119
fun(phantomFun2(phantasmic[Clyde]))
120120
fun(phantomFun2(phantasmic[PhantomNothing]))
121121

122122
fun(phantomFun3(phantasmic[PhantomAny]))
123123
fun(phantomFun3(phantasmic[Blinky]))
124-
fun(phantomFun3(phantasmic[Inky]))
125-
fun(phantomFun3(phantasmic[Pinky]))
124+
fun(phantomFun3(new Inky {}))
125+
fun(phantomFun3(new Pinky))
126126
fun(phantomFun3(phantasmic[Clyde]))
127127
fun(phantomFun3(phantasmic[PhantomNothing]))
128128

129129
fun(phantomFun4(phantasmic[PhantomAny]))
130130
fun(phantomFun4(phantasmic[Blinky]))
131-
fun(phantomFun4(phantasmic[Inky]))
132-
fun(phantomFun4(phantasmic[Pinky]))
131+
fun(phantomFun4(new Inky {}))
132+
fun(phantomFun4(new Pinky))
133133
fun(phantomFun4(phantasmic[Clyde]))
134134
fun(phantomFun4(phantasmic[PhantomNothing]))
135135

@@ -148,26 +148,28 @@ object Test {
148148

149149
hkFun1(phantasmic[PhantomAny])
150150
hkFun1(phantasmic[Blinky])
151-
hkFun1(phantasmic[Inky])
152-
hkFun1(phantasmic[Pinky])
151+
hkFun1(new Inky {})
152+
hkFun1(new Pinky)
153153
hkFun1(phantasmic[Clyde])
154154
hkFun1(phantasmic[PhantomNothing])
155155

156156
fun(hkFun2(phantasmic[PhantomAny]))
157157
fun(hkFun2(phantasmic[Blinky]))
158-
fun(hkFun2(phantasmic[Inky]))
159-
fun(hkFun2(phantasmic[Pinky]))
158+
fun(hkFun2(new Inky {}))
159+
fun(hkFun2(new Pinky))
160160
fun(hkFun2(phantasmic[Clyde]))
161161
fun(hkFun2(phantasmic[PhantomNothing]))
162162
}
163163

164164
trait Phantoms {
165-
type Blinky <: PhantomAny
166-
type Inky <: Blinky
167-
type Pinky <: Inky
168165
type Clyde >: Pinky <: Inky
169166
}
170-
object Phantoms extends Phantoms
167+
168+
object Phantoms extends Phantoms {
169+
trait Blinky extends PhantomAny
170+
abstract class Inky extends Blinky
171+
class Pinky extends Inky
172+
}
171173

172174
def fun(top: PhantomAny): Unit = ()
173175

@@ -253,13 +255,13 @@ object Test {
253255
def phantomFun3[P <: PhantomAny](p: P): PhantomAny = p
254256
def phantomFun4[P <: PhantomAny](p: P): P = p
255257

256-
def pacFun1(blinky: Phantoms.Blinky) = {
258+
def pacFun1(blinky: Blinky) = {
257259
println("customPhantomsFun1")
258260
}
259-
def pacFun2(pinky: Phantoms.Pinky) = {
261+
def pacFun2(pinky: Pinky) = {
260262
println("customPhantomsFun2")
261263
}
262-
def pacFun3(clyde: Phantoms.Clyde) = {
264+
def pacFun3(clyde: Clyde) = {
263265
println("customPhantomsFun3")
264266
}
265267

0 commit comments

Comments
 (0)