summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJason Zaugg <jzaugg@gmail.com>2013-09-16 11:12:13 +0200
committerJason Zaugg <jzaugg@gmail.com>2013-09-16 16:44:28 +0200
commitcff8b569c39fb2ce57157fa6adf4ab9289721033 (patch)
tree793273d29da6141bc7b4414a82a5d59c6a2f44fb /src
parent4aad4eb6c1f10abd64ec5dfd8eeba091491349e2 (diff)
downloadscala-cff8b569c39fb2ce57157fa6adf4ab9289721033.tar.gz
scala-cff8b569c39fb2ce57157fa6adf4ab9289721033.tar.bz2
scala-cff8b569c39fb2ce57157fa6adf4ab9289721033.zip
SI-7841 More robust unspecialization of names
Names like `T1$sp`, which can arise from `AnyRef` specialization, were leading to negative length Names if they ever passed through `unspecializedName` or `splitSpecializedName`. This code path was touched when printing the tree of a certain AnyRef specialized classes after specialization, such as `AbstractPartialFunction` (which had such specialization until a few commits ago.) This commit handles that case correctly, and generally hardens against unexpected names, which could pop up from third party classes. The documentation for `splitSpecializedName` transposed the class and method specializations. The things you discover when you turn examples in documentation in to test cases! In addition, we now require non-negative length and offset in `newTermName`
Diffstat (limited to 'src')
-rw-r--r--src/reflect/scala/reflect/internal/Names.scala2
-rw-r--r--src/reflect/scala/reflect/internal/StdNames.scala39
2 files changed, 27 insertions, 14 deletions
diff --git a/src/reflect/scala/reflect/internal/Names.scala b/src/reflect/scala/reflect/internal/Names.scala
index ed248d6e1e..f998d95349 100644
--- a/src/reflect/scala/reflect/internal/Names.scala
+++ b/src/reflect/scala/reflect/internal/Names.scala
@@ -90,6 +90,8 @@ trait Names extends api.Names {
*/
final def newTermName(cs: Array[Char], offset: Int, len: Int, cachedString: String): TermName = {
def body = {
+ require(offset >= 0, "offset must be non-negative, got " + offset)
+ require(len >= 0, "length must be non-negative, got " + len)
val h = hashValue(cs, offset, len) & HASH_MASK
var n = termHashtable(h)
while ((n ne null) && (n.length != len || !equals(n.start, cs, offset, len)))
diff --git a/src/reflect/scala/reflect/internal/StdNames.scala b/src/reflect/scala/reflect/internal/StdNames.scala
index 686ebf5a1e..6407a3979c 100644
--- a/src/reflect/scala/reflect/internal/StdNames.scala
+++ b/src/reflect/scala/reflect/internal/StdNames.scala
@@ -413,32 +413,43 @@ trait StdNames {
@deprecated("Use Name#getterName", "2.11.0") def getterName(name: TermName): TermName = name.getterName
@deprecated("Use Name#getterName", "2.11.0") def setterToGetter(name: TermName): TermName = name.getterName
+ /**
+ * Convert `Tuple2$mcII` to `Tuple2`, or `T1$sp` to `T1`.
+ */
def unspecializedName(name: Name): Name = (
- if (name endsWith SPECIALIZED_SUFFIX)
- name.subName(0, name.lastIndexOf('m') - 1)
+ // DUPLICATED LOGIC WITH `splitSpecializedName`
+ if (name endsWith SPECIALIZED_SUFFIX) {
+ val idxM = name.lastIndexOf('m')
+ val to = (if (idxM > 0) idxM - 1 else name.length - SPECIALIZED_SUFFIX.length)
+ name.subName(0, to)
+ }
else name
)
/** Return the original name and the types on which this name
* is specialized. For example,
* {{{
- * splitSpecializedName("foo$mIcD$sp") == ('foo', "I", "D")
+ * splitSpecializedName("foo$mIcD$sp") == ('foo', "D", "I")
* }}}
* `foo$mIcD$sp` is the name of a method specialized on two type
* parameters, the first one belonging to the method itself, on Int,
* and another one belonging to the enclosing class, on Double.
+ *
+ * @return (unspecializedName, class tparam specializations, method tparam specializations)
*/
- def splitSpecializedName(name: Name): (Name, String, String) =
- if (name endsWith SPECIALIZED_SUFFIX) {
- val name1 = name dropRight SPECIALIZED_SUFFIX.length
- val idxC = name1 lastIndexOf 'c'
- val idxM = name1 lastIndexOf 'm'
-
- (name1.subName(0, idxM - 1),
- name1.subName(idxC + 1, name1.length).toString,
- name1.subName(idxM + 1, idxC).toString)
- } else
- (name, "", "")
+ def splitSpecializedName(name: Name): (Name, String, String) = {
+ // DUPLICATED LOGIC WITH `unspecializedName`
+ if (name endsWith SPECIALIZED_SUFFIX) {
+ val name1 = name dropRight SPECIALIZED_SUFFIX.length
+ val idxC = name1 lastIndexOf 'c'
+ val idxM = name1 lastIndexOf 'm'
+ if (idxC > idxM && idxM > 0)
+ (name1.subName(0, idxM - 1), name1.subName(idxC + 1, name1.length).toString, name1.subName(idxM + 1, idxC).toString)
+ else
+ (name.subName(0, name.length - SPECIALIZED_SUFFIX.length), "", "")
+ }
+ else (name, "", "")
+ }
// Nominally, name$default$N, encoded for <init>
def defaultGetterName(name: Name, pos: Int): TermName = (