diff --git a/compiler/src/dotty/tools/dotc/ast/Desugar.scala b/compiler/src/dotty/tools/dotc/ast/Desugar.scala index ab3bb6897ed2..f634ba8e28e0 100644 --- a/compiler/src/dotty/tools/dotc/ast/Desugar.scala +++ b/compiler/src/dotty/tools/dotc/ast/Desugar.scala @@ -430,6 +430,8 @@ object desugar { // new C[Ts](paramss) lazy val creatorExpr = New(classTypeRef, constrVparamss nestedMap refOfDef) + val copiedAccessFlags = if (ctx.scala2Setting) EmptyFlags else AccessFlags + // Methods to add to a case class C[..](p1: T1, ..., pN: Tn)(moreParams) // def _1: T1 = this.p1 // ... @@ -469,7 +471,7 @@ object desugar { val copyRestParamss = derivedVparamss.tail.nestedMap(vparam => cpy.ValDef(vparam)(rhs = EmptyTree)) DefDef(nme.copy, derivedTparams, copyFirstParams :: copyRestParamss, TypeTree(), creatorExpr) - .withMods(synthetic) :: Nil + .withFlags(Synthetic | constr1.mods.flags & copiedAccessFlags) :: Nil } } @@ -574,7 +576,7 @@ object desugar { if (mods is Abstract) Nil else DefDef(nme.apply, derivedTparams, derivedVparamss, applyResultTpt, widenedCreatorExpr) - .withFlags(Synthetic | (constr1.mods.flags & DefaultParameterized)) :: widenDefs + .withFlags(Synthetic | constr1.mods.flags & (DefaultParameterized | copiedAccessFlags)) :: widenDefs val unapplyMeth = { val unapplyParam = makeSyntheticParameter(tpt = classTypeRef) val unapplyRHS = if (arity == 0) Literal(Constant(true)) else Ident(unapplyParam.name) @@ -658,8 +660,6 @@ object desugar { flatTree(cdef1 :: companions ::: implicitWrappers) } - val AccessOrSynthetic: FlagSet = AccessFlags | Synthetic - /** Expand * * object name extends parents { self => body } diff --git a/compiler/src/dotty/tools/dotc/core/TypeOps.scala b/compiler/src/dotty/tools/dotc/core/TypeOps.scala index 857b8e59ef59..aa6ce6207706 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeOps.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeOps.scala @@ -327,6 +327,11 @@ trait TypeOps { this: Context => // TODO: Make standalone object. } scala2Mode } + + /** Is option -language:Scala2 set? + * This test is used when we are too early in the pipeline to consider imports. + */ + def scala2Setting = ctx.settings.language.value.contains(nme.Scala2.toString) } object TypeOps { diff --git a/compiler/src/dotty/tools/dotc/interactive/Interactive.scala b/compiler/src/dotty/tools/dotc/interactive/Interactive.scala index b33d6c0407e1..77b0b5ec134c 100644 --- a/compiler/src/dotty/tools/dotc/interactive/Interactive.scala +++ b/compiler/src/dotty/tools/dotc/interactive/Interactive.scala @@ -22,7 +22,7 @@ object Interactive { import ast.tpd._ object Include { - case class Set private (val bits: Int) extends AnyVal { + case class Set private[Include] (val bits: Int) extends AnyVal { def | (that: Set): Set = Set(bits | that.bits) def except(that: Set): Set = Set(bits & ~that.bits) diff --git a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala index 63f907af6b5c..4fce70cfdcf9 100644 --- a/compiler/src/dotty/tools/dotc/parsing/Scanners.scala +++ b/compiler/src/dotty/tools/dotc/parsing/Scanners.scala @@ -242,7 +242,7 @@ object Scanners { // Scala 2 compatibility - val isScala2Mode: Boolean = ctx.settings.language.value.contains(nme.Scala2.toString) + val isScala2Mode: Boolean = ctx.scala2Setting /** Cannot use ctx.featureEnabled because accessing the context would force too much */ def testScala2Mode(msg: String, pos: Position = Position(offset)): Boolean = { diff --git a/tests/neg/private-case-class-constr.scala b/tests/neg/private-case-class-constr.scala new file mode 100644 index 000000000000..b8a045e62e89 --- /dev/null +++ b/tests/neg/private-case-class-constr.scala @@ -0,0 +1,10 @@ +case class Nat private (value: Int) +object Nat { + def apply(value: Int, disable: Boolean): Option[Nat] = + if (value < 0 || disable) None else Some(new Nat(value)) +} +object Test { + val n1o = Nat(2) // error + val n2o = Nat(2, false) // ok + for (n <- n2o) yield n.copy() // error +} \ No newline at end of file diff --git a/tests/pos/i4564.scala b/tests/pos/i4564.scala index fce09873a40f..3334d219e805 100644 --- a/tests/pos/i4564.scala +++ b/tests/pos/i4564.scala @@ -51,7 +51,7 @@ class BaseNCP[T] { } object NoClashPoly extends BaseNCP[Boolean] -case class NoClashPoly private(x: Int) +case class NoClashPoly (x: Int) class BaseCP[T] {