diff options
author | Jason Zaugg <jzaugg@gmail.com> | 2013-09-19 14:53:03 +0200 |
---|---|---|
committer | Jason Zaugg <jzaugg@gmail.com> | 2013-09-29 22:37:17 +0200 |
commit | 4595ac665674b25af776d499ae1da61bb297d379 (patch) | |
tree | 5ce5c591fba9237a10abc82e7efeb6e61b755952 /test/files/run | |
parent | 0aaf59149871f817a67c1fefcc3b0457fcb5e4fc (diff) | |
download | scala-4595ac665674b25af776d499ae1da61bb297d379.tar.gz scala-4595ac665674b25af776d499ae1da61bb297d379.tar.bz2 scala-4595ac665674b25af776d499ae1da61bb297d379.zip |
SI-7859 Value classes may wrap a non-public member
We allow value class constructors to be non-public, so to be regular,
we should also allow the same for the param accessor.
This commit uses the 'makeNotPrivate' machinery to ensure that
the backend can generate the requisite unboxing calls.
This commit:
- refactors the code that enforced the restrictions, improving
a few error messages and positions. The remaining restrictions
needed some rewording in light of this change.
- allows value classes to have non-public, val parameters.
private[this] / protected[this] are still disallowed as value
classes don't have a concept of `this`, and because trying to
accomdate then would complicate the implementation.
This means that `class C(x: Int) extends AnyVal` is not allowed,
the user still must write `private val x: Int` or `val x: Int`.
- Outlaw `class C()()(val x: Int) extends AnyVal` to curtail any
bugs that might lurk in such a formulation.
The tests:
- Show that the privacy is respected in the typer phase, under
joint and separate compilation. We don't want a repeat performance
of SI-6601.
- Show that code that needs compiler-generated unboxes works under
both compilation scenarios
- Checks that the remaining restrictions are enforced and well
communicated.
Diffstat (limited to 'test/files/run')
-rw-r--r-- | test/files/run/t7859/A_1.scala | 11 | ||||
-rw-r--r-- | test/files/run/t7859/B_2.scala | 47 |
2 files changed, 58 insertions, 0 deletions
diff --git a/test/files/run/t7859/A_1.scala b/test/files/run/t7859/A_1.scala new file mode 100644 index 0000000000..74f0709d4d --- /dev/null +++ b/test/files/run/t7859/A_1.scala @@ -0,0 +1,11 @@ +class A(private val x: Int) extends AnyVal + +object A { + val Const = new A(0) +} + +class A1(protected val x: Int) extends AnyVal + +package p { + class A2(private[p] val x: Int) extends AnyVal +} diff --git a/test/files/run/t7859/B_2.scala b/test/files/run/t7859/B_2.scala new file mode 100644 index 0000000000..6b23af3abb --- /dev/null +++ b/test/files/run/t7859/B_2.scala @@ -0,0 +1,47 @@ +class B private (private val b: Int) extends AnyVal +object B { + val Const = new B(0) +} + +// These tests will require erasure to unbox the value class. +// We need to test under joint and separate compilation to check +// that the 'notPRIVATE' flag on the param accessor is pickled. +// +// See also SI-6601. +object Test { + def main(args: Array[String]) { + unboxA + unboxA1 + unboxA2 + unboxB + } + + def unboxA { + val o: Some[A] = Some(A.Const) + val a = o.get + def id(a: A): A = a + id(a) + } + + def unboxA1 { + val o: Some[A1] = Some(new A1(0)) + val a = o.get + def id(a: A1): A1 = a + id(a) + } + + def unboxA2 { + import p.A2 + val o: Some[A2] = Some(new A2(0)) + val a = o.get + def id(a: A2): A2 = a + id(a) + } + + def unboxB { + val o: Some[B] = Some(B.Const) + val b = o.get + def id(b: B): B = b + id(b) + } +} |