Skip to content

Commit 43a332e

Browse files
committed
Rename Phantoms in defn and add Type.isPhantom.
1 parent db35b0e commit 43a332e

File tree

8 files changed

+23
-29
lines changed

8 files changed

+23
-29
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -976,11 +976,11 @@ class Definitions {
976976

977977
cls
978978
}
979-
lazy val PhantomAnyClass = PhantomClass.unforcedDecls.find(_.name eq tpnme.Any).asClass
980-
lazy val PhantomNothingClass = PhantomClass.unforcedDecls.find(_.name eq tpnme.Nothing).asClass
981-
lazy val PhantomAssume = PhantomClass.unforcedDecls.find(_.name eq nme.assume_)
979+
lazy val Phantom_AnyClass = PhantomClass.unforcedDecls.find(_.name eq tpnme.Any).asClass
980+
lazy val Phantom_NothingClass = PhantomClass.unforcedDecls.find(_.name eq tpnme.Nothing).asClass
981+
lazy val Phantom_assume = PhantomClass.unforcedDecls.find(_.name eq nme.assume_)
982982

983983
/** If the symbol is of the class scala.Phantom.Any or scala.Phantom.Nothing */
984-
def isPhantomClass(sym: Symbol) = (sym eq PhantomAnyClass) || (sym eq PhantomNothingClass)
984+
def isPhantomTerminalClass(sym: Symbol) = (sym eq Phantom_AnyClass) || (sym eq Phantom_NothingClass)
985985

986986
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,7 @@ object SymDenotations {
635635

636636
/** Is this symbol a class references to which that are supertypes of null? */
637637
final def isNullableClass(implicit ctx: Context): Boolean =
638-
isClass && !isValueClass && !(this is ModuleClass) && symbol != defn.NothingClass && !defn.isPhantomClass(symbol)
638+
isClass && !isValueClass && !(this is ModuleClass) && symbol != defn.NothingClass && !defn.isPhantomTerminalClass(symbol)
639639

640640
/** Is this definition accessible as a member of tree with type `pre`?
641641
* @param pre The type of the tree from which the selection is made

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

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import config.Printers.{typr, constr, subtyping, noPrinter}
1212
import TypeErasure.{erasedLub, erasedGlb}
1313
import TypeApplications._
1414
import scala.util.control.NonFatal
15-
import scala.annotation.tailrec
1615

1716
/** Provides methods to compare types.
1817
*/
@@ -48,7 +47,6 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
4847
private var myAnyClass: ClassSymbol = null
4948
private var myNothingClass: ClassSymbol = null
5049
private var myNullClass: ClassSymbol = null
51-
private var myPhantomAnyClass: ClassSymbol = null
5250
private var myPhantomNothingClass: ClassSymbol = null
5351
private var myObjectClass: ClassSymbol = null
5452
private var myAnyType: TypeRef = null
@@ -66,12 +64,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
6664
if (myNullClass == null) myNullClass = defn.NullClass
6765
myNullClass
6866
}
69-
def PhantomAnyClass = {
70-
if (myPhantomAnyClass == null) myPhantomAnyClass = defn.PhantomAnyClass
71-
myPhantomAnyClass
72-
}
7367
def PhantomNothingClass = {
74-
if (myPhantomNothingClass == null) myPhantomNothingClass = defn.PhantomNothingClass
68+
if (myPhantomNothingClass == null) myPhantomNothingClass = defn.Phantom_NothingClass
7569
myPhantomNothingClass
7670
}
7771
def ObjectClass = {
@@ -561,14 +555,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
561555
case OrType(tp1, tp2) => isNullable(tp1) || isNullable(tp2)
562556
case _ => false
563557
}
564-
def isPhantom(tp: Type): Boolean = {
565-
// note that the only phantom classes are PhantomAnyClass and PhantomNothingClass
566-
val sym = tp.typeSymbol
567-
(sym eq PhantomAnyClass) || (sym eq PhantomNothingClass) ||
568-
(!sym.isClass && tp.phantomLatticeType.exists)
569-
}
570558
val sym1 = tp1.symbol
571-
(sym1 eq NothingClass) && tp2.isValueTypeOrLambda && !isPhantom(tp2) ||
559+
(sym1 eq NothingClass) && tp2.isValueTypeOrLambda && !tp2.isPhantom ||
572560
(sym1 eq NullClass) && isNullable(tp2) ||
573561
(sym1 eq PhantomNothingClass) && tp1.topType == tp2.topType
574562
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -364,7 +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.isPhantomClass(tp.symbol)) defn.BoxedUnitType
367+
else if (defn.isPhantomTerminalClass(tp.symbol)) defn.BoxedUnitType
368368
else eraseNormalClassRef(tp)
369369
case tp: RefinedType =>
370370
val parent = tp.parent
@@ -507,7 +507,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
507507
}
508508
if (defn.isSyntheticFunctionClass(sym))
509509
sigName(defn.erasedFunctionType(sym))
510-
else if (defn.isPhantomClass(tp.symbol))
510+
else if (defn.isPhantomTerminalClass(tp.symbol))
511511
sigName(defn.BoxedUnitType)
512512
else
513513
normalizeClass(sym.asClass).fullName.asTypeName

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

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ object Types {
175175
loop(this)
176176
}
177177

178+
/** Returns true if the type is a phantom type
179+
* - true if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
180+
* - false otherwise
181+
*/
182+
final def isPhantom(implicit ctx: Context): Boolean = phantomLatticeType.exists
183+
178184
/** Returns the top type of the lattice
179185
* - XYX.Any if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
180186
* - scala.Any otherwise
@@ -195,13 +201,13 @@ object Types {
195201
else defn.NothingType
196202
}
197203

198-
/** Returns the type of the lattice
204+
/** Returns the type of the lattice (i.e. the prefix of the phantom type)
199205
* - XYZ if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
200206
* - NoType otherwise
201207
*/
202-
final def phantomLatticeType(implicit ctx: Context): Type = widen match {
203-
case tp: ClassInfo if defn.isPhantomClass(tp.classSymbol) => tp.prefix
204-
case tp: TypeProxy if tp.superType ne this => tp.superType.phantomLatticeType
208+
private final def phantomLatticeType(implicit ctx: Context): Type = widen match {
209+
case tp: ClassInfo if defn.isPhantomTerminalClass(tp.classSymbol) => tp.prefix
210+
case tp: TypeProxy if tp.underlying ne this => tp.underlying.phantomLatticeType
205211
case tp: AndOrType => tp.tp1.phantomLatticeType
206212
case _ => NoType
207213
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,7 @@ object Erasure extends TypeTestsCasts{
454454
val Apply(fun, args) = tree
455455
if (fun.symbol == defn.cbnArg)
456456
typedUnadapted(args.head, pt)
457-
else if (fun.symbol eq defn.PhantomAssume)
457+
else if (fun.symbol eq defn.Phantom_assume)
458458
ref(defn.BoxedUnit_UNIT)
459459
else typedExpr(fun, FunProto(args, pt, this)) match {
460460
case fun1: Apply => // arguments passed in prototype were already passed

compiler/src/dotty/tools/dotc/typer/TypeAssigner.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ trait TypeAssigner {
431431
tree.withType(body.tpe)
432432

433433
def assignType(tree: untpd.Match, cases: List[CaseDef])(implicit ctx: Context) = {
434-
if (tree.selector.typeOpt.phantomLatticeType.exists)
434+
if (tree.selector.typeOpt.isPhantom)
435435
ctx.error("Cannot pattern match on phantoms", tree.selector.pos)
436436
if (cases.nonEmpty) {
437437
val head = cases.head

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1126,8 +1126,8 @@ class Typer extends Namer with TypeAssigner with Applications with Implicits wit
11261126
val lo1 = typed(lo)
11271127
val hi1 = typed(hi)
11281128

1129-
val lo2 = if (!lo1.isEmpty) lo1 else typed(untpd.TypeTree(hi1.typeOpt.bottomType))
1130-
val hi2 = if (!hi1.isEmpty) hi1 else typed(untpd.TypeTree(lo1.typeOpt.topType))
1129+
val lo2 = if (lo1.isEmpty) typed(untpd.TypeTree(hi1.typeOpt.bottomType)) else lo1
1130+
val hi2 = if (hi1.isEmpty) typed(untpd.TypeTree(lo1.typeOpt.topType)) else hi1
11311131

11321132
val tree1 = assignType(cpy.TypeBoundsTree(tree)(lo2, hi2), lo2, hi2)
11331133
if (ctx.mode.is(Mode.Pattern)) {

0 commit comments

Comments
 (0)