diff options
30 files changed, 181 insertions, 26 deletions
diff --git a/src/compiler/scala/tools/cmd/gen/AnyVals.scala b/src/compiler/scala/tools/cmd/gen/AnyVals.scala index 910804245b..b4f5641b98 100644 --- a/src/compiler/scala/tools/cmd/gen/AnyVals.scala +++ b/src/compiler/scala/tools/cmd/gen/AnyVals.scala @@ -320,7 +320,13 @@ def unbox(x: java.lang.Object): @name@ = @unboxImpl@ override def toString = "object scala.@name@" """ - def nonUnitCompanions = "" // todo + def nonUnitCompanions = """ +/** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[@name@] + */ +final val emptyArray = new Array[@name@](0)""" def cardinalCompanion = """ /** The smallest value representable as a @name@. @@ -341,9 +347,6 @@ final val NaN = @boxed@.NaN final val PositiveInfinity = @boxed@.POSITIVE_INFINITY final val NegativeInfinity = @boxed@.NEGATIVE_INFINITY -@deprecated("use @name@.MinPositiveValue instead", "2.9.0") -final val Epsilon = MinPositiveValue - /** The negative number with the greatest (finite) absolute value which is representable * by a @name@. Note that it differs from [[java.lang.@name@.MIN_VALUE]], which * is the smallest positive value representable by a @name@. In Scala that number diff --git a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala index bbeb549289..5ca4712cd2 100644 --- a/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala +++ b/src/compiler/scala/tools/nsc/typechecker/ContextErrors.scala @@ -106,7 +106,7 @@ trait ContextErrors { else s"$name extends Any, not AnyRef" ) - if (isPrimitiveValueType(found)) "" else "\n" + + if (isPrimitiveValueType(found) || isTrivialTopType(tp)) "" else "\n" + s"""|Note that $what. |Such types can participate in value classes, but instances |cannot appear in singleton types or in reference comparisons.""".stripMargin diff --git a/src/compiler/scala/tools/nsc/typechecker/SuperAccessors.scala b/src/compiler/scala/tools/nsc/typechecker/SuperAccessors.scala index 63050bc032..981ba10183 100644 --- a/src/compiler/scala/tools/nsc/typechecker/SuperAccessors.scala +++ b/src/compiler/scala/tools/nsc/typechecker/SuperAccessors.scala @@ -287,16 +287,18 @@ abstract class SuperAccessors extends transform.Transform with transform.TypingT // FIXME - this should be unified with needsProtectedAccessor, but some // subtlety which presently eludes me is foiling my attempts. val shouldEnsureAccessor = ( - currentClass.isTrait + currentClass.isTrait && sym.isProtected && sym.enclClass != currentClass && !sym.owner.isTrait && (sym.owner.enclosingPackageClass != currentClass.enclosingPackageClass) - && (qual.symbol.info.member(sym.name) ne NoSymbol)) + && (qual.symbol.info.member(sym.name) ne NoSymbol) + && !needsProtectedAccessor(sym, tree.pos)) if (shouldEnsureAccessor) { log("Ensuring accessor for call to protected " + sym.fullLocationString + " from " + currentClass) ensureAccessor(sel) - } else + } + else mayNeedProtectedAccessor(sel, EmptyTree.asList, false) } @@ -525,7 +527,14 @@ abstract class SuperAccessors extends transform.Transform with transform.TypingT ) true } - isCandidate && !host.isPackageClass && !isSelfType + def isJavaProtected = host.isTrait && sym.isJavaDefined && { + restrictionError(pos, unit, + s"""|$clazz accesses protected $sym inside a concrete trait method. + |Add an accessor in a class extending ${sym.enclClass} as a workaround.""".stripMargin + ) + true + } + isCandidate && !host.isPackageClass && !isSelfType && !isJavaProtected } /** Return the innermost enclosing class C of referencingClass for which either diff --git a/src/library/scala/Boolean.scala b/src/library/scala/Boolean.scala index 440e546f19..d7311078ab 100644 --- a/src/library/scala/Boolean.scala +++ b/src/library/scala/Boolean.scala @@ -135,5 +135,12 @@ object Boolean extends AnyValCompanion { */ override def toString = "object scala.Boolean" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Boolean] + */ + final val emptyArray = new Array[Boolean](0) } diff --git a/src/library/scala/Byte.scala b/src/library/scala/Byte.scala index df0d2c73b1..02ef913fc5 100644 --- a/src/library/scala/Byte.scala +++ b/src/library/scala/Byte.scala @@ -625,6 +625,14 @@ object Byte extends AnyValCompanion { */ override def toString = "object scala.Byte" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Byte] + */ + final val emptyArray = new Array[Byte](0) + /** Language mandated coercions from Byte to "wider" types. */ implicit def byte2short(x: Byte): Short = x.toShort diff --git a/src/library/scala/Char.scala b/src/library/scala/Char.scala index 1fa0c0d9e8..5a1bf16f1d 100644 --- a/src/library/scala/Char.scala +++ b/src/library/scala/Char.scala @@ -625,6 +625,14 @@ object Char extends AnyValCompanion { */ override def toString = "object scala.Char" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Char] + */ + final val emptyArray = new Array[Char](0) + /** Language mandated coercions from Char to "wider" types. */ implicit def char2int(x: Char): Int = x.toInt diff --git a/src/library/scala/Double.scala b/src/library/scala/Double.scala index f058d7c26b..7bebbf9418 100644 --- a/src/library/scala/Double.scala +++ b/src/library/scala/Double.scala @@ -400,5 +400,13 @@ object Double extends AnyValCompanion { /** The String representation of the scala.Double companion object. */ override def toString = "object scala.Double" + + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Double] + */ + final val emptyArray = new Array[Double](0) } diff --git a/src/library/scala/Float.scala b/src/library/scala/Float.scala index d942acec23..79ed2ac20b 100644 --- a/src/library/scala/Float.scala +++ b/src/library/scala/Float.scala @@ -401,6 +401,14 @@ object Float extends AnyValCompanion { */ override def toString = "object scala.Float" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Float] + */ + final val emptyArray = new Array[Float](0) + /** Language mandated coercions from Float to "wider" types. */ implicit def float2double(x: Float): Double = x.toDouble diff --git a/src/library/scala/Int.scala b/src/library/scala/Int.scala index ae36413469..fa2f9a97e7 100644 --- a/src/library/scala/Int.scala +++ b/src/library/scala/Int.scala @@ -625,6 +625,14 @@ object Int extends AnyValCompanion { */ override def toString = "object scala.Int" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Int] + */ + final val emptyArray = new Array[Int](0) + /** Language mandated coercions from Int to "wider" types. */ implicit def int2long(x: Int): Long = x.toLong diff --git a/src/library/scala/Long.scala b/src/library/scala/Long.scala index 4ee9383c2a..94204e893c 100644 --- a/src/library/scala/Long.scala +++ b/src/library/scala/Long.scala @@ -625,6 +625,14 @@ object Long extends AnyValCompanion { */ override def toString = "object scala.Long" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Long] + */ + final val emptyArray = new Array[Long](0) + /** Language mandated coercions from Long to "wider" types. */ implicit def long2float(x: Long): Float = x.toFloat diff --git a/src/library/scala/Short.scala b/src/library/scala/Short.scala index 35c5fe3ff0..aef8608d2e 100644 --- a/src/library/scala/Short.scala +++ b/src/library/scala/Short.scala @@ -625,6 +625,14 @@ object Short extends AnyValCompanion { */ override def toString = "object scala.Short" + + /** A highly reusable empty array, useful for avoiding + * allocations when you need one. + * + * @return a constant 0-length Array[Short] + */ + final val emptyArray = new Array[Short](0) + /** Language mandated coercions from Short to "wider" types. */ implicit def short2int(x: Short): Int = x.toInt diff --git a/src/library/scala/runtime/StringAdd.scala b/src/library/scala/runtime/StringAdd.scala index 4693b0bf44..f074b5407e 100644 --- a/src/library/scala/runtime/StringAdd.scala +++ b/src/library/scala/runtime/StringAdd.scala @@ -10,10 +10,5 @@ package scala.runtime /** A wrapper class that adds string concatenation `+` to any value */ final class StringAdd(val self: Any) extends AnyVal { - // Note: The implicit conversion from Any to StringAdd is one of two - // implicit conversions from Any to AnyRef in Predef. It is important to have at least - // two such conversions, so that silent conversions from value types to AnyRef - // are avoided. If StringFormat should become a value class, another - // implicit conversion from Any to AnyRef has to be introduced in Predef def +(other: String) = String.valueOf(self) + other } diff --git a/src/library/scala/runtime/StringFormat.scala b/src/library/scala/runtime/StringFormat.scala index 1f5feec9e1..7d34e82812 100644 --- a/src/library/scala/runtime/StringFormat.scala +++ b/src/library/scala/runtime/StringFormat.scala @@ -11,12 +11,6 @@ package scala.runtime /** A wrapper class that adds a `formatted` operation to any value */ final class StringFormat(val self: Any) extends AnyVal { - // Note: The implicit conversion from Any to StringFormat is one of two - // implicit conversions from Any to AnyRef in Predef. It is important to have at least - // two such conversions, so that silent conversions from value types to AnyRef - // are avoided. If StringFormat should become a value class, another - // implicit conversion from Any to AnyRef has to be introduced in Predef - /** Returns string formatted according to given `format` string. * Format strings are as for `String.format` * (@see java.lang.String.format). diff --git a/src/reflect/scala/tools/nsc/io/VirtualFile.scala b/src/reflect/scala/tools/nsc/io/VirtualFile.scala index be888e92e6..9061534edc 100644 --- a/src/reflect/scala/tools/nsc/io/VirtualFile.scala +++ b/src/reflect/scala/tools/nsc/io/VirtualFile.scala @@ -33,7 +33,7 @@ class VirtualFile(val name: String, override val path: String) extends AbstractF //######################################################################## // Private data - private var content = new Array[Byte](0) + private var content = Byte.emptyArray //######################################################################## // Public Methods diff --git a/src/reflect/scala/tools/nsc/io/ZipArchive.scala b/src/reflect/scala/tools/nsc/io/ZipArchive.scala index d7ec209525..9d9d9a46f2 100644 --- a/src/reflect/scala/tools/nsc/io/ZipArchive.scala +++ b/src/reflect/scala/tools/nsc/io/ZipArchive.scala @@ -177,7 +177,7 @@ final class URLZipArchive(val url: URL) extends ZipArchive(null) { class FileEntry() extends Entry(zipEntry.getName) { override val toByteArray: Array[Byte] = { val len = zipEntry.getSize().toInt - val arr = new Array[Byte](len) + val arr = if (len == 0) Byte.emptyArray else new Array[Byte](len) var offset = 0 def loop() { diff --git a/test/files/neg/no-implicit-to-anyref.check b/test/files/neg/no-implicit-to-anyref.check new file mode 100644 index 0000000000..d94b57a30a --- /dev/null +++ b/test/files/neg/no-implicit-to-anyref.check @@ -0,0 +1,28 @@ +no-implicit-to-anyref.scala:11: error: type mismatch; + found : Int(1) + required: AnyRef +Note: an implicit exists from scala.Int => java.lang.Integer, but +methods inherited from Object are rendered ambiguous. This is to avoid +a blanket implicit which would convert any scala.Int to any AnyRef. +You may wish to use a type ascription: `x: java.lang.Integer`. + 1: AnyRef + ^ +no-implicit-to-anyref.scala:17: error: type mismatch; + found : Any + required: AnyRef + (null: Any): AnyRef + ^ +no-implicit-to-anyref.scala:21: error: type mismatch; + found : AnyVal + required: AnyRef + (0: AnyVal): AnyRef + ^ +no-implicit-to-anyref.scala:27: error: type mismatch; + found : Test.AV + required: AnyRef +Note that AV extends Any, not AnyRef. +Such types can participate in value classes, but instances +cannot appear in singleton types or in reference comparisons. + new AV(0): AnyRef + ^ +four errors found diff --git a/test/files/neg/no-implicit-to-anyref.scala b/test/files/neg/no-implicit-to-anyref.scala new file mode 100644 index 0000000000..3e3d373e38 --- /dev/null +++ b/test/files/neg/no-implicit-to-anyref.scala @@ -0,0 +1,29 @@ +// Checks that the state of standard implicits in Predef._ and scala._ +// doesn't allow us to unambiguously and implicitly convert AnyVal +// and subtypes to AnyRef. +// +// In the days before value classes, this was precariously held be +// the competing implicits Any => StringAdd and Any => StringFormat. +// Since then, these have both become value classes, but seeing as +// this happened simultaneously, we're still okay. +object Test { + locally { + 1: AnyRef + } + + locally { + // before this test case was added and ContextErrors was tweaked, this + // emitted: "Note that Any extends Any, not AnyRef." + (null: Any): AnyRef + } + + locally { + (0: AnyVal): AnyRef + } + + class AV(val a: Int) extends AnyVal + + locally { + new AV(0): AnyRef + } +} diff --git a/test/files/neg/t2296a.check b/test/files/neg/t2296a.check new file mode 100644 index 0000000000..863b861046 --- /dev/null +++ b/test/files/neg/t2296a.check @@ -0,0 +1,5 @@ +S.scala:6: error: Implementation restriction: trait S accesses protected method foo inside a concrete trait method. +Add an accessor in a class extending class J as a workaround. + foo() + ^ +one error found diff --git a/test/files/run/t2296a/J.java b/test/files/neg/t2296a/J.java index 78ff3e9804..78ff3e9804 100644 --- a/test/files/run/t2296a/J.java +++ b/test/files/neg/t2296a/J.java diff --git a/test/files/run/t2296a/S.scala b/test/files/neg/t2296a/S.scala index 532d038a42..532d038a42 100644 --- a/test/files/run/t2296a/S.scala +++ b/test/files/neg/t2296a/S.scala diff --git a/test/files/neg/t2296b.check b/test/files/neg/t2296b.check new file mode 100644 index 0000000000..07cc54d573 --- /dev/null +++ b/test/files/neg/t2296b.check @@ -0,0 +1,5 @@ +S_2.scala:6: error: Implementation restriction: trait S accesses protected method foo inside a concrete trait method. +Add an accessor in a class extending class J_1 as a workaround. + foo() + ^ +one error found diff --git a/test/files/run/t2296b/J_1.java b/test/files/neg/t2296b/J_1.java index 4c91d47073..4c91d47073 100644 --- a/test/files/run/t2296b/J_1.java +++ b/test/files/neg/t2296b/J_1.java diff --git a/test/files/run/t2296b/S_2.scala b/test/files/neg/t2296b/S_2.scala index 6cdb0cfaba..6cdb0cfaba 100644 --- a/test/files/run/t2296b/S_2.scala +++ b/test/files/neg/t2296b/S_2.scala diff --git a/test/files/pos/t6245/Base.java b/test/files/pos/t6245/Base.java new file mode 100644 index 0000000000..651ea08bf2 --- /dev/null +++ b/test/files/pos/t6245/Base.java @@ -0,0 +1,5 @@ +package t1; + +public class Base { + protected Vis inner; +} diff --git a/test/files/pos/t6245/Foo.scala b/test/files/pos/t6245/Foo.scala new file mode 100644 index 0000000000..f5f997fbff --- /dev/null +++ b/test/files/pos/t6245/Foo.scala @@ -0,0 +1,9 @@ +import t1.Vis + +abstract class Foo extends t1.Base { + trait Nested { + def crash() { + inner + } + } +} diff --git a/test/files/pos/t6245/Vis.java b/test/files/pos/t6245/Vis.java new file mode 100644 index 0000000000..4267f4e40b --- /dev/null +++ b/test/files/pos/t6245/Vis.java @@ -0,0 +1,3 @@ +package t1; + +public class Vis { } diff --git a/test/files/run/empty-array.check b/test/files/run/empty-array.check new file mode 100644 index 0000000000..bb0b1cf658 --- /dev/null +++ b/test/files/run/empty-array.check @@ -0,0 +1,3 @@ +0 +0 +0 diff --git a/test/files/run/empty-array.scala b/test/files/run/empty-array.scala new file mode 100644 index 0000000000..e56c86df5c --- /dev/null +++ b/test/files/run/empty-array.scala @@ -0,0 +1,8 @@ +object Test { + def main(args: Array[String]): Unit = { + println(Byte.emptyArray.length) + println(Double.emptyArray.length) + println(Boolean.emptyArray.length) + // okay okay okay + } +} diff --git a/test/files/run/t2296a.check b/test/files/run/t2296a.check deleted file mode 100644 index f75aec9d81..0000000000 --- a/test/files/run/t2296a.check +++ /dev/null @@ -1,2 +0,0 @@ -J.foo() -J.foo() diff --git a/test/files/run/t2296b.check b/test/files/run/t2296b.check deleted file mode 100644 index f75aec9d81..0000000000 --- a/test/files/run/t2296b.check +++ /dev/null @@ -1,2 +0,0 @@ -J.foo() -J.foo() |