Files
kotlin-fork/compiler/testData/diagnostics/tests/operatorsOverloading/plusAssignOnVarAndCollections.kt
T
Tianyu Geng 4d2e3a2379 FIR: Fix resolving += to ignore + if it doesn't return correct type
This is the FIR fix of the FE change tracked by
https://youtrack.jetbrains.com/issue/KT-45503

Consider the following code:

```
fun foo() {
  var l = mutableListOf("")
  l += ""
}
```

The above code used to be considered invalid due to ambiguous `+=`. But
after KT-45503, it's now considered valid code because `plus` is not
really a valid option for this code. This is because the return type
of `plus` for `List` is `List`, which does not match the type of
variable `l`.

As for now, FIR rejects the above code due to ambiguous `+=`. This
change fixes that by also consider the return type when resolving `+=`.
2021-04-15 10:34:05 +03:00

32 lines
416 B
Kotlin
Vendored

// FIR_IDENTICAL
// WITH_RUNTIME
fun test1() {
var list = ArrayList<Int>()
list -= 2
}
fun test2() {
var set = HashMap<Int, Int>()
set += 2 to 2
}
fun test3() {
var set = HashSet<Int>()
set -= 2
}
fun test4() {
var list = mutableListOf(1)
list += 2
}
fun test5() {
var map = mutableMapOf(1 to 1)
map += 2 to 2
}
fun test6() {
var set = mutableSetOf(1)
set += 2
}