Skip to content
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
3 changes: 3 additions & 0 deletions src/dotty/tools/dotc/transform/SymUtils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,9 @@ class SymUtils(val self: Symbol) extends AnyVal {
def accessorNamed(name: TermName)(implicit ctx: Context): Symbol =
self.owner.info.decl(name).suchThat(_ is Accessor).symbol

def termParamAccessors(implicit ctx: Context): List[Symbol] =
self.info.decls.filter(_ is TermParamAccessor).toList

def caseAccessors(implicit ctx:Context) =
self.info.decls.filter(_ is CaseAccessor).toList

Expand Down
26 changes: 22 additions & 4 deletions src/dotty/tools/dotc/transform/SyntheticMethods.scala
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,11 @@ class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer
*/
def syntheticMethods(clazz: ClassSymbol)(implicit ctx: Context): List[Tree] = {
val clazzType = clazz.typeRef
lazy val accessors = clazz.caseAccessors
lazy val accessors =
if (isDerivedValueClass(clazz))
clazz.termParamAccessors
else
clazz.caseAccessors

val symbolsToSynthesize: List[Symbol] =
if (clazz.is(Case)) caseSymbols
Expand All @@ -72,7 +76,8 @@ class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer
ref(defn.runtimeMethod("_" + sym.name.toString)).appliedToArgs(This(clazz) :: vrefss.head)

def syntheticRHS(implicit ctx: Context): List[List[Tree]] => Tree = synthetic.name match {
case nme.hashCode_ => vrefss => hashCodeBody
case nme.hashCode_ if isDerivedValueClass(clazz) => vrefss => valueHashCodeBody
case nme.hashCode_ => vrefss => caseHashCodeBody
case nme.toString_ => forwardToRuntime
case nme.equals_ => vrefss => equalsBody(vrefss.head.head)
case nme.canEqual_ => vrefss => canEqualBody(vrefss.head.head)
Expand Down Expand Up @@ -116,11 +121,24 @@ class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer
}
}

/** The class
*
* class C(x: T) extends AnyVal
*
* gets the hashCode method:
*
* def hashCode: Int = x.hashCode()
*/
def valueHashCodeBody(implicit ctx: Context): Tree = {
assert(accessors.length == 1)
ref(accessors.head).select(nme.hashCode_).ensureApplied
}

/** The class
*
* case class C(x: T, y: T)
*
* get the hashCode method:
* gets the hashCode method:
*
* def hashCode: Int = {
* <synthetic> var acc: Int = 0xcafebabe;
Expand All @@ -129,7 +147,7 @@ class SyntheticMethods extends MiniPhaseTransform with IdentityDenotTransformer
* Statics.finalizeHash(acc, 2)
* }
*/
def hashCodeBody(implicit ctx: Context): Tree = {
def caseHashCodeBody(implicit ctx: Context): Tree = {
val acc = ctx.newSymbol(ctx.owner, "acc".toTermName, Mutable | Synthetic, defn.IntType, coord = ctx.owner.pos)
val accDef = ValDef(acc, Literal(Constant(0xcafebabe)))
val mixes = for (accessor <- accessors.toList) yield
Expand Down
2 changes: 1 addition & 1 deletion tests/pos/extmethods.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
trait That1[A]
class T[A, This <: That1[A]] extends AnyVal {
class T[A, This <: That1[A]](val x: Int) extends AnyVal {
self: This =>
var next: This = _
final def loop(x: This, cnt: Int): Int = loop(x, cnt + 1)
Expand Down
4 changes: 1 addition & 3 deletions tests/pos/i143.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@ import dotty.tools.dotc.core.Denotations._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.Contexts._

class TC5 extends AnyVal {
implicit val ctx: Context = ???

class TC5(val ctx: Context) extends AnyVal {
def candidates(mbr: SingleDenotation): Boolean = {
mbr.symbol.denot(ctx).exists
}
Expand Down