-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[J2KT] Reproduce a problem with nullability inference in `Optional.or…
…Else()`, which occurs on j2kt-native only. PiperOrigin-RevId: 705206366
- Loading branch information
1 parent
558b06b
commit 14291c0
Showing
4 changed files
with
231 additions
and
0 deletions.
There are no files selected for viewing
56 changes: 56 additions & 0 deletions
56
transpiler/javatests/com/google/j2cl/readable/java/j2kt/NullabilityInferenceProblem.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
73 changes: 73 additions & 0 deletions
73
...javatests/com/google/j2cl/readable/java/j2kt/output_kt/NullabilityInferenceProblem.kt.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
.../javatests/com/google/j2cl/readable/java/j2ktnotpassing/NullabilityInferenceProblems.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
...om/google/j2cl/readable/java/j2ktnotpassing/output_kt/NullabilityInferenceProblems.kt.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | ||
} | ||
} |