KT-2991 Don't generate UNNECESSARY_NOT_NULL_ASSERTION on variables of

generic type T:Any?

 #KT-2991 Fixed
This commit is contained in:
Andrey Breslav
2012-10-25 07:15:30 +04:00
parent edb8797fa6
commit 3e38870ecc
3 changed files with 24 additions and 10 deletions
@@ -28,7 +28,9 @@ import org.jetbrains.jet.lang.descriptors.*;
import org.jetbrains.jet.lang.diagnostics.Errors;
import org.jetbrains.jet.lang.psi.*;
import org.jetbrains.jet.lang.resolve.*;
import org.jetbrains.jet.lang.resolve.calls.*;
import org.jetbrains.jet.lang.resolve.calls.CallMaker;
import org.jetbrains.jet.lang.resolve.calls.OverloadResolutionResults;
import org.jetbrains.jet.lang.resolve.calls.OverloadResolutionResultsUtil;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowInfo;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowValue;
import org.jetbrains.jet.lang.resolve.calls.autocasts.DataFlowValueFactory;
@@ -918,15 +920,8 @@ public class BasicExpressionTypingVisitor extends ExpressionTypingVisitor {
private boolean isKnownToBeNotNull(JetExpression expression, ExpressionTypingContext context) {
JetType type = context.trace.get(EXPRESSION_TYPE, expression);
assert type != null : "This method is only supposed to be called when the type is not null";
if (!type.isNullable()) return true;
List<JetType> possibleTypes = context.dataFlowInfo
.getPossibleTypes(DataFlowValueFactory.INSTANCE.createDataFlowValue(expression, type, context.trace.getBindingContext()));
for (JetType possibleType : possibleTypes) {
if (!possibleType.isNullable()) {
return true;
}
}
return false;
DataFlowValue dataFlowValue = DataFlowValueFactory.INSTANCE.createDataFlowValue(expression, type, context.trace.getBindingContext());
return !context.dataFlowInfo.getNullability(dataFlowValue).canBeNull();
}
public void checkLValue(BindingTrace trace, JetExpression expression) {
@@ -0,0 +1,14 @@
fun <T> test(t: T): T {
if (t != null) {
return t<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>
}
return t!!
}
fun <T> T.testThis(): String {
if (this != null) {
return this<!UNNECESSARY_NOT_NULL_ASSERTION!>!!<!>.toString()
}
return this!!.toString()
}
@@ -2304,6 +2304,11 @@ public class JetDiagnosticsTestGenerated extends AbstractDiagnosticsTestWithEage
doTest("compiler/testData/diagnostics/tests/nullableTypes/baseWithNullableUpperBound.kt");
}
@TestMetadata("nullAssertOnTypeWithNullableUpperBound.kt")
public void testNullAssertOnTypeWithNullableUpperBound() throws Exception {
doTest("compiler/testData/diagnostics/tests/nullableTypes/nullAssertOnTypeWithNullableUpperBound.kt");
}
@TestMetadata("redundantNullable.kt")
public void testRedundantNullable() throws Exception {
doTest("compiler/testData/diagnostics/tests/nullableTypes/redundantNullable.kt");