Skip to content

Commit 0d9587a

Browse files
authored
Merge pull request scala#5258 from szeiger/issue/9019
SI-9019 TraversableLike stringPrefix broken for inner classes
2 parents 14c02ac + 30876fe commit 0d9587a

File tree

3 files changed

+55
-13
lines changed

3 files changed

+55
-13
lines changed

src/library/scala/collection/TraversableLike.scala

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,21 @@ trait TraversableLike[+A, +Repr] extends Any
606606
* simple name of the collection class $coll.
607607
*/
608608
def stringPrefix : String = {
609-
var string = repr.getClass.getName
610-
val idx1 = string.lastIndexOf('.' : Int)
611-
if (idx1 != -1) string = string.substring(idx1 + 1)
612-
val idx2 = string.indexOf('$')
613-
if (idx2 != -1) string = string.substring(0, idx2)
614-
string
609+
val fqn = repr.getClass.getName
610+
val cls = {
611+
val idx1 = fqn.lastIndexOf('.' : Int)
612+
if (idx1 != -1) fqn.substring(idx1 + 1) else fqn
613+
}
614+
val parts = cls.split('$')
615+
val last = parts.length - 1
616+
parts.zipWithIndex.foldLeft("") { case (z, (s, i)) =>
617+
if (s.isEmpty) z
618+
else if (i != last && s.forall(java.lang.Character.isDigit)) "" // drop prefix in method-local classes
619+
else if (i == 0 || java.lang.Character.isUpperCase(s.charAt(0))) {
620+
if (z.isEmpty) s else z + '.' + s
621+
}
622+
else z
623+
}
615624
}
616625

617626
/** Creates a non-strict view of this $coll.

test/files/run/xMigration.check

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11

22
scala> Map(1 -> "eis").values // no warn
3-
res0: Iterable[String] = MapLike(eis)
3+
res0: Iterable[String] = MapLike.DefaultValuesIterable(eis)
44

55
scala> :setting -Xmigration:none
66

77
scala> Map(1 -> "eis").values // no warn
8-
res1: Iterable[String] = MapLike(eis)
8+
res1: Iterable[String] = MapLike.DefaultValuesIterable(eis)
99

1010
scala> :setting -Xmigration:any
1111

@@ -14,12 +14,12 @@ scala> Map(1 -> "eis").values // warn
1414
`values` returns `Iterable[V]` rather than `Iterator[V]`.
1515
Map(1 -> "eis").values // warn
1616
^
17-
res2: Iterable[String] = MapLike(eis)
17+
res2: Iterable[String] = MapLike.DefaultValuesIterable(eis)
1818

1919
scala> :setting -Xmigration:2.8
2020

2121
scala> Map(1 -> "eis").values // no warn
22-
res3: Iterable[String] = MapLike(eis)
22+
res3: Iterable[String] = MapLike.DefaultValuesIterable(eis)
2323

2424
scala> :setting -Xmigration:2.7
2525

@@ -28,12 +28,12 @@ scala> Map(1 -> "eis").values // warn
2828
`values` returns `Iterable[V]` rather than `Iterator[V]`.
2929
Map(1 -> "eis").values // warn
3030
^
31-
res4: Iterable[String] = MapLike(eis)
31+
res4: Iterable[String] = MapLike.DefaultValuesIterable(eis)
3232

3333
scala> :setting -Xmigration:2.11
3434

3535
scala> Map(1 -> "eis").values // no warn
36-
res5: Iterable[String] = MapLike(eis)
36+
res5: Iterable[String] = MapLike.DefaultValuesIterable(eis)
3737

3838
scala> :setting -Xmigration // same as :any
3939

@@ -42,6 +42,6 @@ scala> Map(1 -> "eis").values // warn
4242
`values` returns `Iterable[V]` rather than `Iterator[V]`.
4343
Map(1 -> "eis").values // warn
4444
^
45-
res6: Iterable[String] = MapLike(eis)
45+
res6: Iterable[String] = MapLike.DefaultValuesIterable(eis)
4646

4747
scala> :quit
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package scala.collection
2+
3+
import org.junit.Assert._
4+
import org.junit.Test
5+
import org.junit.runner.RunWith
6+
import org.junit.runners.JUnit4
7+
8+
@RunWith(classOf[JUnit4])
9+
class TraversableLikeTest {
10+
// For test_SI9019; out here because as of test writing, putting this in a method would crash compiler
11+
class Baz[@specialized(Int) A]() extends IndexedSeq[A] {
12+
def apply(i: Int) = ???
13+
def length: Int = 0
14+
}
15+
16+
@Test
17+
def test_SI9019 {
18+
object Foo {
19+
def mkBar = () => {
20+
class Bar extends IndexedSeq[Int] {
21+
def apply(i: Int) = ???
22+
def length: Int = 0
23+
}
24+
new Bar
25+
}
26+
}
27+
val bar = Foo.mkBar()
28+
assertEquals("Bar", bar.stringPrefix) // Previously would have been outermost class, TraversableLikeTest
29+
30+
val baz = new Baz[Int]()
31+
assertEquals("TraversableLikeTest.Baz", baz.stringPrefix) // Make sure we don't see specialization $mcI$sp stuff
32+
}
33+
}

0 commit comments

Comments
 (0)