New diagnostic for Java default method call via super in trait

This commit is contained in:
Michael Bogdanov
2015-04-24 12:22:40 +03:00
parent 0854c601b4
commit 39fabda611
13 changed files with 192 additions and 16 deletions
+2 -2
View File
@@ -7,9 +7,9 @@
</pattern>
</extension>
<module name="java8-tests" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="false" />
<option name="ALTERNATIVE_JRE_PATH_ENABLED" value="true" />
<option name="ALTERNATIVE_JRE_PATH" value="1.8" />
<option name="PACKAGE_NAME" value="org.jetbrains.kotlin.codegen" />
<option name="PACKAGE_NAME" value="org.jetbrains.kotlin" />
<option name="MAIN_CLASS_NAME" value="" />
<option name="METHOD_NAME" value="" />
<option name="TEST_OBJECT" value="package" />
@@ -59,6 +59,7 @@ import org.jetbrains.kotlin.resolve.BindingContextUtils;
import org.jetbrains.kotlin.resolve.DescriptorToSourceUtils;
import org.jetbrains.kotlin.resolve.DescriptorUtils;
import org.jetbrains.kotlin.resolve.annotations.AnnotationsPackage;
import org.jetbrains.kotlin.resolve.calls.CallResolverUtil;
import org.jetbrains.kotlin.resolve.calls.model.*;
import org.jetbrains.kotlin.resolve.calls.util.CallMaker;
import org.jetbrains.kotlin.resolve.calls.util.FakeCallableDescriptorForObject;
@@ -2257,7 +2258,7 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
@NotNull
public StackValue invokeFunction(@NotNull Call call, @NotNull ResolvedCall<?> resolvedCall, @NotNull StackValue receiver) {
FunctionDescriptor fd = accessibleFunctionDescriptor(resolvedCall);
JetSuperExpression superCallExpression = getSuperCallExpression(call);
JetSuperExpression superCallExpression = CallResolverUtil.getSuperCallExpression(call);
boolean superCall = superCallExpression != null;
if (superCall && !isInterface(fd.getContainingDeclaration())) {
@@ -2274,18 +2275,6 @@ public class ExpressionCodegen extends JetVisitor<StackValue, StackValue> implem
return callable.invokeMethodWithArguments(resolvedCall, receiver, this);
}
@Nullable
private static JetSuperExpression getSuperCallExpression(@NotNull Call call) {
ReceiverValue explicitReceiver = call.getExplicitReceiver();
if (explicitReceiver instanceof ExpressionReceiver) {
JetExpression receiverExpression = ((ExpressionReceiver) explicitReceiver).getExpression();
if (receiverExpression instanceof JetSuperExpression) {
return (JetSuperExpression) receiverExpression;
}
}
return null;
}
// Find the first parent of the current context which corresponds to a subclass of a given class
@NotNull
private static CodegenContext getParentContextSubclassOf(ClassDescriptor descriptor, CodegenContext context) {
@@ -37,6 +37,7 @@ import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValue
import org.jetbrains.kotlin.resolve.calls.smartcasts.DataFlowValueFactory
import org.jetbrains.kotlin.resolve.calls.smartcasts.Nullability
import org.jetbrains.kotlin.resolve.jvm.calls.checkers.NeedSyntheticChecker
import org.jetbrains.kotlin.resolve.jvm.calls.checkers.TraitDefaultMethodCallChecker
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm.NullabilityInformationSource
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver
@@ -52,7 +53,10 @@ public object KotlinJvmCheckerProvider : AdditionalCheckerProvider(
ReifiedTypeParameterAnnotationChecker(),
NativeFunChecker(),
OverloadsAnnotationChecker()),
additionalCallCheckers = listOf(NeedSyntheticChecker(), JavaAnnotationCallChecker(), JavaAnnotationMethodCallChecker()),
additionalCallCheckers = listOf(NeedSyntheticChecker(), JavaAnnotationCallChecker(),
JavaAnnotationMethodCallChecker(), TraitDefaultMethodCallChecker()),
additionalTypeCheckers = listOf(JavaNullabilityWarningsChecker())
)
@@ -0,0 +1,54 @@
/*
* 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.jvm.calls.checkers
import com.intellij.psi.util.PsiTreeUtil
import org.jetbrains.kotlin.descriptors.CallableDescriptor
import org.jetbrains.kotlin.descriptors.ClassifierDescriptor
import org.jetbrains.kotlin.load.java.descriptors.JavaClassDescriptor
import org.jetbrains.kotlin.psi.JetExpression
import org.jetbrains.kotlin.resolve.DescriptorUtils
import org.jetbrains.kotlin.resolve.calls.CallResolverUtil
import org.jetbrains.kotlin.resolve.calls.checkers.CallChecker
import org.jetbrains.kotlin.resolve.calls.context.BasicCallResolutionContext
import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall
import org.jetbrains.kotlin.resolve.jvm.diagnostics.ErrorsJvm
public class TraitDefaultMethodCallChecker : CallChecker {
override fun <F : CallableDescriptor> check(resolvedCall: ResolvedCall<F>, context: BasicCallResolutionContext) {
val jetSuperExpression = CallResolverUtil.getSuperCallExpression(resolvedCall.getCall())
if (jetSuperExpression == null) return
val targetDescriptor = resolvedCall.getResultingDescriptor().getOriginal()
val containerDescriptor = targetDescriptor.getContainingDeclaration()
if (containerDescriptor is JavaClassDescriptor && DescriptorUtils.isTrait(containerDescriptor)) {
//is java interface default method called from trait
val classifier = DescriptorUtils.getParentOfType(context.scope.getContainingDeclaration(), javaClass<ClassifierDescriptor>())
if (classifier != null && DescriptorUtils.isTrait(classifier)) {
context.trace.report(
ErrorsJvm.TRAIT_CANT_CALL_DEFAULT_METHOD_VIA_SUPER.on(
PsiTreeUtil.getParentOfType(resolvedCall.getCall().getCallElement(), javaClass<JetExpression>())
)
)
}
}
}
}
@@ -64,6 +64,8 @@ public class DefaultErrorMessagesJvm implements DefaultErrorMessages.Extension {
MAP.put(ErrorsJvm.NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS,
"Expected type does not accept nulls in {0}, but the value may be null in {1}", Renderers.TO_STRING, Renderers.TO_STRING);
MAP.put(ErrorsJvm.TRAIT_CANT_CALL_DEFAULT_METHOD_VIA_SUPER, "Traits can't call Java default methods via super");
}
@@ -55,6 +55,8 @@ public interface ErrorsJvm {
DiagnosticFactory0<JetExpression> JAVA_LANG_CLASS_ARGUMENT_IN_ANNOTATION = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<JetElement> DEPRECATED_ANNOTATION_METHOD_CALL = DiagnosticFactory0.create(WARNING);
DiagnosticFactory0<JetElement> TRAIT_CANT_CALL_DEFAULT_METHOD_VIA_SUPER = DiagnosticFactory0.create(ERROR);
// TODO: make this a warning
DiagnosticFactory1<JetExpression, JetExpression> NO_REFLECTION_IN_CLASS_PATH = DiagnosticFactory1.create(ERROR);
@@ -26,6 +26,7 @@ import org.jetbrains.kotlin.descriptors.TypeParameterDescriptor;
import org.jetbrains.kotlin.psi.Call;
import org.jetbrains.kotlin.psi.JetExpression;
import org.jetbrains.kotlin.psi.JetSimpleNameExpression;
import org.jetbrains.kotlin.psi.JetSuperExpression;
import org.jetbrains.kotlin.resolve.calls.inference.ConstraintSystem;
import org.jetbrains.kotlin.resolve.scopes.receivers.ExpressionReceiver;
import org.jetbrains.kotlin.resolve.scopes.receivers.ReceiverValue;
@@ -151,4 +152,15 @@ public class CallResolverUtil {
if (call.getCallType() != Call.CallType.INVOKE || isInvokeCallOnVariable(call)) return false;
return call.getExplicitReceiver().exists() && call.getDispatchReceiver().exists();
}
public static JetSuperExpression getSuperCallExpression(@NotNull Call call) {
ReceiverValue explicitReceiver = call.getExplicitReceiver();
if (explicitReceiver instanceof ExpressionReceiver) {
JetExpression receiverExpression = ((ExpressionReceiver) explicitReceiver).getExpression();
if (receiverExpression instanceof JetSuperExpression) {
return (JetSuperExpression) receiverExpression;
}
}
return null;
}
}
@@ -0,0 +1,24 @@
// FILE: Test.java
public interface Test {
default String test() {
return "123";
}
}
// FILE: test.kt
trait KTrait : Test {
fun ktest() {
<!TRAIT_CANT_CALL_DEFAULT_METHOD_VIA_SUPER!>super.test()<!>
test()
}
}
class A : KTrait {
fun a() {
super.test()
test()
}
}
@@ -0,0 +1,26 @@
package
internal final class A : KTrait {
public constructor A()
internal final fun a(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal open override /*1*/ /*fake_override*/ fun ktest(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun test(): kotlin.String!
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
internal trait KTrait : Test {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal open fun ktest(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun test(): kotlin.String!
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public trait Test {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
public open fun test(): kotlin.String!
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -0,0 +1,24 @@
// FILE: test.kt
public trait Test {
fun test(): String {
return "123";
}
}
trait KTrait : Test {
fun ktest() {
super.test()
test()
}
}
class A : KTrait {
fun b() {
super.test()
test()
}
}
@@ -0,0 +1,26 @@
package
internal final class A : KTrait {
public constructor A()
internal final fun b(): kotlin.Unit
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal open override /*1*/ /*fake_override*/ fun ktest(): kotlin.Unit
internal open override /*1*/ /*fake_override*/ fun test(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
internal trait KTrait : Test {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal open fun ktest(): kotlin.Unit
internal open override /*1*/ /*fake_override*/ fun test(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
public trait Test {
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
internal open fun test(): kotlin.String
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
}
@@ -7347,6 +7347,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
doTest(fileName);
}
@TestMetadata("traitDefaultCall.kt")
public void testTraitDefaultCall() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/j+k/traitDefaultCall.kt");
doTest(fileName);
}
@TestMetadata("UnboxingNulls.kt")
public void testUnboxingNulls() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/j+k/UnboxingNulls.kt");
@@ -12326,6 +12332,12 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/thisAndSuper/thisInToplevelFunction.kt");
doTest(fileName);
}
@TestMetadata("traitSuperCall.kt")
public void testTraitSuperCall() throws Exception {
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/thisAndSuper/traitSuperCall.kt");
doTest(fileName);
}
}
@TestMetadata("compiler/testData/diagnostics/tests/traitWithRequired")
@@ -313,6 +313,7 @@ fun main(args: Array<String>) {
}
}
testGroup("idea/tests", "idea/testData") {
testClass(javaClass<AbstractJavaTypeSubstitutorTest>()) {