Skip to content

Commit 69ef081

Browse files
authored
Merge pull request #5472 from dotty-staging/align-case-class-methods
Restrict visibility of copy and apply
2 parents eed948f + 6b840fc commit 69ef081

File tree

6 files changed

+22
-7
lines changed

6 files changed

+22
-7
lines changed

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -436,6 +436,8 @@ object desugar {
436436
// new C[Ts](paramss)
437437
lazy val creatorExpr = New(classTypeRef, constrVparamss nestedMap refOfDef)
438438

439+
val copiedAccessFlags = if (ctx.scala2Setting) EmptyFlags else AccessFlags
440+
439441
// Methods to add to a case class C[..](p1: T1, ..., pN: Tn)(moreParams)
440442
// def _1: T1 = this.p1
441443
// ...
@@ -475,7 +477,7 @@ object desugar {
475477
val copyRestParamss = derivedVparamss.tail.nestedMap(vparam =>
476478
cpy.ValDef(vparam)(rhs = EmptyTree))
477479
DefDef(nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, TypeTree(), creatorExpr)
478-
.withMods(synthetic) :: Nil
480+
.withFlags(Synthetic | constr1.mods.flags & copiedAccessFlags) :: Nil
479481
}
480482
}
481483

@@ -580,7 +582,7 @@ object desugar {
580582
if (mods is Abstract) Nil
581583
else
582584
DefDef(nme.apply, derivedTparams, derivedVparamss, applyResultTpt, widenedCreatorExpr)
583-
.withFlags(Synthetic | (constr1.mods.flags & DefaultParameterized)) :: widenDefs
585+
.withFlags(Synthetic | constr1.mods.flags & (DefaultParameterized | copiedAccessFlags)) :: widenDefs
584586
val unapplyMeth = {
585587
val unapplyParam = makeSyntheticParameter(tpt = classTypeRef)
586588
val unapplyRHS = if (arity == 0) Literal(Constant(true)) else Ident(unapplyParam.name)
@@ -664,8 +666,6 @@ object desugar {
664666
flatTree(cdef1 :: companions ::: implicitWrappers)
665667
}
666668

667-
val AccessOrSynthetic: FlagSet = AccessFlags | Synthetic
668-
669669
/** Expand
670670
*
671671
* object name extends parents { self => body }

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,11 @@ trait TypeOps { this: Context => // TODO: Make standalone object.
327327
}
328328
scala2Mode
329329
}
330+
331+
/** Is option -language:Scala2 set?
332+
* This test is used when we are too early in the pipeline to consider imports.
333+
*/
334+
def scala2Setting = ctx.settings.language.value.contains(nme.Scala2.toString)
330335
}
331336

332337
object TypeOps {

compiler/src/dotty/tools/dotc/interactive/Interactive.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ object Interactive {
2222
import ast.tpd._
2323

2424
object Include {
25-
case class Set private (val bits: Int) extends AnyVal {
25+
case class Set private[Include] (val bits: Int) extends AnyVal {
2626
def | (that: Set): Set = Set(bits | that.bits)
2727
def except(that: Set): Set = Set(bits & ~that.bits)
2828

compiler/src/dotty/tools/dotc/parsing/Scanners.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ object Scanners {
242242

243243
// Scala 2 compatibility
244244

245-
val isScala2Mode: Boolean = ctx.settings.language.value.contains(nme.Scala2.toString)
245+
val isScala2Mode: Boolean = ctx.scala2Setting
246246

247247
/** Cannot use ctx.featureEnabled because accessing the context would force too much */
248248
def testScala2Mode(msg: String, pos: Position = Position(offset)): Boolean = {
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
case class Nat private (value: Int)
2+
object Nat {
3+
def apply(value: Int, disable: Boolean): Option[Nat] =
4+
if (value < 0 || disable) None else Some(new Nat(value))
5+
}
6+
object Test {
7+
val n1o = Nat(2) // error
8+
val n2o = Nat(2, false) // ok
9+
for (n <- n2o) yield n.copy() // error
10+
}

tests/pos/i4564.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ class BaseNCP[T] {
5151
}
5252

5353
object NoClashPoly extends BaseNCP[Boolean]
54-
case class NoClashPoly private(x: Int)
54+
case class NoClashPoly (x: Int)
5555

5656

5757
class BaseCP[T] {

0 commit comments

Comments
 (0)