From 344215b92d22d2101067d6abd8172ac791a30f09 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Sun, 16 Sep 2012 21:13:12 +0200 Subject: SI-6336 Disallows value types in structuralal refinements Structural refinements already have a number of restrictions, e.g. cannot refer to type parameters of enclosing classes. We need to disallow value classes as well. --- src/compiler/scala/tools/nsc/typechecker/Typers.scala | 11 +++++++---- test/files/neg/t6336.check | 4 ++++ test/files/neg/t6336.scala | 11 +++++++++++ 3 files changed, 22 insertions(+), 4 deletions(-) create mode 100644 test/files/neg/t6336.check create mode 100644 test/files/neg/t6336.scala diff --git a/src/compiler/scala/tools/nsc/typechecker/Typers.scala b/src/compiler/scala/tools/nsc/typechecker/Typers.scala index 5200aae8d1..85f260f2b4 100644 --- a/src/compiler/scala/tools/nsc/typechecker/Typers.scala +++ b/src/compiler/scala/tools/nsc/typechecker/Typers.scala @@ -1984,18 +1984,21 @@ trait Typers extends Modes with Adaptations with Tags { case PolyType(_, restpe) => restpe case _ => NoType } - + def failStruct(what: String) = + fail(s"Parameter type in structural refinement may not refer to $what") for (paramType <- tp.paramTypes) { val sym = paramType.typeSymbol if (sym.isAbstractType) { if (!sym.hasTransOwner(meth.owner)) - fail("Parameter type in structural refinement may not refer to an abstract type defined outside that refinement") + failStruct("an abstract type defined outside that refinement") else if (!sym.hasTransOwner(meth)) - fail("Parameter type in structural refinement may not refer to a type member of that refinement") + failStruct("a type member of that refinement") } + if (sym.isDerivedValueClass) + failStruct("a user-defined value class") if (paramType.isInstanceOf[ThisType] && sym == meth.owner) - fail("Parameter type in structural refinement may not refer to the type of that refinement (self type)") + failStruct("the type of that refinement (self type)") } } def typedUseCase(useCase: UseCase) { diff --git a/test/files/neg/t6336.check b/test/files/neg/t6336.check new file mode 100644 index 0000000000..f6b35ad232 --- /dev/null +++ b/test/files/neg/t6336.check @@ -0,0 +1,4 @@ +t6336.scala:3: error: Parameter type in structural refinement may not refer to a user-defined value class + val a = new { def y[T](x: X[T]) = x.i } + ^ +one error found diff --git a/test/files/neg/t6336.scala b/test/files/neg/t6336.scala new file mode 100644 index 0000000000..a9844ff94f --- /dev/null +++ b/test/files/neg/t6336.scala @@ -0,0 +1,11 @@ +object D { + def main(args: Array[String]) { + val a = new { def y[T](x: X[T]) = x.i } + val x = new X(3) + val t = a.y(x) + println(t) + } +} + +class X[T](val i: Int) extends AnyVal + -- cgit v1.2.3