Check presence of dispatch receiver parameter in modifier checks

Also fix typo in "inapplicable infix" diagnostic message
This commit is contained in:
Alexander Udalov
2016-07-21 15:00:31 +03:00
parent 2a390155a9
commit 9be219b69c
8 changed files with 16 additions and 23 deletions
@@ -14,11 +14,11 @@ class OkTest {
<!INAPPLICABLE_INFIX_MODIFIER(must have a single value parameter)!>infix<!> fun String.e1(o: String, o2: String? = null) = o
<!INAPPLICABLE_INFIX_MODIFIER(must have a single value parameter)!>infix<!> fun String.e2(o: String = "", o2: String? = null) = o
<!INAPPLICABLE_INFIX_MODIFIER(must be a member of an extension function)!>infix<!> fun e3() {}
<!INAPPLICABLE_INFIX_MODIFIER(must be a member of an extension function)!>infix<!> fun e4(s: String) {}
<!INAPPLICABLE_INFIX_MODIFIER(must be a member or an extension function)!>infix<!> fun e3() {}
<!INAPPLICABLE_INFIX_MODIFIER(must be a member or an extension function)!>infix<!> fun e4(s: String) {}
<!INAPPLICABLE_INFIX_MODIFIER(must have a single value parameter)!>infix<!> fun String.e5() {}
<!INAPPLICABLE_INFIX_MODIFIER(must have a single value parameter)!>infix<!> fun String.e6(a: Int, b: Int) {}
<!INAPPLICABLE_INFIX_MODIFIER(must be a member of an extension function)!>infix<!> fun e7(a: Int, b: Int) {}
<!INAPPLICABLE_INFIX_MODIFIER(must be a member or an extension function)!>infix<!> fun e7(a: Int, b: Int) {}
class Example {
<!INAPPLICABLE_INFIX_MODIFIER(must have a single value parameter)!>infix<!> fun e8(s: String, a: Int = 0) {}
@@ -9,5 +9,5 @@ public/*package*/ open class Java {
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
// Static members
public open operator fun get(/*0*/ o: kotlin.collections.(Mutable)List<kotlin.Int!>!): kotlin.collections.(Mutable)List<kotlin.Int!>!
public open fun get(/*0*/ o: kotlin.collections.(Mutable)List<kotlin.Int!>!): kotlin.collections.(Mutable)List<kotlin.Int!>!
}
@@ -25,7 +25,6 @@ import org.jetbrains.kotlin.name.Name
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.descriptorUtil.builtIns
import org.jetbrains.kotlin.resolve.descriptorUtil.hasDefaultValue
import org.jetbrains.kotlin.resolve.descriptorUtil.isExtension
import org.jetbrains.kotlin.resolve.descriptorUtil.module
import org.jetbrains.kotlin.types.KotlinType
import org.jetbrains.kotlin.types.typeUtil.*
@@ -70,10 +69,11 @@ interface Check {
sealed class MemberKindCheck(override val description: String) : Check {
object MemberOrExtension : MemberKindCheck("must be a member or an extension function") {
override fun check(functionDescriptor: FunctionDescriptor) =
functionDescriptor.isExtension || functionDescriptor.containingDeclaration is ClassDescriptor
functionDescriptor.dispatchReceiverParameter != null || functionDescriptor.extensionReceiverParameter != null
}
object Member : MemberKindCheck("must be a member function") {
override fun check(functionDescriptor: FunctionDescriptor) = functionDescriptor.containingDeclaration is ClassDescriptor
override fun check(functionDescriptor: FunctionDescriptor) =
functionDescriptor.dispatchReceiverParameter != null
}
}
@@ -92,13 +92,6 @@ sealed class ValueParameterCountCheck(override val description: String) : Check
}
}
private object HasDispatchOrExtensionReceiverParameter : Check {
override val description = "must be a member of an extension function"
override fun check(functionDescriptor: FunctionDescriptor): Boolean {
return functionDescriptor.dispatchReceiverParameter != null || functionDescriptor.extensionReceiverParameter != null
}
}
private object NoDefaultAndVarargsCheck : Check {
override val description = "should not have varargs or parameters with default values"
override fun check(functionDescriptor: FunctionDescriptor) =
@@ -238,7 +231,7 @@ object OperatorChecks : AbstractModifierChecks() {
object InfixChecks : AbstractModifierChecks() {
override val checks = listOf(
Checks(HasDispatchOrExtensionReceiverParameter, SingleValueParameter, NoDefaultAndVarargsCheck))
Checks(MemberKindCheck.MemberOrExtension, SingleValueParameter, NoDefaultAndVarargsCheck))
}
fun FunctionDescriptor.isValidOperator() = isOperator && OperatorChecks.check(this).isSuccess
+2 -2
View File
@@ -1,4 +1,4 @@
// IS_APPLICABLE: false
// ERROR: 'infix' modifier is inapplicable on this function: must be a member of an extension function
// ERROR: 'infix' modifier is inapplicable on this function: must be a member or an extension function
infix fun id(s: String) = s
val x = <caret>id("0").get(0)
val x = <caret>id("0").get(0)
@@ -1,9 +1,9 @@
// IS_APPLICABLE: false
// ERROR: 'infix' modifier is inapplicable on this function: must be a member of an extension function
// ERROR: 'infix' modifier is inapplicable on this function: must be a member or an extension function
package ppp
infix fun foo(p: String){}
fun main() {
ppp.<caret>foo("")
}
}
@@ -1,6 +1,6 @@
// WITH_RUNTIME
// IS_APPLICABLE: false
// ERROR: 'infix' modifier is inapplicable on this function: must be a member of an extension function
// ERROR: 'infix' modifier is inapplicable on this function: must be a member or an extension function
package demo
@@ -8,4 +8,4 @@ infix fun foo(str: String) = kotlin.io.println(str)
fun main() {
<caret>demo.foo("")
}
}
+1 -1
View File
@@ -1,5 +1,5 @@
// "Replace with 'newFun(p, this)'" "true"
// ERROR: 'infix' modifier is inapplicable on this function: must be a member of an extension function
// ERROR: 'infix' modifier is inapplicable on this function: must be a member or an extension function
@Deprecated("", ReplaceWith("newFun(p, this)"))
infix fun String.oldFun(p: Int) {
@@ -1,5 +1,5 @@
// "Replace with 'newFun(p, this)'" "true"
// ERROR: 'infix' modifier is inapplicable on this function: must be a member of an extension function
// ERROR: 'infix' modifier is inapplicable on this function: must be a member or an extension function
@Deprecated("", ReplaceWith("newFun(p, this)"))
infix fun String.oldFun(p: Int) {