Skip to content

Implement value classes with phantom parameters #2926

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 2 commits into from
Sep 5, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,7 @@ object desugar {
companionDefs(anyRef, companionMeths)
else if (isValueClass) {
constr0.vparamss match {
case List(_ :: Nil) => companionDefs(anyRef, Nil)
case (_ :: Nil) :: _ => companionDefs(anyRef, Nil)
case _ => Nil // error will be emitted in typer
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1620,9 +1620,9 @@ object messages {
|"""
}

case class ValueClassNeedsExactlyOneValParam(valueClass: Symbol)(implicit ctx: Context)
case class ValueClassNeedsOneValParam(valueClass: Symbol)(implicit ctx: Context)
extends Message(ValueClassNeedsExactlyOneValParamID) {
val msg = hl"""value class needs to have exactly one ${"val"} parameter"""
val msg = hl"""value class needs one ${"val"} parameter"""
val kind = "Syntax"
val explanation = ""
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Constructors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class Constructors extends MiniPhaseTransform with IdentityDenotTransformer { th
// Produce aligned accessors and constructor parameters. We have to adjust
// for any outer parameters, which are last in the sequence of original
// parameter accessors but come first in the constructor parameter list.
val accessors = cls.paramAccessors.filterNot(_.isSetter)
val accessors = cls.paramAccessors.filterNot(x => x.isSetter || x.info.resultType.classSymbol == defn.ErasedPhantomClass)
val vparamsWithOuterLast = vparams match {
case vparam :: rest if vparam.name == nme.OUTER => rest ::: vparam :: Nil
case _ => vparams
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,8 @@ object Erasure {
}
}

if (tree.symbol eq defn.Phantom_assume) PhantomErasure.erasedAssume
if ((origSym eq defn.Phantom_assume) || (origSym.is(Flags.ParamAccessor) && wasPhantom(pt)))
PhantomErasure.erasedAssume
else recur(typed(tree.qualifier, AnySelectionProto))
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ class SyntheticMethods(thisTransformer: DenotTransformer) {
val thatAsClazz = ctx.newSymbol(ctx.owner, nme.x_0, Synthetic, clazzType, coord = ctx.owner.pos) // x$0
def wildcardAscription(tp: Type) = Typed(Underscore(tp), TypeTree(tp))
val pattern = Bind(thatAsClazz, wildcardAscription(clazzType)) // x$0 @ (_: C)
val comparisons = accessors map (accessor =>
This(clazz).select(accessor).select(defn.Any_==).appliedTo(ref(thatAsClazz).select(accessor)))
val comparisons = accessors collect { case accessor if !accessor.info.isPhantom =>
This(clazz).select(accessor).select(defn.Any_==).appliedTo(ref(thatAsClazz).select(accessor)) }
val rhs = // this.x == this$0.x && this.y == x$0.y
if (comparisons.isEmpty) Literal(Constant(true)) else comparisons.reduceLeft(_ and _)
val matchingCase = CaseDef(pattern, EmptyTree, rhs) // case x$0 @ (_: C) => this.x == this$0.x && this.y == x$0.y
Expand All @@ -186,7 +186,8 @@ class SyntheticMethods(thisTransformer: DenotTransformer) {
* ```
*/
def valueHashCodeBody(implicit ctx: Context): Tree = {
assert(accessors.length == 1)
assert(accessors.nonEmpty)
assert(accessors.tail.forall(_.info.isPhantom))
ref(accessors.head).select(nme.hashCode_).ensureApplied
}

Expand Down
12 changes: 8 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Checking.scala
Original file line number Diff line number Diff line change
Expand Up @@ -489,13 +489,17 @@ object Checking {
param.isTerm && !param.is(Flags.Accessor)
}
clParamAccessors match {
case List(param) =>
case param :: params =>
if (param.is(Mutable))
ctx.error(ValueClassParameterMayNotBeAVar(clazz, param), param.pos)
if (param.info.isPhantom)
ctx.error("value class parameter must not be phantom", param.pos)
case _ =>
ctx.error(ValueClassNeedsExactlyOneValParam(clazz), clazz.pos)
ctx.error("value class first parameter must not be phantom", param.pos)
else {
for (p <- params if !p.info.isPhantom)
ctx.error("value class can only have one non phantom parameter", p.pos)
}
case Nil =>
ctx.error(ValueClassNeedsOneValParam(clazz), clazz.pos)
}
}
stats.foreach(checkValueClassMember)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -772,14 +772,14 @@ class ErrorMessagesTests extends ErrorMessagesTest {
assertEquals("variable i", param.show)
}

@Test def valueClassNeedsExactlyOneVal =
@Test def valueClassNeedsOneVal =
checkMessagesAfter("refchecks") {
"""class MyValue(var i: Int, j: Int) extends AnyVal"""
"""class MyValue() extends AnyVal"""
}
.expect { (ictx, messages) =>
implicit val ctx: Context = ictx
assertMessageCount(1, messages)
val ValueClassNeedsExactlyOneValParam(valueClass) :: Nil = messages
val ValueClassNeedsOneValParam(valueClass) :: Nil = messages
assertEquals("class MyValue", valueClass.show)
}

Expand Down
11 changes: 11 additions & 0 deletions tests/neg/phantom-in-value-class.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import MyPhantom._


class Cursed1(val p: Boo) extends AnyVal // error

class Cursed2(val n: Int)(val a: Int) extends AnyVal // error

object MyPhantom extends Phantom {
type Boo <: super[MyPhantom].Any
def boo: Boo = assume
}
22 changes: 22 additions & 0 deletions tests/run/phantom-in-value-class.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import MyPhantom._

object Test {
def main(args: Array[String]): Unit = {
val cursed = new Cursed(7)(boo)
val cursed2 = new Cursed(7)(boo)
cursed.p
cursed2.p
}
}


class Cursed(val n: Int)(val p: Boo) extends AnyVal

class Cursed2[B <: Boo](val n: Int)(val p: B) extends AnyVal

class Cursed3[B <: Boo](val n: Int)(val p1: Boo, val p2: B) extends AnyVal

object MyPhantom extends Phantom {
type Boo <: super[MyPhantom].Any
def boo: Boo = assume
}