Skip to content

Commit 0555a33

Browse files
committed
Simplify the definition of Phantom.assume.
1 parent 91ce44b commit 0555a33

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

+1-4
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

+9-9
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

+2-2
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

+2-2
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

+6-7
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

+2-2
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

+1-1
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

+2-2
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

+1-1
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

+1-1
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
}

tests/neg/phantom-instanceOf-2.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,5 @@ class phantomInstanceOf2 {
1010
object Boo extends Phantom {
1111
type BooAny <: this.Any
1212
type Blinky <: this.Any
13-
def boo[B <: this.Any]: B = assume[B]
13+
def boo[B <: this.Any]: B = assume
1414
}

tests/neg/phantom-type-param-bounds-1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ class phantomTypeParamBounds1 {
55
}
66

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

tests/neg/phantom-type-param-bounds-2.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ class phantomTypeParamBounds2 {
99
}
1010

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

tests/pos/phantom-Eq.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ object EqUtil extends Phantom {
3131
def ===[U] (y: U)(implicit ce: PhantomEq[T, U]) = x.equals(y)
3232
}
3333

34-
implicit def eqString: PhantomEqEq[String] = assume[PhantomEqEq[String]]
35-
implicit def eqInt: PhantomEqEq[Int] = assume[PhantomEqEq[Int]]
36-
implicit def eqDouble: PhantomEqEq[Double] = assume[PhantomEqEq[Double]]
34+
implicit def eqString: PhantomEqEq[String] = assume
35+
implicit def eqInt: PhantomEqEq[Int] = assume
36+
implicit def eqDouble: PhantomEqEq[Double] = assume
3737

38-
implicit def eqByteNum: PhantomEq[Byte, Number] = assume[PhantomEq[Byte, Number]]
39-
implicit def eqNumByte: PhantomEq[Number, Byte] = assume[PhantomEq[Number, Byte]]
38+
implicit def eqByteNum: PhantomEq[Byte, Number] = assume
39+
implicit def eqNumByte: PhantomEq[Number, Byte] = assume
4040

41-
implicit def eqSeq[T, U](implicit eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = assume[PhantomEq[Seq[T], Seq[U]]]
41+
implicit def eqSeq[T, U](implicit eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = assume
4242
}

tests/pos/phantom-Eq2/Phantom-Eq_1.scala

+6-6
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,12 @@ object EqUtil extends Phantom {
99
def ===[U] (y: U)(implicit ce: PhantomEq[T, U]) = x.equals(y)
1010
}
1111

12-
implicit def eqString: PhantomEqEq[String] = assume[PhantomEqEq[String]]
13-
implicit def eqInt: PhantomEqEq[Int] = assume[PhantomEqEq[Int]]
14-
implicit def eqDouble: PhantomEqEq[Double] = assume[PhantomEqEq[Double]]
12+
implicit def eqString: PhantomEqEq[String] = assume
13+
implicit def eqInt: PhantomEqEq[Int] = assume
14+
implicit def eqDouble: PhantomEqEq[Double] = assume
1515

16-
implicit def eqByteNum: PhantomEq[Byte, Number] = assume[PhantomEq[Byte, Number]]
17-
implicit def eqNumByte: PhantomEq[Number, Byte] = assume[PhantomEq[Number, Byte]]
16+
implicit def eqByteNum: PhantomEq[Byte, Number] = assume
17+
implicit def eqNumByte: PhantomEq[Number, Byte] = assume
1818

19-
implicit def eqSeq[T, U](implicit eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = assume[PhantomEq[Seq[T], Seq[U]]]
19+
implicit def eqSeq[T, U](implicit eq: PhantomEq[T, U]): PhantomEq[Seq[T], Seq[U]] = assume
2020
}

tests/run/phantom-1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ object Test {
1818

1919
object Boo extends Phantom {
2020
type BooAny = this.Any
21-
def any: BooAny = assume[BooAny]
21+
def any: BooAny = assume
2222
}

tests/run/phantom-2.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,5 @@ object Test {
1616

1717
object Boo extends Phantom {
1818
type BooNothing = this.Nothing
19-
def nothig: BooNothing = assume[BooNothing] // Should be allowed?
19+
def nothig: BooNothing = assume
2020
}

tests/run/phantom-3.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Boo extends Phantom {
2222
type Inky <: Blinky
2323
type Pinky <: Inky
2424
type Casper = Pinky
25-
def boo[B <: Blinky]: B = assume[B]
25+
def boo[B <: Blinky]: B = assume
2626
}

tests/run/phantom-4.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ object Boo extends Phantom {
2323
type Inky <: Blinky
2424
type Pinky <: Inky
2525
type Casper = Pinky
26-
def boo[B <: Blinky]: B = assume[B]
26+
def boo[B <: Blinky]: B = assume
2727
}

tests/run/phantom-5.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ object Boo extends Phantom {
2323
type Pinky <: Inky
2424
type Clyde >: Pinky <: Inky
2525
type Casper = Pinky
26-
def boo[B <: Blinky]: B = assume[B]
26+
def boo[B <: Blinky]: B = assume
2727
}

tests/run/phantom-decls-1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ object Boo extends Phantom {
2525
type BooAny = this.Any
2626
type Blinky <: this.Any
2727
type Inky <: Blinky
28-
def boo[B <: Blinky]: B = assume[B]
28+
def boo[B <: Blinky]: B = assume
2929
}

tests/run/phantom-decls-2.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ object Boo extends Phantom {
2727
type Inky <: Blinky
2828
type Pinky <: Inky
2929
type Casper = Pinky
30-
def boo[B <: this.Any]: B = assume[B]
30+
def boo[B <: this.Any]: B = assume
3131
}

tests/run/phantom-decls-3.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,5 @@ object Boo extends Phantom {
3232
type Blinky <: this.Any
3333
type Inky <: Blinky
3434
type Pinky <: Inky
35-
def boo[B <: BooAny]: B = assume[B]
35+
def boo[B <: BooAny]: B = assume
3636
}

tests/run/phantom-decls-4.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ object Boo extends Phantom {
2121
type Blinky <: this.Any
2222
type Inky <: Blinky
2323
type Pinky <: Inky
24-
def boo[B <: Blinky]: B = assume[B]
24+
def boo[B <: Blinky]: B = assume
2525
}

tests/run/phantom-decls-5.scala

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

tests/run/phantom-hk-1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ object Boo extends Phantom {
2727
type Blinky <: this.Any
2828
type Inky <: Blinky
2929
type Pinky <: Inky
30-
def boo[B <: this.Any]: B = assume[B]
30+
def boo[B <: this.Any]: B = assume
3131
}

tests/run/phantom-hk-2.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,5 @@ object Boo extends Phantom {
2525
type Blinky <: Boo.Any
2626
type Inky <: Blinky
2727
type Pinky <: Inky
28-
def boo[B <: Boo.Any]: B = assume[B]
28+
def boo[B <: Boo.Any]: B = assume
2929
}

tests/run/phantom-lazy-val.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,5 +23,5 @@ object Test {
2323

2424
object Boo extends Phantom {
2525
type BooAny = this.Any
26-
def any: BooAny = assume[BooAny]
26+
def any: BooAny = assume
2727
}

tests/run/phantom-methods-1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ object Test {
1818
object Boo extends Phantom {
1919
type BooAny = Boo.Any
2020
type Pinky <: Boo.Any
21-
def boo[B <: Boo.Any]: B = assume[B]
21+
def boo[B <: Boo.Any]: B = assume
2222
}

tests/run/phantom-methods-10.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ object Test {
2727

2828
object Boo extends Phantom {
2929
type Inky <: this.Any
30-
def boo[B <: this.Any]: B = assume[B]
30+
def boo[B <: this.Any]: B = assume
3131
}

tests/run/phantom-methods-2.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Boo extends Phantom {
2222
type Blinky <: Boo.Any
2323
type Inky <: Blinky
2424
type Pinky <: Inky
25-
def boo[B <: Boo.Any]: B = assume[B]
25+
def boo[B <: Boo.Any]: B = assume
2626
}

tests/run/phantom-methods-3.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Boo extends Phantom {
2222
type Blinky <: Boo.Any
2323
type Inky <: Blinky
2424
type Pinky <: Inky
25-
def boo[B <: Boo.Any]: B = assume[B]
25+
def boo[B <: Boo.Any]: B = assume
2626
}

tests/run/phantom-methods-4.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Boo extends Phantom {
2222
type Blinky <: Boo.Any
2323
type Inky <: Blinky
2424
type Pinky <: Inky
25-
def boo[B <: Boo.Any]: B = assume[B]
25+
def boo[B <: Boo.Any]: B = assume
2626
}

tests/run/phantom-methods-5.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Boo extends Phantom {
2222
type Blinky <: Boo.Any
2323
type Inky <: Blinky
2424
type Pinky <: Inky
25-
def boo[B <: Boo.Any]: B = assume[B]
25+
def boo[B <: Boo.Any]: B = assume
2626
}

tests/run/phantom-methods-6.scala

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

tests/run/phantom-methods-7.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,5 +20,5 @@ object Boo extends Phantom {
2020
type Inky <: Boo.Any
2121
type Pinky <: Inky
2222
type Clyde >: Pinky <: Inky
23-
def boo[B <: Boo.Any]: B = assume[B]
23+
def boo[B <: Boo.Any]: B = assume
2424
}

tests/run/phantom-methods-8.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Test {
2222

2323
object Boo extends Phantom {
2424
type Inky <: Boo.Any
25-
def boo[B <: Boo.Any]: B = assume[B]
25+
def boo[B <: Boo.Any]: B = assume
2626
}

tests/run/phantom-methods-9.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,5 @@ object Test {
2727

2828
object Boo extends Phantom {
2929
type Inky <: Boo.Any
30-
def boo[B <: Boo.Any]: B = assume[B]
30+
def boo[B <: Boo.Any]: B = assume
3131
}

tests/run/phantom-poly-1.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,5 @@ object Test {
1919

2020
object Boo extends Phantom {
2121
type Casper <: this.Any
22-
def boo[B <: this.Any]: B = assume[B]
22+
def boo[B <: this.Any]: B = assume
2323
}

tests/run/phantom-poly-2.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,5 @@ object Boo extends Phantom {
2121
type Blinky <: this.Any
2222
type Inky <: Blinky
2323
type Pinky <: Inky
24-
def boo[B <: this.Any]: B = assume[B]
24+
def boo[B <: this.Any]: B = assume
2525
}

tests/run/phantom-poly-3.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Boo extends Phantom {
2222
type Blinky <: this.Any
2323
type Inky <: Blinky
2424
type Pinky <: Inky
25-
def boo[B <: Blinky]: B = assume[B]
25+
def boo[B <: Blinky]: B = assume
2626
}

tests/run/phantom-poly-4.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Boo extends Phantom {
2222
type Blinky <: this.Any
2323
type Inky <: Blinky
2424
type Pinky <: Inky
25-
def boo[B <: Blinky]: B = assume[B]
25+
def boo[B <: Blinky]: B = assume
2626
}

tests/run/phantom-val.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,5 @@ object Test {
2222

2323
object Boo extends Phantom {
2424
type BooAny = this.Any
25-
def any: BooAny = assume[BooAny]
25+
def any: BooAny = assume
2626
}

0 commit comments

Comments
 (0)