diff --git a/compiler/frontend/src/org/jetbrains/jet/checkers/CheckerTestUtil.java b/compiler/frontend/src/org/jetbrains/jet/checkers/CheckerTestUtil.java index 1ea243434dd..c364fa0b31f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/checkers/CheckerTestUtil.java +++ b/compiler/frontend/src/org/jetbrains/jet/checkers/CheckerTestUtil.java @@ -23,6 +23,7 @@ import com.intellij.psi.PsiElement; import com.intellij.psi.PsiErrorElement; import com.intellij.psi.PsiFile; import com.intellij.psi.util.PsiTreeUtil; +import com.intellij.util.Function; import com.intellij.util.containers.Stack; import org.jetbrains.annotations.NotNull; import org.jetbrains.jet.lang.diagnostics.Diagnostic; @@ -32,7 +33,6 @@ import org.jetbrains.jet.lang.psi.JetExpression; import org.jetbrains.jet.lang.psi.JetReferenceExpression; import org.jetbrains.jet.lang.resolve.AnalyzingUtils; import org.jetbrains.jet.lang.resolve.BindingContext; -import org.jetbrains.jet.lang.types.JetType; import java.util.*; import java.util.regex.Matcher; @@ -255,9 +255,22 @@ public class CheckerTestUtil { return matcher.replaceAll(""); } - public static StringBuffer addDiagnosticMarkersToText(@NotNull final PsiFile psiFile, Collection diagnostics) { + public static StringBuffer addDiagnosticMarkersToText(@NotNull final PsiFile psiFile, @NotNull Collection diagnostics) { + return addDiagnosticMarkersToText(psiFile, diagnostics, new Function() { + @Override + public String fun(PsiFile file) { + return file.getText(); + } + }); + } + + public static StringBuffer addDiagnosticMarkersToText( + @NotNull final PsiFile psiFile, + @NotNull Collection diagnostics, + @NotNull Function getFileText + ) { + String text = getFileText.fun(psiFile); StringBuffer result = new StringBuffer(); - String text = psiFile.getText(); diagnostics = Collections2.filter(diagnostics, new Predicate() { @Override public boolean apply(Diagnostic diagnostic) { diff --git a/compiler/testData/diagnostics/tests/checkType.kt b/compiler/testData/diagnostics/tests/checkType.kt new file mode 100644 index 00000000000..e9dc7d6ce1e --- /dev/null +++ b/compiler/testData/diagnostics/tests/checkType.kt @@ -0,0 +1,11 @@ +// !CHECK_TYPE + +trait A +trait B : A +trait C : B + +fun test(b: B) { + b checkType { it : _ } + b checkType { it : _ } + b checkType { it : _ } +} diff --git a/compiler/testData/diagnostics/tests/functionLiterals/functionLIteralInBlockInIf.kt b/compiler/testData/diagnostics/tests/functionLiterals/functionLIteralInBlockInIf.kt index 4133a0318bb..c658d0c01e3 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/functionLIteralInBlockInIf.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/functionLIteralInBlockInIf.kt @@ -1,3 +1,4 @@ +// !CHECK_TYPE fun test() { val a = if (true) { val x = 1 @@ -5,7 +6,5 @@ fun test() { } else { { 2 } } - TypeOf(a): TypeOf> -} - -class TypeOf(t: T) \ No newline at end of file + a checkType { it : _<() -> Int> } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/functionLiterals/functionLiteralWithoutArgumentList.kt b/compiler/testData/diagnostics/tests/functionLiterals/functionLiteralWithoutArgumentList.kt index ed84584db2b..78190d17a22 100644 --- a/compiler/testData/diagnostics/tests/functionLiterals/functionLiteralWithoutArgumentList.kt +++ b/compiler/testData/diagnostics/tests/functionLiterals/functionLiteralWithoutArgumentList.kt @@ -1,8 +1,7 @@ -class TypeOf(t: T) - +// !CHECK_TYPE fun id(t: T) = t fun foo() { val i = id { 22 } //type inference error: no information for parameter - TypeOf(i): TypeOf<()->Int> + i checkType { it : _<()->Int> } } diff --git a/compiler/testData/diagnostics/tests/generics/varProjection/setterNotProjectedOutAssign.kt b/compiler/testData/diagnostics/tests/generics/varProjection/setterNotProjectedOutAssign.kt index e082d821eb4..4aa9cad51af 100644 --- a/compiler/testData/diagnostics/tests/generics/varProjection/setterNotProjectedOutAssign.kt +++ b/compiler/testData/diagnostics/tests/generics/varProjection/setterNotProjectedOutAssign.kt @@ -1,11 +1,9 @@ +// !CHECK_TYPE trait Tr { var v: Tr } fun test(t: Tr<*>) { t.v = t - val v = TypeOf(t.v) - v: TypeOf> -} - -class TypeOf(t: T) \ No newline at end of file + t.v checkType { it : _> } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutAssign.kt b/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutAssign.kt index 8fc8a74ae62..5396cc7c331 100644 --- a/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutAssign.kt +++ b/compiler/testData/diagnostics/tests/generics/varProjection/setterProjectedOutAssign.kt @@ -1,4 +1,5 @@ // !DIAGNOSTICS: -UNREACHABLE_CODE -UNUSED_PARAMETER +// !CHECK_TYPE // t is unused due to KT-4233 trait Tr { var v: T @@ -6,8 +7,5 @@ trait Tr { fun test(t: Tr<*>) { t.v = null!! - val v = TypeOf(t.v) - v: TypeOf -} - -class TypeOf(t: T) \ No newline at end of file + t.v checkType { it : _ } +} \ No newline at end of file diff --git a/compiler/testData/diagnostics/tests/inference/constraints/equalityConstraintOnNullableType.kt b/compiler/testData/diagnostics/tests/inference/constraints/equalityConstraintOnNullableType.kt index 4454c3f8fde..b458c7e80a9 100644 --- a/compiler/testData/diagnostics/tests/inference/constraints/equalityConstraintOnNullableType.kt +++ b/compiler/testData/diagnostics/tests/inference/constraints/equalityConstraintOnNullableType.kt @@ -1,6 +1,5 @@ // !DIAGNOSTICS: -BASE_WITH_NULLABLE_UPPER_BOUND -class TypeOf(t: T) - +// !CHECK_TYPE trait A fun foo(a: A, aN: A): T = throw Exception("$a $aN") @@ -9,13 +8,13 @@ fun doA(a: A): T = throw Exception("$a") fun test(a: A, aN: A) { val aa = doA(aN) - TypeOf(aa): TypeOf + aa checkType { it : _ } val nullable = foo(aN, aN) //T = Int?, T? = Int? => T = Int? - TypeOf(nullable): TypeOf + nullable checkType { it : _ } val notNullable = foo(a, aN) //T = Int, T? = Int? => T = Int - TypeOf(notNullable): TypeOf + notNullable checkType { it : _ } } diff --git a/compiler/testData/diagnostics/tests/inference/constraints/notNullConstraintOnNullableType.kt b/compiler/testData/diagnostics/tests/inference/constraints/notNullConstraintOnNullableType.kt index af4c22b6ca5..4691a4e3e27 100644 --- a/compiler/testData/diagnostics/tests/inference/constraints/notNullConstraintOnNullableType.kt +++ b/compiler/testData/diagnostics/tests/inference/constraints/notNullConstraintOnNullableType.kt @@ -1,5 +1,4 @@ -class TypeOf(t: T) - +// !CHECK_TYPE trait A trait In @@ -15,7 +14,7 @@ fun test(out: Out, i: In, inv: A) { // T? >: Int => T = Int doT(1) val r = doOut(out) - TypeOf(r): TypeOf + r checkType { it : _ } // T? <: Int => error doIn(i) diff --git a/compiler/testData/diagnostics/tests/inference/constraints/subtypeConstraintOnNullableType.kt b/compiler/testData/diagnostics/tests/inference/constraints/subtypeConstraintOnNullableType.kt index de1d49a45eb..1f12fd4ba8a 100644 --- a/compiler/testData/diagnostics/tests/inference/constraints/subtypeConstraintOnNullableType.kt +++ b/compiler/testData/diagnostics/tests/inference/constraints/subtypeConstraintOnNullableType.kt @@ -1,6 +1,5 @@ // !DIAGNOSTICS: -BASE_WITH_NULLABLE_UPPER_BOUND -class TypeOf(t: T) - +// !CHECK_TYPE trait A trait Out @@ -12,13 +11,13 @@ fun doOut(o: Out): T = throw Exception("$o") fun test(a: A, aN: A, o: Out) { val out = doOut(o) //T? >: Int? => T >: Int - TypeOf(out): TypeOf + out checkType { it : _ } val nullable = foo(aN, o) //T = Int?, T? >: Int? => T = Int? - TypeOf(nullable): TypeOf + nullable checkType { it : _ } val notNullable = foo(a, o) //T = Int, T? >: Int? => T = Int - TypeOf(notNullable): TypeOf + notNullable checkType { it : _ } } diff --git a/compiler/testData/diagnostics/tests/inference/constraints/supertypeConstraintOnNullableType.kt b/compiler/testData/diagnostics/tests/inference/constraints/supertypeConstraintOnNullableType.kt index 9aacaca6268..982f67e8f6d 100644 --- a/compiler/testData/diagnostics/tests/inference/constraints/supertypeConstraintOnNullableType.kt +++ b/compiler/testData/diagnostics/tests/inference/constraints/supertypeConstraintOnNullableType.kt @@ -1,6 +1,5 @@ // !DIAGNOSTICS: -BASE_WITH_NULLABLE_UPPER_BOUND -class TypeOf(t: T) - +// !CHECK_TYPE trait A trait In @@ -12,13 +11,13 @@ fun doIn(i: In): T = throw Exception("$i") fun test(a: A, aN: A, i: In) { val _in = doIn(i) //T? <: Int? => T <: Int? - TypeOf(_in): TypeOf + _in checkType { it : _ } val notNullable = foo(a, i) //T = Int, T? <: Int? => T = Int - TypeOf(notNullable): TypeOf + notNullable checkType { it : _ } val nullable = foo(aN, i) //T = Int?, T? <: Int? => T = Int? - TypeOf(nullable): TypeOf + nullable checkType { it : _ } } diff --git a/compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt b/compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt index 5f5f9585445..be29bca7ba2 100644 --- a/compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt +++ b/compiler/testData/diagnostics/tests/numbers/numbersInSimpleConstraints.kt @@ -1,7 +1,6 @@ +// !CHECK_TYPE package a -class TypeOf(t: T) - fun id(t: T): T = t fun either(t1: T, t2: T): T = t1 @@ -21,7 +20,7 @@ fun test() { d: Int val e = id(9223372036854775807) - TypeOf(e): TypeOf + e checkType { it : _ } val f: Byte = either(1, 2) @@ -32,7 +31,7 @@ fun test() { otherGeneric(1) val r = either(1, "") - TypeOf(r): TypeOf + r checkType { it : _> } use(a, b, c, d, e, f, g, r) } @@ -48,7 +47,7 @@ fun testExactBound(invS: Inv, invI: Inv, invB: Inv) { exactBound(1, invI) val b = exactBound(1, invB) - TypeOf(b): TypeOf + b checkType { it : _ } } trait Cov @@ -57,10 +56,10 @@ fun lowerBound(t: T, l : Cov): T = throw Exception("$t $l") fun testLowerBound(cov: Cov, covN: Cov) { val r = lowerBound(1, cov) - TypeOf(r): TypeOf + r checkType { it : _> } val n = lowerBound(1, covN) - TypeOf(n): TypeOf + n checkType { it : _ } } trait Contr @@ -71,8 +70,8 @@ fun testUpperBound(contrS: Contr, contrB: Contr, contrN: ContrupperBound(1, contrS) val n = upperBound(1, contrN) - TypeOf(n): TypeOf + n checkType { it : _ } val b = upperBound(1, contrB) - TypeOf(b): TypeOf + b checkType { it : _ } } diff --git a/compiler/tests/org/jetbrains/jet/JetTestUtils.java b/compiler/tests/org/jetbrains/jet/JetTestUtils.java index 130a33ccc6c..f3e0c9e318e 100644 --- a/compiler/tests/org/jetbrains/jet/JetTestUtils.java +++ b/compiler/tests/org/jetbrains/jet/JetTestUtils.java @@ -87,10 +87,10 @@ import static org.jetbrains.jet.cli.jvm.JVMConfigurationKeys.CLASSPATH_KEY; public class JetTestUtils { private static final Pattern KT_FILES = Pattern.compile(".*?.kt"); - private static List filesToDelete = new ArrayList(); + private static final List filesToDelete = new ArrayList(); public static final Pattern FILE_PATTERN = Pattern.compile("//\\s*FILE:\\s*(.*)$", Pattern.MULTILINE); - public static final Pattern DIRECTIVE_PATTERN = Pattern.compile("^//\\s*!(\\w+):\\s*(.*)$", Pattern.MULTILINE); + public static final Pattern DIRECTIVE_PATTERN = Pattern.compile("^//\\s*!(\\w+)(:\\s*(.*)$)?", Pattern.MULTILINE); public static final BindingTrace DUMMY_TRACE = new BindingTrace() { @@ -453,7 +453,7 @@ public class JetTestUtils { Assert.fail("Directives should only occur at the beginning of a file: " + directiveMatcher.group()); } String name = directiveMatcher.group(1); - String value = directiveMatcher.group(2); + String value = directiveMatcher.group(3); String oldValue = directives.put(name, value); Assert.assertNull("Directive overwritten: " + name + " old value: " + oldValue + " new value: " + value, oldValue); start = directiveMatcher.end() + 1; diff --git a/compiler/tests/org/jetbrains/jet/checkers/AbstractJetDiagnosticsTest.java b/compiler/tests/org/jetbrains/jet/checkers/AbstractJetDiagnosticsTest.java index c42c3ca6d64..6baa5a3a866 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/AbstractJetDiagnosticsTest.java +++ b/compiler/tests/org/jetbrains/jet/checkers/AbstractJetDiagnosticsTest.java @@ -24,7 +24,10 @@ import com.intellij.openapi.util.Condition; import com.intellij.openapi.util.Conditions; import com.intellij.openapi.util.TextRange; import com.intellij.openapi.util.io.FileUtil; +import com.intellij.openapi.util.text.StringUtil; +import com.intellij.psi.PsiFile; import com.intellij.psi.PsiFileFactory; +import com.intellij.util.Function; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; @@ -61,6 +64,9 @@ public abstract class AbstractJetDiagnosticsTest extends JetLiteFixture { CheckerTestUtil.DebugInfoDiagnosticFactory.MISSING_UNRESOLVED, CheckerTestUtil.DebugInfoDiagnosticFactory.UNRESOLVED_WITH_TARGET ); + public static final String CHECK_TYPE_DIRECTIVE = "CHECK_TYPE"; + private static final String CHECK_TYPE_DECLARATIONS = "\nclass _" + + "\nfun T.checkType(f: (_) -> Unit) = f"; @Override protected JetCoreEnvironment createEnvironment() { @@ -103,7 +109,7 @@ public abstract class AbstractJetDiagnosticsTest extends JetLiteFixture { if (fileName.endsWith(".java")) { writeJavaFile(fileName, text, javaFilesDir); } - return new TestFile(fileName, text, parseDiagnosticFilterDirective(directives)); + return new TestFile(fileName, text, directives); } }); @@ -125,8 +131,8 @@ public abstract class AbstractJetDiagnosticsTest extends JetLiteFixture { return jetFiles; } - public static Condition parseDiagnosticFilterDirective(Map diagnostics) { - String directives = diagnostics.get(DIAGNOSTICS_DIRECTIVE); + public static Condition parseDiagnosticFilterDirective(Map directiveMap) { + String directives = directiveMap.get(DIAGNOSTICS_DIRECTIVE); if (directives == null) { return Conditions.alwaysTrue(); } @@ -197,9 +203,15 @@ public abstract class AbstractJetDiagnosticsTest extends JetLiteFixture { private final String clearText; private final JetFile jetFile; private final Condition whatDiagnosticsToConsider; + private final boolean declareCheckType; - public TestFile(String fileName, String textWithMarkers, Condition whatDiagnosticsToConsider) { - this.whatDiagnosticsToConsider = whatDiagnosticsToConsider; + public TestFile( + String fileName, + String textWithMarkers, + Map directives + ) { + this.whatDiagnosticsToConsider = parseDiagnosticFilterDirective(directives); + this.declareCheckType = directives.containsKey(CHECK_TYPE_DIRECTIVE); if (fileName.endsWith(".java")) { PsiFileFactory.getInstance(getProject()).createFileFromText(fileName, JavaLanguage.INSTANCE, textWithMarkers); // TODO: check there's not syntax errors @@ -209,13 +221,15 @@ public abstract class AbstractJetDiagnosticsTest extends JetLiteFixture { else { expectedText = textWithMarkers; clearText = CheckerTestUtil.parseDiagnosedRanges(expectedText, diagnosedRanges); - this.jetFile = createCheckAndReturnPsiFile(null, fileName, clearText); + this.jetFile = createCheckAndReturnPsiFile( + null, fileName, declareCheckType ? clearText + CHECK_TYPE_DECLARATIONS : clearText); for (CheckerTestUtil.DiagnosedRange diagnosedRange : diagnosedRanges) { diagnosedRange.setFile(jetFile); } } } + @Nullable public JetFile getJetFile() { return jetFile; @@ -250,10 +264,14 @@ public abstract class AbstractJetDiagnosticsTest extends JetLiteFixture { } }); - actualText.append(CheckerTestUtil.addDiagnosticMarkersToText(jetFile, diagnostics)); + actualText.append(CheckerTestUtil.addDiagnosticMarkersToText(jetFile, diagnostics, new Function() { + @Override + public String fun(PsiFile file) { + String text = file.getText(); + return declareCheckType ? StringUtil.trimEnd(text, CHECK_TYPE_DECLARATIONS) : text; + } + })); return ok[0]; } - } - } diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index ed47a6cb699..66ce350fd41 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -119,6 +119,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage doTest("compiler/testData/diagnostics/tests/CharacterLiterals.kt"); } + @TestMetadata("checkType.kt") + public void testCheckType() throws Exception { + doTest("compiler/testData/diagnostics/tests/checkType.kt"); + } + @TestMetadata("CompareToWithErrorType.kt") public void testCompareToWithErrorType() throws Exception { doTest("compiler/testData/diagnostics/tests/CompareToWithErrorType.kt");