Properly check whether nullability of receiver argument fits to parameter's nullability
It's should behave like we check isSubtype(receiverType, parameterType) + use nullability info from smart cast #KT-1090 Fixed
This commit is contained in:
@@ -55,6 +55,7 @@ import org.jetbrains.kotlin.types.TypeSubstitutor
|
||||
import org.jetbrains.kotlin.types.TypeUtils
|
||||
import org.jetbrains.kotlin.types.TypeUtils.noExpectedType
|
||||
import org.jetbrains.kotlin.types.checker.JetTypeChecker
|
||||
import org.jetbrains.kotlin.types.checker.TypeCheckingProcedure
|
||||
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils
|
||||
import java.util.ArrayList
|
||||
|
||||
@@ -452,8 +453,13 @@ public class CandidateResolver(
|
||||
val receiverArgumentType = receiverArgument.getType()
|
||||
|
||||
val bindingContext = trace.getBindingContext()
|
||||
if (!safeAccess && !receiverParameter.getType().isMarkedNullable() && receiverArgumentType.isMarkedNullable()) {
|
||||
if (!smartCastManager.canBeSmartCast(receiverParameter, receiverArgument, this)) {
|
||||
// We just checked isSubtypeIgnoringNullability(receiverType, parameter)
|
||||
// Here we do almost the same thing as isSubtypeOf does (but pay attention only to nullability):
|
||||
// - find corresponding supertype, than check it's nullability is not weaker than parameter's one
|
||||
// - if latter failed, check whether value can be smart cast
|
||||
val commonReceiverType = TypeCheckingProcedure.findCorrespondingSupertype(receiverArgumentType, receiverParameter.type)
|
||||
if (!safeAccess && !receiverParameter.type.isMarkedNullable && (commonReceiverType?.isMarkedNullable ?: false)) {
|
||||
if (!smartCastManager.recordSmartCastToNotNullIfPossible(receiverArgument, this)) {
|
||||
tracing.unsafeCall(trace, receiverArgumentType, implicitInvokeCheck)
|
||||
return UNSAFE_CALL_ERROR
|
||||
}
|
||||
|
||||
+15
@@ -34,6 +34,7 @@ import org.jetbrains.kotlin.resolve.calls.model.ResolvedCall;
|
||||
import org.jetbrains.kotlin.resolve.scopes.receivers.*;
|
||||
import org.jetbrains.kotlin.types.JetType;
|
||||
import org.jetbrains.kotlin.types.TypeUtils;
|
||||
import org.jetbrains.kotlin.types.expressions.ExpressionTypingUtils;
|
||||
|
||||
import static org.jetbrains.kotlin.builtins.KotlinBuiltIns.isNullableNothing;
|
||||
import static org.jetbrains.kotlin.resolve.BindingContext.REFERENCE_TARGET;
|
||||
@@ -71,6 +72,20 @@ public class DataFlowValueFactory {
|
||||
if (isNullableNothing(type)) {
|
||||
return DataFlowValue.NULL; // 'null' is the only inhabitant of 'Nothing?'
|
||||
}
|
||||
|
||||
if (ExpressionTypingUtils.isExclExclExpression(JetPsiUtil.deparenthesize(expression))) {
|
||||
// In most cases type of `E!!`-expression is strictly not nullable and we could get proper Nullability
|
||||
// by calling `getImmanentNullability` (as it happens below).
|
||||
//
|
||||
// But there are some problem with types built on type parameters, e.g.
|
||||
// fun <T : Any?> foo(x: T) = x!!.hashCode() // there no way in type system to denote that `x!!` is not nullable
|
||||
return new DataFlowValue(expression,
|
||||
type,
|
||||
/* stableIdentifier = */false,
|
||||
/* uncapturedLocalVariable = */false,
|
||||
Nullability.NOT_NULL);
|
||||
}
|
||||
|
||||
IdentifierInfo result = getIdForStableIdentifier(expression, bindingContext, containingDeclarationOrModule);
|
||||
return new DataFlowValue(result == NO_IDENTIFIER_INFO ? expression : result.id,
|
||||
type,
|
||||
|
||||
+36
-8
@@ -220,16 +220,44 @@ public class SmartCastManager {
|
||||
return canBeCast;
|
||||
}
|
||||
|
||||
public boolean canBeSmartCast(
|
||||
@NotNull ReceiverParameterDescriptor receiverParameter,
|
||||
public boolean recordSmartCastToNotNullIfPossible(
|
||||
@NotNull ReceiverValue receiver,
|
||||
@NotNull ResolutionContext context) {
|
||||
if (!receiver.getType().isMarkedNullable()) return true;
|
||||
@NotNull ResolutionContext context
|
||||
) {
|
||||
if (!TypeUtils.isNullableType(receiver.getType())) return true;
|
||||
|
||||
List<JetType> smartCastVariants = getSmartCastVariants(receiver, context);
|
||||
for (JetType smartCastVariant : smartCastVariants) {
|
||||
if (JetTypeChecker.DEFAULT.isSubtypeOf(smartCastVariant, receiverParameter.getType())) return true;
|
||||
DataFlowValue dataFlowValue = getDataFlowValueExcludingReceiver(
|
||||
context.trace.getBindingContext(),
|
||||
context.scope.getContainingDeclaration(),
|
||||
receiver
|
||||
);
|
||||
|
||||
if (dataFlowValue == null) return false;
|
||||
|
||||
if (!context.dataFlowInfo.getNullability(dataFlowValue).canBeNull()) {
|
||||
JetExpression receiverExpression = getReceiverExpression(receiver);
|
||||
|
||||
// report smart cast only on predictable expressions that were not reported before
|
||||
if (receiverExpression != null
|
||||
&& dataFlowValue.isPredictable()
|
||||
&& context.trace.getBindingContext().get(SMARTCAST, receiverExpression) == null) {
|
||||
recordCastOrError(
|
||||
receiverExpression, receiver.getType(), context.trace,
|
||||
/* canBeCast = */ true, /* recordExpressionType = */ false
|
||||
);
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
|
||||
return !context.dataFlowInfo.getNullability(dataFlowValue).canBeNull();
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private static JetExpression getReceiverExpression(@NotNull ReceiverValue value) {
|
||||
if (value instanceof ExpressionReceiver) {
|
||||
return ((ExpressionReceiver) value).getExpression();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
+5
@@ -183,6 +183,11 @@ public class ExpressionTypingUtils {
|
||||
return expression.getOperationReference().getReferencedNameElementType() == JetTokens.EXCLEXCL;
|
||||
}
|
||||
|
||||
public static boolean isExclExclExpression(@Nullable JetExpression expression) {
|
||||
return expression instanceof JetUnaryExpression
|
||||
&& ((JetUnaryExpression) expression).getOperationReference().getReferencedNameElementType() == JetTokens.EXCLEXCL;
|
||||
}
|
||||
|
||||
@NotNull
|
||||
public static List<JetType> getValueParametersTypes(@NotNull List<ValueParameterDescriptor> valueParameters) {
|
||||
List<JetType> parameterTypes = new ArrayList<JetType>(valueParameters.size());
|
||||
|
||||
@@ -0,0 +1,45 @@
|
||||
// !DIAGNOSTICS: -UNUSED_EXPRESSION,-UNUSED_VARIABLE
|
||||
|
||||
fun <T : CharSequence?> T.bar1() {}
|
||||
fun CharSequence?.bar2() {}
|
||||
|
||||
fun <T : CharSequence> T.bar3() {}
|
||||
fun CharSequence.bar4() {}
|
||||
|
||||
fun <T : CharSequence?> foo(x: T) {
|
||||
|
||||
if (x != null) {
|
||||
if (<!SENSELESS_COMPARISON!>x != null<!>) {}
|
||||
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!>.length()
|
||||
x<!UNNECESSARY_SAFE_CALL!>?.<!>length()
|
||||
|
||||
x.bar1()
|
||||
x.bar2()
|
||||
x.<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar3<!>()
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!>.bar4()
|
||||
|
||||
|
||||
x<!UNNECESSARY_SAFE_CALL!>?.<!>bar1()
|
||||
}
|
||||
|
||||
x<!UNSAFE_CALL!>.<!>length()
|
||||
|
||||
if (x is String) {
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!>.length()
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!><!UNNECESSARY_SAFE_CALL!>?.<!>length()
|
||||
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!>.bar1()
|
||||
x.bar2()
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!>.bar3()
|
||||
}
|
||||
|
||||
if (x is CharSequence) {
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!>.length()
|
||||
x<!UNNECESSARY_SAFE_CALL!>?.<!>length()
|
||||
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!>.bar1()
|
||||
x.bar2()
|
||||
<!DEBUG_INFO_SMARTCAST!>x<!>.bar3()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package
|
||||
|
||||
internal fun </*0*/ T : kotlin.CharSequence?> foo(/*0*/ x: T): kotlin.Unit
|
||||
internal fun </*0*/ T : kotlin.CharSequence?> T.bar1(): kotlin.Unit
|
||||
internal fun kotlin.CharSequence?.bar2(): kotlin.Unit
|
||||
internal fun </*0*/ T : kotlin.CharSequence> T.bar3(): kotlin.Unit
|
||||
internal fun kotlin.CharSequence.bar4(): kotlin.Unit
|
||||
@@ -0,0 +1,35 @@
|
||||
// !DIAGNOSTICS: -UNUSED_EXPRESSION,-UNUSED_VARIABLE
|
||||
|
||||
fun <T : CharSequence?> T.bar1() {}
|
||||
fun CharSequence?.bar2() {}
|
||||
|
||||
fun <T : CharSequence> T.bar3() {}
|
||||
fun CharSequence.bar4() {}
|
||||
|
||||
fun <T : String?> T.foo() {
|
||||
if (this != null) {
|
||||
if (<!SENSELESS_COMPARISON!>this != null<!>) {}
|
||||
|
||||
length()
|
||||
this<!UNNECESSARY_SAFE_CALL!>?.<!>length()
|
||||
|
||||
bar1()
|
||||
bar2()
|
||||
<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar3<!>()
|
||||
bar4()
|
||||
|
||||
|
||||
this<!UNNECESSARY_SAFE_CALL!>?.<!>bar1()
|
||||
}
|
||||
|
||||
<!UNSAFE_CALL!>length<!>()
|
||||
|
||||
if (this is String) {
|
||||
length()
|
||||
this<!UNNECESSARY_SAFE_CALL!>?.<!>length()
|
||||
|
||||
bar1()
|
||||
bar2()
|
||||
<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar3<!>()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package
|
||||
|
||||
internal fun </*0*/ T : kotlin.CharSequence?> T.bar1(): kotlin.Unit
|
||||
internal fun kotlin.CharSequence?.bar2(): kotlin.Unit
|
||||
internal fun </*0*/ T : kotlin.CharSequence> T.bar3(): kotlin.Unit
|
||||
internal fun kotlin.CharSequence.bar4(): kotlin.Unit
|
||||
internal fun </*0*/ T : kotlin.String?> T.foo(): kotlin.Unit
|
||||
@@ -0,0 +1,40 @@
|
||||
// !DIAGNOSTICS: -UNUSED_VALUE,-UNUSED_VARIABLE,-ASSIGNED_BUT_NEVER_ACCESSED_VARIABLE,-BASE_WITH_NULLABLE_UPPER_BOUND,-VARIABLE_WITH_REDUNDANT_INITIALIZER
|
||||
|
||||
class A<T : CharSequence?, E1 : T, E2: T?> {
|
||||
fun T.bar() {}
|
||||
|
||||
fun foo(x: E1, y: E2) {
|
||||
x.bar()
|
||||
|
||||
if (1 == 1) {
|
||||
y<!UNSAFE_CALL!>.<!>bar()
|
||||
}
|
||||
|
||||
x?.bar()
|
||||
y?.bar()
|
||||
|
||||
|
||||
var t: T = x
|
||||
var tN: T? = y
|
||||
|
||||
// condition needed to make smart cast on tN impossible
|
||||
if (1 == 1) {
|
||||
tN = x
|
||||
}
|
||||
|
||||
if (1 == 1) {
|
||||
t = <!TYPE_MISMATCH!>tN<!>
|
||||
}
|
||||
|
||||
t = <!TYPE_MISMATCH!>y<!>
|
||||
|
||||
// Could be smart-cast
|
||||
if (y != null) {
|
||||
t = <!TYPE_MISMATCH!>y<!>
|
||||
}
|
||||
|
||||
if (tN != null) {
|
||||
t = <!DEBUG_INFO_SMARTCAST!>tN<!>
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package
|
||||
|
||||
internal final class A</*0*/ T : kotlin.CharSequence?, /*1*/ E1 : T, /*2*/ E2 : T?> {
|
||||
public constructor A</*0*/ T : kotlin.CharSequence?, /*1*/ E1 : T, /*2*/ E2 : T?>()
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
internal final fun foo(/*0*/ x: E1, /*1*/ y: E2): kotlin.Unit
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
internal final fun T.bar(): kotlin.Unit
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
// !DIAGNOSTICS: -UNUSED_EXPRESSION,-UNUSED_VARIABLE
|
||||
|
||||
fun <T : CharSequence?> T.bar1() {}
|
||||
fun CharSequence?.bar2() {}
|
||||
|
||||
fun <T : CharSequence> T.bar3() {}
|
||||
|
||||
fun <T, R> T.let(f: (T) -> R): R = f(this)
|
||||
|
||||
fun <T : String?> foo(x: T) {
|
||||
x<!UNSAFE_CALL!>.<!>length()
|
||||
x?.length()
|
||||
|
||||
if (1 == 1) {
|
||||
x!!.length()
|
||||
}
|
||||
|
||||
|
||||
x.bar1()
|
||||
x.bar2()
|
||||
|
||||
x?.bar1()
|
||||
x?.bar2()
|
||||
|
||||
x.<!TYPE_INFERENCE_UPPER_BOUND_VIOLATED!>bar3<!>()
|
||||
|
||||
x?.let { it<!UNSAFE_CALL!>.<!>length() }
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
package
|
||||
|
||||
internal fun </*0*/ T : kotlin.String?> foo(/*0*/ x: T): kotlin.Unit
|
||||
internal fun </*0*/ T : kotlin.CharSequence?> T.bar1(): kotlin.Unit
|
||||
internal fun kotlin.CharSequence?.bar2(): kotlin.Unit
|
||||
internal fun </*0*/ T : kotlin.CharSequence> T.bar3(): kotlin.Unit
|
||||
internal fun </*0*/ T, /*1*/ R> T.let(/*0*/ f: (T) -> R): R
|
||||
@@ -6155,12 +6155,36 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("smartCasts.kt")
|
||||
public void testSmartCasts() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/smartCasts.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("smartCastsOnThis.kt")
|
||||
public void testSmartCastsOnThis() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/smartCastsOnThis.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("tpBoundsViolation.kt")
|
||||
public void testTpBoundsViolation() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/tpBoundsViolation.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("tpInBounds.kt")
|
||||
public void testTpInBounds() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/tpInBounds.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("useAsReceiver.kt")
|
||||
public void testUseAsReceiver() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/useAsReceiver.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("useAsValueArgument.kt")
|
||||
public void testUseAsValueArgument() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/useAsValueArgument.kt");
|
||||
|
||||
Reference in New Issue
Block a user