Allow kotlin.Result only for declarations that are effectively fields
This commit is contained in:
+10
-10
@@ -11,6 +11,7 @@ import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
|
||||
import org.jetbrains.kotlin.psi.KtCallableDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtDeclaration
|
||||
import org.jetbrains.kotlin.psi.KtProperty
|
||||
import org.jetbrains.kotlin.resolve.DescriptorUtils
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
|
||||
@@ -22,25 +23,24 @@ class ResultClassInReturnTypeChecker : DeclarationChecker {
|
||||
if (declaration !is KtCallableDeclaration || descriptor !is CallableMemberDescriptor) return
|
||||
|
||||
val returnType = descriptor.returnType ?: return
|
||||
if (isForbiddenReturnType(returnType, descriptor)) {
|
||||
if (isForbiddenReturnType(returnType, declaration, descriptor)) {
|
||||
val typeReferenceOrDeclarationName = declaration.typeReference ?: declaration.nameIdentifier ?: return
|
||||
context.trace.reportDiagnosticOnce(Errors.RESULT_CLASS_IN_RETURN_TYPE.on(typeReferenceOrDeclarationName))
|
||||
}
|
||||
}
|
||||
|
||||
private fun isForbiddenReturnType(returnType: KotlinType, declarationDescriptor: DeclarationDescriptor): Boolean {
|
||||
private fun isForbiddenReturnType(
|
||||
returnType: KotlinType, declaration: KtDeclaration, declarationDescriptor: DeclarationDescriptor
|
||||
): Boolean {
|
||||
if (!returnType.isResultType()) return false
|
||||
|
||||
if (declarationDescriptor is PropertyDescriptor || declarationDescriptor is PropertyGetterDescriptor) {
|
||||
val visibility = (declarationDescriptor as DeclarationDescriptorWithVisibility).effectiveVisibility()
|
||||
return when (visibility) {
|
||||
is EffectiveVisibility.Private, is EffectiveVisibility.Local,
|
||||
is EffectiveVisibility.InternalOrPackage, is EffectiveVisibility.InternalProtected,
|
||||
is EffectiveVisibility.InternalProtectedBound -> false
|
||||
|
||||
is EffectiveVisibility.Public, is EffectiveVisibility.Protected,
|
||||
is EffectiveVisibility.ProtectedBound -> true
|
||||
if (declaration is KtProperty && declaration.getter?.hasBody() == true) {
|
||||
return true
|
||||
}
|
||||
|
||||
val visibility = (declarationDescriptor as DeclarationDescriptorWithVisibility).visibility
|
||||
return !Visibilities.isPrivate(visibility) && visibility != Visibilities.LOCAL
|
||||
}
|
||||
|
||||
return true
|
||||
|
||||
Vendored
+2
-2
@@ -138,6 +138,6 @@ object IObject {
|
||||
<!INAPPLICABLE_JVM_FIELD!>@JvmField<!>
|
||||
val u = <!EXPERIMENTAL_UNSIGNED_LITERALS!>42u<!>
|
||||
|
||||
@JvmField
|
||||
internal val r: Result<Int> = TODO()
|
||||
<!INAPPLICABLE_JVM_FIELD!>@JvmField<!>
|
||||
private val r: Result<Int> = TODO()
|
||||
}
|
||||
|
||||
Vendored
+1
-1
@@ -76,7 +76,7 @@ public interface I {
|
||||
public object IObject {
|
||||
private constructor IObject()
|
||||
@field:kotlin.jvm.JvmField public final val c: Foo
|
||||
@field:kotlin.jvm.JvmField internal final val r: kotlin.Result<kotlin.Int>
|
||||
@field:kotlin.jvm.JvmField private final val r: kotlin.Result<kotlin.Int>
|
||||
@field:kotlin.jvm.JvmField public final val u: kotlin.UInt = 42.toUInt()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
|
||||
+26
-18
@@ -28,10 +28,14 @@ fun returnContainer(): List<Result<Int>> = TODO()
|
||||
|
||||
val topLevelP: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
val <!RESULT_CLASS_IN_RETURN_TYPE!>topLevelPInferred<!> = topLevelP
|
||||
internal val topLevelPInternal: Result<Int> = TODO()
|
||||
internal val topLevelPInternal: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
|
||||
private val topLevelPPrivate: Result<Int> = TODO()
|
||||
private val topLevelPPrivateInferred = topLevelP
|
||||
|
||||
private val topLevelPPrivateCustomGetter: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!>
|
||||
get() = TODO()
|
||||
|
||||
val asFunctional: () -> Result<Int> = TODO()
|
||||
|
||||
open class PublicCls(
|
||||
@@ -41,7 +45,9 @@ open class PublicCls(
|
||||
val r4: <!RESULT_CLASS_IN_RETURN_TYPE!>ResultAlias<Int>?<!>,
|
||||
|
||||
val r5: InlineResult<Int>,
|
||||
internal val r6: Result<Int>,
|
||||
|
||||
internal val r6: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!>,
|
||||
|
||||
private val r7: Result<Int>,
|
||||
val r8: List<Result<Int>>
|
||||
) {
|
||||
@@ -51,11 +57,11 @@ open class PublicCls(
|
||||
|
||||
val <!RESULT_CLASS_IN_RETURN_TYPE!>p4<!> = p1
|
||||
|
||||
internal val p5: Result<Int> = TODO()
|
||||
internal val p5: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
|
||||
private var p6: Result<Int> = TODO()
|
||||
|
||||
internal val p7 = p1
|
||||
|
||||
internal val <!RESULT_CLASS_IN_RETURN_TYPE!>p7<!> = p1
|
||||
protected val <!RESULT_CLASS_IN_RETURN_TYPE!>p8<!> = p1
|
||||
|
||||
fun returnInCls(): <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
@@ -64,36 +70,38 @@ open class PublicCls(
|
||||
}
|
||||
|
||||
internal open class InternalCls(
|
||||
val r1: Result<Int>,
|
||||
val r2: ResultAlias<Int>?,
|
||||
val r1: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!>,
|
||||
val r2: <!RESULT_CLASS_IN_RETURN_TYPE!>ResultAlias<Int>?<!>,
|
||||
|
||||
val r3: List<Result<Int>>
|
||||
) {
|
||||
companion object {
|
||||
val cr1: Result<Int> = TODO()
|
||||
val cr1: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
|
||||
private val cr2: Result<Int> = TODO()
|
||||
}
|
||||
|
||||
val p1 = r1
|
||||
val p2: Result<String> = TODO()
|
||||
val <!RESULT_CLASS_IN_RETURN_TYPE!>p1<!> = r1
|
||||
val p2: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<String><!> = TODO()
|
||||
|
||||
protected val p3 = p1
|
||||
protected val <!RESULT_CLASS_IN_RETURN_TYPE!>p3<!> = p1
|
||||
|
||||
fun returnInInternal(): <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
protected fun returnInClsProtected(): <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
}
|
||||
|
||||
private class PrivateCls(
|
||||
val r1: Result<Int>,
|
||||
val r2: ResultAlias<Int>?,
|
||||
val r1: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!>,
|
||||
val r2: <!RESULT_CLASS_IN_RETURN_TYPE!>ResultAlias<Int>?<!>,
|
||||
val r3: List<Result<Int>>
|
||||
) {
|
||||
companion object {
|
||||
val cr1: Result<Int> = TODO()
|
||||
val cr1: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
private val cr2: Result<Int> = TODO()
|
||||
}
|
||||
|
||||
val p1 = r1
|
||||
val p2: Result<String> = TODO()
|
||||
val <!RESULT_CLASS_IN_RETURN_TYPE!>p1<!> = r1
|
||||
val p2: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<String><!> = TODO()
|
||||
|
||||
fun returnInPrivate(): <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
}
|
||||
@@ -105,8 +113,8 @@ fun local(r: Result<Int>) {
|
||||
fun localFun(): <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
|
||||
|
||||
class F {
|
||||
val p1: Result<Int> = r
|
||||
val p2 = r
|
||||
val p1: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = r
|
||||
val <!RESULT_CLASS_IN_RETURN_TYPE!>p2<!> = r
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
+1
@@ -7,6 +7,7 @@ public val topLevelP: kotlin.Result<kotlin.Int>
|
||||
public val topLevelPInferred: kotlin.Result<kotlin.Int>
|
||||
internal val topLevelPInternal: kotlin.Result<kotlin.Int>
|
||||
private val topLevelPPrivate: kotlin.Result<kotlin.Int>
|
||||
private val topLevelPPrivateCustomGetter: kotlin.Result<kotlin.Int>
|
||||
private val topLevelPPrivateInferred: kotlin.Result<kotlin.Int>
|
||||
public fun local(/*0*/ r: kotlin.Result<kotlin.Int>): kotlin.Unit
|
||||
public fun params(/*0*/ r1: kotlin.Result<kotlin.Int>, /*1*/ r2: kotlin.Result<kotlin.Int>?, /*2*/ r3: ResultAlias<kotlin.String> /* = kotlin.Result<kotlin.String> */, /*3*/ r4: kotlin.collections.List<kotlin.Result<kotlin.Int>>, /*4*/ r5: InlineResult<kotlin.Int>, /*5*/ vararg r6: kotlin.Result<kotlin.Int> /*kotlin.Array<out kotlin.Result<kotlin.Int>>*/): kotlin.Unit
|
||||
|
||||
Vendored
+1
-1
@@ -3,7 +3,7 @@
|
||||
fun <T> id(x: T): T = x
|
||||
|
||||
private val asFun: () -> Result<Int>? = TODO()
|
||||
private val Int.intResult: Result<Int>?
|
||||
private val Int.intResult: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int>?<!>
|
||||
get() = null
|
||||
|
||||
fun returnInt(): Int? = 0
|
||||
|
||||
Reference in New Issue
Block a user