Skip to content

Fix #9068: Don't duplicate _N selectors #9218

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 1 commit into from
Jun 22, 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
21 changes: 15 additions & 6 deletions compiler/src/dotty/tools/dotc/ast/Desugar.scala
Original file line number Diff line number Diff line change
Expand Up @@ -559,9 +559,9 @@ object desugar {
val copiedAccessFlags = if migrateTo3 then EmptyFlags else AccessFlags

// Methods to add to a case class C[..](p1: T1, ..., pN: Tn)(moreParams)
// def _1: T1 = this.p1
// def _1: T1 = this.p1
// ...
// def _N: TN = this.pN
// def _N: TN = this.pN (unless already given as valdef or parameterless defdef)
// def copy(p1: T1 = p1: @uncheckedVariance, ...,
// pN: TN = pN: @uncheckedVariance)(moreParams) =
// new C[...](p1, ..., pN)(moreParams)
Expand All @@ -572,12 +572,21 @@ object desugar {
val caseClassMeths = {
def syntheticProperty(name: TermName, tpt: Tree, rhs: Tree) =
DefDef(name, Nil, Nil, tpt, rhs).withMods(synthetic)
def productElemMeths = {

def productElemMeths =
val caseParams = derivedVparamss.head.toArray
for (i <- List.range(0, arity) if nme.selectorName(i) `ne` caseParams(i).name)
yield syntheticProperty(nme.selectorName(i), caseParams(i).tpt,
val selectorNamesInBody = normalizedBody.collect {
case vdef: ValDef if vdef.name.isSelectorName =>
vdef.name
case ddef: DefDef if ddef.name.isSelectorName && ddef.tparams.isEmpty && ddef.vparamss.isEmpty =>
ddef.name
}
for i <- List.range(0, arity)
selName = nme.selectorName(i)
if (selName ne caseParams(i).name) && !selectorNamesInBody.contains(selName)
yield syntheticProperty(selName, caseParams(i).tpt,
Select(This(EmptyTypeIdent), caseParams(i).name))
}

def ordinalMeths = if (isEnumCase) ordinalMethLit(nextOrdinal(CaseKind.Class)._1) :: Nil else Nil
def copyMeths = {
val hasRepeatedParam = constrVparamss.exists(_.exists {
Expand Down
12 changes: 12 additions & 0 deletions tests/run/i9068.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
case class MyClass(v1: Int, v2: Int, v3: Int, v4: Int) extends Product3[Int, Int, Int]:
val _1: Int = v2
def _2: Int = v3
var _3: Int = v4
def _4(x: Boolean): Int = 0

@main def Test =
val c = MyClass(1, 2, 3, 4)
assert(c._1 == 2)
assert(c._2 == 3)
assert(c._3 == 4)
assert(c._4 == 4)