Allow null operators for result and using Result as return type with enabled InlineClasses

This commit is contained in:
Dmitriy Novozhilov
2019-12-17 11:21:09 +03:00
parent eed5b3f1d1
commit aa8578b675
18 changed files with 336 additions and 2 deletions
@@ -6,6 +6,7 @@
package org.jetbrains.kotlin.resolve.calls.checkers
import com.intellij.psi.PsiElement
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
import org.jetbrains.kotlin.lexer.KtTokens
@@ -16,6 +17,8 @@ import org.jetbrains.kotlin.types.expressions.ControlStructureTypingUtils
class ResultTypeWithNullableOperatorsChecker : CallChecker {
override fun check(resolvedCall: ResolvedCall<*>, reportOn: PsiElement, context: CallCheckerContext) {
if (context.languageVersionSettings.supportsFeature(LanguageFeature.AllowNullOperatorsForResult)) return
val name = resolvedCall.resultingDescriptor.name
val operationNode = resolvedCall.call.callOperationNode
@@ -6,6 +6,8 @@
package org.jetbrains.kotlin.resolve.checkers
import org.jetbrains.kotlin.config.AnalysisFlags
import org.jetbrains.kotlin.config.LanguageFeature
import org.jetbrains.kotlin.config.LanguageVersion
import org.jetbrains.kotlin.descriptors.*
import org.jetbrains.kotlin.diagnostics.Errors
import org.jetbrains.kotlin.diagnostics.reportDiagnosticOnce
@@ -17,7 +19,12 @@ import org.jetbrains.kotlin.types.KotlinType
class ResultClassInReturnTypeChecker : DeclarationChecker {
override fun check(declaration: KtDeclaration, descriptor: DeclarationDescriptor, context: DeclarationCheckerContext) {
if (context.languageVersionSettings.getFlag(AnalysisFlags.allowResultReturnType)) return
val languageVersionSettings = context.languageVersionSettings
if (
languageVersionSettings.getFlag(AnalysisFlags.allowResultReturnType) ||
languageVersionSettings.supportsFeature(LanguageFeature.InlineClasses) &&
languageVersionSettings.supportsFeature(LanguageFeature.AllowResultInReturnType)
) return
if (declaration !is KtCallableDeclaration || descriptor !is CallableMemberDescriptor) return
@@ -0,0 +1,7 @@
// !LANGUAGE: -AllowNullOperatorsForResult
// !DIAGNOSTICS: -UNUSED_EXPRESSION
fun test(r: Result<Int>?) {
r <!RESULT_CLASS_WITH_NULLABLE_OPERATOR!>?:<!> 0
r<!RESULT_CLASS_WITH_NULLABLE_OPERATOR!>?.<!>isFailure
}
@@ -0,0 +1,3 @@
package
public fun test(/*0*/ r: kotlin.Result<kotlin.Int>?): kotlin.Unit
@@ -0,0 +1,7 @@
// !LANGUAGE: +AllowNullOperatorsForResult
// !DIAGNOSTICS: -UNUSED_EXPRESSION
fun test(r: Result<Int>?) {
r ?: 0
r?.isFailure
}
@@ -0,0 +1,3 @@
package
public fun test(/*0*/ r: kotlin.Result<kotlin.Int>?): kotlin.Unit
@@ -1,4 +1,5 @@
// !ALLOW_RESULT_RETURN_TYPE
// !LANGUAGE: -AllowNullOperatorsForResult
fun result(): Result<Int> = TODO()
val resultP: Result<Int> = result()
@@ -0,0 +1,4 @@
// !LANGUAGE: +InlineClasses -AllowResultInReturnType
fun result(): <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = TODO()
val resultP: <!RESULT_CLASS_IN_RETURN_TYPE!>Result<Int><!> = result()
@@ -0,0 +1,4 @@
package
public val resultP: kotlin.Result<kotlin.Int>
public fun result(): kotlin.Result<kotlin.Int>
@@ -0,0 +1,4 @@
// !LANGUAGE: +InlineClasses +AllowResultInReturnType
fun result(): Result<Int> = TODO()
val resultP: Result<Int> = result()
@@ -0,0 +1,4 @@
package
public val resultP: kotlin.Result<kotlin.Int>
public fun result(): kotlin.Result<kotlin.Int>
@@ -1,5 +1,5 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER, -UNUSED_EXPRESSION, -UNUSED_VARIABLE
// !LANGUAGE: +InlineClasses
// !LANGUAGE: +InlineClasses -AllowResultInReturnType
typealias ResultAlias<T> = Result<T>
@@ -0,0 +1,124 @@
// !DIAGNOSTICS: -UNUSED_PARAMETER, -UNUSED_EXPRESSION, -UNUSED_VARIABLE
// !LANGUAGE: +InlineClasses +AllowResultInReturnType
typealias ResultAlias<T> = Result<T>
inline class InlineResult<out T>(private val r: Result<T>)
fun params(
r1: Result<Int>,
r2: Result<Int>?,
r3: ResultAlias<String>,
r4: List<Result<Int>>,
r5: InlineResult<Int>,
<!FORBIDDEN_VARARG_PARAMETER_TYPE!>vararg<!> r6: Result<Int>
) {}
class CtorParams(r1: Result<Int>)
fun returnTypePublic(): Result<Int> = TODO()
internal fun returnTypeInternal(): Result<Int> = TODO()
private fun returnTypePrivate(): Result<Int> = TODO()
fun returnTypeNullable(): Result<Int>? = TODO()
fun returnTypeAlias(): ResultAlias<Int> = TODO()
fun returnInferred(r1: Result<Int>, r2: ResultAlias<Int>, b: Boolean) = if (b) r1 else r2
fun returnTypeInline(): InlineResult<Int> = TODO()
fun returnContainer(): List<Result<Int>> = TODO()
val topLevelP: Result<Int> = TODO()
val topLevelPInferred = topLevelP
internal val topLevelPInternal: Result<Int> = TODO()
private val topLevelPPrivate: Result<Int> = TODO()
private val topLevelPPrivateInferred = topLevelP
private val topLevelPPrivateCustomGetter: Result<Int>
get() = TODO()
val asFunctional: () -> Result<Int> = TODO()
open class PublicCls(
val r1: Result<String>,
val r2: Result<Int>?,
val r3: ResultAlias<Int>,
val r4: ResultAlias<Int>?,
val r5: InlineResult<Int>,
internal val r6: Result<Int>,
private val r7: Result<Int>,
val r8: List<Result<Int>>
) {
val p1: Result<Int> = TODO()
var p2: Result<Int> = TODO()
val p3: ResultAlias<Int>? = TODO()
val p4 = p1
internal val p5: Result<Int> = TODO()
private var p6: Result<Int> = TODO()
internal val p7 = p1
protected val p8 = p1
fun returnInCls(): Result<Int> = TODO()
protected fun returnInClsProtected(): Result<Int> = TODO()
private fun returnInClsPrivate(): Result<Int> = TODO()
}
internal open class InternalCls(
val r1: Result<Int>,
val r2: ResultAlias<Int>?,
val r3: List<Result<Int>>
) {
companion object {
val cr1: Result<Int> = TODO()
private val cr2: Result<Int> = TODO()
}
val p1 = r1
val p2: Result<String> = TODO()
protected val p3 = p1
fun returnInInternal(): Result<Int> = TODO()
protected fun returnInClsProtected(): Result<Int> = TODO()
}
private class PrivateCls(
val r1: Result<Int>,
val r2: ResultAlias<Int>?,
val r3: List<Result<Int>>
) {
companion object {
val cr1: Result<Int> = TODO()
private val cr2: Result<Int> = TODO()
}
val p1 = r1
val p2: Result<String> = TODO()
fun returnInPrivate(): Result<Int> = TODO()
}
fun local(r: Result<Int>) {
val l1: Result<Int>? = null
val l2 = r
fun localFun(): Result<Int> = TODO()
class F {
val p1: Result<Int> = r
val p2 = r
}
}
fun <T> resultInGenericFun(r: Result<Int>): T = r <!UNCHECKED_CAST!>as T<!>
val asFunPublic: () -> Result<Int> = TODO()
private val asFun: () -> Result<Int>? = TODO()
@@ -0,0 +1,110 @@
package
private val asFun: () -> kotlin.Result<kotlin.Int>?
public val asFunPublic: () -> kotlin.Result<kotlin.Int>
public val asFunctional: () -> kotlin.Result<kotlin.Int>
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
public fun </*0*/ T> resultInGenericFun(/*0*/ r: kotlin.Result<kotlin.Int>): T
public fun returnContainer(): kotlin.collections.List<kotlin.Result<kotlin.Int>>
public fun returnInferred(/*0*/ r1: kotlin.Result<kotlin.Int>, /*1*/ r2: ResultAlias<kotlin.Int> /* = kotlin.Result<kotlin.Int> */, /*2*/ b: kotlin.Boolean): kotlin.Result<kotlin.Int>
public fun returnTypeAlias(): ResultAlias<kotlin.Int> /* = kotlin.Result<kotlin.Int> */
public fun returnTypeInline(): InlineResult<kotlin.Int>
internal fun returnTypeInternal(): kotlin.Result<kotlin.Int>
public fun returnTypeNullable(): kotlin.Result<kotlin.Int>?
private fun returnTypePrivate(): kotlin.Result<kotlin.Int>
public fun returnTypePublic(): kotlin.Result<kotlin.Int>
public final class CtorParams {
public constructor CtorParams(/*0*/ r1: kotlin.Result<kotlin.Int>)
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public final inline class InlineResult</*0*/ out T> {
public constructor InlineResult</*0*/ out T>(/*0*/ r: kotlin.Result<T>)
private final val r: kotlin.Result<T>
public open override /*1*/ /*synthesized*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*synthesized*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*synthesized*/ fun toString(): kotlin.String
}
internal open class InternalCls {
public constructor InternalCls(/*0*/ r1: kotlin.Result<kotlin.Int>, /*1*/ r2: ResultAlias<kotlin.Int>? /* = kotlin.Result<kotlin.Int>? */, /*2*/ r3: kotlin.collections.List<kotlin.Result<kotlin.Int>>)
public final val p1: kotlin.Result<kotlin.Int>
public final val p2: kotlin.Result<kotlin.String>
protected final val p3: kotlin.Result<kotlin.Int>
public final val r1: kotlin.Result<kotlin.Int>
public final val r2: ResultAlias<kotlin.Int>? /* = kotlin.Result<kotlin.Int>? */
public final val r3: kotlin.collections.List<kotlin.Result<kotlin.Int>>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
protected final fun returnInClsProtected(): kotlin.Result<kotlin.Int>
public final fun returnInInternal(): kotlin.Result<kotlin.Int>
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public companion object Companion {
private constructor Companion()
public final val cr1: kotlin.Result<kotlin.Int>
private final val cr2: kotlin.Result<kotlin.Int>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
private final class PrivateCls {
public constructor PrivateCls(/*0*/ r1: kotlin.Result<kotlin.Int>, /*1*/ r2: ResultAlias<kotlin.Int>? /* = kotlin.Result<kotlin.Int>? */, /*2*/ r3: kotlin.collections.List<kotlin.Result<kotlin.Int>>)
public final val p1: kotlin.Result<kotlin.Int>
public final val p2: kotlin.Result<kotlin.String>
public final val r1: kotlin.Result<kotlin.Int>
public final val r2: ResultAlias<kotlin.Int>? /* = kotlin.Result<kotlin.Int>? */
public final val r3: kotlin.collections.List<kotlin.Result<kotlin.Int>>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun returnInPrivate(): kotlin.Result<kotlin.Int>
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
public companion object Companion {
private constructor Companion()
public final val cr1: kotlin.Result<kotlin.Int>
private final val cr2: kotlin.Result<kotlin.Int>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
}
public open class PublicCls {
public constructor PublicCls(/*0*/ r1: kotlin.Result<kotlin.String>, /*1*/ r2: kotlin.Result<kotlin.Int>?, /*2*/ r3: ResultAlias<kotlin.Int> /* = kotlin.Result<kotlin.Int> */, /*3*/ r4: ResultAlias<kotlin.Int>? /* = kotlin.Result<kotlin.Int>? */, /*4*/ r5: InlineResult<kotlin.Int>, /*5*/ r6: kotlin.Result<kotlin.Int>, /*6*/ r7: kotlin.Result<kotlin.Int>, /*7*/ r8: kotlin.collections.List<kotlin.Result<kotlin.Int>>)
public final val p1: kotlin.Result<kotlin.Int>
public final var p2: kotlin.Result<kotlin.Int>
public final val p3: ResultAlias<kotlin.Int>? /* = kotlin.Result<kotlin.Int>? */
public final val p4: kotlin.Result<kotlin.Int>
internal final val p5: kotlin.Result<kotlin.Int>
private final var p6: kotlin.Result<kotlin.Int>
internal final val p7: kotlin.Result<kotlin.Int>
protected final val p8: kotlin.Result<kotlin.Int>
public final val r1: kotlin.Result<kotlin.String>
public final val r2: kotlin.Result<kotlin.Int>?
public final val r3: ResultAlias<kotlin.Int> /* = kotlin.Result<kotlin.Int> */
public final val r4: ResultAlias<kotlin.Int>? /* = kotlin.Result<kotlin.Int>? */
public final val r5: InlineResult<kotlin.Int>
internal final val r6: kotlin.Result<kotlin.Int>
private final val r7: kotlin.Result<kotlin.Int>
public final val r8: kotlin.collections.List<kotlin.Result<kotlin.Int>>
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public final fun returnInCls(): kotlin.Result<kotlin.Int>
private final fun returnInClsPrivate(): kotlin.Result<kotlin.Int>
protected final fun returnInClsProtected(): kotlin.Result<kotlin.Int>
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public typealias ResultAlias</*0*/ T> = kotlin.Result<T>
@@ -1,4 +1,5 @@
// !DIAGNOSTICS: -UNUSED_EXPRESSION
// !LANGUAGE: -AllowNullOperatorsForResult -AllowResultInReturnType
fun <T> id(x: T): T = x
@@ -1513,11 +1513,31 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/coroutines"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("allowNullOperatorsForResult_1_3.kt")
public void testAllowNullOperatorsForResult_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowNullOperatorsForResult_1_3.kt");
}
@TestMetadata("allowNullOperatorsForResult_1_4.kt")
public void testAllowNullOperatorsForResult_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowNullOperatorsForResult_1_4.kt");
}
@TestMetadata("allowResultInReturnTypeWithFlag.kt")
public void testAllowResultInReturnTypeWithFlag() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowResultInReturnTypeWithFlag.kt");
}
@TestMetadata("allowResultInReturnType_1_3.kt")
public void testAllowResultInReturnType_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowResultInReturnType_1_3.kt");
}
@TestMetadata("allowResultInReturnType_1_4.kt")
public void testAllowResultInReturnType_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowResultInReturnType_1_4.kt");
}
@TestMetadata("callableReferences.kt")
public void testCallableReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/callableReferences.kt");
@@ -1763,6 +1783,11 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/usageOfResultTypeInReturnType.kt");
}
@TestMetadata("usageOfResultTypeInReturnType_1_4.kt")
public void testUsageOfResultTypeInReturnType_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/usageOfResultTypeInReturnType_1_4.kt");
}
@TestMetadata("usageOfResultTypeWithNullableOperators.kt")
public void testUsageOfResultTypeWithNullableOperators() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/usageOfResultTypeWithNullableOperators.kt");
@@ -1513,11 +1513,31 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
KotlinTestUtils.assertAllTestsPresentByMetadataWithExcluded(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib/coroutines"), Pattern.compile("^(.+)\\.kt$"), Pattern.compile("^(.+)\\.fir\\.kts?$"), true);
}
@TestMetadata("allowNullOperatorsForResult_1_3.kt")
public void testAllowNullOperatorsForResult_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowNullOperatorsForResult_1_3.kt");
}
@TestMetadata("allowNullOperatorsForResult_1_4.kt")
public void testAllowNullOperatorsForResult_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowNullOperatorsForResult_1_4.kt");
}
@TestMetadata("allowResultInReturnTypeWithFlag.kt")
public void testAllowResultInReturnTypeWithFlag() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowResultInReturnTypeWithFlag.kt");
}
@TestMetadata("allowResultInReturnType_1_3.kt")
public void testAllowResultInReturnType_1_3() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowResultInReturnType_1_3.kt");
}
@TestMetadata("allowResultInReturnType_1_4.kt")
public void testAllowResultInReturnType_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/allowResultInReturnType_1_4.kt");
}
@TestMetadata("callableReferences.kt")
public void testCallableReferences() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/callableReferences.kt");
@@ -1763,6 +1783,11 @@ public class DiagnosticsTestWithStdLibUsingJavacGenerated extends AbstractDiagno
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/usageOfResultTypeInReturnType.kt");
}
@TestMetadata("usageOfResultTypeInReturnType_1_4.kt")
public void testUsageOfResultTypeInReturnType_1_4() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/usageOfResultTypeInReturnType_1_4.kt");
}
@TestMetadata("usageOfResultTypeWithNullableOperators.kt")
public void testUsageOfResultTypeWithNullableOperators() throws Exception {
runTest("compiler/testData/diagnostics/testsWithStdLib/coroutines/usageOfResultTypeWithNullableOperators.kt");
@@ -118,6 +118,8 @@ enum class LanguageFeature(
ProperFinally(KOTLIN_1_4, kind = BUG_FIX),
ProhibitVarargAsArrayAfterSamArgument(KOTLIN_1_4, kind = BUG_FIX),
AllowAssigningArrayElementsToVarargsInNamedFormForFunctions(KOTLIN_1_4),
AllowNullOperatorsForResult(KOTLIN_1_4),
AllowResultInReturnType(KOTLIN_1_4),
ProperVisibilityForCompanionObjectInstanceField(sinceVersion = null, kind = BUG_FIX),
// Temporarily disabled, see KT-27084/KT-22379