Prohibit Array<Nothing>

This commit is contained in:
Alexey Tsvetkov
2015-11-27 12:57:34 +03:00
parent 35f788a89c
commit 7384d25cb3
8 changed files with 154 additions and 5 deletions
@@ -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)
}
@@ -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() }
}
}