Skip to content

Commit 1b50493

Browse files
committed
Bring back tests for nonvariant collections
1 parent 9fe541e commit 1b50493

File tree

2 files changed

+52
-14
lines changed

2 files changed

+52
-14
lines changed

tests/neg/harmonize.scala

Lines changed: 32 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collection.mutable.ArrayBuffer
12
object Test {
23

34
def main(args: Array[String]) = {
@@ -50,20 +51,37 @@ object Test {
5051

5152
}
5253

53-
inline val b = 33
54-
def f(): Int = b + 1
55-
val a1 = Array(b, 33, 'a')
56-
val b1: Array[Int] = a1 // OK, Array constructor selection uses weak conformance
57-
val a2 = Array(b, 33, 'a', f())
58-
val b2: Array[Int] = a2 // OK, Array constructor selection uses weak conformance
59-
val a3 = Array(1.0f, 'a', 0)
60-
val b3: Array[Float] = a3 // OK, Array constructor selection uses weak conformance
61-
val a4 = Array(1.0f, 1L)
62-
val b4: Array[Double] = a4 // error: Array[Float] is picked
63-
val a5 = Array(1.0f, 1L, f())
64-
val b5: Array[AnyVal] = a5 // error: Array[Float] is picked
65-
val a6 = Array(1.0f, 1234567890)
66-
val b6: Array[AnyVal] = a6 // error: Array[Float] is picked
54+
def arraytest =
55+
inline val b = 33
56+
def f(): Int = b + 1
57+
val a1 = Array(b, 33, 'a')
58+
val b1: Array[Int] = a1 // OK, Array constructor selection uses weak conformance
59+
val a2 = Array(b, 33, 'a', f())
60+
val b2: Array[Int] = a2 // OK, Array constructor selection uses weak conformance
61+
val a3 = Array(1.0f, 'a', 0)
62+
val b3: Array[Float] = a3 // OK, Array constructor selection uses weak conformance
63+
val a4 = Array(1.0f, 1L)
64+
val b4: Array[Double] = a4 // error: Array[Float] is picked
65+
val a5 = Array(1.0f, 1L, f())
66+
val b5: Array[AnyVal] = a5 // error: Array[Float] is picked
67+
val a6 = Array(1.0f, 1234567890)
68+
val b6: Array[AnyVal] = a6 // error: Array[Float] is picked
69+
70+
def arrayBufferTest =
71+
inline val b = 33
72+
def f(): Int = b + 1
73+
val a1 = ArrayBuffer(b, 33, 'a')
74+
val b1: ArrayBuffer[Int] = a1 // error: no widening
75+
val a2 = ArrayBuffer(b, 33, 'a', f())
76+
val b2: ArrayBuffer[Int] = a2 // error: no widening
77+
val a3 = ArrayBuffer(1.0f, 'a', 0)
78+
val b3: ArrayBuffer[Float] = a3 // error: no widening
79+
val a4 = ArrayBuffer(1.0f, 1L)
80+
val b4: ArrayBuffer[Double] = a4 // error: no widening
81+
val a5 = ArrayBuffer(1.0f, 1L, f())
82+
val b5: ArrayBuffer[AnyVal] = a5
83+
val a6 = ArrayBuffer(1.0f, 1234567890)
84+
val b6: ArrayBuffer[AnyVal] = a6
6785

6886
def totalDuration(results: List[Long], cond: Boolean): Long =
6987
results.map(r => if (cond) r else 0).sum

tests/run/weak-conformance.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import collection.mutable.ArrayBuffer
12
object Test extends App {
23
inline val b = 33
34

@@ -20,6 +21,25 @@ object Test extends App {
2021
assert(x10(1).getClass == classOf[java.lang.Integer])
2122
}
2223

24+
locally {
25+
def f(): Int = b + 1
26+
val x1 = ArrayBuffer(b, 33, 5.5) ; x1: ArrayBuffer[Double] // b is an inline val
27+
val x2 = ArrayBuffer(f(), 33, 5.5) ; x2: ArrayBuffer[AnyVal] // f() is not a constant
28+
val x3 = ArrayBuffer(5, 11L) ; x3: ArrayBuffer[Long]
29+
val x4 = ArrayBuffer(5, 11L, 5.5) ; x4: ArrayBuffer[AnyVal] // Long and Double found
30+
val x5 = ArrayBuffer(1.0f, 2) ; x5: ArrayBuffer[Float]
31+
val x6 = ArrayBuffer(1.0f, 1234567890); x6: ArrayBuffer[AnyVal] // loss of precision
32+
val x7 = ArrayBuffer(b, 33, 'a') ; x7: ArrayBuffer[Char]
33+
val x8 = ArrayBuffer(5.toByte, 11) ; x8: ArrayBuffer[Byte]
34+
35+
val x9: ArrayBuffer[AnyVal] = ArrayBuffer(1.0f, 0)
36+
assert(x9(0).getClass == classOf[java.lang.Float])
37+
assert(x9(1).getClass == classOf[java.lang.Integer]) // expected type fully defined since ArrayBuffer is nonvariant
38+
val x10 = ArrayBuffer[Any](1.0f, 0)
39+
assert(x10(0).getClass == classOf[java.lang.Float])
40+
assert(x10(1).getClass == classOf[java.lang.Integer])
41+
}
42+
2343
locally {
2444
// Arrays behave differently from lists since they have overloaded constructors, and weak
2545
// conformance does apply for selecting one. See Issue #7630.

0 commit comments

Comments
 (0)