From 3e38870ecc3fda80609c774d480235dae2ed8af2 Mon Sep 17 00:00:00 2001 From: Andrey Breslav Date: Thu, 25 Oct 2012 07:15:30 +0400 Subject: [PATCH] KT-2991 Don't generate UNNECESSARY_NOT_NULL_ASSERTION on variables of generic type T:Any? #KT-2991 Fixed --- .../expressions/BasicExpressionTypingVisitor.java | 15 +++++---------- .../nullAssertOnTypeWithNullableUpperBound.kt | 14 ++++++++++++++ .../jet/checkers/JetDiagnosticsTestGenerated.java | 5 +++++ 3 files changed, 24 insertions(+), 10 deletions(-) create mode 100644 compiler/testData/diagnostics/tests/nullableTypes/nullAssertOnTypeWithNullableUpperBound.kt diff --git a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java index 2ce106a0817..a1b4d401b2f 100644 --- a/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java +++ b/compiler/frontend/src/org/jetbrains/jet/lang/types/expressions/BasicExpressionTypingVisitor.java @@ -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 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) { diff --git a/compiler/testData/diagnostics/tests/nullableTypes/nullAssertOnTypeWithNullableUpperBound.kt b/compiler/testData/diagnostics/tests/nullableTypes/nullAssertOnTypeWithNullableUpperBound.kt new file mode 100644 index 00000000000..2b51253a6c2 --- /dev/null +++ b/compiler/testData/diagnostics/tests/nullableTypes/nullAssertOnTypeWithNullableUpperBound.kt @@ -0,0 +1,14 @@ +fun test(t: T): T { + if (t != null) { + return t!! + } + return t!! +} + +fun T.testThis(): String { + if (this != null) { + return this!!.toString() + } + return this!!.toString() +} + diff --git a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java index 957d503ea77..95779106db5 100644 --- a/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java +++ b/compiler/tests/org/jetbrains/jet/checkers/JetDiagnosticsTestGenerated.java @@ -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");