diff options
author | Guillaume Martres <smarter@ubuntu.com> | 2016-03-13 16:23:05 +0100 |
---|---|---|
committer | Guillaume Martres <smarter@ubuntu.com> | 2016-03-13 16:23:05 +0100 |
commit | daffba9de6176ab75f292e2281aec87d5bb23124 (patch) | |
tree | 3b81e96c2b04cbabb469f711cb7dfe1264701620 | |
parent | e68d68414fa346fad9dee204746d826ee172e861 (diff) | |
parent | 6b983e39ff7880994c6edfec7e79a151f83e83f3 (diff) | |
download | dotty-daffba9de6176ab75f292e2281aec87d5bb23124.tar.gz dotty-daffba9de6176ab75f292e2281aec87d5bb23124.tar.bz2 dotty-daffba9de6176ab75f292e2281aec87d5bb23124.zip |
Merge pull request #1164 from dotty-staging/fix-#1140
Make sure lazy accessors in traits are not private.
-rw-r--r-- | src/dotty/tools/dotc/core/Flags.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/transform/ExpandPrivate.scala | 5 | ||||
-rw-r--r-- | src/dotty/tools/dotc/transform/FirstTransform.scala | 2 | ||||
-rw-r--r-- | src/dotty/tools/dotc/transform/Mixin.scala | 32 | ||||
-rw-r--r-- | src/dotty/tools/dotc/transform/TreeChecker.scala | 5 | ||||
-rw-r--r-- | tests/run/i1140/A_1.scala | 4 | ||||
-rw-r--r-- | tests/run/i1140/B_2.scala | 5 |
7 files changed, 38 insertions, 17 deletions
diff --git a/src/dotty/tools/dotc/core/Flags.scala b/src/dotty/tools/dotc/core/Flags.scala index ebf4c637a..f866621f2 100644 --- a/src/dotty/tools/dotc/core/Flags.scala +++ b/src/dotty/tools/dotc/core/Flags.scala @@ -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) diff --git a/src/dotty/tools/dotc/transform/ExpandPrivate.scala b/src/dotty/tools/dotc/transform/ExpandPrivate.scala index f44be3958..a6f203478 100644 --- a/src/dotty/tools/dotc/transform/ExpandPrivate.scala +++ b/src/dotty/tools/dotc/transform/ExpandPrivate.scala @@ -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) diff --git a/src/dotty/tools/dotc/transform/FirstTransform.scala b/src/dotty/tools/dotc/transform/FirstTransform.scala index 6b13d46b2..37ae1d94e 100644 --- a/src/dotty/tools/dotc/transform/FirstTransform.scala +++ b/src/dotty/tools/dotc/transform/FirstTransform.scala @@ -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) diff --git a/src/dotty/tools/dotc/transform/Mixin.scala b/src/dotty/tools/dotc/transform/Mixin.scala index 32b268fc7..3ec2aedcb 100644 --- a/src/dotty/tools/dotc/transform/Mixin.scala +++ b/src/dotty/tools/dotc/transform/Mixin.scala @@ -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, @@ -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 diff --git a/src/dotty/tools/dotc/transform/TreeChecker.scala b/src/dotty/tools/dotc/transform/TreeChecker.scala index 150a632a1..a260963e9 100644 --- a/src/dotty/tools/dotc/transform/TreeChecker.scala +++ b/src/dotty/tools/dotc/transform/TreeChecker.scala @@ -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) } diff --git a/tests/run/i1140/A_1.scala b/tests/run/i1140/A_1.scala new file mode 100644 index 000000000..e16a8162d --- /dev/null +++ b/tests/run/i1140/A_1.scala @@ -0,0 +1,4 @@ +trait A { + val foo = 0 + x + private lazy val x = 1 +} diff --git a/tests/run/i1140/B_2.scala b/tests/run/i1140/B_2.scala new file mode 100644 index 000000000..aa576c256 --- /dev/null +++ b/tests/run/i1140/B_2.scala @@ -0,0 +1,5 @@ +object Test extends A { + def main(args: Array[String]): Unit = { + assert(foo == 1) + } +} |