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) {
KFunction<?> function = ReflectJvmMapping.getKotlinFunction(method);
if (function != null && function.getReturnType().isMarkedNullable()) {
return Nullness.NULLABLE;
if (function != null && ReflectJvmMapping.getJavaType(function.getReturnType()) != void.class) {
return (function.getReturnType().isMarkedNullable() ? Nullness.NULLABLE : Nullness.NON_NULL);
}
return Nullness.NON_NULL;
return Nullness.UNSPECIFIED;
}
public static Nullness forParameter(Executable executable, int parameterIndex) {

View File

@ -377,6 +377,13 @@ public class NullnessTests {
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
@Test

View File

@ -37,6 +37,13 @@ class NullnessKotlinTests {
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
fun nullableParameter() {
val method = ::nullable.javaMethod!!
@ -78,4 +85,7 @@ class NullnessKotlinTests {
@Suppress("unused_parameter")
fun nonNull(nonNull: String): String = "foo"
fun unit() {
}
}

View File

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