Skip to content

Commit

Permalink
Case class copy and apply inherit access modifiers from constructor
Browse files Browse the repository at this point in the history
  • Loading branch information
Jasper-M committed Jan 31, 2019
1 parent caf0db9 commit b4630df
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions src/compiler/scala/tools/nsc/typechecker/Unapplies.scala
Expand Up @@ -108,7 +108,7 @@ trait Unapplies extends ast.TreeDSL {
def inheritFromFun = !cdef.mods.hasAbstractFlag && cdef.tparams.isEmpty && (params match {
case List(ps) if ps.length <= MaxFunctionArity => true
case _ => false
})
}) && !constrMods(cdef).hasFlag(PRIVATE | PROTECTED)
def createFun = {
def primaries = params.head map (_.tpt)
gen.scalaFunctionConstr(primaries, toIdent(cdef), abstractFun = true)
Expand Down Expand Up @@ -141,11 +141,14 @@ trait Unapplies extends ast.TreeDSL {
val cparamss = constrParamss(cdef)
def classtpe = classType(cdef, tparams)
atPos(cdef.pos.focus)(
DefDef(mods, name, tparams, cparamss, classtpe,
DefDef(mods | (constrMods(cdef).flags & AccessFlags), name, tparams, cparamss, classtpe,
New(classtpe, mmap(cparamss)(gen.paramToArg)))
)
}


def constrMods(cdef: ClassDef): Modifiers = cdef.impl.body.collect { case DefDef(mods, termNames.CONSTRUCTOR, _, _, _, _) => mods }.head

/** The apply method corresponding to a case class
*/
def caseModuleApplyMeth(cdef: ClassDef): DefDef = factoryMeth(caseMods, nme.apply, cdef)
Expand Down Expand Up @@ -232,7 +235,7 @@ trait Unapplies extends ast.TreeDSL {
val argss = mmap(paramss)(toIdent)
val body: Tree = New(classTpe, argss)
val copyDefDef = atPos(cdef.pos.focus)(
DefDef(Modifiers(SYNTHETIC), nme.copy, tparams, paramss, TypeTree(), body)
DefDef(Modifiers(SYNTHETIC | (constrMods(cdef).flags & AccessFlags)), nme.copy, tparams, paramss, TypeTree(), body)
)
Some(copyDefDef)
}
Expand Down

0 comments on commit b4630df

Please sign in to comment.