Skip to content

Commit 4d41e4f

Browse files
authored
Merge pull request #3410 from dotty-staging/make-phantoms-unused
Remove scala.Phantom (replaced by `unused`)
2 parents bedbd5a + 23b61fa commit 4d41e4f

File tree

141 files changed

+74
-2208
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

141 files changed

+74
-2208
lines changed

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,7 @@ class Compiler {
8080
new ShortcutImplicits, // Allow implicit functions without creating closures
8181
new CrossCastAnd, // Normalize selections involving intersection types.
8282
new Splitter) :: // Expand selections involving union types into conditionals
83-
List(new PhantomArgLift, // Extracts the evaluation of phantom arguments placing them before the call.
84-
new UnusedDecls, // Removes all unused defs and vals decls (except for parameters)
83+
List(new UnusedDecls, // Removes all unused defs and vals decls (except for parameters)
8584
new VCInlineMethods, // Inlines calls to value class methods
8685
new SeqLiterals, // Express vararg arguments as arrays
8786
new InterceptedMethods, // Special handling of `==`, `|=`, `getClass` methods

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1161,8 +1161,7 @@ class Definitions {
11611161
NullClass,
11621162
NothingClass,
11631163
SingletonClass,
1164-
EqualsPatternClass,
1165-
PhantomClass)
1164+
EqualsPatternClass)
11661165

11671166
lazy val syntheticCoreClasses = syntheticScalaClasses ++ List(
11681167
EmptyPackageVal,
@@ -1191,28 +1190,4 @@ class Definitions {
11911190
}
11921191
}
11931192

1194-
// ----- Phantoms ---------------------------------------------------------
1195-
1196-
lazy val PhantomClass: ClassSymbol = {
1197-
val cls = completeClass(enterCompleteClassSymbol(ScalaPackageClass, tpnme.Phantom, NoInitsTrait, List(AnyType)))
1198-
1199-
val any = enterCompleteClassSymbol(cls, tpnme.Any, Protected | Final | NoInitsTrait, Nil)
1200-
val nothing = enterCompleteClassSymbol(cls, tpnme.Nothing, Protected | Final | NoInitsTrait, List(any.typeRef))
1201-
enterMethod(cls, nme.assume_, ExprType(nothing.typeRef), Protected | Final | Method)
1202-
1203-
cls
1204-
}
1205-
lazy val Phantom_AnyClass = PhantomClass.unforcedDecls.find(_.name eq tpnme.Any).asClass
1206-
lazy val Phantom_NothingClass = PhantomClass.unforcedDecls.find(_.name eq tpnme.Nothing).asClass
1207-
lazy val Phantom_assume = PhantomClass.unforcedDecls.find(_.name eq nme.assume_)
1208-
1209-
/** If the symbol is of the class scala.Phantom.Any or scala.Phantom.Nothing */
1210-
def isPhantomTerminalClass(sym: Symbol) = (sym eq Phantom_AnyClass) || (sym eq Phantom_NothingClass)
1211-
1212-
1213-
lazy val ErasedPhantomType: TypeRef = ctx.requiredClassRef("dotty.runtime.ErasedPhantom")
1214-
def ErasedPhantomClass(implicit ctx: Context) = ErasedPhantomType.symbol.asClass
1215-
1216-
def ErasedPhantom_UNIT(implicit ctx: Context) = ErasedPhantomClass.linkedClass.requiredValue("UNIT")
1217-
12181193
}

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

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ case class Signature(paramsSig: List[TypeName], resSig: TypeName) {
8484
* to the parameter part of this signature.
8585
*/
8686
def prepend(params: List[Type], isJava: Boolean)(implicit ctx: Context) =
87-
Signature(params.collect { case p if !p.isPhantom => sigName(p, isJava) } ++ paramsSig, resSig)
87+
Signature(params.map(p => sigName(p, isJava)) ++ paramsSig, resSig)
8888

8989
/** A signature is under-defined if its paramsSig part contains at least one
9090
* `tpnme.Uninstantiated`. Under-defined signatures arise when taking a signature

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,6 @@ object StdNames {
241241
final val SourceFileATTR: N = "SourceFile"
242242
final val SyntheticATTR: N = "Synthetic"
243243

244-
final val Phantom: N = "Phantom"
245244

246245
// ----- Term names -----------------------------------------
247246

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

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

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

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

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,6 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
5050
private[this] var myAnyClass: ClassSymbol = null
5151
private[this] var myNothingClass: ClassSymbol = null
5252
private[this] var myNullClass: ClassSymbol = null
53-
private[this] var myPhantomNothingClass: ClassSymbol = null
5453
private[this] var myObjectClass: ClassSymbol = null
5554
private[this] var myAnyType: TypeRef = null
5655
private[this] var myNothingType: TypeRef = null
@@ -67,10 +66,6 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
6766
if (myNullClass == null) myNullClass = defn.NullClass
6867
myNullClass
6968
}
70-
def PhantomNothingClass = {
71-
if (myPhantomNothingClass == null) myPhantomNothingClass = defn.Phantom_NothingClass
72-
myPhantomNothingClass
73-
}
7469
def ObjectClass = {
7570
if (myObjectClass == null) myObjectClass = defn.ObjectClass
7671
myObjectClass
@@ -287,7 +282,7 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
287282
if (recur(info1.alias, tp2)) return true
288283
if (tp1.prefix.isStable) return false
289284
case _ =>
290-
if (tp1 eq NothingType) return tp1 == tp2.bottomType
285+
if (tp1 eq NothingType) return true
291286
}
292287
thirdTry
293288
case tp1: TypeParamRef =>
@@ -586,9 +581,8 @@ class TypeComparer(initctx: Context) extends DotClass with ConstraintHandling {
586581
case _ => false
587582
}
588583
val sym1 = tp1.symbol
589-
(sym1 eq NothingClass) && tp2.isValueTypeOrLambda && !tp2.isPhantom ||
590-
(sym1 eq NullClass) && isNullable(tp2) ||
591-
(sym1 eq PhantomNothingClass) && tp1.topType == tp2.topType
584+
(sym1 eq NothingClass) && tp2.isValueTypeOrLambda ||
585+
(sym1 eq NullClass) && isNullable(tp2)
592586
}
593587
case tp1 @ AppliedType(tycon1, args1) =>
594588
compareAppliedType1(tp1, tycon1, args1)

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

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -380,8 +380,6 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
380380
else if (semiEraseVCs && isDerivedValueClass(sym)) eraseDerivedValueClassRef(tp)
381381
else if (sym == defn.ArrayClass) apply(tp.appliedTo(TypeBounds.empty)) // i966 shows that we can hit a raw Array type.
382382
else if (defn.isSyntheticFunctionClass(sym)) defn.erasedFunctionType(sym)
383-
else if (defn.isPhantomTerminalClass(sym)) PhantomErasure.erasedPhantomType
384-
else if (sym eq defn.PhantomClass) defn.ObjectType // To erase the definitions of Phantom.{assume, Any, Nothing}
385383
else eraseNormalClassRef(tp)
386384
case tp: AppliedType =>
387385
if (tp.tycon.isRef(defn.ArrayClass)) eraseArray(tp)
@@ -401,10 +399,7 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
401399
case tp: MethodType =>
402400
def paramErasure(tpToErase: Type) =
403401
erasureFn(tp.isJavaMethod, semiEraseVCs, isConstructor, wildcardOK)(tpToErase)
404-
val (names, formals0) =
405-
if (tp.isUnusedMethod) (Nil, Nil)
406-
else if (tp.paramInfos.exists(_.isPhantom)) tp.paramNames.zip(tp.paramInfos).filterNot(_._2.isPhantom).unzip
407-
else (tp.paramNames, tp.paramInfos)
402+
val (names, formals0) = if (tp.isUnusedMethod) (Nil, Nil) else (tp.paramNames, tp.paramInfos)
408403
val formals = formals0.mapConserve(paramErasure)
409404
eraseResult(tp.resultType) match {
410405
case rt: MethodType =>
@@ -527,8 +522,6 @@ class TypeErasure(isJava: Boolean, semiEraseVCs: Boolean, isConstructor: Boolean
527522
}
528523
if (defn.isSyntheticFunctionClass(sym))
529524
sigName(defn.erasedFunctionType(sym))
530-
else if (defn.isPhantomTerminalClass(tp.symbol))
531-
sigName(PhantomErasure.erasedPhantomType)
532525
else
533526
normalizeClass(sym.asClass).fullName.asTypeName
534527
case tp: AppliedType =>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
4242
case pre: SuperType => toPrefix(pre.thistpe, cls, thiscls)
4343
case _ =>
4444
if (thiscls.derivesFrom(cls) && pre.baseType(thiscls).exists)
45-
if (variance <= 0 && !isLegalPrefix(pre)) range(pre.bottomType, pre)
45+
if (variance <= 0 && !isLegalPrefix(pre)) range(defn.NothingType, pre)
4646
else pre
4747
else if ((pre.termSymbol is Package) && !(thiscls is Package))
4848
toPrefix(pre.select(nme.PACKAGE), cls, thiscls)

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

Lines changed: 6 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -193,59 +193,18 @@ object Types {
193193
}
194194
}
195195

196-
/** Returns true if the type is a phantom type
197-
* - true if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
198-
* - false otherwise
199-
*/
200-
final def isPhantom(implicit ctx: Context): Boolean = phantomLatticeType.exists
201-
202-
/** Returns the top type of the lattice
203-
* - XYX.Any if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
204-
* - scala.Any otherwise
205-
*/
206-
final def topType(implicit ctx: Context): Type = {
207-
val lattice = phantomLatticeType
208-
if (lattice.exists) lattice.select(tpnme.Any)
209-
else defn.AnyType
210-
}
211-
212-
/** Returns the bottom type of the lattice
213-
* - XYZ.Nothing if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
214-
* - scala.Nothing otherwise
215-
*/
216-
final def bottomType(implicit ctx: Context): Type = {
217-
val lattice = phantomLatticeType
218-
if (lattice.exists) lattice.select(tpnme.Nothing)
219-
else defn.NothingType
220-
}
221-
222196
/** Is this type exactly Nothing (no vars, aliases, refinements etc allowed)? */
223197
def isBottomType(implicit ctx: Context): Boolean = this match {
224-
case tp: TypeRef =>
225-
val sym = tp.symbol
226-
(sym eq defn.NothingClass) || (sym eq defn.Phantom_NothingClass)
198+
case tp: TypeRef => tp.symbol eq defn.NothingClass
227199
case _ => false
228200
}
229201

230202
/** Is this type exactly Any (no vars, aliases, refinements etc allowed)? */
231203
def isTopType(implicit ctx: Context): Boolean = this match {
232-
case tp: TypeRef =>
233-
val sym = tp.symbol
234-
(sym eq defn.AnyClass) || (sym eq defn.Phantom_AnyClass)
204+
case tp: TypeRef => tp.symbol eq defn.AnyClass
235205
case _ => false
236206
}
237207

238-
/** Returns the type of the phantom lattice (i.e. the prefix of the phantom type)
239-
* - XYZ if XYZ extends scala.Phantom and this type is upper bounded XYZ.Any
240-
* - NoType otherwise
241-
*/
242-
private final def phantomLatticeType(implicit ctx: Context): Type = widen match {
243-
case tp: ClassInfo if defn.isPhantomTerminalClass(tp.classSymbol) => tp.prefix
244-
case tp: TypeProxy => tp.underlying.phantomLatticeType
245-
case tp: AndOrType => tp.tp1.phantomLatticeType
246-
case _ => NoType
247-
}
248-
249208
/** Is this type a (possibly aliased) singleton type? */
250209
def isSingleton(implicit ctx: Context) = dealias.isInstanceOf[SingletonType]
251210

@@ -2860,7 +2819,7 @@ object Types {
28602819
val dropDependencies = new ApproximatingTypeMap {
28612820
def apply(tp: Type) = tp match {
28622821
case tp @ TermParamRef(thisLambdaType, _) =>
2863-
range(tp.bottomType, atVariance(1)(apply(tp.underlying)))
2822+
range(defn.NothingType, atVariance(1)(apply(tp.underlying)))
28642823
case _ => mapOver(tp)
28652824
}
28662825
}
@@ -4177,7 +4136,7 @@ object Types {
41774136
case Range(infoLo: TypeBounds, infoHi: TypeBounds) =>
41784137
assert(variance == 0)
41794138
if (!infoLo.isAlias && !infoHi.isAlias) propagate(infoLo, infoHi)
4180-
else range(tp.bottomType, tp.parent)
4139+
else range(defn.NothingType, tp.parent)
41814140
case Range(infoLo, infoHi) =>
41824141
propagate(infoLo, infoHi)
41834142
case _ =>
@@ -4209,7 +4168,7 @@ object Types {
42094168
else tp.derivedTypeBounds(lo, hi)
42104169

42114170
override protected def derivedSuperType(tp: SuperType, thistp: Type, supertp: Type) =
4212-
if (isRange(thistp) || isRange(supertp)) range(thistp.bottomType, thistp.topType)
4171+
if (isRange(thistp) || isRange(supertp)) range(defn.NothingType, defn.AnyType)
42134172
else tp.derivedSuperType(thistp, supertp)
42144173

42154174
override protected def derivedAppliedType(tp: AppliedType, tycon: Type, args: List[Type]): Type =
@@ -4246,7 +4205,7 @@ object Types {
42464205
if (distributeArgs(args, tp.typeParams))
42474206
range(tp.derivedAppliedType(tycon, loBuf.toList),
42484207
tp.derivedAppliedType(tycon, hiBuf.toList))
4249-
else range(tp.bottomType, tp.topType)
4208+
else range(defn.NothingType, defn.AnyType)
42504209
// TODO: can we give a better bound than `topType`?
42514210
}
42524211
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ class Constructors extends MiniPhase with IdentityDenotTransformer { thisPhase =
131131
// Produce aligned accessors and constructor parameters. We have to adjust
132132
// for any outer parameters, which are last in the sequence of original
133133
// parameter accessors but come first in the constructor parameter list.
134-
val accessors = cls.paramAccessors.filterNot(x => x.isSetter || x.info.resultType.classSymbol == defn.ErasedPhantomClass)
134+
val accessors = cls.paramAccessors.filterNot(x => x.isSetter)
135135
val vparamsWithOuterLast = vparams match {
136136
case vparam :: rest if vparam.name == nme.OUTER => rest ::: vparam :: Nil
137137
case _ => vparams

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

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import ValueClasses._
2828
import TypeUtils._
2929
import ExplicitOuter._
3030
import core.Mode
31-
import core.PhantomErasure
3231
import reporting.trace
3332

3433
class Erasure extends Phase with DenotTransformer {
@@ -213,8 +212,6 @@ object Erasure {
213212
val tree1 =
214213
if (tree.tpe isRef defn.NullClass)
215214
adaptToType(tree, underlying)
216-
else if (wasPhantom(underlying))
217-
PhantomErasure.erasedParameterRef
218215
else if (!(tree.tpe <:< tycon)) {
219216
assert(!(tree.tpe.typeSymbol.isPrimitiveValueClass))
220217
val nullTree = Literal(Constant(null))
@@ -437,16 +434,9 @@ object Erasure {
437434
}
438435
}
439436

440-
if ((origSym eq defn.Phantom_assume) || (origSym.is(Flags.ParamAccessor) && wasPhantom(pt)))
441-
PhantomErasure.erasedAssume
442-
else recur(typed(tree.qualifier, AnySelectionProto))
437+
recur(typed(tree.qualifier, AnySelectionProto))
443438
}
444439

445-
override def typedIdent(tree: untpd.Ident, pt: Type)(implicit ctx: Context): tpd.Tree =
446-
if (tree.symbol eq defn.Phantom_assume) PhantomErasure.erasedAssume
447-
else if (tree.symbol.is(Flags.Param) && wasPhantom(tree.typeOpt)) PhantomErasure.erasedParameterRef
448-
else super.typedIdent(tree, pt)
449-
450440
override def typedThis(tree: untpd.This)(implicit ctx: Context): Tree =
451441
if (tree.symbol == ctx.owner.lexicallyEnclosingClass || tree.symbol.isStaticOwner) promote(tree)
452442
else {
@@ -507,11 +497,9 @@ object Erasure {
507497
.withType(defn.ArrayOf(defn.ObjectType))
508498
args0 = bunchedArgs :: Nil
509499
}
510-
// Arguments are phantom if an only if the parameters are phantom, guaranteed by the separation of type lattices
511-
val args1 = args0.filterConserve(arg => !wasPhantom(arg.typeOpt))
512-
assert(args1 hasSameLengthAs mt.paramInfos)
513-
val args2 = args1.zipWithConserve(mt.paramInfos)(typedExpr)
514-
untpd.cpy.Apply(tree)(fun1, args2) withType mt.resultType
500+
assert(args0 hasSameLengthAs mt.paramInfos)
501+
val args1 = args0.zipWithConserve(mt.paramInfos)(typedExpr)
502+
untpd.cpy.Apply(tree)(fun1, args1) withType mt.resultType
515503
case _ =>
516504
throw new MatchError(i"tree $tree has unexpected type of function ${fun1.tpe.widen}, was ${fun.typeOpt.widen}")
517505
}
@@ -572,11 +560,6 @@ object Erasure {
572560
rhs1 = untpd.Block(paramDefs, rhs1)
573561
}
574562
vparamss1 = vparamss1.mapConserve(_.filterConserve(!_.symbol.is(Flags.Unused)))
575-
vparamss1 = vparamss1.mapConserve(_.filterConserve(vparam => !wasPhantom(vparam.tpe)))
576-
if (sym.is(Flags.ParamAccessor) && wasPhantom(ddef.tpt.tpe)) {
577-
sym.resetFlag(Flags.ParamAccessor)
578-
rhs1 = PhantomErasure.erasedParameterRef
579-
}
580563
val ddef1 = untpd.cpy.DefDef(ddef)(
581564
tparams = Nil,
582565
vparamss = vparamss1,
@@ -703,7 +686,4 @@ object Erasure {
703686

704687
def takesBridges(sym: Symbol)(implicit ctx: Context) =
705688
sym.isClass && !sym.is(Flags.Trait | Flags.Package)
706-
707-
private def wasPhantom(tp: Type)(implicit ctx: Context): Boolean =
708-
tp.widenDealias.classSymbol eq defn.ErasedPhantomClass
709689
}

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -161,14 +161,6 @@ class FirstTransform extends MiniPhase with InfoTransformer { thisPhase =>
161161
} else ddef
162162
}
163163

164-
override def transformValDef(vdef: tpd.ValDef)(implicit ctx: Context): tpd.Tree = {
165-
if (vdef.tpt.tpe.isPhantom) {
166-
if (vdef.symbol.is(Mutable)) ctx.error("var fields cannot have Phantom types", vdef.pos)
167-
else if (vdef.symbol.hasAnnotation(defn.VolatileAnnot)) ctx.error("Phantom fields cannot be @volatile", vdef.pos)
168-
}
169-
vdef
170-
}
171-
172164
override def transformStats(trees: List[Tree])(implicit ctx: Context): List[Tree] =
173165
ast.Trees.flatten(reorderAndComplete(trees)(ctx.withPhase(thisPhase.next)))
174166

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

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,6 @@ object GenericSignatures {
212212
else
213213
jsig(unboxedSeen, toplevel, primitiveOK)
214214
}
215-
else if (tp.isPhantom)
216-
jsig(defn.ErasedPhantomType)
217215
else if (sym.isClass)
218216
classSig
219217
else
@@ -242,7 +240,7 @@ object GenericSignatures {
242240
// unused method parameters do not make it to the bytecode.
243241
def effectiveParamInfoss(t: Type)(implicit ctx: Context): List[List[Type]] = t match {
244242
case t: MethodType if t.isUnusedMethod => effectiveParamInfoss(t.resType)
245-
case t: MethodType => t.paramInfos.filterNot(_.isPhantom) :: effectiveParamInfoss(t.resType)
243+
case t: MethodType => t.paramInfos :: effectiveParamInfoss(t.resType)
246244
case _ => Nil
247245
}
248246
val params = effectiveParamInfoss(mtpe).flatten

0 commit comments

Comments
 (0)