Skip to content

Commit 6a93dd0

Browse files
committed
Copy access flags to derived definitions during desugaring
Previously, some definitions were too public, others too private.
1 parent ea407f1 commit 6a93dd0

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

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

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,7 @@ object desugar {
232232
def classDef(cdef: TypeDef)(implicit ctx: Context): Tree = {
233233
val TypeDef(name, impl @ Template(constr0, parents, self, _)) = cdef
234234
val mods = cdef.mods
235+
val accessFlags = (mods.flags & AccessFlags).toCommonFlags
235236

236237
val (constr1, defaultGetters) = defDef(constr0, isPrimaryConstructor = true) match {
237238
case meth: DefDef => (meth, Nil)
@@ -312,6 +313,7 @@ object desugar {
312313
case ValDef(_, tpt, _) => isRepeated(tpt)
313314
case _ => false
314315
})
316+
315317
val copyMeths =
316318
if (mods.is(Abstract) || hasRepeatedParam) Nil // cannot have default arguments for repeated parameters, hence copy method is not issued
317319
else {
@@ -346,7 +348,7 @@ object desugar {
346348
moduleDef(
347349
ModuleDef(
348350
name.toTermName, Template(emptyConstructor, parentTpt :: Nil, EmptyValDef, defs))
349-
.withMods(synthetic))
351+
.withFlags(Synthetic | accessFlags))
350352
.withPos(cdef.pos).toList
351353

352354
// The companion object definitions, if a companion is needed, Nil otherwise.
@@ -371,7 +373,7 @@ object desugar {
371373
if (mods is Abstract) Nil
372374
else
373375
DefDef(nme.apply, derivedTparams, derivedVparamss, TypeTree(), creatorExpr)
374-
.withMods(synthetic | (constr1.mods.flags & DefaultParameterized)) :: Nil
376+
.withFlags(Synthetic | (constr1.mods.flags & DefaultParameterized)) :: Nil
375377
val unapplyMeth = {
376378
val unapplyParam = makeSyntheticParameter(tpt = classTypeRef)
377379
val unapplyRHS = if (arity == 0) Literal(Constant(true)) else Ident(unapplyParam.name)
@@ -403,7 +405,7 @@ object desugar {
403405
// implicit wrapper is typechecked in same scope as constructor, so
404406
// we can reuse the constructor parameters; no derived params are needed.
405407
DefDef(name.toTermName, constrTparams, constrVparamss, classTypeRef, creatorExpr)
406-
.withFlags(Synthetic | Implicit)
408+
.withFlags(Synthetic | Implicit | accessFlags)
407409
.withPos(cdef.pos) :: Nil
408410

409411

@@ -453,7 +455,7 @@ object desugar {
453455
val clsName = name.moduleClassName
454456
val clsRef = Ident(clsName)
455457
val modul = ValDef(name, clsRef, New(clsRef, Nil))
456-
.withMods(mods | ModuleCreationFlags)
458+
.withMods(mods | ModuleCreationFlags | mods.flags & AccessFlags)
457459
.withPos(mdef.pos)
458460
val ValDef(selfName, selfTpt, _) = tmpl.self
459461
val selfMods = tmpl.self.mods
@@ -515,7 +517,7 @@ object desugar {
515517
derivedValDef(named, tpt, matchExpr, mods)
516518
case _ =>
517519
val tmpName = ctx.freshName().toTermName
518-
val patFlags = PrivateLocal | Synthetic | (mods.flags & Lazy)
520+
val patFlags = mods.flags & AccessFlags | Synthetic | (mods.flags & Lazy)
519521
val firstDef = ValDef(tmpName, TypeTree(), matchExpr).withFlags(patFlags)
520522
def selector(n: Int) = Select(Ident(tmpName), nme.selectorName(n))
521523
val restDefs =

tests/neg/i997a.scala

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class C {
2+
3+
class Super
4+
5+
object O {
6+
7+
private case class CC(x: Int)
8+
9+
private implicit class D(x: Int) extends Super
10+
}
11+
12+
import O._
13+
14+
println(O.CC(1))
15+
16+
val s: Super = 1
17+
18+
19+
}

0 commit comments

Comments
 (0)