Skip to content

Commit 47f8b03

Browse files
committed
Remove PhantomTypeErasure and integrate into Erasure.
This removes an unnecessary additional phase and fixes siggnatures of erased methods.
1 parent 63db9b7 commit 47f8b03

File tree

8 files changed

+43
-67
lines changed

8 files changed

+43
-67
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ import typer.{FrontEnd, Typer, ImportInfo, RefChecks}
1111
import reporting.{Reporter, ConsoleReporter}
1212
import Phases.Phase
1313
import transform._
14-
import transform.phantom._
1514
import util.FreshNameCreator
1615
import transform.TreeTransforms.{TreeTransform, TreeTransformer}
1716
import core.DenotTransformers.DenotTransformer
@@ -75,7 +74,6 @@ class Compiler {
7574
new ResolveSuper, // Implement super accessors and add forwarders to trait methods
7675
new PrimitiveForwarders, // Add forwarders to trait methods that have a mismatch between generic and primitives
7776
new ArrayConstructors), // Intercept creation of (non-generic) arrays and intrinsify.
78-
List(new PhantomTypeErasure), // Erases phantom types to ErasedPhantom
7977
List(new Erasure), // Rewrite types to JVM model, erasing all type parameters, abstract types and refinements.
8078
List(new ElimErasedValueType, // Expand erased value types to their underlying implmementation types
8179
new VCElideAllocations, // Peep-hole optimization to eliminate unnecessary value class allocations

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -988,8 +988,6 @@ class Definitions {
988988
sym.name == nme.assume_ && (sym.owner eq PhantomClass)
989989

990990
lazy val ErasedPhantomClass = ctx.requiredClass("dotty.runtime.ErasedPhantom")
991-
def ErasedPhantomType = ErasedPhantomClass.typeRef
991+
def ErasedPhantomType(implicit ctx: Context) = ErasedPhantomClass.typeRef
992992

993-
lazy val ErasedPhantomLatticeClass = ctx.requiredClass("dotty.runtime.ErasedPhantomLattice")
994-
def ErasedPhantomLatticeType = ErasedPhantomLatticeClass.typeRef
995993
}

compiler/src/dotty/tools/dotc/core/TypeErasure.scala

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
364364
else if (semiEraseVCs && isDerivedValueClass(sym)) eraseDerivedValueClassRef(tp)
365365
else if (sym == defn.ArrayClass) apply(tp.appliedTo(TypeBounds.empty)) // i966 shows that we can hit a raw Array type.
366366
else if (defn.isSyntheticFunctionClass(sym)) defn.erasedFunctionType(sym)
367+
else if (defn.isPhantomAnyClass(tp.symbol) || defn.isPhantomNothingClass(tp.symbol)) defn.ErasedPhantomType
367368
else eraseNormalClassRef(tp)
368369
case tp: RefinedType =>
369370
val parent = tp.parent
@@ -400,8 +401,8 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
400401
else classParents.mapConserve(eraseTypeRef) match {
401402
case tr :: trs1 =>
402403
assert(!tr.classSymbol.is(Trait), cls)
403-
val tr1 = if (cls is Trait) defn.ObjectType else tr
404-
tr1 :: trs1.filterNot(_ isRef defn.ObjectClass)
404+
val tr1 = if (cls.is(Trait) || (tr.symbol eq defn.PhantomClass)) defn.ObjectType else tr
405+
tr1 :: trs1.filterNot(x => x.isRef(defn.ObjectClass) || x.isRef(defn.PhantomClass))
405406
case nil => nil
406407
}
407408
val erasedDecls = decls.filteredScope(sym => !sym.isType || sym.isClass)
@@ -504,6 +505,8 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
504505
}
505506
if (defn.isSyntheticFunctionClass(sym))
506507
sigName(defn.erasedFunctionType(sym))
508+
else if (defn.isPhantomAnyClass(tp.symbol) || defn.isPhantomNothingClass(tp.symbol))
509+
sigName(defn.ErasedPhantomType)
507510
else
508511
normalizeClass(sym.asClass).fullName.asTypeName
509512
case defn.ArrayOf(elem) =>

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -434,7 +434,14 @@ object Erasure extends TypeTestsCasts{
434434
override def typedTypeApply(tree: untpd.TypeApply, pt: Type)(implicit ctx: Context) = {
435435
val ntree = interceptTypeApply(tree.asInstanceOf[TypeApply])(ctx.withPhase(ctx.erasurePhase))
436436

437-
ntree match {
437+
if (defn.isPhantomAssume(tree.fun.symbol)) {
438+
/* All phantom types are erased to `ErasedPhantom` (an uninstantiable final abstract class),
439+
* hence the only valid term for a `ErasedPhantom` is `null`.
440+
* As `Phantom.assume[P <: Phantom.Any]` is the only way to instantiate phantoms, all runtime
441+
* values of phantom type become `null` (no instantiation overhead).
442+
*/
443+
Literal(Constant(null)).withType(defn.ErasedPhantomType)
444+
} else ntree match {
438445
case TypeApply(fun, args) =>
439446
val fun1 = typedExpr(fun, WildcardType)
440447
fun1.tpe.widen match {

compiler/src/dotty/tools/dotc/transform/phantom/PhantomTypeErasure.scala

Lines changed: 0 additions & 56 deletions
This file was deleted.

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ class CompilationTests extends ParallelSummaryReport with ParallelTesting {
151151
compileFile("../tests/neg/customArgs/nopredef.scala", defaultOptions.and("-Yno-predef")) +
152152
compileFile("../tests/neg/customArgs/noimports.scala", defaultOptions.and("-Yno-imports")) +
153153
compileFile("../tests/neg/customArgs/noimports2.scala", defaultOptions.and("-Yno-imports")) +
154+
compileFile("../tests/neg/customArgs/phantom-overload.scala", allowDoubleBindings) +
154155
compileFile("../tests/neg/tailcall/t1672b.scala", defaultOptions) +
155156
compileFile("../tests/neg/tailcall/t3275.scala", defaultOptions) +
156157
compileFile("../tests/neg/tailcall/t6574.scala", defaultOptions) +

library/src/dotty/runtime/ErasedPhantomLattice.scala

Lines changed: 0 additions & 3 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
class phantomOverload {
3+
import Boo._
4+
import Boo2._
5+
6+
def foo1(): A = nothing
7+
def foo1(): B = nothing // error
8+
def foo1(): C = nothing2 // error
9+
def foo1(): N = nothing // error
10+
11+
def foo2(x: A) = ???
12+
def foo2(x: A) = ??? // error
13+
def foo2(x: B) = ??? // error
14+
def foo2(x: C) = ??? // error
15+
def foo2(x: N) = ??? // error
16+
}
17+
18+
object Boo extends Phantom {
19+
type A <: this.Any
20+
type B <: this.Any
21+
type N = this.Nothing
22+
def nothing = assume[this.Nothing]
23+
}
24+
25+
object Boo2 extends Phantom {
26+
type C <: this.Any
27+
def nothing2 = assume[this.Nothing]
28+
}

0 commit comments

Comments
 (0)