001 /*
002 * Copyright 2010-2015 JetBrains s.r.o.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 * http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017 package org.jetbrains.kotlin.resolve.calls.checkers;
018
019 import com.intellij.psi.PsiElement;
020 import kotlin.collections.CollectionsKt;
021 import org.jetbrains.annotations.NotNull;
022 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
023 import org.jetbrains.kotlin.descriptors.ClassDescriptor;
024 import org.jetbrains.kotlin.descriptors.ClassifierDescriptor;
025 import org.jetbrains.kotlin.descriptors.DeclarationDescriptor;
026 import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
027 import org.jetbrains.kotlin.diagnostics.Errors;
028 import org.jetbrains.kotlin.psi.KtTypeProjection;
029 import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
030 import org.jetbrains.kotlin.types.KotlinType;
031 import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
032
033 import java.util.Map;
034
035 public class ReifiedTypeParameterSubstitutionChecker implements CallChecker {
036 @Override
037 public void check(@NotNull ResolvedCall<?> resolvedCall, @NotNull PsiElement reportOn, @NotNull CallCheckerContext context) {
038 Map<TypeParameterDescriptor, KotlinType> typeArguments = resolvedCall.getTypeArguments();
039 for (Map.Entry<TypeParameterDescriptor, KotlinType> entry : typeArguments.entrySet()) {
040 TypeParameterDescriptor parameter = entry.getKey();
041 KotlinType argument = entry.getValue();
042 ClassifierDescriptor argumentDeclarationDescriptor = argument.getConstructor().getDeclarationDescriptor();
043
044 if (!parameter.isReified() && !isTypeParameterOfKotlinArray(parameter)) {
045 continue;
046 }
047
048 KtTypeProjection typeProjection = CollectionsKt.getOrNull(resolvedCall.getCall().getTypeArguments(), parameter.getIndex());
049 PsiElement reportErrorOn = typeProjection != null ? typeProjection : reportOn;
050
051 if (argumentDeclarationDescriptor instanceof TypeParameterDescriptor &&
052 !((TypeParameterDescriptor) argumentDeclarationDescriptor).isReified()) {
053 context.getTrace().report(Errors.TYPE_PARAMETER_AS_REIFIED.on(reportErrorOn, (TypeParameterDescriptor) argumentDeclarationDescriptor));
054 }
055 else if (TypeUtilsKt.cannotBeReified(argument)) {
056 context.getTrace().report(Errors.REIFIED_TYPE_FORBIDDEN_SUBSTITUTION.on(reportErrorOn, argument));
057 }
058 // REIFIED_TYPE_UNSAFE_SUBSTITUTION is temporary disabled because it seems too strict now (see KT-10847)
059 //else if (TypeUtilsKt.unsafeAsReifiedArgument(argument) && !hasPureReifiableAnnotation(parameter)) {
060 // context.getTrace().report(Errors.REIFIED_TYPE_UNSAFE_SUBSTITUTION.on(reportErrorOn, argument));
061 //}
062 }
063 }
064
065 /*
066 private static final FqName PURE_REIFIABLE_ANNOTATION_FQ_NAME = new FqName("kotlin.internal.PureReifiable");
067
068 private static boolean hasPureReifiableAnnotation(@NotNull TypeParameterDescriptor parameter) {
069 return parameter.getAnnotations().hasAnnotation(PURE_REIFIABLE_ANNOTATION_FQ_NAME) ||
070 isTypeParameterOfKotlinArray(parameter);
071 }
072 */
073
074 private static boolean isTypeParameterOfKotlinArray(@NotNull TypeParameterDescriptor parameter) {
075 DeclarationDescriptor container = parameter.getContainingDeclaration();
076 return container instanceof ClassDescriptor && KotlinBuiltIns.isNonPrimitiveArray((ClassDescriptor) container);
077 }
078 }