From d04cda14d722ff365b4c3b543de008d93772410b Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Wed, 28 Sep 2016 10:15:12 -0700 Subject: Make some name suffixes constants There's still a lot of duplication, as well as plenty of opportunities for constant folding / simplification. --- src/library/scala/reflect/NameTransformer.scala | 19 ++++++++++--------- src/reflect/scala/reflect/internal/StdNames.scala | 19 ++++++++++--------- 2 files changed, 20 insertions(+), 18 deletions(-) (limited to 'src') diff --git a/src/library/scala/reflect/NameTransformer.scala b/src/library/scala/reflect/NameTransformer.scala index ae36f5edc2..bdf5165df5 100644 --- a/src/library/scala/reflect/NameTransformer.scala +++ b/src/library/scala/reflect/NameTransformer.scala @@ -13,15 +13,16 @@ package reflect * Also provides some constants. */ object NameTransformer { - // XXX Short term: providing a way to alter these without having to recompile - // the compiler before recompiling the compiler. - val MODULE_SUFFIX_STRING = sys.props.getOrElse("SCALA_MODULE_SUFFIX_STRING", "$") - val NAME_JOIN_STRING = sys.props.getOrElse("SCALA_NAME_JOIN_STRING", "$") - val MODULE_INSTANCE_NAME = "MODULE$" - val LOCAL_SUFFIX_STRING = " " - val LAZY_LOCAL_SUFFIX_STRING = "$lzy" - val SETTER_SUFFIX_STRING = "_$eq" - val TRAIT_SETTER_SEPARATOR_STRING = "$_setter_$" + // TODO: reduce duplication with and in StdNames + // I made these constants because we cannot change them without bumping our major version anyway. + final val NAME_JOIN_STRING = "$" + final val MODULE_SUFFIX_STRING = "$" + final val MODULE_INSTANCE_NAME = "MODULE$" + final val LOCAL_SUFFIX_STRING = " " + final val LAZY_LOCAL_SUFFIX_STRING = "$lzy" + final val MODULE_VAR_SUFFIX_STRING = "$module" + final val SETTER_SUFFIX_STRING = "_$eq" + final val TRAIT_SETTER_SEPARATOR_STRING = "$_setter_$" private val nops = 128 private val ncodes = 26 * 26 diff --git a/src/reflect/scala/reflect/internal/StdNames.scala b/src/reflect/scala/reflect/internal/StdNames.scala index 1a6c84b19e..2e820a68e0 100644 --- a/src/reflect/scala/reflect/internal/StdNames.scala +++ b/src/reflect/scala/reflect/internal/StdNames.scala @@ -92,14 +92,15 @@ trait StdNames { def flattenedName(segments: Name*): NameType = compactify(segments mkString NAME_JOIN_STRING) - val NAME_JOIN_STRING: String = NameTransformer.NAME_JOIN_STRING - val MODULE_SUFFIX_STRING: String = NameTransformer.MODULE_SUFFIX_STRING - val LOCAL_SUFFIX_STRING: String = NameTransformer.LOCAL_SUFFIX_STRING - val LAZY_LOCAL_SUFFIX_STRING: String = NameTransformer.LAZY_LOCAL_SUFFIX_STRING - - val TRAIT_SETTER_SEPARATOR_STRING: String = NameTransformer.TRAIT_SETTER_SEPARATOR_STRING - - val SINGLETON_SUFFIX: String = ".type" + // TODO: what is the purpose of all this duplication!?!?! + // I made these constants because we cannot change them without bumping our major version anyway. + final val NAME_JOIN_STRING = NameTransformer.NAME_JOIN_STRING + final val MODULE_SUFFIX_STRING = NameTransformer.MODULE_SUFFIX_STRING + final val MODULE_VAR_SUFFIX_STRING = NameTransformer.MODULE_VAR_SUFFIX_STRING + final val LOCAL_SUFFIX_STRING = NameTransformer.LOCAL_SUFFIX_STRING + final val LAZY_LOCAL_SUFFIX_STRING = NameTransformer.LAZY_LOCAL_SUFFIX_STRING + final val TRAIT_SETTER_SEPARATOR_STRING = NameTransformer.TRAIT_SETTER_SEPARATOR_STRING + final val SINGLETON_SUFFIX = ".type" val ANON_CLASS_NAME: NameType = "$anon" val DELAMBDAFY_LAMBDA_CLASS_NAME: NameType = "$lambda" @@ -108,7 +109,7 @@ trait StdNames { val EMPTY_PACKAGE_NAME: NameType = "" val IMPORT: NameType = "" val MODULE_SUFFIX_NAME: NameType = MODULE_SUFFIX_STRING - val MODULE_VAR_SUFFIX: NameType = "$module" + val MODULE_VAR_SUFFIX: NameType = MODULE_VAR_SUFFIX_STRING val PACKAGE: NameType = "package" val ROOT: NameType = "" val SPECIALIZED_SUFFIX: NameType = "$sp" -- cgit v1.2.3 From 493e22f321d6e7774e74419242b6e3d61eff6bad Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Tue, 27 Sep 2016 16:58:25 -0700 Subject: Emit local module like lazy val The motivation is to use the new fine-grained lock scoping that local lazies have since #5294. Fixes scala/scala-dev#235 Co-Authored-By: Jason Zaugg --- .../scala/tools/nsc/transform/Fields.scala | 42 +++++++++++----------- test/files/run/SD-235.scala | 39 ++++++++++++++++++++ test/files/run/delambdafy_t6028.check | 17 ++++----- test/files/run/local_obj.scala | 9 +++++ test/files/run/t6028.check | 17 ++++----- .../nsc/backend/jvm/opt/ScalaInlineInfoTest.scala | 4 +-- 6 files changed, 88 insertions(+), 40 deletions(-) create mode 100644 test/files/run/SD-235.scala create mode 100644 test/files/run/local_obj.scala (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/transform/Fields.scala b/src/compiler/scala/tools/nsc/transform/Fields.scala index 6cf6a5abce..0c6982384d 100644 --- a/src/compiler/scala/tools/nsc/transform/Fields.scala +++ b/src/compiler/scala/tools/nsc/transform/Fields.scala @@ -193,20 +193,6 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor // not be emitted as ACC_FINAL. They are FINAL in the Scala sense, though: cannot be overridden. private final val ModuleOrLazyFieldFlags = FINAL | PrivateLocal | SYNTHETIC | NEEDS_TREES - private def newModuleVarSymbol(owner: Symbol, module: Symbol, tp: Type): TermSymbol = { -// println(s"new module var in $site for $module of type $tp") - val flags = MODULEVAR | (if (owner.isClass) ModuleOrLazyFieldFlags else 0) - - val moduleVar = - (owner.newVariable(nme.moduleVarName(module.name.toTermName), module.pos.focus, flags) - setInfo tp - addAnnotation VolatileAttr) - - moduleOrLazyVarOf(module) = moduleVar - - moduleVar - } - private def moduleInit(module: Symbol, moduleVar: Symbol) = { // println(s"moduleInit for $module in ${module.ownerChain} --> ${moduleVarOf.get(module)}") def moduleVarRef = gen.mkAttributedRef(moduleVar) @@ -380,8 +366,16 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor (existingGetter ne NoSymbol) && (tp matches (site memberInfo existingGetter).resultType) // !existingGetter.isDeferred && -- see (3) } - def newModuleVarMember(member: Symbol): TermSymbol = - newModuleVarSymbol(clazz, member, site.memberType(member).resultType) + def newModuleVarMember(module: Symbol): TermSymbol = { + val moduleVar = + (clazz.newVariable(nme.moduleVarName(module.name.toTermName), module.pos.focus, MODULEVAR | ModuleOrLazyFieldFlags) + setInfo site.memberType(module).resultType + addAnnotation VolatileAttr) + + moduleOrLazyVarOf(module) = moduleVar + + moduleVar + } def newLazyVarMember(member: Symbol): TermSymbol = Fields.this.newLazyVarMember(clazz, member, site.memberType(member).resultType) @@ -531,7 +525,8 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor def mkTypedValDef(sym: Symbol, rhs: Tree = EmptyTree) = typedPos(sym.pos)(ValDef(sym, rhs)).asInstanceOf[ValDef] /** - * Desugar a local `lazy val x: Int = rhs` into + * Desugar a local `lazy val x: Int = rhs` or a local object into + * * ``` * val x$lzy = new scala.runtime.LazyInt() * def x$lzycompute(): Int = @@ -541,10 +536,13 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor * } * def x(): Int = if (x$lzy.initialized()) x$lzy.value() else x$lzycompute() * ``` + * + * The expansion is the same for local lazy vals and local objects, + * except for the name of the val ($lzy or */ private def mkLazyLocalDef(lazyVal: Symbol, rhs: Tree): Tree = { import CODE._ - import scala.reflect.NameTransformer.LAZY_LOCAL_SUFFIX_STRING + import scala.reflect.{NameTransformer => nx} val owner = lazyVal.owner val lazyValType = lazyVal.tpe.resultType @@ -555,8 +553,7 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor val lazyName = lazyVal.name.toTermName val pos = lazyVal.pos.focus - // used twice: once in the same owner as the lazy val, another time inside the compute method - val localLazyName = lazyName append LAZY_LOCAL_SUFFIX_STRING + val localLazyName = lazyName append (if (lazyVal.isModule) nx.MODULE_VAR_SUFFIX_STRING else nx.LAZY_LOCAL_SUFFIX_STRING) // The lazy holder val need not be mutable, as we write to its field. // In fact, it MUST not be mutable to avoid capturing it as an ObjectRef in lambdalift @@ -730,8 +727,9 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor val cd = super.transform(ClassDef(statSym.moduleClass, impl) setType NoType) if (currOwner.isClass) cd else { // local module -- symbols cannot be generated by info transformer, so do it all here - val moduleVar = newModuleVarSymbol(currOwner, statSym, statSym.info.resultType) - Thicket(cd :: mkTypedValDef(moduleVar) :: mkAccessor(statSym)(moduleInit(statSym, moduleVar)) :: Nil) + val Block(stats, _) = mkLazyLocalDef(statSym, gen.newModule(statSym, statSym.info.resultType)) + + Thicket(cd :: stats) } case tree => diff --git a/test/files/run/SD-235.scala b/test/files/run/SD-235.scala new file mode 100644 index 0000000000..eb79c6fe71 --- /dev/null +++ b/test/files/run/SD-235.scala @@ -0,0 +1,39 @@ +class C { + var ORef: Object = null + def test = { + object O { + assert(!Thread.holdsLock(C.this)) + assert(Thread.holdsLock(ORef)) + } + val captor = new { def oh = O } + val refField = captor.getClass.getDeclaredFields.last + refField.setAccessible(true) + assert(refField.getType.toString.contains("LazyRef"), refField) + ORef = refField.get(captor) + O + } +} + +class D { + var ORef: Object = null + def test = { + lazy val O = { + assert(!Thread.holdsLock(D.this)) + assert(Thread.holdsLock(ORef)) + "O" + } + val captor = new { def oh = O } + val refField = captor.getClass.getDeclaredFields.last + refField.setAccessible(true) + assert(refField.getType.toString.contains("LazyRef"), refField) + ORef = refField.get(captor) + O + } +} + +object Test { + def main(args: Array[String]): Unit = { + new C().test + new D().test + } +} diff --git a/test/files/run/delambdafy_t6028.check b/test/files/run/delambdafy_t6028.check index 7b319c92dd..86cb1d5e97 100644 --- a/test/files/run/delambdafy_t6028.check +++ b/test/files/run/delambdafy_t6028.check @@ -15,7 +15,7 @@ package { } }; def bar(barParam: String): Object = { - @volatile var MethodLocalObject$module: runtime.VolatileObjectRef = scala.runtime.VolatileObjectRef.zero(); + lazy val MethodLocalObject$module: scala.runtime.LazyRef = new scala.runtime.LazyRef(); T.this.MethodLocalObject$1(barParam, MethodLocalObject$module) }; def tryy(tryyParam: String): Function0 = { @@ -42,13 +42,14 @@ package { def $outer(): T = MethodLocalObject$2.this.$outer; def $outer(): T = MethodLocalObject$2.this.$outer }; - final private[this] def MethodLocalObject$lzycompute$1(barParam$1: String, MethodLocalObject$module$1: runtime.VolatileObjectRef): Unit = T.this.synchronized[Unit](if (MethodLocalObject$module$1.elem.$asInstanceOf[T#MethodLocalObject$2.type]().eq(null)) - MethodLocalObject$module$1.elem = new T#MethodLocalObject$2.type(T.this, barParam$1)); - final private[this] def MethodLocalObject$1(barParam$1: String, MethodLocalObject$module$1: runtime.VolatileObjectRef): T#MethodLocalObject$2.type = { - if (MethodLocalObject$module$1.elem.$asInstanceOf[T#MethodLocalObject$2.type]().eq(null)) - T.this.MethodLocalObject$lzycompute$1(barParam$1, MethodLocalObject$module$1); - (MethodLocalObject$module$1.elem.$asInstanceOf[T#MethodLocalObject$2.type](): T#MethodLocalObject$2.type) - }; + final private[this] def MethodLocalObject$lzycompute$1(barParam$1: String, MethodLocalObject$module$1: scala.runtime.LazyRef): T#MethodLocalObject$2.type = MethodLocalObject$module$1.synchronized[T#MethodLocalObject$2.type](if (MethodLocalObject$module$1.initialized()) + MethodLocalObject$module$1.value().$asInstanceOf[T#MethodLocalObject$2.type]() + else + MethodLocalObject$module$1.initialize(new T#MethodLocalObject$2.type(T.this, barParam$1)).$asInstanceOf[T#MethodLocalObject$2.type]()); + final private[this] def MethodLocalObject$1(barParam$1: String, MethodLocalObject$module$1: scala.runtime.LazyRef): T#MethodLocalObject$2.type = if (MethodLocalObject$module$1.initialized()) + MethodLocalObject$module$1.value().$asInstanceOf[T#MethodLocalObject$2.type]() + else + T.this.MethodLocalObject$lzycompute$1(barParam$1, MethodLocalObject$module$1); final private[this] def $anonfun$tryy$1(tryyParam$1: String, tryyLocal$1: runtime.ObjectRef): Unit = try { tryyLocal$1.elem = tryyParam$1 } finally () diff --git a/test/files/run/local_obj.scala b/test/files/run/local_obj.scala new file mode 100644 index 0000000000..25123f7078 --- /dev/null +++ b/test/files/run/local_obj.scala @@ -0,0 +1,9 @@ +class C { + val z = 2 + def mod = { object x { val y = z } ; x.y } +} + +object Test extends App { + val c = new C + assert(c.mod == c.z, s"${c.mod} != ${c.z}") +} diff --git a/test/files/run/t6028.check b/test/files/run/t6028.check index 903ea3b753..05634fa8eb 100644 --- a/test/files/run/t6028.check +++ b/test/files/run/t6028.check @@ -15,7 +15,7 @@ package { } }; def bar(barParam: Int): Object = { - @volatile var MethodLocalObject$module: runtime.VolatileObjectRef = scala.runtime.VolatileObjectRef.zero(); + lazy val MethodLocalObject$module: scala.runtime.LazyRef = new scala.runtime.LazyRef(); T.this.MethodLocalObject$1(barParam, MethodLocalObject$module) }; def tryy(tryyParam: Int): Function0 = { @@ -54,13 +54,14 @@ package { def $outer(): T = MethodLocalObject$2.this.$outer; def $outer(): T = MethodLocalObject$2.this.$outer }; - final private[this] def MethodLocalObject$lzycompute$1(barParam$1: Int, MethodLocalObject$module$1: runtime.VolatileObjectRef): Unit = T.this.synchronized[Unit](if (MethodLocalObject$module$1.elem.$asInstanceOf[T#MethodLocalObject$2.type]().eq(null)) - MethodLocalObject$module$1.elem = new T#MethodLocalObject$2.type(T.this, barParam$1)); - final private[this] def MethodLocalObject$1(barParam$1: Int, MethodLocalObject$module$1: runtime.VolatileObjectRef): T#MethodLocalObject$2.type = { - if (MethodLocalObject$module$1.elem.$asInstanceOf[T#MethodLocalObject$2.type]().eq(null)) - T.this.MethodLocalObject$lzycompute$1(barParam$1, MethodLocalObject$module$1); - (MethodLocalObject$module$1.elem.$asInstanceOf[T#MethodLocalObject$2.type](): T#MethodLocalObject$2.type) - }; + final private[this] def MethodLocalObject$lzycompute$1(barParam$1: Int, MethodLocalObject$module$1: scala.runtime.LazyRef): T#MethodLocalObject$2.type = MethodLocalObject$module$1.synchronized[T#MethodLocalObject$2.type](if (MethodLocalObject$module$1.initialized()) + MethodLocalObject$module$1.value().$asInstanceOf[T#MethodLocalObject$2.type]() + else + MethodLocalObject$module$1.initialize(new T#MethodLocalObject$2.type(T.this, barParam$1)).$asInstanceOf[T#MethodLocalObject$2.type]()); + final private[this] def MethodLocalObject$1(barParam$1: Int, MethodLocalObject$module$1: scala.runtime.LazyRef): T#MethodLocalObject$2.type = if (MethodLocalObject$module$1.initialized()) + MethodLocalObject$module$1.value().$asInstanceOf[T#MethodLocalObject$2.type]() + else + T.this.MethodLocalObject$lzycompute$1(barParam$1, MethodLocalObject$module$1); @SerialVersionUID(value = 0) final class $anonfun$tryy$1 extends scala.runtime.AbstractFunction0$mcV$sp with Serializable { def ($outer: T, tryyParam$1: Int, tryyLocal$1: runtime.IntRef): <$anon: Function0> = { $anonfun$tryy$1.super.(); diff --git a/test/junit/scala/tools/nsc/backend/jvm/opt/ScalaInlineInfoTest.scala b/test/junit/scala/tools/nsc/backend/jvm/opt/ScalaInlineInfoTest.scala index 8861577366..5cedc483cd 100644 --- a/test/junit/scala/tools/nsc/backend/jvm/opt/ScalaInlineInfoTest.scala +++ b/test/junit/scala/tools/nsc/backend/jvm/opt/ScalaInlineInfoTest.scala @@ -105,10 +105,10 @@ class ScalaInlineInfoTest extends BytecodeTesting { ("x4$(LT;)I", MethodInlineInfo(true ,false,false)), ("x5()I", MethodInlineInfo(true, false,false)), ("x5$(LT;)I", MethodInlineInfo(true ,false,false)), - ("L$1(Lscala/runtime/VolatileObjectRef;)LT$L$2$;", MethodInlineInfo(true, false,false)), + ("L$1(Lscala/runtime/LazyRef;)LT$L$2$;", MethodInlineInfo(true, false,false)), ("nest$1()I", MethodInlineInfo(true, false,false)), ("$init$(LT;)V", MethodInlineInfo(true,false,false)), - ("L$lzycompute$1(Lscala/runtime/VolatileObjectRef;)V", MethodInlineInfo(true,false,false)) + ("L$lzycompute$1(Lscala/runtime/LazyRef;)LT$L$2$;", MethodInlineInfo(true,false,false)) ), None // warning ) -- cgit v1.2.3 From 515bc60e028c6d1c204e914b0869b4f0bd6dab8e Mon Sep 17 00:00:00 2001 From: Adriaan Moors Date: Thu, 29 Sep 2016 11:54:45 -0700 Subject: Clarify docs, variable name A local lazy val and a local object are expanded in the same way. --- .../scala/tools/nsc/transform/Fields.scala | 23 +++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'src') diff --git a/src/compiler/scala/tools/nsc/transform/Fields.scala b/src/compiler/scala/tools/nsc/transform/Fields.scala index 0c6982384d..aa2ccd9788 100644 --- a/src/compiler/scala/tools/nsc/transform/Fields.scala +++ b/src/compiler/scala/tools/nsc/transform/Fields.scala @@ -525,7 +525,8 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor def mkTypedValDef(sym: Symbol, rhs: Tree = EmptyTree) = typedPos(sym.pos)(ValDef(sym, rhs)).asInstanceOf[ValDef] /** - * Desugar a local `lazy val x: Int = rhs` or a local object into + * Desugar a local `lazy val x: Int = rhs` + * or a local `object x { ...}` (the rhs will be instantiating the module's class) into: * * ``` * val x$lzy = new scala.runtime.LazyInt() @@ -538,22 +539,22 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor * ``` * * The expansion is the same for local lazy vals and local objects, - * except for the name of the val ($lzy or + * except for the suffix of the underlying val's name ($lzy or $module) */ - private def mkLazyLocalDef(lazyVal: Symbol, rhs: Tree): Tree = { + private def mkLazyLocalDef(lazySym: Symbol, rhs: Tree): Tree = { import CODE._ import scala.reflect.{NameTransformer => nx} - val owner = lazyVal.owner + val owner = lazySym.owner - val lazyValType = lazyVal.tpe.resultType + val lazyValType = lazySym.tpe.resultType val refClass = lazyHolders.getOrElse(lazyValType.typeSymbol, LazyRefClass) val isUnit = refClass == LazyUnitClass val refTpe = if (refClass != LazyRefClass) refClass.tpe else appliedType(refClass.typeConstructor, List(lazyValType)) - val lazyName = lazyVal.name.toTermName - val pos = lazyVal.pos.focus + val lazyName = lazySym.name.toTermName + val pos = lazySym.pos.focus - val localLazyName = lazyName append (if (lazyVal.isModule) nx.MODULE_VAR_SUFFIX_STRING else nx.LAZY_LOCAL_SUFFIX_STRING) + val localLazyName = lazyName append (if (lazySym.isModule) nx.MODULE_VAR_SUFFIX_STRING else nx.LAZY_LOCAL_SUFFIX_STRING) // The lazy holder val need not be mutable, as we write to its field. // In fact, it MUST not be mutable to avoid capturing it as an ObjectRef in lambdalift @@ -573,14 +574,14 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor val computerSym = owner.newMethod(lazyName append nme.LAZY_SLOW_SUFFIX, pos, ARTIFACT | PRIVATE) setInfo MethodType(Nil, lazyValType) - val rhsAtComputer = rhs.changeOwner(lazyVal -> computerSym) + val rhsAtComputer = rhs.changeOwner(lazySym -> computerSym) val computer = mkAccessor(computerSym)(gen.mkSynchronized(Ident(holderSym))( If(initialized, getValue, if (isUnit) Block(rhsAtComputer :: Nil, Apply(initialize, Nil)) else Apply(initialize, rhsAtComputer :: Nil)))) - val accessor = mkAccessor(lazyVal)( + val accessor = mkAccessor(lazySym)( If(initialized, getValue, Apply(Ident(computerSym), Nil))) @@ -588,7 +589,7 @@ abstract class Fields extends InfoTransform with ast.TreeDSL with TypingTransfor // remove STABLE: prevent replacing accessor call of type Unit by BoxedUnit.UNIT in erasure // remove ACCESSOR: prevent constructors from eliminating the method body if the lazy val is // lifted into a trait (TODO: not sure about the details here) - lazyVal.resetFlag(STABLE | ACCESSOR) + lazySym.resetFlag(STABLE | ACCESSOR) Thicket(mkTypedValDef(holderSym, New(refTpe)) :: computer :: accessor :: Nil) } -- cgit v1.2.3