Forbid provideDelegate, setValue and getValue suspend operators
#KT-24866 Fixed
This commit is contained in:
+2
-1
@@ -26,7 +26,8 @@ import org.jetbrains.kotlin.util.OperatorNameConventions
|
||||
object SuspendOperatorsCheckers : DeclarationChecker {
|
||||
private val UNSUPPORTED_OPERATOR_NAMES = setOf(
|
||||
OperatorNameConventions.CONTAINS,
|
||||
OperatorNameConventions.GET, OperatorNameConventions.SET
|
||||
OperatorNameConventions.GET, OperatorNameConventions.SET,
|
||||
OperatorNameConventions.PROVIDE_DELEGATE, OperatorNameConventions.GET_VALUE, OperatorNameConventions.SET_VALUE
|
||||
)
|
||||
|
||||
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
|
||||
|
||||
+29
-29
@@ -18,19 +18,20 @@ class A(val x: String) {
|
||||
var isMinusAssignCalled = false
|
||||
var isIncCalled = false
|
||||
operator suspend fun component1() = suspendThere(x + "K")
|
||||
operator suspend fun getValue(thisRef: Any?, property: KProperty<*>) = suspendThere(x + "K")
|
||||
operator suspend fun setValue(thisRef: Any?, property: KProperty<*>, value: String): Unit = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
if (value != "56") return@suspendCoroutineUninterceptedOrReturn Unit
|
||||
isSetValueCalled = true
|
||||
x.resume(Unit)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
|
||||
operator suspend fun provideDelegate(host: Any?, p: Any): A = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
isProvideDelegateCalled = true
|
||||
x.resume(this)
|
||||
COROUTINE_SUSPENDED
|
||||
}
|
||||
// There is no reason to support these operators until suspend properties are supported
|
||||
// operator suspend fun getValue(thisRef: Any?, property: KProperty<*>) = suspendThere(x + "K")
|
||||
// operator suspend fun setValue(thisRef: Any?, property: KProperty<*>, value: String): Unit = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
// if (value != "56") return@suspendCoroutineUninterceptedOrReturn Unit
|
||||
// isSetValueCalled = true
|
||||
// x.resume(Unit)
|
||||
// COROUTINE_SUSPENDED
|
||||
// }
|
||||
//
|
||||
// operator suspend fun provideDelegate(host: Any?, p: Any): A = suspendCoroutineUninterceptedOrReturn { x ->
|
||||
// isProvideDelegateCalled = true
|
||||
// x.resume(this)
|
||||
// COROUTINE_SUSPENDED
|
||||
// }
|
||||
|
||||
operator suspend fun plus(y: String) = suspendThere(x + y)
|
||||
operator suspend fun unaryPlus() = suspendThere(x + "K")
|
||||
@@ -65,15 +66,15 @@ fun builder(c: suspend () -> Unit) {
|
||||
|
||||
var a = A("O")
|
||||
|
||||
suspend fun foo1() {
|
||||
var x by a
|
||||
|
||||
if (x != "OK") throw RuntimeException("fail 1")
|
||||
|
||||
x = "56"
|
||||
|
||||
if (!a.isSetValueCalled || !a.isProvideDelegateCalled) throw RuntimeException("fail 2")
|
||||
}
|
||||
//suspend fun foo1() {
|
||||
// var x by a
|
||||
//
|
||||
// if (x != "OK") throw RuntimeException("fail 1")
|
||||
//
|
||||
// x = "56"
|
||||
//
|
||||
// if (!a.isSetValueCalled || !a.isProvideDelegateCalled) throw RuntimeException("fail 2")
|
||||
//}
|
||||
|
||||
suspend fun foo2() {
|
||||
val (y) = a
|
||||
@@ -90,11 +91,10 @@ suspend fun foo4() {
|
||||
if (y != "OK") throw RuntimeException("fail 5")
|
||||
}
|
||||
|
||||
// TODO: KT-15930
|
||||
//suspend fun foo5() {
|
||||
// a -= "56"
|
||||
// if (!a.isMinusAssignCalled) throw RuntimeException("fail 6")
|
||||
//}
|
||||
suspend fun foo5() {
|
||||
a -= "56"
|
||||
if (!a.isMinusAssignCalled) throw RuntimeException("fail 6")
|
||||
}
|
||||
|
||||
suspend fun foo6() {
|
||||
var y = a++
|
||||
@@ -126,11 +126,11 @@ suspend fun foo9() {
|
||||
fun box(): String {
|
||||
|
||||
builder {
|
||||
foo1()
|
||||
//foo1()
|
||||
foo2()
|
||||
foo3()
|
||||
foo4()
|
||||
//foo5()
|
||||
foo5()
|
||||
foo6()
|
||||
foo7()
|
||||
//foo8()
|
||||
|
||||
Vendored
+1
-1
@@ -4,5 +4,5 @@ fun bar(d: Delegate): String {
|
||||
}
|
||||
|
||||
class Delegate {
|
||||
suspend operator fun getValue(thisRef: Any?, property: Any?): String = ""
|
||||
suspend <!UNSUPPORTED!>operator<!> fun getValue(thisRef: Any?, property: Any?): String = ""
|
||||
}
|
||||
|
||||
@@ -1,11 +1,43 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER
|
||||
// SKIP_TXT
|
||||
|
||||
import kotlin.reflect.KProperty
|
||||
|
||||
class A {
|
||||
suspend <!UNSUPPORTED!>operator<!> fun get(x: Int) = 1
|
||||
suspend <!UNSUPPORTED!>operator<!> fun set(x: Int, v: String) {}
|
||||
|
||||
<!UNSUPPORTED!>operator<!> suspend fun contains(y: String): Boolean = true
|
||||
|
||||
suspend operator fun unaryPlus() = this
|
||||
suspend operator fun unaryMinus() = this
|
||||
suspend operator fun not() = this
|
||||
suspend operator fun inc() = this
|
||||
suspend operator fun dec() = this
|
||||
|
||||
suspend operator fun plus(a: A) = a
|
||||
suspend operator fun minus(a: A) = a
|
||||
suspend operator fun times(a: A) = a
|
||||
suspend operator fun div(a: A) = a
|
||||
suspend operator fun rem(a: A) = a
|
||||
suspend operator fun rangeTo(a: A) = a
|
||||
|
||||
suspend operator fun invoke(a: A) = a
|
||||
|
||||
suspend operator fun compareTo(a: A) = hashCode().compareTo(a.hashCode())
|
||||
|
||||
suspend operator fun iterator() = this
|
||||
suspend operator fun hasNext() = false
|
||||
suspend operator fun next() = this
|
||||
|
||||
suspend <!UNSUPPORTED!>operator<!> fun contains(b: A) = this == b
|
||||
suspend <!UNSUPPORTED!>operator<!> fun get(a: A) = a
|
||||
suspend <!INAPPLICABLE_OPERATOR_MODIFIER!>operator<!> fun equals(a: A) = a === this
|
||||
suspend <!UNSUPPORTED!>operator<!> fun set(a: A, b: A) {}
|
||||
|
||||
suspend <!UNSUPPORTED!>operator<!> fun provideDelegate(a: A, p: KProperty<*>) = a
|
||||
suspend <!UNSUPPORTED!>operator<!> fun getValue(a: A, p: KProperty<*>) = a
|
||||
suspend <!UNSUPPORTED!>operator<!> fun setValue(a: A, p: KProperty<*>, b: A) {}
|
||||
}
|
||||
|
||||
class B
|
||||
|
||||
Reference in New Issue
Block a user