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 org.jetbrains.annotations.NotNull;
021 import org.jetbrains.kotlin.builtins.KotlinBuiltIns;
022 import org.jetbrains.kotlin.descriptors.*;
023 import org.jetbrains.kotlin.diagnostics.Errors;
024 import org.jetbrains.kotlin.name.FqName;
025 import org.jetbrains.kotlin.psi.KtExpression;
026 import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext;
027 import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
028 import org.jetbrains.kotlin.types.KotlinType;
029 import org.jetbrains.kotlin.types.typeUtil.TypeUtilsKt;
030
031 import java.util.Map;
032
033 public class ReifiedTypeParameterSubstitutionChecker implements CallChecker {
034 @Override
035 public <F extends CallableDescriptor> void check(
036 @NotNull ResolvedCall<F> resolvedCall, @NotNull BasicCallResolutionContext context
037 ) {
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 if (argumentDeclarationDescriptor instanceof TypeParameterDescriptor &&
049 !((TypeParameterDescriptor) argumentDeclarationDescriptor).isReified()) {
050 context.trace.report(
051 Errors.TYPE_PARAMETER_AS_REIFIED.on(getElementToReport(context, parameter.getIndex()), parameter)
052 );
053 }
054 else if (TypeUtilsKt.cannotBeReified(argument)) {
055 context.trace.report(
056 Errors.REIFIED_TYPE_FORBIDDEN_SUBSTITUTION.on(getElementToReport(context, parameter.getIndex()), 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.trace.report(
061 // Errors.REIFIED_TYPE_UNSAFE_SUBSTITUTION.on(getElementToReport(context, parameter.getIndex()), argument));
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 private static boolean isTypeParameterOfKotlinArray(@NotNull TypeParameterDescriptor parameter) {
074 DeclarationDescriptor container = parameter.getContainingDeclaration();
075 return container instanceof ClassDescriptor && KotlinBuiltIns.isNonPrimitiveArray((ClassDescriptor) container);
076 }
077
078 @NotNull
079 private static PsiElement getElementToReport(@NotNull BasicCallResolutionContext context, int parameterIndex) {
080 if (context.call.getTypeArguments().size() > parameterIndex) {
081 return context.call.getTypeArguments().get(parameterIndex);
082 }
083 KtExpression callee = context.call.getCalleeExpression();
084 return callee != null ? callee : context.call.getCallElement();
085 }
086 }