added CHECK_TYPE directive to diagnostic tests
This commit is contained in:
@@ -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<Diagnostic> diagnostics) {
|
||||
public static StringBuffer addDiagnosticMarkersToText(@NotNull final PsiFile psiFile, @NotNull Collection<Diagnostic> diagnostics) {
|
||||
return addDiagnosticMarkersToText(psiFile, diagnostics, new Function<PsiFile, String>() {
|
||||
@Override
|
||||
public String fun(PsiFile file) {
|
||||
return file.getText();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public static StringBuffer addDiagnosticMarkersToText(
|
||||
@NotNull final PsiFile psiFile,
|
||||
@NotNull Collection<Diagnostic> diagnostics,
|
||||
@NotNull Function<PsiFile, String> getFileText
|
||||
) {
|
||||
String text = getFileText.fun(psiFile);
|
||||
StringBuffer result = new StringBuffer();
|
||||
String text = psiFile.getText();
|
||||
diagnostics = Collections2.filter(diagnostics, new Predicate<Diagnostic>() {
|
||||
@Override
|
||||
public boolean apply(Diagnostic diagnostic) {
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
// !CHECK_TYPE
|
||||
|
||||
trait A
|
||||
trait B : A
|
||||
trait C : B
|
||||
|
||||
fun test(b: B) {
|
||||
b checkType { it : _<B> }
|
||||
b checkType { <!TYPE_MISMATCH!>it<!> : _<A> }
|
||||
b checkType { <!TYPE_MISMATCH!>it<!> : _<C> }
|
||||
}
|
||||
@@ -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<Function0<Int>>
|
||||
}
|
||||
|
||||
class TypeOf<T>(t: T)
|
||||
a checkType { it : _<() -> Int> }
|
||||
}
|
||||
+2
-3
@@ -1,8 +1,7 @@
|
||||
class TypeOf<T>(t: T)
|
||||
|
||||
// !CHECK_TYPE
|
||||
fun <T> 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> }
|
||||
}
|
||||
|
||||
+3
-5
@@ -1,11 +1,9 @@
|
||||
// !CHECK_TYPE
|
||||
trait Tr<T> {
|
||||
var v: Tr<T>
|
||||
}
|
||||
|
||||
fun test(t: Tr<*>) {
|
||||
t.v = t
|
||||
val v = TypeOf(t.v)
|
||||
v: TypeOf<Tr<*>>
|
||||
}
|
||||
|
||||
class TypeOf<T>(t: T)
|
||||
t.v checkType { it : _<Tr<*>> }
|
||||
}
|
||||
+3
-5
@@ -1,4 +1,5 @@
|
||||
// !DIAGNOSTICS: -UNREACHABLE_CODE -UNUSED_PARAMETER
|
||||
// !CHECK_TYPE
|
||||
// t is unused due to KT-4233
|
||||
trait Tr<T> {
|
||||
var v: T
|
||||
@@ -6,8 +7,5 @@ trait Tr<T> {
|
||||
|
||||
fun test(t: Tr<*>) {
|
||||
<!SETTER_PROJECTED_OUT!>t.v<!> = null!!
|
||||
val v = TypeOf(t.v)
|
||||
v: TypeOf<Any?>
|
||||
}
|
||||
|
||||
class TypeOf<T>(t: T)
|
||||
t.v checkType { it : _<Any?> }
|
||||
}
|
||||
+4
-5
@@ -1,6 +1,5 @@
|
||||
// !DIAGNOSTICS: -BASE_WITH_NULLABLE_UPPER_BOUND
|
||||
class TypeOf<T>(t: T)
|
||||
|
||||
// !CHECK_TYPE
|
||||
trait A<T>
|
||||
|
||||
fun <T> foo(a: A<T>, aN: A<T?>): T = throw Exception("$a $aN")
|
||||
@@ -9,13 +8,13 @@ fun <T> doA(a: A<T>): T = throw Exception("$a")
|
||||
|
||||
fun test(a: A<Int>, aN: A<Int?>) {
|
||||
val aa = doA(aN)
|
||||
TypeOf(aa): TypeOf<Int?>
|
||||
aa checkType { it : _<Int?> }
|
||||
|
||||
val nullable = foo(aN, aN)
|
||||
//T = Int?, T? = Int? => T = Int?
|
||||
TypeOf(nullable): TypeOf<Int?>
|
||||
nullable checkType { it : _<Int?> }
|
||||
|
||||
val notNullable = foo(a, aN)
|
||||
//T = Int, T? = Int? => T = Int
|
||||
TypeOf(notNullable): TypeOf<Int>
|
||||
notNullable checkType { it : _<Int> }
|
||||
}
|
||||
|
||||
+2
-3
@@ -1,5 +1,4 @@
|
||||
class TypeOf<T>(t: T)
|
||||
|
||||
// !CHECK_TYPE
|
||||
trait A<T>
|
||||
|
||||
trait In<in T>
|
||||
@@ -15,7 +14,7 @@ fun test(out: Out<Int>, i: In<Int>, inv: A<Int>) {
|
||||
// T? >: Int => T = Int
|
||||
doT(1)
|
||||
val r = doOut(out)
|
||||
TypeOf(r): TypeOf<Int>
|
||||
r checkType { it : _<Int> }
|
||||
|
||||
// T? <: Int => error
|
||||
doIn(<!TYPE_MISMATCH!>i<!>)
|
||||
|
||||
+4
-5
@@ -1,6 +1,5 @@
|
||||
// !DIAGNOSTICS: -BASE_WITH_NULLABLE_UPPER_BOUND
|
||||
class TypeOf<T>(t: T)
|
||||
|
||||
// !CHECK_TYPE
|
||||
trait A<T>
|
||||
|
||||
trait Out<out T>
|
||||
@@ -12,13 +11,13 @@ fun <T> doOut(o: Out<T?>): T = throw Exception("$o")
|
||||
fun test(a: A<Int>, aN: A<Int?>, o: Out<Int?>) {
|
||||
val out = doOut(o)
|
||||
//T? >: Int? => T >: Int
|
||||
TypeOf(out): TypeOf<Int>
|
||||
out checkType { it : _<Int> }
|
||||
|
||||
val nullable = foo(aN, o)
|
||||
//T = Int?, T? >: Int? => T = Int?
|
||||
TypeOf(nullable): TypeOf<Int?>
|
||||
nullable checkType { it : _<Int?> }
|
||||
|
||||
val notNullable = foo(a, o)
|
||||
//T = Int, T? >: Int? => T = Int
|
||||
TypeOf(notNullable): TypeOf<Int>
|
||||
notNullable checkType { it : _<Int> }
|
||||
}
|
||||
|
||||
+4
-5
@@ -1,6 +1,5 @@
|
||||
// !DIAGNOSTICS: -BASE_WITH_NULLABLE_UPPER_BOUND
|
||||
class TypeOf<T>(t: T)
|
||||
|
||||
// !CHECK_TYPE
|
||||
trait A<T>
|
||||
|
||||
trait In<in T>
|
||||
@@ -12,13 +11,13 @@ fun <T> doIn(i: In<T?>): T = throw Exception("$i")
|
||||
fun test(a: A<Int>, aN: A<Int?>, i: In<Int?>) {
|
||||
val _in = doIn(i)
|
||||
//T? <: Int? => T <: Int?
|
||||
TypeOf(_in): TypeOf<Int?>
|
||||
_in checkType { it : _<Int?> }
|
||||
|
||||
val notNullable = foo(a, i)
|
||||
//T = Int, T? <: Int? => T = Int
|
||||
TypeOf(notNullable): TypeOf<Int>
|
||||
notNullable checkType { it : _<Int> }
|
||||
|
||||
val nullable = foo(aN, i)
|
||||
//T = Int?, T? <: Int? => T = Int?
|
||||
TypeOf(nullable): TypeOf<Int?>
|
||||
nullable checkType { it : _<Int?> }
|
||||
}
|
||||
|
||||
@@ -1,7 +1,6 @@
|
||||
// !CHECK_TYPE
|
||||
package a
|
||||
|
||||
class TypeOf<T>(t: T)
|
||||
|
||||
fun <T> id(t: T): T = t
|
||||
|
||||
fun <T> either(t1: T, <!UNUSED_PARAMETER!>t2<!>: T): T = t1
|
||||
@@ -21,7 +20,7 @@ fun test() {
|
||||
d: Int
|
||||
|
||||
val e = id(9223372036854775807)
|
||||
TypeOf(e): TypeOf<Long>
|
||||
e checkType { it : _<Long> }
|
||||
|
||||
val f: Byte = either(1, 2)
|
||||
|
||||
@@ -32,7 +31,7 @@ fun test() {
|
||||
<!TYPE_INFERENCE_TYPE_CONSTRUCTOR_MISMATCH!>otherGeneric<!>(<!CONSTANT_EXPECTED_TYPE_MISMATCH!>1<!>)
|
||||
|
||||
val r = either(1, "")
|
||||
TypeOf(r): TypeOf<Any>
|
||||
r checkType { it : _<Comparable<*>> }
|
||||
|
||||
use(a, b, c, d, e, f, g, r)
|
||||
}
|
||||
@@ -48,7 +47,7 @@ fun testExactBound(invS: Inv<String>, invI: Inv<Int>, invB: Inv<Byte>) {
|
||||
exactBound(1, invI)
|
||||
|
||||
val b = exactBound(1, invB)
|
||||
TypeOf(b): TypeOf<Byte>
|
||||
b checkType { it : _<Byte> }
|
||||
}
|
||||
|
||||
trait Cov<out T>
|
||||
@@ -57,10 +56,10 @@ fun <T> lowerBound(t: T, l : Cov<T>): T = throw Exception("$t $l")
|
||||
|
||||
fun testLowerBound(cov: Cov<String>, covN: Cov<Number>) {
|
||||
val r = lowerBound(1, cov)
|
||||
TypeOf(r): TypeOf<Any>
|
||||
r checkType { it : _<Comparable<*>> }
|
||||
|
||||
val n = lowerBound(1, covN)
|
||||
TypeOf(n): TypeOf<Number>
|
||||
n checkType { it : _<Number> }
|
||||
}
|
||||
|
||||
trait Contr<in T>
|
||||
@@ -71,8 +70,8 @@ fun testUpperBound(contrS: Contr<String>, contrB: Contr<Byte>, contrN: Contr<Num
|
||||
<!TYPE_INFERENCE_CONFLICTING_SUBSTITUTIONS!>upperBound<!>(1, contrS)
|
||||
|
||||
val n = upperBound(1, contrN)
|
||||
TypeOf(n): TypeOf<Int>
|
||||
n checkType { it : _<Int> }
|
||||
|
||||
val b = upperBound(1, contrB)
|
||||
TypeOf(b): TypeOf<Byte>
|
||||
b checkType { it : _<Byte> }
|
||||
}
|
||||
|
||||
@@ -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<File> filesToDelete = new ArrayList<File>();
|
||||
private static final List<File> filesToDelete = new ArrayList<File>();
|
||||
|
||||
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;
|
||||
|
||||
@@ -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 _<T>" +
|
||||
"\nfun <T> T.checkType(f: (_<T>) -> 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<Diagnostic> parseDiagnosticFilterDirective(Map<String, String> diagnostics) {
|
||||
String directives = diagnostics.get(DIAGNOSTICS_DIRECTIVE);
|
||||
public static Condition<Diagnostic> parseDiagnosticFilterDirective(Map<String, String> 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<Diagnostic> whatDiagnosticsToConsider;
|
||||
private final boolean declareCheckType;
|
||||
|
||||
public TestFile(String fileName, String textWithMarkers, Condition<Diagnostic> whatDiagnosticsToConsider) {
|
||||
this.whatDiagnosticsToConsider = whatDiagnosticsToConsider;
|
||||
public TestFile(
|
||||
String fileName,
|
||||
String textWithMarkers,
|
||||
Map<String, String> 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<PsiFile, String>() {
|
||||
@Override
|
||||
public String fun(PsiFile file) {
|
||||
String text = file.getText();
|
||||
return declareCheckType ? StringUtil.trimEnd(text, CHECK_TYPE_DECLARATIONS) : text;
|
||||
}
|
||||
}));
|
||||
return ok[0];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
|
||||
Reference in New Issue
Block a user