KT-10322, KT-10646, KT-10647:

- update diagnostic to (supposedly) more useful
- also report IMPLICIT_CAST_TO_ANY if expected type is DONT_CARE
 (effectively "no expected type" for lambda expression).
This commit is contained in:
Dmitry Petrov
2016-01-13 19:31:23 +03:00
parent f54de08073
commit 609ffc10a9
15 changed files with 118 additions and 40 deletions
@@ -53,6 +53,7 @@ import org.jetbrains.kotlin.diagnostics.Errors;
import org.jetbrains.kotlin.idea.MainFunctionDetector; import org.jetbrains.kotlin.idea.MainFunctionDetector;
import org.jetbrains.kotlin.lexer.KtTokens; import org.jetbrains.kotlin.lexer.KtTokens;
import org.jetbrains.kotlin.psi.*; import org.jetbrains.kotlin.psi.*;
import org.jetbrains.kotlin.psi.psiUtil.PsiUtilsKt;
import org.jetbrains.kotlin.resolve.*; import org.jetbrains.kotlin.resolve.*;
import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt; import org.jetbrains.kotlin.resolve.bindingContextUtil.BindingContextUtilsKt;
import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt; import org.jetbrains.kotlin.resolve.calls.callUtil.CallUtilKt;
@@ -69,6 +70,7 @@ import static org.jetbrains.kotlin.cfg.TailRecursionKind.*;
import static org.jetbrains.kotlin.diagnostics.Errors.*; import static org.jetbrains.kotlin.diagnostics.Errors.*;
import static org.jetbrains.kotlin.diagnostics.Errors.UNREACHABLE_CODE; import static org.jetbrains.kotlin.diagnostics.Errors.UNREACHABLE_CODE;
import static org.jetbrains.kotlin.resolve.BindingContext.*; import static org.jetbrains.kotlin.resolve.BindingContext.*;
import static org.jetbrains.kotlin.types.TypeUtils.DONT_CARE;
import static org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE; import static org.jetbrains.kotlin.types.TypeUtils.NO_EXPECTED_TYPE;
import static org.jetbrains.kotlin.types.TypeUtils.noExpectedType; import static org.jetbrains.kotlin.types.TypeUtils.noExpectedType;
@@ -124,9 +126,9 @@ public class ControlFlowInformationProvider {
markUnusedExpressions(); markUnusedExpressions();
markIfWithoutElse(); checkIfExpressions();
markWhenWithoutElse(); checkWhenExpressions();
} }
public void checkFunction(@Nullable KotlinType expectedReturnType) { public void checkFunction(@Nullable KotlinType expectedReturnType) {
@@ -691,7 +693,7 @@ public class ControlFlowInformationProvider {
); );
} }
public void markIfWithoutElse() { public void checkIfExpressions() {
PseudocodeTraverserKt.traverse( PseudocodeTraverserKt.traverse(
pseudocode, TraversalOrder.FORWARD, new ControlFlowInformationProvider.FunctionVoid1<Instruction>() { pseudocode, TraversalOrder.FORWARD, new ControlFlowInformationProvider.FunctionVoid1<Instruction>() {
@Override @Override
@@ -725,7 +727,7 @@ public class ControlFlowInformationProvider {
@NotNull Collection<KtExpression> branchExpressions @NotNull Collection<KtExpression> branchExpressions
) { ) {
KotlinType expectedExpressionType = trace.get(EXPECTED_EXPRESSION_TYPE, expression); KotlinType expectedExpressionType = trace.get(EXPECTED_EXPRESSION_TYPE, expression);
if (expectedExpressionType != null) return; if (expectedExpressionType != null && expectedExpressionType != DONT_CARE) return;
KotlinType expressionType = trace.getType(expression); KotlinType expressionType = trace.getType(expression);
if (expressionType == null) { if (expressionType == null) {
@@ -733,19 +735,33 @@ public class ControlFlowInformationProvider {
} }
if (KotlinBuiltIns.isAnyOrNullableAny(expressionType)) { if (KotlinBuiltIns.isAnyOrNullableAny(expressionType)) {
for (KtExpression branchExpression : branchExpressions) { for (KtExpression branchExpression : branchExpressions) {
if (branchExpression == null) continue;
KotlinType branchType = trace.getType(branchExpression); KotlinType branchType = trace.getType(branchExpression);
if (branchType == null || KotlinBuiltIns.isAnyOrNullableAny(branchType)) { if (branchType == null || KotlinBuiltIns.isAnyOrNullableAny(branchType)) {
return; return;
} }
} }
trace.report(IMPLICIT_CAST_TO_UNIT_OR_ANY.on(expression, expressionType)); for (KtExpression branchExpression : branchExpressions) {
} if (branchExpression == null) continue;
else if (KotlinBuiltIns.isUnit(expressionType)) { KotlinType branchType = trace.getType(branchExpression);
trace.report(IMPLICIT_CAST_TO_UNIT_OR_ANY.on(expression, expressionType)); assert branchType != null : "Branch expression type should be non-null";
trace.report(IMPLICIT_CAST_TO_ANY.on(getResultingExpression(branchExpression), branchType, expressionType));
}
} }
} }
public void markWhenWithoutElse() { private static @NotNull KtExpression getResultingExpression(@NotNull KtExpression expression) {
KtExpression finger = expression;
while (true) {
KtExpression deparenthesized = KtPsiUtil.deparenthesize(finger);
deparenthesized = KtPsiUtil.getExpressionOrLastStatementInBlock(deparenthesized);
if (deparenthesized == null || deparenthesized == finger) break;
finger = deparenthesized;
}
return finger;
}
public void checkWhenExpressions() {
final Map<Instruction, Edges<InitControlFlowInfo>> initializers = pseudocodeVariablesData.getVariableInitializers(); final Map<Instruction, Edges<InitControlFlowInfo>> initializers = pseudocodeVariablesData.getVariableInitializers();
PseudocodeTraverserKt.traverse( PseudocodeTraverserKt.traverse(
pseudocode, TraversalOrder.FORWARD, new ControlFlowInformationProvider.FunctionVoid1<Instruction>() { pseudocode, TraversalOrder.FORWARD, new ControlFlowInformationProvider.FunctionVoid1<Instruction>() {
@@ -776,13 +792,10 @@ public class ControlFlowInformationProvider {
KtWhenExpression whenExpression = (KtWhenExpression) element; KtWhenExpression whenExpression = (KtWhenExpression) element;
if (BindingContextUtilsKt.isUsedAsExpression(whenExpression, trace.getBindingContext())) { if (BindingContextUtilsKt.isUsedAsExpression(whenExpression, trace.getBindingContext())) {
List<KtExpression> branchExpressions = new ArrayList<KtExpression>(whenExpression.getEntries().size() + 1); List<KtExpression> branchExpressions = new ArrayList<KtExpression>(whenExpression.getEntries().size());
for (KtWhenEntry whenEntry : whenExpression.getEntries()) { for (KtWhenEntry whenEntry : whenExpression.getEntries()) {
branchExpressions.add(whenEntry.getExpression()); branchExpressions.add(whenEntry.getExpression());
} }
if (whenExpression.getElseExpression() != null) {
branchExpressions.add(whenExpression.getElseExpression());
}
checkImplicitCastOnConditionalExpression(whenExpression, branchExpressions); checkImplicitCastOnConditionalExpression(whenExpression, branchExpressions);
} }
@@ -806,8 +819,6 @@ public class ControlFlowInformationProvider {
} }
} }
} }
} }
} }
); );
@@ -681,7 +681,7 @@ public interface Errors {
DiagnosticFactory0<KtTypeReference> IS_ENUM_ENTRY = DiagnosticFactory0.create(ERROR); DiagnosticFactory0<KtTypeReference> IS_ENUM_ENTRY = DiagnosticFactory0.create(ERROR);
DiagnosticFactory0<KtTypeReference> ENUM_ENTRY_AS_TYPE = DiagnosticFactory0.create(ERROR); DiagnosticFactory0<KtTypeReference> ENUM_ENTRY_AS_TYPE = DiagnosticFactory0.create(ERROR);
DiagnosticFactory1<KtExpression, KotlinType> IMPLICIT_CAST_TO_UNIT_OR_ANY = DiagnosticFactory1.create(WARNING); DiagnosticFactory2<KtExpression, KotlinType, KotlinType> IMPLICIT_CAST_TO_ANY = DiagnosticFactory2.create(WARNING);
DiagnosticFactory3<KtExpression, KotlinType, String, String> SMARTCAST_IMPOSSIBLE = DiagnosticFactory3.create(ERROR); DiagnosticFactory3<KtExpression, KotlinType, String, String> SMARTCAST_IMPOSSIBLE = DiagnosticFactory3.create(ERROR);
DiagnosticFactory0<KtExpression> ALWAYS_NULL = DiagnosticFactory0.create(WARNING); DiagnosticFactory0<KtExpression> ALWAYS_NULL = DiagnosticFactory0.create(WARNING);
@@ -379,8 +379,8 @@ public class DefaultErrorMessages {
MAP.put(EXPECTED_PARAMETER_TYPE_MISMATCH, "Expected parameter of type {0}", RENDER_TYPE); MAP.put(EXPECTED_PARAMETER_TYPE_MISMATCH, "Expected parameter of type {0}", RENDER_TYPE);
MAP.put(EXPECTED_PARAMETERS_NUMBER_MISMATCH, "Expected {0,choice,0#no parameters|1#one parameter of type|1<{0,number,integer} parameters of types} {1}", null, RENDER_COLLECTION_OF_TYPES); MAP.put(EXPECTED_PARAMETERS_NUMBER_MISMATCH, "Expected {0,choice,0#no parameters|1#one parameter of type|1<{0,number,integer} parameters of types} {1}", null, RENDER_COLLECTION_OF_TYPES);
MAP.put(IMPLICIT_CAST_TO_UNIT_OR_ANY, "Type is cast to ''{0}''. Please specify ''{0}'' as expected type, if you mean such cast", MAP.put(IMPLICIT_CAST_TO_ANY, "Conditional branch result of type {0} is implicitly cast to {1}",
RENDER_TYPE); RENDER_TYPE, RENDER_TYPE);
MAP.put(EXPRESSION_EXPECTED, "{0} is not an expression, and only expressions are allowed here", new Renderer<KtExpression>() { MAP.put(EXPRESSION_EXPECTED, "{0} is not an expression, and only expressions are allowed here", new Renderer<KtExpression>() {
@NotNull @NotNull
@Override @Override
@@ -1,6 +1,6 @@
val test1 = { <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) 1 else ""<!> } val test1 = { if (true) <!IMPLICIT_CAST_TO_ANY!>1<!> else <!IMPLICIT_CAST_TO_ANY!>""<!> }
val test2 = { { <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) 1 else ""<!> } } val test2 = { { if (true) <!IMPLICIT_CAST_TO_ANY!>1<!> else <!IMPLICIT_CAST_TO_ANY!>""<!> } }
val test3: (Boolean) -> Any = { if (it) 1 else "" } val test3: (Boolean) -> Any = { if (it) 1 else "" }
@@ -46,7 +46,7 @@ fun f3() = <!INVALID_IF_AS_EXPRESSION!>if (true) 42<!>
fun f4(): Unit = <!INVALID_IF_AS_EXPRESSION!>if (true) work()<!> fun f4(): Unit = <!INVALID_IF_AS_EXPRESSION!>if (true) work()<!>
fun f5(): Unit = <!INVALID_IF_AS_EXPRESSION!>if (true) mlist.add()<!> fun f5(): Unit = <!INVALID_IF_AS_EXPRESSION!>if (true) mlist.add()<!>
fun f6(): Unit = <!INVALID_IF_AS_EXPRESSION!>if (true) 42<!> fun f6(): Unit = <!INVALID_IF_AS_EXPRESSION!>if (true) 42<!>
fun g1() = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!><!NO_ELSE_IN_WHEN!>when<!> { true -> work() }<!> fun g1() = <!NO_ELSE_IN_WHEN!>when<!> { true -> work() }
fun g2() = <!NO_ELSE_IN_WHEN!>when<!> { true -> mlist.add() } fun g2() = <!NO_ELSE_IN_WHEN!>when<!> { true -> mlist.add() }
fun g3() = <!NO_ELSE_IN_WHEN!>when<!> { true -> 42 } fun g3() = <!NO_ELSE_IN_WHEN!>when<!> { true -> 42 }
fun g4(): Unit = <!NO_ELSE_IN_WHEN!>when<!> { true -> work() } fun g4(): Unit = <!NO_ELSE_IN_WHEN!>when<!> { true -> work() }
@@ -5,8 +5,8 @@ fun example() {
val b = <!INVALID_IF_AS_EXPRESSION!>if (true) else false<!> val b = <!INVALID_IF_AS_EXPRESSION!>if (true) else false<!>
val c = <!INVALID_IF_AS_EXPRESSION!>if (true) true<!> val c = <!INVALID_IF_AS_EXPRESSION!>if (true) true<!>
val d = <!INVALID_IF_AS_EXPRESSION!>if (true) true else<!>; val d = <!INVALID_IF_AS_EXPRESSION!>if (true) true else<!>;
val e = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) {} else false<!> val e = if (true) <!IMPLICIT_CAST_TO_ANY!>{}<!> else <!IMPLICIT_CAST_TO_ANY!>false<!>
val f = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) true else {}<!> val f = if (true) <!IMPLICIT_CAST_TO_ANY!>true<!> else <!IMPLICIT_CAST_TO_ANY!>{}<!>
{ {
if (true) <!UNUSED_EXPRESSION!>true<!> if (true) <!UNUSED_EXPRESSION!>true<!>
@@ -17,12 +17,12 @@ fun example() {
}(); }();
{ {
<!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) {} else false<!> if (true) <!IMPLICIT_CAST_TO_ANY!>{}<!> else <!IMPLICIT_CAST_TO_ANY!>false<!>
}(); }();
{ {
<!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) true else {}<!> if (true) <!IMPLICIT_CAST_TO_ANY!>true<!> else <!IMPLICIT_CAST_TO_ANY!>{}<!>
}() }()
fun t(): Boolean { fun t(): Boolean {
@@ -0,0 +1,12 @@
fun <T> run(block: () -> T) : T = block()
fun test1() {
run {
if (true) {
<!INVALID_IF_AS_EXPRESSION, IMPLICIT_CAST_TO_ANY!>if (true) {}<!>
}
else {
<!IMPLICIT_CAST_TO_ANY!>1<!>
}
}
}
@@ -0,0 +1,4 @@
package
public fun </*0*/ T> run(/*0*/ block: () -> T): T
public fun test1(): kotlin.Unit
@@ -19,12 +19,12 @@ val w = <!EXPRESSION_EXPECTED!>while (true) {}<!>
fun foo() { fun foo() {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>z<!> = 2 var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>z<!> = 2
val r = { // type fun(): Any is inferred val r = { // type fun(): Any is inferred
<!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) { if (true) {
2 <!IMPLICIT_CAST_TO_ANY!>2<!>
} }
else { else {
z = 34 <!IMPLICIT_CAST_TO_ANY!>z = 34<!>
}<!> }
} }
val <!UNUSED_VARIABLE!>f<!>: ()-> Int = <!TYPE_MISMATCH!>r<!> val <!UNUSED_VARIABLE!>f<!>: ()-> Int = <!TYPE_MISMATCH!>r<!>
val <!UNUSED_VARIABLE!>g<!>: ()-> Any = r val <!UNUSED_VARIABLE!>g<!>: ()-> Any = r
@@ -73,11 +73,11 @@ fun testCoercionToUnit() {
var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!> = 43 var <!ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE!>x<!> = 43
val checkType = { val checkType = {
<!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (true) { if (true) {
x = 4 <!IMPLICIT_CAST_TO_ANY!>x = 4<!>
} else { } else {
45 <!IMPLICIT_CAST_TO_ANY!>45<!>
}<!> }
} }
val <!UNUSED_VARIABLE!>f<!> : () -> String = <!TYPE_MISMATCH!>checkType<!> val <!UNUSED_VARIABLE!>f<!> : () -> String = <!TYPE_MISMATCH!>checkType<!>
} }
@@ -93,18 +93,18 @@ fun testImplicitCoercion() {
else -> z = 20 else -> z = 20
} }
var <!UNUSED_VARIABLE!>u<!> = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>when(d) { var <!UNUSED_VARIABLE!>u<!> = when(d) {
3 -> { 3 -> {
<!UNUSED_VALUE!>z =<!> 34 <!IMPLICIT_CAST_TO_ANY!><!UNUSED_VALUE!>z =<!> 34<!>
} }
else -> <!UNUSED_CHANGED_VALUE!>z--<!> else -> <!UNUSED_CHANGED_VALUE, IMPLICIT_CAST_TO_ANY!>z--<!>
}<!> }
var <!UNUSED_VARIABLE!>iff<!> = <!INVALID_IF_AS_EXPRESSION!>if (true) { var <!UNUSED_VARIABLE!>iff<!> = <!INVALID_IF_AS_EXPRESSION!>if (true) {
<!UNUSED_VALUE!>z =<!> 34 <!UNUSED_VALUE!>z =<!> 34
}<!> }<!>
val <!UNUSED_VARIABLE!>g<!> = <!INVALID_IF_AS_EXPRESSION!>if (true) 4<!> val <!UNUSED_VARIABLE!>g<!> = <!INVALID_IF_AS_EXPRESSION!>if (true) 4<!>
val <!UNUSED_VARIABLE!>h<!> = <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>if (false) 4 else {}<!> val <!UNUSED_VARIABLE!>h<!> = if (false) <!IMPLICIT_CAST_TO_ANY!>4<!> else <!IMPLICIT_CAST_TO_ANY!>{}<!>
bar(if (true) { bar(if (true) {
<!CONSTANT_EXPECTED_TYPE_MISMATCH!>4<!> <!CONSTANT_EXPECTED_TYPE_MISMATCH!>4<!>
@@ -1,7 +1,16 @@
val test1 = { <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>when (true) { true -> 1; else -> "" }<!> } val test1 = { when (true) { true -> <!IMPLICIT_CAST_TO_ANY!>1<!>; else -> <!IMPLICIT_CAST_TO_ANY!>""<!> } }
val test2 = { { <!IMPLICIT_CAST_TO_UNIT_OR_ANY!>when (true) { true -> 1; else -> "" }<!> } } val test2 = { { when (true) { true -> <!IMPLICIT_CAST_TO_ANY!>1<!>; else -> <!IMPLICIT_CAST_TO_ANY!>""<!> } } }
val test3: (Boolean) -> Any = { when (true) { true -> 1; else -> "" } } val test3: (Boolean) -> Any = { when (true) { true -> 1; else -> "" } }
val test4: (Boolean) -> Any? = { when (true) { true -> 1; else -> "" } } val test4: (Boolean) -> Any? = { when (true) { true -> 1; else -> "" } }
fun println() {}
val test5 = {
when (true) {
true -> println()
else -> println()
}
}
@@ -4,3 +4,5 @@ public val test1: () -> kotlin.Any
public val test2: () -> () -> kotlin.Any public val test2: () -> () -> kotlin.Any
public val test3: (kotlin.Boolean) -> kotlin.Any public val test3: (kotlin.Boolean) -> kotlin.Any
public val test4: (kotlin.Boolean) -> kotlin.Any? public val test4: (kotlin.Boolean) -> kotlin.Any?
public val test5: () -> kotlin.Unit
public fun println(): kotlin.Unit
@@ -0,0 +1,22 @@
var longWords = 0
val smallWords = hashSetOf<String>()
fun test1(word: String) =
run {
if (word.length > 4) {
<!IMPLICIT_CAST_TO_ANY!>longWords++<!>
}
else {
<!IMPLICIT_CAST_TO_ANY!>smallWords.add(word)<!>
}
}
fun test2(word: String) =
run {
if (word.length > 4) {
<!INVALID_IF_AS_EXPRESSION, IMPLICIT_CAST_TO_ANY!>if (word.startsWith("a")) longWords++<!>
}
else {
<!IMPLICIT_CAST_TO_ANY!>smallWords.add(word)<!>
}
}
@@ -0,0 +1,6 @@
package
public var longWords: kotlin.Int
public val smallWords: java.util.HashSet<kotlin.String>
public fun test1(/*0*/ word: kotlin.String): kotlin.Any
public fun test2(/*0*/ word: kotlin.String): kotlin.Any
@@ -3342,6 +3342,12 @@ public class DiagnosticsTestGenerated extends AbstractDiagnosticsTest {
doTest(fileName); doTest(fileName);
} }
@TestMetadata("kt10322.kt")
public void testKt10322() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlStructures/kt10322.kt");
doTest(fileName);
}
@TestMetadata("kt1075.kt") @TestMetadata("kt1075.kt")
public void testKt1075() throws Exception { public void testKt1075() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlStructures/kt1075.kt"); String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/controlStructures/kt1075.kt");
@@ -71,6 +71,12 @@ public class DiagnosticsTestWithStdLibGenerated extends AbstractDiagnosticsTestW
doTest(fileName); doTest(fileName);
} }
@TestMetadata("implicitCastToAny.kt")
public void testImplicitCastToAny() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/implicitCastToAny.kt");
doTest(fileName);
}
@TestMetadata("instar.kt") @TestMetadata("instar.kt")
public void testInstar() throws Exception { public void testInstar() throws Exception {
String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/instar.kt"); String fileName = KotlinTestUtils.navigationMetadata("compiler/testData/diagnostics/testsWithStdLib/instar.kt");