Skip to content

Fix #10285: Fix scheme for handling setter names #10584

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
Dec 2, 2020
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
1 change: 1 addition & 0 deletions compiler/src/dotty/tools/dotc/core/NameKinds.scala
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,7 @@ object NameKinds {
val ModuleClassName: SuffixNameKind = new SuffixNameKind(OBJECTCLASS, "$", optInfoString = "ModuleClass")
val ImplMethName: SuffixNameKind = new SuffixNameKind(IMPLMETH, "$")
val AdaptedClosureName: SuffixNameKind = new SuffixNameKind(ADAPTEDCLOSURE, "$adapted") { override def definesNewName = true }
val SyntheticSetterName: SuffixNameKind = new SuffixNameKind(SETTER, "_$eq")

/** A name together with a signature. Used in Tasty trees. */
object SignedName extends NameKind(SIGNED) {
Expand Down
21 changes: 13 additions & 8 deletions compiler/src/dotty/tools/dotc/core/NameOps.scala
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
package dotty.tools.dotc
package dotty.tools
package dotc
package core

import java.security.MessageDigest
Expand All @@ -9,7 +10,7 @@ import Names._, StdNames._, Contexts._, Symbols._, Flags._, NameKinds._, Types._
import util.Chars.{isOperatorPart, digit2int}
import Definitions._
import nme._
import Decorators.concat
import Decorators._

object NameOps {

Expand Down Expand Up @@ -69,7 +70,7 @@ object NameOps {
def isLocalDummyName: Boolean = name startsWith str.LOCALDUMMY_PREFIX
def isReplWrapperName: Boolean = name.toString contains str.REPL_SESSION_LINE
def isReplAssignName: Boolean = name.toString contains str.REPL_ASSIGN_SUFFIX
def isSetterName: Boolean = name endsWith str.SETTER_SUFFIX
def isSetterName: Boolean = name.endsWith(str.SETTER_SUFFIX) || name.is(SyntheticSetterName)
def isScala2LocalSuffix: Boolean = testSimple(_.endsWith(" "))
def isSelectorName: Boolean = testSimple(n => n.startsWith("_") && n.drop(1).forall(_.isDigit))
def isAnonymousClassName: Boolean = name.startsWith(str.ANON_CLASS)
Expand Down Expand Up @@ -347,17 +348,21 @@ object NameOps {

def setterName: TermName = name.exclude(FieldName) ++ str.SETTER_SUFFIX

def syntheticSetterName = SyntheticSetterName(name.exclude(FieldName))

def getterName: TermName =
name.exclude(FieldName).mapLast(n =>
val name1 = name.exclude(FieldName)
if name1.is(SyntheticSetterName) then name1.exclude(SyntheticSetterName)
else name1.mapLast(n =>
if (n.endsWith(str.SETTER_SUFFIX)) n.take(n.length - str.SETTER_SUFFIX.length).asSimpleName
else n)

def fieldName: TermName =
if (name.isSetterName)
if (name.is(TraitSetterName)) {
val TraitSetterName(_, original) = name
original.fieldName
}
if name.is(SyntheticSetterName) then
name.exclude(SyntheticSetterName)
.replace { case TraitSetterName(_, original) => original }
.fieldName
else getterName.fieldName
else FieldName(name.toSimpleName)

Expand Down
2 changes: 2 additions & 0 deletions compiler/src/dotty/tools/dotc/core/NameTags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ object NameTags extends TastyFormat.NameTags {

final val PARAMACC = 33 // Used for a private parameter alias

final val SETTER = 34 // A synthesized += suffix.

def nameTagToString(tag: Int): String = tag match {
case UTF8 => "UTF8"
case QUALIFIED => "QUALIFIED"
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Mixin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ object Mixin {
def traitSetterName(getter: TermSymbol)(using Context): TermName =
getter.ensureNotPrivate.name
.expandedName(getter.owner, TraitSetterName)
.asTermName.setterName
.asTermName.syntheticSetterName
}

/** This phase performs the following transformations:
Expand Down
25 changes: 25 additions & 0 deletions tests/run/i10285.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
trait Companion {

trait AbstractFoo {
val a0 = 1
private val b0 = 2
def b1 = b0
val (a, b) = {
val av = 1
val bv = 2
(av, bv)
}
}
}

object Bar extends Companion {
class FooImpl extends AbstractFoo
val foo = new FooImpl()
}

object Test {
val foo = new Bar.FooImpl
def main(args: Array[String]): Unit =
assert(Bar.foo.a0 + Bar.foo.b1 == 3)
assert(Bar.foo.a + Bar.foo.b == 3)
}