Skip to content

Commit 5ee0a9a

Browse files
committed
Simplify the definition of Phantom.assume.
1 parent 9388ce4 commit 5ee0a9a

Some content is hidden

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

44 files changed

+71
-75
lines changed

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -972,10 +972,7 @@ class Definitions {
972972

973973
val any = enterCompleteClassSymbol(cls, tpnme.Any, Protected | Final | NoInitsTrait, Nil)
974974
val nothing = enterCompleteClassSymbol(cls, tpnme.Nothing, Protected | Final | NoInitsTrait, List(any.typeRef))
975-
976-
val tparamNames = List("P".toTypeName)
977-
val ptype = PolyType(tparamNames)(_ => TypeBounds(nothing.typeRef, any.typeRef) :: Nil, TypeParamRef(_, 0))
978-
newSymbol(cls, nme.assume_, Protected | Final | Method, ptype).entered
975+
enterMethod(cls, nme.assume_, MethodType(Nil, nothing.typeRef), Protected | Final | Method)
979976

980977
cls
981978
}

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

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -434,14 +434,7 @@ 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-
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 {
437+
ntree match {
445438
case TypeApply(fun, args) =>
446439
val fun1 = typedExpr(fun, WildcardType)
447440
fun1.tpe.widen match {
@@ -461,7 +454,14 @@ object Erasure extends TypeTestsCasts{
461454
val Apply(fun, args) = tree
462455
if (fun.symbol == defn.dummyApply)
463456
typedUnadapted(args.head, pt)
464-
else typedExpr(fun, FunProto(args, pt, this)) match {
457+
else if (defn.isPhantomAssume(fun.symbol)) {
458+
/* All phantom types are erased to `ErasedPhantom` (an un-instantiable final abstract class),
459+
* hence the only valid term for a `ErasedPhantom` is `null`.
460+
* As `Phantom.assume` is the only way to instantiate phantoms, all runtime values of
461+
* phantom type become `null` (no instantiation overhead).
462+
*/
463+
Literal(Constant(null)).withType(defn.ErasedPhantomType)
464+
} else typedExpr(fun, FunProto(args, pt, this)) match {
465465
case fun1: Apply => // arguments passed in prototype were already passed
466466
fun1
467467
case fun1 =>

library/src/scala/Phantom.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ trait Phantom {
77
88
protected final abstract class Nothing extends Any
99
10-
protected final def assume[P >: this.Nothing <: this.Any]: P =
11-
null.asInstanceOf[P] // This implementation matches the erased implementation
10+
protected final def assume: this.Nothing =
11+
null.asInstanceOf[this.Nothing] // This implementation matches the erased implementation
1212
}
1313
*/

tests/neg/customArgs/phantom-overload.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ object Boo extends Phantom {
1919
type A <: this.Any
2020
type B <: this.Any
2121
type N = this.Nothing
22-
def nothing = assume[this.Nothing]
22+
def nothing: this.Nothing = assume
2323
}
2424

2525
object Boo2 extends Phantom {
2626
type C <: this.Any
27-
def nothing2 = assume[this.Nothing]
27+
def nothing2: this.Nothing = assume
2828
}

tests/neg/phantom-Eq.scala

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,13 @@ object EqUtil extends Phantom {
2626
def ===[U] (y: U)(implicit ce: PhantomEq[T, U]) = x.equals(y)
2727
}
2828

29-
implicit def eqString: PhantomEqEq[String] = assume[PhantomEqEq[String]]
30-
implicit def eqInt: PhantomEqEq[Int] = assume[PhantomEqEq[Int]]
31-
implicit def eqDouble: PhantomEqEq[Double] = assume[PhantomEqEq[Double]]
29+
implicit def eqString: PhantomEqEq[String] = assume
30+
implicit def eqInt: PhantomEqEq[Int] = assume
31+
implicit def eqDouble: PhantomEqEq[Double] = assume
3232

33-
implicit def eqByteNum: PhantomEq[Byte, Number] = assume[PhantomEq[Byte, Number]]
34-
implicit def eqNumByte: PhantomEq[Number, Byte] = assume[PhantomEq[Number, Byte]]
33+
implicit def eqByteNum: PhantomEq[Byte, Number] = assume
34+
implicit def eqNumByte: PhantomEq[Number, Byte] = assume
3535

36-
implicit def eqSeq[T, U](implicit eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] =
37-
assume[PhantomEq[Seq[T], Seq[U]]]
36+
implicit def eqSeq[T, U](implicit eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = assume
3837

3938
}

tests/neg/phantom-bottom.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,10 @@ class Foo {
1616
object Boo extends Phantom {
1717
type BooAny = this.Any
1818
type BooNothing = this.Nothing
19-
def nothing: BooNothing = assume[BooNothing]
19+
def nothing: BooNothing = assume
2020
}
2121

2222
object Boo2 extends Phantom {
2323
type BooNothing2 = this.Nothing
24-
def nothing: BooNothing2 = assume[BooNothing2]
24+
def nothing: BooNothing2 = assume
2525
}

tests/neg/phantom-evidence.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ object WithNormalState extends Phantom {
1313

1414
type =::=[From, To] <: this.Any
1515

16-
implicit inline def tpEquals[A]: A =::= A = assume[=::=[A, A]]
16+
implicit inline def tpEquals[A]: A =::= A = assume
1717

1818
trait State
1919
sealed trait On extends State

tests/neg/phantom-expr.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,10 @@ class Foo {
3636

3737
object Boo extends Phantom {
3838
type BooAny = this.Any
39-
def boo[B <: BooAny]: B = assume[B]
39+
def boo[B <: BooAny]: B = assume
4040
}
4141

4242
object Boo1 extends Phantom {
4343
type Boo1Any = this.Any
44-
def boo1: Boo1Any = assume[Boo1Any]
44+
def boo1: Boo1Any = assume
4545
}

tests/neg/phantom-fun-app.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ object Boo extends Phantom {
2020
type BooAny = this.Any
2121
type Blinky <: BooAny
2222
type Pinky <: Blinky
23-
def boo[B <: BooAny]: B = assume[B]
23+
def boo[B <: BooAny]: B = assume
2424
}

tests/neg/phantom-instanceOf-1.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ class phantomInstanceOf1 {
77
}
88

99
object Boo extends Phantom {
10-
def boo[B <: Boo.Any]: B = assume[B]
10+
def boo[B <: Boo.Any]: B = assume
1111
}

0 commit comments

Comments
 (0)