Prohibit using null as a value of generic type
#KT-7350 Fixed #KT-7736 Fixed #KT-7485 Fixed
This commit is contained in:
+1
-1
@@ -143,7 +143,7 @@ public class CompileTimeConstantChecker {
|
||||
}
|
||||
|
||||
private boolean checkNullValue(@NotNull JetType expectedType, @NotNull JetConstantExpression expression) {
|
||||
if (!noExpectedTypeOrError(expectedType) && !TypeUtils.isNullableType(expectedType)) {
|
||||
if (!noExpectedTypeOrError(expectedType) && !TypeUtils.acceptsNullable(expectedType)) {
|
||||
return reportError(NULL_FOR_NONNULL_TYPE.on(expression, expectedType));
|
||||
}
|
||||
return false;
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
// !DIAGNOSTICS: -UNUSED_PARAMETER,-UNUSED_VARIABLE,-BASE_WITH_NULLABLE_UPPER_BOUND
|
||||
|
||||
fun <E> bar(x: E) {}
|
||||
|
||||
fun <T> foo(): T {
|
||||
val x1: T = <!NULL_FOR_NONNULL_TYPE!>null<!>
|
||||
val x2: T? = null
|
||||
|
||||
bar<T>(<!NULL_FOR_NONNULL_TYPE!>null<!>)
|
||||
bar<T?>(null)
|
||||
|
||||
return <!NULL_FOR_NONNULL_TYPE!>null<!>
|
||||
}
|
||||
|
||||
fun <T> baz(): T? = null
|
||||
|
||||
fun <T> foobar(): T = <!NULL_FOR_NONNULL_TYPE!>null<!>
|
||||
|
||||
class A<F> {
|
||||
fun xyz(x: F) {}
|
||||
|
||||
fun foo(): F {
|
||||
val x1: F = <!NULL_FOR_NONNULL_TYPE!>null<!>
|
||||
val x2: F? = null
|
||||
|
||||
xyz(<!NULL_FOR_NONNULL_TYPE!>null<!>)
|
||||
bar<F?>(null)
|
||||
|
||||
return <!NULL_FOR_NONNULL_TYPE!>null<!>
|
||||
}
|
||||
|
||||
fun baz(): F? = null
|
||||
|
||||
fun foobar(): F = <!NULL_FOR_NONNULL_TYPE!>null<!>
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package
|
||||
|
||||
internal fun </*0*/ E> bar(/*0*/ x: E): kotlin.Unit
|
||||
internal fun </*0*/ T> baz(): T?
|
||||
internal fun </*0*/ T> foo(): T
|
||||
internal fun </*0*/ T> foobar(): T
|
||||
|
||||
internal final class A</*0*/ F> {
|
||||
public constructor A</*0*/ F>()
|
||||
internal final fun baz(): F?
|
||||
public open override /*1*/ /*fake_override*/ fun equals(/*0*/ other: kotlin.Any?): kotlin.Boolean
|
||||
internal final fun foo(): F
|
||||
internal final fun foobar(): F
|
||||
public open override /*1*/ /*fake_override*/ fun hashCode(): kotlin.Int
|
||||
public open override /*1*/ /*fake_override*/ fun toString(): kotlin.String
|
||||
internal final fun xyz(/*0*/ x: F): kotlin.Unit
|
||||
}
|
||||
@@ -6129,6 +6129,21 @@ public class JetDiagnosticsTestGenerated extends AbstractJetDiagnosticsTest {
|
||||
doTest(fileName);
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/generics/nullability")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
public static class Nullability extends AbstractJetDiagnosticsTest {
|
||||
public void testAllFilesPresentInNullability() throws Exception {
|
||||
JetTestUtils.assertAllTestsPresentByMetadata(this.getClass(), new File("compiler/testData/diagnostics/tests/generics/nullability"), Pattern.compile("^(.+)\\.kt$"), true);
|
||||
}
|
||||
|
||||
@TestMetadata("nullToGeneric.kt")
|
||||
public void testNullToGeneric() throws Exception {
|
||||
String fileName = JetTestUtils.navigationMetadata("compiler/testData/diagnostics/tests/generics/nullability/nullToGeneric.kt");
|
||||
doTest(fileName);
|
||||
}
|
||||
}
|
||||
|
||||
@TestMetadata("compiler/testData/diagnostics/tests/generics/starProjections")
|
||||
@TestDataPath("$PROJECT_ROOT")
|
||||
@RunWith(JUnit3RunnerWithInners.class)
|
||||
|
||||
Reference in New Issue
Block a user