Refine Nullness for Kotlin functions returning Unit

Should have unspecified nullness like for Java void/Void.

Closes gh-35420
This commit is contained in:
Sébastien Deleuze 2025-09-05 11:16:27 +02:00
parent e5b58effa3
commit d2bdf11b39
4 changed files with 24 additions and 3 deletions

View File

@ -181,10 +181,10 @@ public enum Nullness {
public static Nullness forMethodReturnType(Method method) { public static Nullness forMethodReturnType(Method method) {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method); KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
if (function != null && function.getReturnType().isMarkedNullable()) { if (function != null && ReflectJvmMapping.getJavaType(function.getReturnType()) != void.class) {
return Nullness.NULLABLE; return (function.getReturnType().isMarkedNullable() ? Nullness.NULLABLE : Nullness.NON_NULL);
} }
return Nullness.NON_NULL; return Nullness.UNSPECIFIED;
} }
public static Nullness forParameter(Executable executable, int parameterIndex) { public static Nullness forParameter(Executable executable, int parameterIndex) {

View File

@ -377,6 +377,13 @@ public class NullnessTests {
Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE); Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE);
} }
@Test
void voidClassMethod() throws NoSuchMethodException {
var method = JSpecifyProcessor.class.getMethod("voidClassProcess");
var nullness = Nullness.forMethodReturnType(method);
Assertions.assertThat(nullness).isEqualTo(Nullness.UNSPECIFIED);
}
// Primitive types // Primitive types
@Test @Test

View File

@ -37,6 +37,13 @@ class NullnessKotlinTests {
Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE) Assertions.assertThat(nullness).isEqualTo(Nullness.NULLABLE)
} }
@Test
fun unitReturnType() {
val method = ::unit.javaMethod!!
val nullness = Nullness.forMethodReturnType(method)
Assertions.assertThat(nullness).isEqualTo(Nullness.UNSPECIFIED)
}
@Test @Test
fun nullableParameter() { fun nullableParameter() {
val method = ::nullable.javaMethod!! val method = ::nullable.javaMethod!!
@ -78,4 +85,7 @@ class NullnessKotlinTests {
@Suppress("unused_parameter") @Suppress("unused_parameter")
fun nonNull(nonNull: String): String = "foo" fun nonNull(nonNull: String): String = "foo"
fun unit() {
}
} }

View File

@ -37,5 +37,9 @@ public interface JSpecifyProcessor {
@NullMarked @NullMarked
@NonNull String nonNullMarkedProcess(); @NonNull String nonNullMarkedProcess();
@NullMarked
void voidProcess(); void voidProcess();
@NullMarked
void voidClassProcess();
} }