diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java index b93bc1a7f79..30ee888f36c 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/Errors.java @@ -187,6 +187,8 @@ public interface Errors { DiagnosticFactory2 NAME_IN_CONSTRAINT_IS_NOT_A_TYPE_PARAMETER = DiagnosticFactory2.create(ERROR); + SimpleDiagnosticFactory VARIANCE_ON_FUNCTION_OR_PROPERTY_PARAMETER = SimpleDiagnosticFactory.create(ERROR, VARIANCE_MODIFIER); + // Members SimpleDiagnosticFactory PACKAGE_MEMBER_CANNOT_BE_PROTECTED = SimpleDiagnosticFactory.create(ERROR); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/PositioningStrategies.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/PositioningStrategies.java index 3c5b2e65ef9..34265afd56c 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/PositioningStrategies.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/PositioningStrategies.java @@ -27,6 +27,7 @@ import org.jetbrains.jet.lang.psi.*; import org.jetbrains.jet.lexer.JetKeywordToken; import org.jetbrains.jet.lexer.JetTokens; +import java.util.Arrays; import java.util.List; /** @@ -182,6 +183,27 @@ public class PositioningStrategies { }; } + public static final PositioningStrategy VARIANCE_MODIFIER = modifierSetPosition(JetTokens.IN_KEYWORD, JetTokens.OUT_KEYWORD); + + public static PositioningStrategy modifierSetPosition(final JetKeywordToken... tokens) { + return new PositioningStrategy() { + @NotNull + @Override + public List mark(@NotNull JetModifierListOwner modifierListOwner) { + JetModifierList modifierList = modifierListOwner.getModifierList(); + assert modifierList != null : "No modifier list, but modifier has been found by the analyzer"; + + for (JetKeywordToken token : tokens) { + ASTNode node = modifierList.getModifierNode(token); + if (node != null) { + return markNode(node); + } + } + throw new IllegalStateException("None of the modifiers is found: " + Arrays.asList(tokens)); + } + }; + } + public static final PositioningStrategy ARRAY_ACCESS = new PositioningStrategy() { @NotNull @Override diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java index 93515f9567a..3fd3bc7f465 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/diagnostics/rendering/DefaultErrorMessages.java @@ -319,6 +319,8 @@ public class DefaultErrorMessages { MAP.put(AUTOCAST_IMPOSSIBLE, "Automatic cast to ''{0}'' is impossible, because ''{1}'' could have changed since the is-check", RENDER_TYPE, NAME); + MAP.put(VARIANCE_ON_FUNCTION_OR_PROPERTY_PARAMETER, "Variance annotations are only allowed for type parameters of classes and traits"); + MAP.put(TYPE_MISMATCH_IN_FOR_LOOP, "The loop iterates over values of type {0} but the parameter is declared to be {1}", RENDER_TYPE, RENDER_TYPE); MAP.put(TYPE_MISMATCH_IN_CONDITION, "Condition must be of type jet.Boolean, but is of type {0}", RENDER_TYPE); diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java index 78aaac2b462..4ef1726e66d 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/resolve/DescriptorResolver.java @@ -522,6 +522,11 @@ public class DescriptorResolver { int index, BindingTrace trace ) { + if (typeParameter.getVariance() != Variance.INVARIANT) { + assert !(containingDescriptor instanceof ClassifierDescriptor) : "This method is intended for functions/properties"; + trace.report(VARIANCE_ON_FUNCTION_OR_PROPERTY_PARAMETER.on(typeParameter)); + } + // TODO: Annotations are not resolved! TypeParameterDescriptorImpl typeParameterDescriptor = TypeParameterDescriptorImpl.createForFurtherModification( containingDescriptor, diff --git a/compiler/testData/diagnostics/tests/declarationChecks/VarianceOnFunctionAndPropertyTypeParameters.kt b/compiler/testData/diagnostics/tests/declarationChecks/VarianceOnFunctionAndPropertyTypeParameters.kt new file mode 100644 index 00000000000..21f16fa375d --- /dev/null +++ b/compiler/testData/diagnostics/tests/declarationChecks/VarianceOnFunctionAndPropertyTypeParameters.kt @@ -0,0 +1,17 @@ +fun <in T> f() { + +} + +fun <out T> g() { + +} + +fun <out T, in X, Y> h() { + +} + +val <out T> T.x: Int + get() = 1 + +val <in T> T.y: Int + get() = 1 \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/listConstructor.kt b/compiler/testData/diagnostics/tests/inference/listConstructor.kt index 149171de1b7..99da206d000 100644 --- a/compiler/testData/diagnostics/tests/inference/listConstructor.kt +++ b/compiler/testData/diagnostics/tests/inference/listConstructor.kt @@ -22,7 +22,7 @@ fun test() { fun arrayList(vararg values: T) : ArrayList = values.toCollection(ArrayList(values.size)) -fun > Array.toCollection(result: C) : C { +fun > Array.toCollection(result: C) : C { for (element in this) result.add(element) return result } diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt1718.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt1718.kt index 71ae969b3b3..8f8a5211fc0 100644 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt1718.kt +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt1718.kt @@ -12,4 +12,4 @@ fun test() { //from library fun arrayList(vararg values: T) : ArrayList {} -fun Iterable.plus(elements: Iterable): List {} \ No newline at end of file +fun Iterable.plus(elements: Iterable): List {} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt2179.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt2179.kt index 06d638aa124..617d58a001d 100644 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt2179.kt +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt2179.kt @@ -35,7 +35,7 @@ fun > Collection.mapTo(result: C, transform return result } -fun > Array.toCollection(result: C) : C { +fun > Array.toCollection(result: C) : C { for (element in this) result.add(element) return result } diff --git a/compiler/testData/diagnostics/tests/inference/regressions/kt2324.kt b/compiler/testData/diagnostics/tests/inference/regressions/kt2324.kt index bc5ecb51bc4..bc2186f9fc0 100644 --- a/compiler/testData/diagnostics/tests/inference/regressions/kt2324.kt +++ b/compiler/testData/diagnostics/tests/inference/regressions/kt2324.kt @@ -27,7 +27,7 @@ fun assertEquals(expected: Any?, actu fun arrayList(vararg values: T) : ArrayList = values.toCollection(ArrayList(values.size)) -fun > Array.toCollection(result: C) : C { +fun > Array.toCollection(result: C) : C { for (element in this) result.add(element) return result } diff --git a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunInParam.kt b/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunInParam.kt deleted file mode 100644 index 1f0d5094f8f..00000000000 --- a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunInParam.kt +++ /dev/null @@ -1,3 +0,0 @@ -package test - -fun f() = 1 diff --git a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunInParam.txt b/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunInParam.txt deleted file mode 100644 index 2addca2eda0..00000000000 --- a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunInParam.txt +++ /dev/null @@ -1,3 +0,0 @@ -namespace test - -internal final fun f(): jet.Int diff --git a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunOutParam.kt b/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunOutParam.kt deleted file mode 100644 index c5f4b318c50..00000000000 --- a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunOutParam.kt +++ /dev/null @@ -1,3 +0,0 @@ -package test - -fun f() = 1 diff --git a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunOutParam.txt b/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunOutParam.txt deleted file mode 100644 index a3b6c2d83d7..00000000000 --- a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunOutParam.txt +++ /dev/null @@ -1,3 +0,0 @@ -namespace test - -internal final fun f(): jet.Int diff --git a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam2.kt b/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam2.kt deleted file mode 100644 index df8c64e1ec0..00000000000 --- a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam2.kt +++ /dev/null @@ -1,3 +0,0 @@ -package test - -fun funParamReferencesParam() = 1 diff --git a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam2.txt b/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam2.txt deleted file mode 100644 index 67fd5d2c5a1..00000000000 --- a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam2.txt +++ /dev/null @@ -1,3 +0,0 @@ -namespace test - -internal final fun funParamReferencesParam(): jet.Int diff --git a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams2.kt b/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams2.kt deleted file mode 100644 index ef12cb5917b..00000000000 --- a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams2.kt +++ /dev/null @@ -1,3 +0,0 @@ -package test - -fun funTwoTypeParams() = 1 diff --git a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams2.txt b/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams2.txt deleted file mode 100644 index 4729785d7c2..00000000000 --- a/compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams2.txt +++ /dev/null @@ -1,3 +0,0 @@ -namespace test - -internal final fun funTwoTypeParams(): jet.Int diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 8bb4ef4ef82..5455d23d521 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -1462,6 +1462,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/declarationChecks/RedeclarationsInMultiDecl.kt"); } + @TestMetadata("VarianceOnFunctionAndPropertyTypeParameters.kt") + public void testVarianceOnFunctionAndPropertyTypeParameters() throws Exception { + doTest("compiler/testData/diagnostics/tests/declarationChecks/VarianceOnFunctionAndPropertyTypeParameters.kt"); + } + @TestMetadata("compiler/testData/diagnostics/tests/declarationChecks/multiDeclarations") public static class MultiDeclarations extends AbstractDiagnosticsTestWithEagerResolve { public void testAllFilesPresentInMultiDeclarations() throws Exception { diff --git a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadCompiledKotlinTestGenerated.java b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadCompiledKotlinTestGenerated.java index 28260c2bb44..3bf78d93496 100644 --- a/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadCompiledKotlinTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/jvm/compiler/LoadCompiledKotlinTestGenerated.java @@ -347,16 +347,6 @@ public class LoadCompiledKotlinTestGenerated extends AbstractLoadCompiledKotlinT doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunGenericParam.kt"); } - @TestMetadata("FunInParam.kt") - public void testFunInParam() throws Exception { - doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunInParam.kt"); - } - - @TestMetadata("FunOutParam.kt") - public void testFunOutParam() throws Exception { - doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunOutParam.kt"); - } - @TestMetadata("FunParamParam.kt") public void testFunParamParam() throws Exception { doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamParam.kt"); @@ -372,11 +362,6 @@ public class LoadCompiledKotlinTestGenerated extends AbstractLoadCompiledKotlinT doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam.kt"); } - @TestMetadata("FunParamReferencesParam2.kt") - public void testFunParamReferencesParam2() throws Exception { - doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam2.kt"); - } - @TestMetadata("FunParamTwoUpperBounds.kt") public void testFunParamTwoUpperBounds() throws Exception { doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamTwoUpperBounds.kt"); @@ -412,11 +397,6 @@ public class LoadCompiledKotlinTestGenerated extends AbstractLoadCompiledKotlinT doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams.kt"); } - @TestMetadata("FunTwoTypeParams2.kt") - public void testFunTwoTypeParams2() throws Exception { - doTest("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams2.kt"); - } - } @TestMetadata("compiler/testData/loadKotlin/fun/genericWithoutTypeVariables") diff --git a/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java b/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java index 7e67673b659..b63f301a9ad 100644 --- a/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/lang/resolve/lazy/LazyResolveNamespaceComparingTestGenerated.java @@ -15,16 +15,13 @@ */ package org.jetbrains.jet.lang.resolve.lazy; -import junit.framework.Assert; import junit.framework.Test; import junit.framework.TestSuite; - -import java.io.File; import org.jetbrains.jet.JetTestUtils; import org.jetbrains.jet.test.InnerTestClasses; import org.jetbrains.jet.test.TestMetadata; -import org.jetbrains.jet.lang.resolve.lazy.AbstractLazyResolveNamespaceComparingTest; +import java.io.File; /** This class is generated by {@link org.jetbrains.jet.generators.tests.GenerateTests}. DO NOT MODIFY MANUALLY */ @InnerTestClasses({LazyResolveNamespaceComparingTestGenerated.LoadKotlin.class, LazyResolveNamespaceComparingTestGenerated.LoadJava.class, LazyResolveNamespaceComparingTestGenerated.NamespaceComparator.class}) @@ -349,16 +346,6 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunGenericParam.kt"); } - @TestMetadata("FunInParam.kt") - public void testFunInParam() throws Exception { - doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunInParam.kt"); - } - - @TestMetadata("FunOutParam.kt") - public void testFunOutParam() throws Exception { - doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunOutParam.kt"); - } - @TestMetadata("FunParamParam.kt") public void testFunParamParam() throws Exception { doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamParam.kt"); @@ -374,11 +361,6 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam.kt"); } - @TestMetadata("FunParamReferencesParam2.kt") - public void testFunParamReferencesParam2() throws Exception { - doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamReferencesParam2.kt"); - } - @TestMetadata("FunParamTwoUpperBounds.kt") public void testFunParamTwoUpperBounds() throws Exception { doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunParamTwoUpperBounds.kt"); @@ -414,11 +396,6 @@ public class LazyResolveNamespaceComparingTestGenerated extends AbstractLazyReso doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams.kt"); } - @TestMetadata("FunTwoTypeParams2.kt") - public void testFunTwoTypeParams2() throws Exception { - doTestSinglePackage("compiler/testData/loadKotlin/fun/genericWithTypeVariables/FunTwoTypeParams2.kt"); - } - } @TestMetadata("compiler/testData/loadKotlin/fun/genericWithoutTypeVariables")