Skip to content

Use ErasedPhantom instead of BoxedUnit as erased unit type. #2449

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions compiler/src/dotty/tools/dotc/core/Definitions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -990,4 +990,10 @@ class Definitions {
/** If the symbol is of the class scala.Phantom.Any or scala.Phantom.Nothing */
def isPhantomTerminalClass(sym: Symbol) = (sym eq Phantom_AnyClass) || (sym eq Phantom_NothingClass)


lazy val ErasedPhantomType: TypeRef = ctx.requiredClassRef("dotty.runtime.ErasedPhantom")
def ErasedPhantomClass(implicit ctx: Context) = ErasedPhantomType.symbol.asClass

def ErasedPhantom_UNIT(implicit ctx: Context) = ErasedPhantomClass.linkedClass.requiredValue("UNIT")

}
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/core/PhantomErasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ import dotty.tools.dotc.core.Types.Type
object PhantomErasure {

/** Returns the default erased type of a phantom type */
def erasedPhantomType(implicit ctx: Context): Type = defn.BoxedUnitType
def erasedPhantomType(implicit ctx: Context): Type = defn.ErasedPhantomType

/** Returns the default erased tree for a call to Phantom.assume */
def erasedAssume(implicit ctx: Context): Tree = ref(defn.BoxedUnit_UNIT)
def erasedAssume(implicit ctx: Context): Tree = ref(defn.ErasedPhantom_UNIT)

}
12 changes: 7 additions & 5 deletions compiler/src/dotty/tools/dotc/transform/Memoize.scala
Original file line number Diff line number Diff line change
Expand Up @@ -99,17 +99,19 @@ import Decorators._
val NoFieldNeeded = Lazy | Deferred | JavaDefined | (if (ctx.settings.YnoInline.value) EmptyFlags else Inline)

def isErasableBottomField(cls: Symbol): Boolean = {
// TODO: For Scala.js, return false if this field is in a js.Object unless it was a Phantom before erasure.
// Could time travel to detect phantom types or add an annotation before erasure.
!field.isVolatile && ((cls eq defn.NothingClass) || (cls eq defn.NullClass) || (cls eq defn.BoxedUnitClass))
// TODO: For Scala.js, return false if this field is in a js.Object unless it is an ErasedPhantomClass.
!field.isVolatile &&
((cls eq defn.NothingClass) || (cls eq defn.NullClass) || (cls eq defn.BoxedUnitClass) || (cls eq defn.ErasedPhantomClass))
}

def erasedBottomTree(sym: Symbol) = {
if (sym eq defn.NothingClass) Throw(Literal(Constant(null)))
else if (sym eq defn.NullClass) Literal(Constant(null))
else if (sym eq defn.BoxedUnitClass) ref(defn.BoxedUnit_UNIT)
else if (sym eq defn.ErasedPhantomClass) ref(defn.ErasedPhantom_UNIT)
else {
assert(sym eq defn.BoxedUnitClass)
ref(defn.BoxedUnit_UNIT)
assert(false) // Not in sync with isErasableBottomField
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert(false, "Check that isErasableBottomField before calling erasedBottomTree")?

EmptyTree
}
}

Expand Down
30 changes: 30 additions & 0 deletions library/src/dotty/runtime/ErasedPhantom.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package dotty.runtime;


/** Unit type representing an erased phantom value.
*
* Based on implementation of BoxedUnit.
*/
public final class ErasedPhantom implements java.io.Serializable {
private static final long serialVersionUID = 4116021023472525845L;

public final static ErasedPhantom UNIT = new ErasedPhantom();

public final static Class<Void> TYPE = java.lang.Void.TYPE;

private Object readResolve() { return UNIT; }

private ErasedPhantom() { }

public boolean equals(java.lang.Object other) {
return this == other;
}

public int hashCode() {
return 0;
}

public String toString() {
return "(\uD83D\uDC7B )";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra space after 👻?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The 👻 defines is printed on two characters but the next character is at 1 space of distance. At least on my terminal.

}
}