Skip to content

Commit

Permalink
[J2KT] Reproduce a problem with nullability inference in `Optional.or…
Browse files Browse the repository at this point in the history
…Else()`, which occurs on j2kt-native only.

PiperOrigin-RevId: 705206366
  • Loading branch information
Googler authored and copybara-github committed Dec 11, 2024
1 parent 558b06b commit 14291c0
Show file tree
Hide file tree
Showing 4 changed files with 231 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package j2kt;

import static java.util.Comparator.comparing;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
class NullabilityInferenceProblem {
interface ImmutableList<T> extends List<T> {}

interface User {
Optional<String> getName();
}

private static <E> ImmutableList<E> sortedCopyOf(
Comparator<? super E> comparator, Iterable<? extends E> elements) {
throw new RuntimeException();
}

private static <T> T checkNotNull(@Nullable T reference) {
throw new RuntimeException();
}

// TODO(b/383581076): Uncomment when fixed.
// private static ImmutableList<User> testImplicitTypeArguments(Iterable<User> users) {
// return sortedCopyOf(comparing(user -> user.getName().orElse("")), users);
// }

private static ImmutableList<User> testExplicitTypeArguments(Iterable<User> users) {
return sortedCopyOf(
Comparator.<User, String>comparing(user -> user.getName().orElse("")), users);
}

private static ImmutableList<User> testCheckNotNull(Iterable<User> users) {
return sortedCopyOf(comparing(user -> checkNotNull(user.getName().orElse(""))), users);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Generated from "j2kt/NullabilityInferenceProblem.java"
@file:OptIn(ExperimentalObjCName::class)
@file:Suppress(
"ALWAYS_NULL",
"PARAMETER_NAME_CHANGED_ON_OVERRIDE",
"SENSELESS_COMPARISON",
"UNCHECKED_CAST",
"UNNECESSARY_LATEINIT",
"UNNECESSARY_NOT_NULL_ASSERTION",
"UNREACHABLE_CODE",
"UNUSED_ANONYMOUS_PARAMETER",
"UNUSED_PARAMETER",
"UNUSED_VARIABLE",
"USELESS_CAST",
"VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL",
"VARIABLE_WITH_REDUNDANT_INITIALIZER",
"REDUNDANT_ELSE_IN_WHEN")

package j2kt

import javaemul.lang.*
import java.lang.RuntimeException
import java.util.Optional
import java.util.function.Function
import javaemul.lang.JavaList
import kotlin.Any
import kotlin.Comparator
import kotlin.OptIn
import kotlin.String
import kotlin.Suppress
import kotlin.collections.MutableIterable
import kotlin.experimental.ExperimentalObjCName
import kotlin.jvm.JvmStatic
import kotlin.native.ObjCName

@ObjCName("J2ktJ2ktNullabilityInferenceProblem", exact = true)
open class NullabilityInferenceProblem internal constructor() {
@ObjCName("J2ktJ2ktNullabilityInferenceProblemCompanion", exact = true)
companion object {
@JvmStatic
private fun <E: Any> sortedCopyOf(comparator: Comparator<in E>, elements: MutableIterable<E>): NullabilityInferenceProblem.ImmutableList<E> {
throw RuntimeException()
}

@JvmStatic
private fun <T: Any> checkNotNull(reference: T?): T {
throw RuntimeException()
}

@JvmStatic
private fun testExplicitTypeArguments(users: MutableIterable<NullabilityInferenceProblem.User>): NullabilityInferenceProblem.ImmutableList<NullabilityInferenceProblem.User> {
return NullabilityInferenceProblem.sortedCopyOf<NullabilityInferenceProblem.User>(java.util.Comparator.comparing<NullabilityInferenceProblem.User, String>(Function { user: NullabilityInferenceProblem.User ->
return@Function user.getName().orElse("")!!
}), users)
}

@JvmStatic
private fun testCheckNotNull(users: MutableIterable<NullabilityInferenceProblem.User>): NullabilityInferenceProblem.ImmutableList<NullabilityInferenceProblem.User> {
return NullabilityInferenceProblem.sortedCopyOf<NullabilityInferenceProblem.User>(java.util.Comparator.comparing<NullabilityInferenceProblem.User, String>(Function { user: NullabilityInferenceProblem.User ->
return@Function NullabilityInferenceProblem.checkNotNull<String>(user.getName().orElse(""))
}), users)
}
}

@ObjCName("J2ktJ2ktNullabilityInferenceProblem_ImmutableList", exact = true)
interface ImmutableList<T: Any>: JavaList<T> {}

@ObjCName("J2ktJ2ktNullabilityInferenceProblem_User", exact = true)
fun interface User {
@ObjCName("getName")
fun getName(): Optional<String>
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Copyright 2024 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package j2ktnotpassing;

import static java.util.Comparator.comparing;

import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import org.jspecify.annotations.NullMarked;

@NullMarked
class NullabilityInferenceProblem {
interface ImmutableList<T> extends List<T> {}

interface User {
Optional<String> getName();
}

private static <E> ImmutableList<E> sortedCopyOf(
Comparator<? super E> comparator, Iterable<? extends E> elements) {
throw new RuntimeException();
}

private static ImmutableList<User> testImplicitTypeArguments(Iterable<User> users) {
return sortedCopyOf(comparing(user -> user.getName().orElse("")), users);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Generated from "j2ktnotpassing/NullabilityInferenceProblems.java"
@file:OptIn(ExperimentalObjCName::class)
@file:Suppress(
"ALWAYS_NULL",
"PARAMETER_NAME_CHANGED_ON_OVERRIDE",
"SENSELESS_COMPARISON",
"UNCHECKED_CAST",
"UNNECESSARY_LATEINIT",
"UNNECESSARY_NOT_NULL_ASSERTION",
"UNREACHABLE_CODE",
"UNUSED_ANONYMOUS_PARAMETER",
"UNUSED_PARAMETER",
"UNUSED_VARIABLE",
"USELESS_CAST",
"VARIABLE_IN_SINGLETON_WITHOUT_THREAD_LOCAL",
"VARIABLE_WITH_REDUNDANT_INITIALIZER",
"REDUNDANT_ELSE_IN_WHEN")

package j2ktnotpassing

import javaemul.lang.*
import java.lang.RuntimeException
import java.util.Optional
import java.util.function.Function
import javaemul.lang.JavaList
import kotlin.Any
import kotlin.Comparator
import kotlin.OptIn
import kotlin.String
import kotlin.Suppress
import kotlin.collections.MutableIterable
import kotlin.experimental.ExperimentalObjCName
import kotlin.jvm.JvmStatic
import kotlin.native.ObjCName

@ObjCName("J2ktJ2ktnotpassingNullabilityInferenceProblem", exact = true)
open class NullabilityInferenceProblem internal constructor() {
@ObjCName("J2ktJ2ktnotpassingNullabilityInferenceProblemCompanion", exact = true)
companion object {
@JvmStatic
private fun <E: Any> sortedCopyOf(comparator: Comparator<in E>, elements: MutableIterable<E>): NullabilityInferenceProblem.ImmutableList<E> {
throw RuntimeException()
}

@JvmStatic
private fun testImplicitTypeArguments(users: MutableIterable<NullabilityInferenceProblem.User>): NullabilityInferenceProblem.ImmutableList<NullabilityInferenceProblem.User> {
return NullabilityInferenceProblem.sortedCopyOf<NullabilityInferenceProblem.User>(java.util.Comparator.comparing<NullabilityInferenceProblem.User, String>(Function { user: NullabilityInferenceProblem.User ->
return@Function user.getName().orElse("")
}), users)
}
}

@ObjCName("J2ktJ2ktnotpassingNullabilityInferenceProblem_ImmutableList", exact = true)
interface ImmutableList<T: Any>: JavaList<T> {}

@ObjCName("J2ktJ2ktnotpassingNullabilityInferenceProblem_User", exact = true)
fun interface User {
@ObjCName("getName")
fun getName(): Optional<String>
}
}

0 comments on commit 14291c0

Please sign in to comment.