Prohibit Array<Nothing>
This commit is contained in:
@@ -25,7 +25,10 @@ import org.jetbrains.kotlin.descriptors.ModuleParameters
|
||||
import org.jetbrains.kotlin.descriptors.impl.ModuleDescriptorImpl
|
||||
import org.jetbrains.kotlin.name.Name
|
||||
import org.jetbrains.kotlin.resolve.calls.checkers.*
|
||||
import org.jetbrains.kotlin.resolve.validation.*
|
||||
import org.jetbrains.kotlin.resolve.validation.DeprecatedSymbolValidator
|
||||
import org.jetbrains.kotlin.resolve.validation.InfixValidator
|
||||
import org.jetbrains.kotlin.resolve.validation.OperatorValidator
|
||||
import org.jetbrains.kotlin.resolve.validation.SymbolUsageValidator
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.types.DynamicTypesSettings
|
||||
|
||||
@@ -58,7 +61,7 @@ private val DEFAULT_DECLARATION_CHECKERS = listOf(
|
||||
InfixModifierChecker())
|
||||
|
||||
private val DEFAULT_CALL_CHECKERS = listOf(CapturingInClosureChecker(), InlineCheckerWrapper(), ReifiedTypeParameterSubstitutionChecker(),
|
||||
SafeCallChecker(), InvokeConventionChecker())
|
||||
SafeCallChecker(), InvokeConventionChecker(), CallReturnsArrayOfNothingChecker())
|
||||
private val DEFAULT_TYPE_CHECKERS = emptyList<AdditionalTypeChecker>()
|
||||
private val DEFAULT_VALIDATORS = listOf(DeprecatedSymbolValidator(), OperatorValidator(), InfixValidator())
|
||||
|
||||
|
||||
@@ -33,14 +33,15 @@ import org.jetbrains.kotlin.resolve.bindingContextUtil.recordScope
|
||||
import org.jetbrains.kotlin.resolve.calls.tasks.DynamicCallableDescriptors
|
||||
import org.jetbrains.kotlin.resolve.lazy.ForceResolveUtil
|
||||
import org.jetbrains.kotlin.resolve.lazy.LazyEntity
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.LazyScopeAdapter
|
||||
import org.jetbrains.kotlin.resolve.scopes.LexicalScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.MemberScope
|
||||
import org.jetbrains.kotlin.resolve.scopes.utils.findClassifier
|
||||
import org.jetbrains.kotlin.storage.LockBasedStorageManager
|
||||
import org.jetbrains.kotlin.storage.StorageManager
|
||||
import org.jetbrains.kotlin.types.*
|
||||
import org.jetbrains.kotlin.types.Variance.*
|
||||
import org.jetbrains.kotlin.types.typeUtil.isArrayOfNothing
|
||||
|
||||
public class TypeResolver(
|
||||
private val annotationResolver: AnnotationResolver,
|
||||
@@ -349,6 +350,10 @@ public class TypeResolver(
|
||||
}
|
||||
}
|
||||
|
||||
if (resultingType.isArrayOfNothing()) {
|
||||
c.trace.report(UNSUPPORTED.on(type, "Array<Nothing> is illegal"))
|
||||
}
|
||||
|
||||
return type(resultingType)
|
||||
}
|
||||
|
||||
|
||||
+48
@@ -0,0 +1,48 @@
|
||||
/*
|
||||
* Copyright 2010-2015 JetBrains s.r.o.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.jetbrains.kotlin.resolve.calls.checkers
|
||||
|
||||
import org.jetbrains.kotlin.descriptors.CallableDescriptor
|
||||
import org.jetbrains.kotlin.diagnostics.Errors
|
||||
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
|
||||
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
|
||||
import org.jetbrains.kotlin.types.DeferredType
|
||||
import org.jetbrains.kotlin.types.KotlinType
|
||||
import org.jetbrains.kotlin.types.typeUtil.isArrayOfNothing
|
||||
|
||||
class CallReturnsArrayOfNothingChecker : CallChecker {
|
||||
override fun <F : CallableDescriptor> check(resolvedCall: ResolvedCall<F>, context: BasicCallResolutionContext) {
|
||||
val returnType = resolvedCall.resultingDescriptor.returnType
|
||||
|
||||
if (returnType.containsArrayOfNothing()) {
|
||||
val callElement = resolvedCall.call.callElement
|
||||
val diagnostic = Errors.UNSUPPORTED.on(callElement, "Array<Nothing> in return type is illegal")
|
||||
context.trace.report(diagnostic)
|
||||
}
|
||||
}
|
||||
|
||||
private fun KotlinType?.containsArrayOfNothing(): Boolean {
|
||||
// if this.isComputing is true, it means that resolve
|
||||
// has run into recursion, so checking for Array<Nothing> is meaningless anyway,
|
||||
// and error about recursion will be reported later
|
||||
if (this == null || this is DeferredType && this.isComputing) return false
|
||||
|
||||
if (isArrayOfNothing()) return true
|
||||
|
||||
return arguments.any { !it.isStarProjection && it.type.containsArrayOfNothing() }
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,54 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER -UNUSED_VARIABLE -UNCHECKED_CAST -USELESS_CAST
|
||||
class A<T>
|
||||
|
||||
fun test1(
|
||||
a: <!UNSUPPORTED!>Array<Nothing><!>,
|
||||
b: <!UNSUPPORTED!>Array<Nothing?><!>,
|
||||
c: <!UNSUPPORTED!>Array<in Nothing><!>,
|
||||
d: <!UNSUPPORTED!>Array<in Nothing?><!>,
|
||||
e: <!UNSUPPORTED!>Array<out Nothing><!>,
|
||||
f: <!UNSUPPORTED!>Array<out Nothing?><!>
|
||||
) {}
|
||||
|
||||
fun test2(
|
||||
a: <!UNSUPPORTED!>Array<Nothing><!>?,
|
||||
b: <!UNSUPPORTED!>Array<Nothing?><!>?,
|
||||
c: <!UNSUPPORTED!>Array<in Nothing><!>?,
|
||||
d: <!UNSUPPORTED!>Array<in Nothing?><!>?,
|
||||
e: <!UNSUPPORTED!>Array<out Nothing><!>?,
|
||||
f: <!UNSUPPORTED!>Array<out Nothing?><!>?
|
||||
) {}
|
||||
|
||||
fun test3(
|
||||
a: A<<!UNSUPPORTED!>Array<Nothing><!>>,
|
||||
b: A<<!UNSUPPORTED!>Array<Nothing?><!>>,
|
||||
c: A<<!UNSUPPORTED!>Array<in Nothing><!>>,
|
||||
d: A<<!UNSUPPORTED!>Array<in Nothing?><!>>,
|
||||
e: A<<!UNSUPPORTED!>Array<out Nothing><!>>,
|
||||
f: A<<!UNSUPPORTED!>Array<out Nothing?><!>>
|
||||
) {}
|
||||
|
||||
fun test4(
|
||||
a: Array<A<Nothing>>,
|
||||
b: Array<A<Nothing?>>,
|
||||
c: Array<A<in Nothing>>,
|
||||
d: Array<A<in Nothing?>>,
|
||||
e: Array<A<out Nothing>>,
|
||||
f: Array<A<out Nothing?>>
|
||||
) {}
|
||||
|
||||
fun test5() {
|
||||
<!UNSUPPORTED!><!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>arrayOf<!><Nothing>()<!>
|
||||
<!UNSUPPORTED!><!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Array<!><Nothing>(10) { throw Exception() }<!>
|
||||
}
|
||||
|
||||
fun <T> foo(): Array<T> = (object {} as Any) as Array<T>
|
||||
|
||||
fun test6() = <!UNSUPPORTED!>foo<Nothing>()<!>
|
||||
|
||||
|
||||
class B<T>(val array: Array<T>)
|
||||
|
||||
fun <T> bar() = B<Array<T>>(arrayOf())
|
||||
|
||||
fun test7() = <!UNSUPPORTED!>bar<Nothing>()<!>
|
||||
@@ -0,0 +1,26 @@
|
||||
package
|
||||
|
||||
public fun </*0*/ T> bar(): B<kotlin.Array<T>>
|
||||
public fun </*0*/ T> foo(): kotlin.Array<T>
|
||||
public fun test1(/*0*/ a: kotlin.Array<kotlin.Nothing>, /*1*/ b: kotlin.Array<kotlin.Nothing?>, /*2*/ c: kotlin.Array<in kotlin.Nothing>, /*3*/ d: kotlin.Array<in kotlin.Nothing?>, /*4*/ e: kotlin.Array<out kotlin.Nothing>, /*5*/ f: kotlin.Array<out kotlin.Nothing?>): kotlin.Unit
|
||||
public fun test2(/*0*/ a: kotlin.Array<kotlin.Nothing>?, /*1*/ b: kotlin.Array<kotlin.Nothing?>?, /*2*/ c: kotlin.Array<in kotlin.Nothing>?, /*3*/ d: kotlin.Array<in kotlin.Nothing?>?, /*4*/ e: kotlin.Array<out kotlin.Nothing>?, /*5*/ f: kotlin.Array<out kotlin.Nothing?>?): kotlin.Unit
|
||||
public fun test3(/*0*/ a: A<kotlin.Array<kotlin.Nothing>>, /*1*/ b: A<kotlin.Array<kotlin.Nothing?>>, /*2*/ c: A<kotlin.Array<in kotlin.Nothing>>, /*3*/ d: A<kotlin.Array<in kotlin.Nothing?>>, /*4*/ e: A<kotlin.Array<out kotlin.Nothing>>, /*5*/ f: A<kotlin.Array<out kotlin.Nothing?>>): kotlin.Unit
|
||||
public fun test4(/*0*/ a: kotlin.Array<A<kotlin.Nothing>>, /*1*/ b: kotlin.Array<A<kotlin.Nothing?>>, /*2*/ c: kotlin.Array<A<in kotlin.Nothing>>, /*3*/ d: kotlin.Array<A<in kotlin.Nothing?>>, /*4*/ e: kotlin.Array<A<out kotlin.Nothing>>, /*5*/ f: kotlin.Array<A<out kotlin.Nothing?>>): kotlin.Unit
|
||||
public fun test5(): kotlin.Unit
|
||||
public fun test6(): kotlin.Array<kotlin.Nothing>
|
||||
public fun test7(): B<kotlin.Array<kotlin.Nothing>>
|
||||
|
||||
public final class A</*0*/ T> {
|
||||
public constructor A</*0*/ T>()
|
||||
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 class B</*0*/ T> {
|
||||
public constructor B</*0*/ T>(/*0*/ array: kotlin.Array<T>)
|
||||
public final val array: kotlin.Array<T>
|
||||
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
|
||||
}
|
||||
+2
-2
@@ -3,8 +3,8 @@
|
||||
inline fun<reified T> foo(block: () -> T): String = block().toString()
|
||||
|
||||
fun box() {
|
||||
val a = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>arrayOf<!>(null!!)
|
||||
val b = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Array<!><Nothing?>(5) { null!! }
|
||||
val a = <!UNSUPPORTED!><!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>arrayOf<!>(null!!)<!>
|
||||
val b = <!UNSUPPORTED!><!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>Array<!><Nothing?>(5) { null!! }<!>
|
||||
val c = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!>() { null!! }
|
||||
val d = foo<Any> { null!! }
|
||||
val e = <!REIFIED_TYPE_FORBIDDEN_SUBSTITUTION!>foo<!> { "1" as Nothing }
|
||||
|
||||
@@ -35,6 +35,12 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
|
||||
KotlinTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/testsWithStdLib"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("ArrayOfNothing.kt")
|
||||
public void testArrayOfNothing() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/ArrayOfNothing.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("CallCompanionProtectedNonStatic.kt")
|
||||
public void testCallCompanionProtectedNonStatic() throws Exception {
|
||||
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/CallCompanionProtectedNonStatic.kt");
|
||||
|
||||
@@ -55,6 +55,13 @@ fun KotlinType.isAnyOrNullableAny(): Boolean = KotlinBuiltIns.isAnyOrNullableAny
|
||||
fun KotlinType.isBoolean(): Boolean = KotlinBuiltIns.isBoolean(this)
|
||||
fun KotlinType.isBooleanOrNullableBoolean(): Boolean = KotlinBuiltIns.isBooleanOrNullableBoolean(this)
|
||||
|
||||
fun KotlinType?.isArrayOfNothing(): Boolean {
|
||||
if (this == null || !KotlinBuiltIns.isArray(this)) return false
|
||||
|
||||
val typeArg = arguments.firstOrNull()?.type
|
||||
return typeArg != null && KotlinBuiltIns.isNothingOrNullableNothing(typeArg)
|
||||
}
|
||||
|
||||
private fun KotlinType.getContainedTypeParameters(): Collection<TypeParameterDescriptor> {
|
||||
val declarationDescriptor = getConstructor().getDeclarationDescriptor()
|
||||
if (declarationDescriptor is TypeParameterDescriptor) return listOf(declarationDescriptor)
|
||||
|
||||
Reference in New Issue
Block a user