Prohibit ''Nothing'' substitution of reified TP

This commit is contained in:
Denis Zharkov
2014-10-23 10:54:34 +04:00
committed by Andrey Breslav
parent b3691b7358
commit 28dc84344c
8 changed files with 353 additions and 9 deletions
@@ -382,6 +382,7 @@ public interface Errors {
DiagnosticFactory1<PsiElement, TypeParameterDescriptor> TYPE_PARAMETER_AS_REIFIED = DiagnosticFactory1.create(ERROR);
DiagnosticFactory0<PsiElement> REIFIED_TYPE_PARAMETER_NO_INLINE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<PsiElement, JetType> REIFIED_TYPE_NOTHING_SUBSTITUTION = DiagnosticFactory1.create(ERROR);
// Type inference
@@ -480,6 +480,7 @@ public class DefaultErrorMessages {
MAP.put(TYPE_PARAMETER_AS_REIFIED, "Cannot use ''{0}'' as reified type parameter. Use a class instead.", NAME);
MAP.put(REIFIED_TYPE_PARAMETER_NO_INLINE, "Only type parameters of inline functions can be reified");
MAP.put(REIFIED_TYPE_NOTHING_SUBSTITUTION, "Cannot use ''{0}'' as reified type parameter", RENDER_TYPE);
MAP.put(SUPERTYPES_FOR_ANNOTATION_CLASS, "Annotation class cannot have supertypes");
MAP.put(MISSING_VAL_ON_ANNOTATION_PARAMETER, "'val' keyword is missing on annotation parameter");
@@ -27,7 +27,7 @@ import java.util.*;
public class CallResolverExtensionProvider {
private final static CompositeExtension DEFAULT =
new CompositeExtension(Arrays.asList(new NeedSyntheticCallResolverExtension(), new TypeParameterAsReifiedCheck()));
new CompositeExtension(Arrays.asList(new NeedSyntheticCallResolverExtension(), new ReifiedTypeParameterSubstitutionCheck()));
private WeakReference<Map<DeclarationDescriptor, List<CallResolverExtension>>> extensionsCache;
@@ -25,12 +25,12 @@ import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.JetExpression;
import org.jetbrains.jet.lang.resolve.calls.context.BasicCallResolutionContext;
import org.jetbrains.jet.lang.resolve.calls.model.ResolvedCall;
import org.jetbrains.jet.lang.resolve.calls.results.OverloadResolutionResultsImpl;
import org.jetbrains.jet.lang.types.JetType;
import org.jetbrains.jet.lang.types.lang.KotlinBuiltIns;
import java.util.Map;
public class TypeParameterAsReifiedCheck implements CallResolverExtension {
public class ReifiedTypeParameterSubstitutionCheck implements CallResolverExtension {
@Override
public <F extends CallableDescriptor> void run(
@NotNull ResolvedCall<F> resolvedCall, @NotNull BasicCallResolutionContext context
@@ -40,13 +40,25 @@ public class TypeParameterAsReifiedCheck implements CallResolverExtension {
TypeParameterDescriptor parameter = entry.getKey();
JetType argument = entry.getValue();
ClassifierDescriptor argumentDeclarationDescription = argument.getConstructor().getDeclarationDescriptor();
if (parameter.isReified() && argumentDeclarationDescription instanceof TypeParameterDescriptor &&
if (parameter.isReified()) {
if (argumentDeclarationDescription instanceof TypeParameterDescriptor &&
!((TypeParameterDescriptor) argumentDeclarationDescription).isReified()
) {
JetExpression callee = context.call.getCalleeExpression();
PsiElement element = callee != null ? callee : context.call.getCallElement();
context.trace.report(Errors.TYPE_PARAMETER_AS_REIFIED.on(element, typeArguments.keySet().iterator().next()));
) {
context.trace.report(
Errors.TYPE_PARAMETER_AS_REIFIED.on(getCallElement(context), typeArguments.keySet().iterator().next())
);
}
else if (KotlinBuiltIns.getInstance().isNothingOrNullableNothing(argument)) {
context.trace.report(Errors.REIFIED_TYPE_NOTHING_SUBSTITUTION.on(getCallElement(context), argument));
}
}
}
}
@NotNull
private static PsiElement getCallElement(@NotNull BasicCallResolutionContext context) {
JetExpression callee = context.call.getCalleeExpression();
return callee != null ? callee : context.call.getCallElement();
}
}