Skip to content

Make sure lazy accessors in traits are not private. #1164

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/core/Flags.scala
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ object Flags {
final val FromStartFlags =
AccessFlags | Module | Package | Deferred | Final | MethodOrHKCommon | Param | ParamAccessor | Scala2ExistentialCommon |
InSuperCall | Touched | JavaStatic | CovariantOrOuter | ContravariantOrLabel | ExpandedName | AccessorOrSealed |
CaseAccessorOrBaseTypeArg | Fresh | Frozen | Erroneous | ImplicitCommon | Permanent |
CaseAccessorOrBaseTypeArg | Fresh | Frozen | Erroneous | ImplicitCommon | Permanent | Synthetic |
LazyOrTrait | SuperAccessorOrScala2x | SelfNameOrImplClass

assert(FromStartFlags.isTermFlags && FromStartFlags.isTypeFlags)
Expand Down
5 changes: 4 additions & 1 deletion src/dotty/tools/dotc/transform/ExpandPrivate.scala
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ class ExpandPrivate extends MiniPhaseTransform with IdentityDenotTransformer { t
* static members of the companion class, we should tighten the condition below.
*/
private def ensurePrivateAccessible(d: SymDenotation)(implicit ctx: Context) =
if (d.is(PrivateTerm) && d.owner != ctx.owner.enclosingClass)
if (d.is(PrivateTerm) && d.owner != ctx.owner.enclosingClass) {
assert(d.symbol.sourceFile == ctx.source.file,
i"private ${d.symbol.showLocated} in ${d.symbol.sourceFile} accessed from ${ctx.owner.showLocated} in ${ctx.source.file}")
d.ensureNotPrivate.installAfter(thisTransform)
}

override def transformIdent(tree: Ident)(implicit ctx: Context, info: TransformerInfo) = {
ensurePrivateAccessible(tree.symbol)
Expand Down
2 changes: 1 addition & 1 deletion src/dotty/tools/dotc/transform/FirstTransform.scala
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class FirstTransform extends MiniPhaseTransform with IdentityDenotTransformer wi

private def newCompanion(name: TermName, forClass: Symbol)(implicit ctx: Context) = {
val modul = ctx.newCompleteModuleSymbol(forClass.owner, name, Synthetic, Synthetic,
defn.ObjectType :: Nil, Scopes.newScope)
defn.ObjectType :: Nil, Scopes.newScope, assocFile = forClass.asClass.assocFile)
val mc = modul.moduleClass

val mcComp = ctx.synthesizeCompanionMethod(nme.COMPANION_CLASS_METHOD, forClass, mc)
Expand Down
32 changes: 19 additions & 13 deletions src/dotty/tools/dotc/transform/Mixin.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,12 @@ class Mixin extends MiniPhaseTransform with SymTransformer { thisTransform =>
override def runsAfter: Set[Class[_ <: Phase]] = Set(classOf[Erasure])

override def transformSym(sym: SymDenotation)(implicit ctx: Context): SymDenotation =
if (sym.is(Accessor, butNot = Deferred | Lazy) && sym.owner.is(Trait))
sym.copySymDenotation(initFlags = sym.flags &~ ParamAccessor | Deferred).ensureNotPrivate
if (sym.is(Accessor, butNot = Deferred) && sym.owner.is(Trait)) {
val sym1 =
if (sym is Lazy) sym
else sym.copySymDenotation(initFlags = sym.flags &~ ParamAccessor | Deferred)
sym1.ensureNotPrivate
}
else if (sym.isConstructor && sym.owner.is(Trait))
sym.copySymDenotation(
name = nme.TRAIT_CONSTRUCTOR,
Expand All @@ -108,17 +112,19 @@ class Mixin extends MiniPhaseTransform with SymTransformer { thisTransform =>
sym

private def initializer(sym: Symbol)(implicit ctx: Context): TermSymbol = {
val initName = if(!sym.is(Lazy)) InitializerName(sym.name.asTermName) else sym.name.asTermName
sym.owner.info.decl(initName).suchThat(_.is(Lazy) == sym.is(Lazy)).symbol
.orElse(
ctx.newSymbol(
sym.owner,
initName,
Protected | Synthetic | Method,
sym.info,
coord = sym.symbol.coord).enteredAfter(thisTransform))
.asTerm
}
if (sym is Lazy) sym
else {
val initName = InitializerName(sym.name.asTermName)
sym.owner.info.decl(initName).symbol
.orElse(
ctx.newSymbol(
sym.owner,
initName,
Protected | Synthetic | Method,
sym.info,
coord = sym.symbol.coord).enteredAfter(thisTransform))
}
}.asTerm

override def transformTemplate(impl: Template)(implicit ctx: Context, info: TransformerInfo) = {
val cls = impl.symbol.owner.asClass
Expand Down
5 changes: 4 additions & 1 deletion src/dotty/tools/dotc/transform/TreeChecker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,10 @@ class TreeChecker extends Phase with SymTransformer {

val symbolsNotDefined = cls.classInfo.decls.toSet.filter(isNonMagicalMethod) -- impl.body.map(_.symbol) - constr.symbol

assert(symbolsNotDefined.isEmpty, i" $cls tree does not define methods: $symbolsNotDefined")
assert(symbolsNotDefined.isEmpty,
i" $cls tree does not define methods: ${symbolsNotDefined.toList}%, %\n" +
i"expected: ${cls.classInfo.decls.toSet.filter(isNonMagicalMethod).toList}%, %\n" +
i"defined: ${impl.body.map(_.symbol)}%, %")

super.typedClassDef(cdef, cls)
}
Expand Down
4 changes: 4 additions & 0 deletions tests/run/i1140/A_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
trait A {
val foo = 0 + x
private lazy val x = 1
}
5 changes: 5 additions & 0 deletions tests/run/i1140/B_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test extends A {
def main(args: Array[String]): Unit = {
assert(foo == 1)
}
}