From f49f00cb311b59ad1f4be97e7c4f4e93e5022cde Mon Sep 17 00:00:00 2001 From: VladimirNik Date: Fri, 19 Feb 2016 17:08:02 +0100 Subject: [PATCH] Add support for private[this] parameter in value classes --- src/dotty/tools/dotc/transform/Getters.scala | 5 +++-- src/dotty/tools/dotc/typer/RefChecks.scala | 2 -- tests/neg/valueClasses.scala | 2 -- tests/pos/valueclasses/privatethisparam.scala | 18 ++++++++++++++++++ 4 files changed, 21 insertions(+), 6 deletions(-) create mode 100644 tests/pos/valueclasses/privatethisparam.scala diff --git a/src/dotty/tools/dotc/transform/Getters.scala b/src/dotty/tools/dotc/transform/Getters.scala index 7657918228b0..882e42d2f632 100644 --- a/src/dotty/tools/dotc/transform/Getters.scala +++ b/src/dotty/tools/dotc/transform/Getters.scala @@ -12,6 +12,7 @@ import Constants._ import TreeTransforms._ import Flags._ import Decorators._ +import ValueClasses._ /** Performs the following rewritings for fields of a class: * @@ -34,7 +35,7 @@ import Decorators._ * * Omitted from the rewritings are * - * - private[this] fields in non-trait classes + * - private[this] fields in classes (excluding traits, value classes) * - fields generated for static modules (TODO: needed?) * - parameters, static fields, and fields coming from Java * @@ -53,7 +54,7 @@ class Getters extends MiniPhaseTransform with SymTransformer { thisTransform => override def transformSym(d: SymDenotation)(implicit ctx: Context): SymDenotation = { def noGetterNeeded = d.is(NoGetterNeeded) || - d.initial.asInstanceOf[SymDenotation].is(PrivateLocal) && !d.owner.is(Trait) && !d.is(Flags.Lazy) || + d.initial.asInstanceOf[SymDenotation].is(PrivateLocal) && !d.owner.is(Trait) && !isDerivedValueClass(d.owner) && !d.is(Flags.Lazy) || d.is(Module) && d.isStatic || d.isSelfSym if (d.isTerm && (d.is(Lazy) || d.owner.isClass) && d.info.isValueType && !noGetterNeeded) { diff --git a/src/dotty/tools/dotc/typer/RefChecks.scala b/src/dotty/tools/dotc/typer/RefChecks.scala index bb411c6b5e11..afbb43faf37d 100644 --- a/src/dotty/tools/dotc/typer/RefChecks.scala +++ b/src/dotty/tools/dotc/typer/RefChecks.scala @@ -715,8 +715,6 @@ object RefChecks { case List(param) => if (param.is(Mutable)) ctx.error("value class parameter must not be a var", param.pos) - if (param.is(PrivateLocal)) - ctx.error("value class parameter must not be private[this]", param.pos) case _ => ctx.error("value class needs to have exactly one val parameter", clazz.pos) } diff --git a/tests/neg/valueClasses.scala b/tests/neg/valueClasses.scala index ae90ef63ce3c..74a576ce6732 100644 --- a/tests/neg/valueClasses.scala +++ b/tests/neg/valueClasses.scala @@ -6,5 +6,3 @@ class B1 { class B2(x: Int) extends AnyVal // error: value class may not be a local class } } -class C(private[this] val u: Int) extends AnyVal // error: value class parameter must not be private[this] -class D(u: Int) extends AnyVal // error: value class parameter must not be private[this] diff --git a/tests/pos/valueclasses/privatethisparam.scala b/tests/pos/valueclasses/privatethisparam.scala new file mode 100644 index 000000000000..77ca9851c510 --- /dev/null +++ b/tests/pos/valueclasses/privatethisparam.scala @@ -0,0 +1,18 @@ +package privatethisparam + +class Meter[T](x: T) extends AnyVal { + def zero: T = x +} + +class Meter2(private[this] val x: Int) extends AnyVal { + def foo = x +} + +object Test { + def bar = new Meter2(42) + def useZero = new Meter(5).zero + def test: Unit = { + val m1 = new Meter(1) + m1.zero + } +} \ No newline at end of file