Search in sources :

Example 1 with KtTypeProjection

use of org.jetbrains.kotlin.psi.KtTypeProjection in project kotlin by JetBrains.

the class ReifiedTypeParameterSubstitutionChecker method check.

@Override
public void check(@NotNull ResolvedCall<?> resolvedCall, @NotNull PsiElement reportOn, @NotNull CallCheckerContext context) {
    Map<TypeParameterDescriptor, KotlinType> typeArguments = resolvedCall.getTypeArguments();
    for (Map.Entry<TypeParameterDescriptor, KotlinType> entry : typeArguments.entrySet()) {
        TypeParameterDescriptor parameter = entry.getKey();
        KotlinType argument = entry.getValue();
        ClassifierDescriptor argumentDeclarationDescriptor = argument.getConstructor().getDeclarationDescriptor();
        if (!parameter.isReified() && !isTypeParameterOfKotlinArray(parameter)) {
            continue;
        }
        KtTypeProjection typeProjection = CollectionsKt.getOrNull(resolvedCall.getCall().getTypeArguments(), parameter.getIndex());
        PsiElement reportErrorOn = typeProjection != null ? typeProjection : reportOn;
        if (argumentDeclarationDescriptor instanceof TypeParameterDescriptor && !((TypeParameterDescriptor) argumentDeclarationDescriptor).isReified()) {
            context.getTrace().report(Errors.TYPE_PARAMETER_AS_REIFIED.on(reportErrorOn, (TypeParameterDescriptor) argumentDeclarationDescriptor));
        } else if (TypeUtilsKt.cannotBeReified(argument)) {
            context.getTrace().report(Errors.REIFIED_TYPE_FORBIDDEN_SUBSTITUTION.on(reportErrorOn, argument));
        }
    // REIFIED_TYPE_UNSAFE_SUBSTITUTION is temporary disabled because it seems too strict now (see KT-10847)
    //else if (TypeUtilsKt.unsafeAsReifiedArgument(argument) && !hasPureReifiableAnnotation(parameter)) {
    //    context.getTrace().report(Errors.REIFIED_TYPE_UNSAFE_SUBSTITUTION.on(reportErrorOn, argument));
    //}
    }
}
Also used : TypeParameterDescriptor(org.jetbrains.kotlin.descriptors.TypeParameterDescriptor) KotlinType(org.jetbrains.kotlin.types.KotlinType) ClassifierDescriptor(org.jetbrains.kotlin.descriptors.ClassifierDescriptor) KtTypeProjection(org.jetbrains.kotlin.psi.KtTypeProjection) Map(java.util.Map) PsiElement(com.intellij.psi.PsiElement)

Example 2 with KtTypeProjection

use of org.jetbrains.kotlin.psi.KtTypeProjection in project kotlin by JetBrains.

the class TypeUnifierTest method makeType.

private TypeProjection makeType(String typeStr) {
    LexicalScope withX = new LexicalScopeImpl(builtinsImportingScope, module, false, null, LexicalScopeKind.SYNTHETIC, LocalRedeclarationChecker.DO_NOTHING.INSTANCE, new Function1<LexicalScopeImpl.InitializeHandler, Unit>() {

        @Override
        public Unit invoke(LexicalScopeImpl.InitializeHandler handler) {
            handler.addClassifierDescriptor(x);
            handler.addClassifierDescriptor(y);
            return Unit.INSTANCE;
        }
    });
    KtTypeProjection projection = KtPsiFactoryKt.KtPsiFactory(getProject()).createTypeArguments("<" + typeStr + ">").getArguments().get(0);
    KtTypeReference typeReference = projection.getTypeReference();
    assert typeReference != null;
    KotlinType type = typeResolver.resolveType(withX, typeReference, KotlinTestUtils.DUMMY_TRACE, true);
    return new TypeProjectionImpl(getProjectionKind(typeStr, projection), type);
}
Also used : KtTypeProjection(org.jetbrains.kotlin.psi.KtTypeProjection) KtTypeReference(org.jetbrains.kotlin.psi.KtTypeReference) Unit(kotlin.Unit)

Aggregations

KtTypeProjection (org.jetbrains.kotlin.psi.KtTypeProjection)2 PsiElement (com.intellij.psi.PsiElement)1 Map (java.util.Map)1 Unit (kotlin.Unit)1 ClassifierDescriptor (org.jetbrains.kotlin.descriptors.ClassifierDescriptor)1 TypeParameterDescriptor (org.jetbrains.kotlin.descriptors.TypeParameterDescriptor)1 KtTypeReference (org.jetbrains.kotlin.psi.KtTypeReference)1 KotlinType (org.jetbrains.kotlin.types.KotlinType)1