diff options
author | Martin Odersky <odersky@gmail.com> | 2012-09-16 20:55:44 +0200 |
---|---|---|
committer | Martin Odersky <odersky@gmail.com> | 2012-09-17 16:28:09 +0200 |
commit | c30a9bddc49592fef6d054cd0ce4e7ec2c035f71 (patch) | |
tree | 21f8401798184c34be4239cd6ce4f6dc6fa142c0 | |
parent | 61c83d2903f1c68c943ea646e75b4ca32d75f991 (diff) | |
download | scala-c30a9bddc49592fef6d054cd0ce4e7ec2c035f71.tar.gz scala-c30a9bddc49592fef6d054cd0ce4e7ec2c035f71.tar.bz2 scala-c30a9bddc49592fef6d054cd0ce4e7ec2c035f71.zip |
Fixes SI-6337 by disallowing nested value classes.
It seems for the moment too hard to allow this, and the functionality to have value classes wrap other value classes does not seem essential.
-rw-r--r-- | src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala | 3 | ||||
-rw-r--r-- | src/compiler/scala/tools/nsc/typechecker/Typers.scala | 4 | ||||
-rw-r--r-- | test/files/neg/t5878.check | 16 | ||||
-rw-r--r-- | test/files/neg/t6337.check | 7 | ||||
-rw-r--r-- | test/files/neg/t6337.scala | 21 |
5 files changed, 41 insertions, 10 deletions
diff --git a/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala b/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala index 0820d3e714..e09f30ae31 100644 --- a/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala +++ b/src/compiler/scala/tools/nsc/transform/ExtensionMethods.scala @@ -135,7 +135,8 @@ abstract class ExtensionMethods extends Transform with TypingTransformers { tree match { case Template(_, _, _) => if (currentOwner.isDerivedValueClass) { - checkNonCyclic(currentOwner.pos, Set(), currentOwner) + /* This is currently redundant since value classes may not + checkNonCyclic(currentOwner.pos, Set(), currentOwner) */ extensionDefs(currentOwner.companionModule) = new mutable.ListBuffer[Tree] currentOwner.primaryConstructor.makeNotPrivate(NoSymbol) super.transform(tree) diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index ce1c6089fb..6689db8156 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -1393,8 +1393,10 @@ trait Typers extends Modes with Adaptations with Tags { case List(acc) => def isUnderlyingAcc(sym: Symbol) = sym == acc || acc.hasAccessorFlag && sym == acc.accessed - if (acc.accessBoundary(clazz) != rootMirror.RootClass) + if (acc.accessBoundary(clazz) != rootMirror.RootClass) unit.error(acc.pos, "value class needs to have a publicly accessible val parameter") + else if (acc.tpe.typeSymbol.isDerivedValueClass) + unit.error(acc.pos, "value class may not wrap another user-defined value class") for (stat <- body) if (!treeInfo.isAllowedInUniversalTrait(stat) && !isUnderlyingAcc(stat.symbol)) unit.error(stat.pos, diff --git a/test/files/neg/t5878.check b/test/files/neg/t5878.check index 50dba0d272..c60c4653a2 100644 --- a/test/files/neg/t5878.check +++ b/test/files/neg/t5878.check @@ -1,13 +1,13 @@ -t5878.scala:1: error: value class may not unbox to itself +t5878.scala:1: error: value class may not wrap another user-defined value class case class Foo(x: Bar) extends AnyVal - ^ -t5878.scala:2: error: value class may not unbox to itself + ^ +t5878.scala:2: error: value class may not wrap another user-defined value class case class Bar(x: Foo) extends AnyVal - ^ -t5878.scala:4: error: value class may not unbox to itself + ^ +t5878.scala:4: error: value class may not wrap another user-defined value class class Foo1(val x: Bar1) extends AnyVal - ^ -t5878.scala:5: error: value class may not unbox to itself + ^ +t5878.scala:5: error: value class may not wrap another user-defined value class class Bar1(val x: Foo1) extends AnyVal - ^ + ^ four errors found diff --git a/test/files/neg/t6337.check b/test/files/neg/t6337.check new file mode 100644 index 0000000000..8448f71320 --- /dev/null +++ b/test/files/neg/t6337.check @@ -0,0 +1,7 @@ +t6337.scala:10: error: value class may not wrap another user-defined value class +class X[T](val i: XX[T]) extends AnyVal + ^ +t6337.scala:20: error: value class may not wrap another user-defined value class +class X1[T](val i: XX1[T]) extends AnyVal + ^ +two errors found diff --git a/test/files/neg/t6337.scala b/test/files/neg/t6337.scala new file mode 100644 index 0000000000..c3858f8c04 --- /dev/null +++ b/test/files/neg/t6337.scala @@ -0,0 +1,21 @@ +object C { + + def main(args: Array[String]) = { + val x = new X(new XX(3)) + println(x.i.x + 9) + } + +} + +class X[T](val i: XX[T]) extends AnyVal +class XX[T](val x: T) extends AnyVal + +object C1 { + def main(args: Array[String]) { + val x = new X1(new XX1(Some(3))) + println(x.i.x.get + 9) + } +} + +class X1[T](val i: XX1[T]) extends AnyVal +class XX1[T](val x: Option[T]) extends AnyVal |