Skip to content

Commit 605c465

Browse files
committed
Don't make selectors _i retractable
If we allow user-defined selectors _1, _2, and so on, we break the link with pattern matching.
1 parent ae2d781 commit 605c465

File tree

4 files changed

+11
-6
lines changed

4 files changed

+11
-6
lines changed

compiler/src/dotty/tools/dotc/ast/Desugar.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,19 @@ object desugar {
2020
/** Info of a variable in a pattern: The named tree and its type */
2121
private type VarInfo = (NameTree, Tree)
2222

23-
/** Names of methods that are added unconditionally to case classes */
24-
def isDesugaredCaseClassMethodName(name: Name)(implicit ctx: Context): Boolean = name match {
23+
/** Is `name` the name of a method that can be invalidated as a compiler-generated
24+
* case class method that clashes with a user-defined method?
25+
*/
26+
def isRetractableCaseClassMethodName(name: Name)(implicit ctx: Context): Boolean = name match {
2527
case nme.apply | nme.unapply | nme.copy => true
2628
case DefaultGetterName(nme.copy, _) => true
27-
case _ => name.isSelectorName
29+
case _ => false
2830
}
2931

32+
/** Is `name` the name of a method that is added unconditionally to case classes? */
33+
def isDesugaredCaseClassMethodName(name: Name)(implicit ctx: Context): Boolean =
34+
isRetractableCaseClassMethodName(name) || name.isSelectorName
35+
3036
// ----- DerivedTypeTrees -----------------------------------
3137

3238
class SetterParamTree extends DerivedTypeTree {

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,7 +780,7 @@ class Namer { typer: Typer =>
780780
}
781781
val isClashingSynthetic =
782782
denot.is(Synthetic) &&
783-
desugar.isDesugaredCaseClassMethodName(denot.name) &&
783+
desugar.isRetractableCaseClassMethodName(denot.name) &&
784784
isCaseClass(denot.owner) &&
785785
denot.owner.info.decls.lookupAll(denot.name).exists(alt =>
786786
alt != denot.symbol && alt.info.matchesLoosely(denot.info))

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1415,7 +1415,7 @@ class Typer extends Namer
14151415

14161416
def typedDefDef(ddef: untpd.DefDef, sym: Symbol)(implicit ctx: Context): Tree = track("typedDefDef") {
14171417
if (!sym.info.exists) { // it's a discarded synthetic case class method, drop it
1418-
assert(sym.is(Synthetic) && desugar.isDesugaredCaseClassMethodName(sym.name))
1418+
assert(sym.is(Synthetic) && desugar.isRetractableCaseClassMethodName(sym.name))
14191419
sym.owner.info.decls.openForMutations.unlink(sym)
14201420
return EmptyTree
14211421
}

tests/pos/i4564.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ object ClashNoSig { // ok
1414
}
1515
}
1616
case class ClashNoSig private (x: Int) {
17-
def _1: Int = x
1817
def copy(y: Int) = ClashNoSig(y)
1918
}
2019

0 commit comments

Comments
 (0)