Skip to content

Commit 019dccb

Browse files
committed
Restrict visibility of copy and apply
Make the synthesized copy and apply methods of a case class have the same visibility as its private constructor.
1 parent 614265d commit 019dccb

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ object desugar {
469469
val copyRestParamss = derivedVparamss.tail.nestedMap(vparam =>
470470
cpy.ValDef(vparam)(rhs = EmptyTree))
471471
DefDef(nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, TypeTree(), creatorExpr)
472-
.withMods(synthetic) :: Nil
472+
.withFlags(Synthetic | constr1.mods.flags & AccessFlags) :: Nil
473473
}
474474
}
475475

@@ -574,7 +574,7 @@ object desugar {
574574
if (mods is Abstract) Nil
575575
else
576576
DefDef(nme.apply, derivedTparams, derivedVparamss, applyResultTpt, widenedCreatorExpr)
577-
.withFlags(Synthetic | (constr1.mods.flags & DefaultParameterized)) :: widenDefs
577+
.withFlags(Synthetic | constr1.mods.flags & (DefaultParameterized | AccessFlags)) :: widenDefs
578578
val unapplyMeth = {
579579
val unapplyParam = makeSyntheticParameter(tpt = classTypeRef)
580580
val unapplyRHS = if (arity == 0) Literal(Constant(true)) else Ident(unapplyParam.name)
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+
}

0 commit comments

Comments
 (0)