Skip to content

Commit

Permalink
Bring back tests for nonvariant collections
Browse files Browse the repository at this point in the history
  • Loading branch information
odersky committed Dec 10, 2019
1 parent 9fe541e commit 1b50493
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 14 deletions.
46 changes: 32 additions & 14 deletions tests/neg/harmonize.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collection.mutable.ArrayBuffer
object Test {

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

}

inline val b = 33
def f(): Int = b + 1
val a1 = Array(b, 33, 'a')
val b1: Array[Int] = a1 // OK, Array constructor selection uses weak conformance
val a2 = Array(b, 33, 'a', f())
val b2: Array[Int] = a2 // OK, Array constructor selection uses weak conformance
val a3 = Array(1.0f, 'a', 0)
val b3: Array[Float] = a3 // OK, Array constructor selection uses weak conformance
val a4 = Array(1.0f, 1L)
val b4: Array[Double] = a4 // error: Array[Float] is picked
val a5 = Array(1.0f, 1L, f())
val b5: Array[AnyVal] = a5 // error: Array[Float] is picked
val a6 = Array(1.0f, 1234567890)
val b6: Array[AnyVal] = a6 // error: Array[Float] is picked
def arraytest =
inline val b = 33
def f(): Int = b + 1
val a1 = Array(b, 33, 'a')
val b1: Array[Int] = a1 // OK, Array constructor selection uses weak conformance
val a2 = Array(b, 33, 'a', f())
val b2: Array[Int] = a2 // OK, Array constructor selection uses weak conformance
val a3 = Array(1.0f, 'a', 0)
val b3: Array[Float] = a3 // OK, Array constructor selection uses weak conformance
val a4 = Array(1.0f, 1L)
val b4: Array[Double] = a4 // error: Array[Float] is picked
val a5 = Array(1.0f, 1L, f())
val b5: Array[AnyVal] = a5 // error: Array[Float] is picked
val a6 = Array(1.0f, 1234567890)
val b6: Array[AnyVal] = a6 // error: Array[Float] is picked

def arrayBufferTest =
inline val b = 33
def f(): Int = b + 1
val a1 = ArrayBuffer(b, 33, 'a')
val b1: ArrayBuffer[Int] = a1 // error: no widening
val a2 = ArrayBuffer(b, 33, 'a', f())
val b2: ArrayBuffer[Int] = a2 // error: no widening
val a3 = ArrayBuffer(1.0f, 'a', 0)
val b3: ArrayBuffer[Float] = a3 // error: no widening
val a4 = ArrayBuffer(1.0f, 1L)
val b4: ArrayBuffer[Double] = a4 // error: no widening
val a5 = ArrayBuffer(1.0f, 1L, f())
val b5: ArrayBuffer[AnyVal] = a5
val a6 = ArrayBuffer(1.0f, 1234567890)
val b6: ArrayBuffer[AnyVal] = a6

def totalDuration(results: List[Long], cond: Boolean): Long =
results.map(r => if (cond) r else 0).sum
Expand Down
20 changes: 20 additions & 0 deletions tests/run/weak-conformance.scala
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collection.mutable.ArrayBuffer
object Test extends App {
inline val b = 33

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

locally {
def f(): Int = b + 1
val x1 = ArrayBuffer(b, 33, 5.5) ; x1: ArrayBuffer[Double] // b is an inline val
val x2 = ArrayBuffer(f(), 33, 5.5) ; x2: ArrayBuffer[AnyVal] // f() is not a constant
val x3 = ArrayBuffer(5, 11L) ; x3: ArrayBuffer[Long]
val x4 = ArrayBuffer(5, 11L, 5.5) ; x4: ArrayBuffer[AnyVal] // Long and Double found
val x5 = ArrayBuffer(1.0f, 2) ; x5: ArrayBuffer[Float]
val x6 = ArrayBuffer(1.0f, 1234567890); x6: ArrayBuffer[AnyVal] // loss of precision
val x7 = ArrayBuffer(b, 33, 'a') ; x7: ArrayBuffer[Char]
val x8 = ArrayBuffer(5.toByte, 11) ; x8: ArrayBuffer[Byte]

val x9: ArrayBuffer[AnyVal] = ArrayBuffer(1.0f, 0)
assert(x9(0).getClass == classOf[java.lang.Float])
assert(x9(1).getClass == classOf[java.lang.Integer]) // expected type fully defined since ArrayBuffer is nonvariant
val x10 = ArrayBuffer[Any](1.0f, 0)
assert(x10(0).getClass == classOf[java.lang.Float])
assert(x10(1).getClass == classOf[java.lang.Integer])
}

locally {
// Arrays behave differently from lists since they have overloaded constructors, and weak
// conformance does apply for selecting one. See Issue #7630.
Expand Down

0 comments on commit 1b50493

Please sign in to comment.