Skip to content

Commit

Permalink
[J2KT] Move already passing readables from j2ktnotpassing to j2kt
Browse files Browse the repository at this point in the history
…, and non-passing pieces as commented-out and assigned with a bug.

PiperOrigin-RevId: 725600446
  • Loading branch information
Googler authored and copybara-github committed Feb 11, 2025
1 parent 10f52d2 commit 17f1290
Show file tree
Hide file tree
Showing 30 changed files with 540 additions and 164 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ public void accidentalOverride() {
}
}
}

interface Parent {
<T extends String> void test(T t);
}

interface Child extends Parent {
// TODO(b/395588204): Uncomment when fixed.
// This method is valid override in Java, but in Kotlin it's accidental override.
// @Override
// void test(String s);
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package j2ktnotpassing;
package j2kt;

import org.jspecify.annotations.NullMarked;

Expand All @@ -24,7 +24,6 @@ public class AnonymousClassUninitializedFieldReference {
private final Object anonymous =
new Object() {
public Object getObj() {
// TODO(b/368275230): This line fails to compile with UNINITIALIZED_VARIABLE error.
return obj;
}
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,25 @@ public static void testNullableBoxedOverload() {
nullableBoxedOverload(1);
}

// TODO(b/279936148): Uncomment when fixed.
// public static void boxedOverload(double d) {}
//
// public static void boxedOverload(Double d) {}
//
// public static void testBoxedOverload() {
// // Dispatches boxedOverload(double)
// boxedOverload(1.25);
//
// // Dispatches boxedOverload(Double)
// boxedOverload(Double.valueOf(1.25));
//
// // Dispatches boxedOverload(Double)
// boxedOverload(new Double(1.25));
//
// // Dispatches boxedOverload(double)
// boxedOverload(1);
// }

public static void nullableNumberOverload(double d) {}

public static void nullableNumberOverload(@Nullable Number n) {}
Expand Down Expand Up @@ -147,6 +166,28 @@ public static void testObjectOverload() {
objectOverload("foo");
}

public interface Generic<T extends @Nullable Object> {
void overload(double d);

void overload(T t);

void comparableOverload(double d);

void comparableOverload(Comparable<T> comparable);
}

// TODO(b/279936148): Uncomment when fixed.
// public static void testGenericBoxedOverload(Generic<Double> generic) {
// // Dispatches overload(double)
// generic.overload(1.25);
//
// // Dispatches overload(T)
// generic.overload(Double.valueOf(1.25));
//
// // Dispatches overload(double)
// generic.overload(1);
// }

// A case from com.google.common.base.MoreObjects.
public static <T> T firstNonNull(@Nullable T t1, T t2) {
return t1 != null ? t1 : t2;
Expand All @@ -156,12 +197,6 @@ public static void testFirstNonNull(@Nullable Integer i) {
firstNonNull(i, 0);
}

public interface Generic<T extends @Nullable Object> {
void comparableOverload(double d);

void comparableOverload(Comparable<T> comparable);
}

public static void testComparableOverload(Generic<Double> generic) {
// Dispatches overload(double)
generic.comparableOverload(1.25);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright 2022 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 org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

public class DefaultNullable {
// TODO(b/395585494): Uncomment when fixed
// static void testNonNullableLambdas() {
// NotNullable.Consumer<String> methodReference = DefaultNullable::accept;
// }

static void accept(String s) {}
}

@NullMarked
class NotNullable {
interface Consumer<T extends @Nullable Object> {
void accept(T t);
}
}

@NullMarked
abstract class Ordering<T extends @Nullable Object> {
<S extends T> Ordering<S> reverse() {
return null;
}

static <E extends @Nullable Object> Ordering<? super E> reversed(Ordering<? super E> ordering) {
return ordering.reverse();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,34 @@ <S extends T> Ordering<S> reverse() {
// See: b/268006049, b/272714235.
return ordering.reverse();
}

<S extends T> Ordering<@Nullable S> nullsLast() {
throw new RuntimeException();
}
}

final class NullsFirstOrdering<T extends @Nullable Object> extends Ordering<@Nullable T> {
@SuppressWarnings("nullness")
final Ordering<? super T> ordering;

NullsFirstOrdering(Ordering<? super T> ordering) {
this.ordering = ordering;
}

// TODO(b/268006049): Uncomment when fixed.
// @Override
// public <S extends @Nullable T> Ordering<S> reverse() {
// // Type inference problem detected in Guava.
// return ordering.reverse().nullsLast();
// }

// TODO(b/268006049): Uncomment when fixed.
// @Override
// @SuppressWarnings("nullness") // probably a bug in our checker?
// public <S extends @Nullable T> Ordering<@Nullable S> nullsLast() {
// // Type inference problem detected in Guava.
// return ordering.nullsLast();
// }
}

// Reproduction of Guava code with immutable lists.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*
* 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 org.jspecify.annotations.NullMarked;
import org.jspecify.annotations.Nullable;

@NullMarked
public class InitializationProblemWithNullLiteral {
static class Foo<V extends @Nullable Object> {
static <V extends @Nullable Object> Foo<V> with(V v) {
return new Foo<>();
}
}

// Kotlin complains that this field is uninitialized, while the actual problems is hidden in the
// next field, which uses null literal in generic method invocation, with type inferred as
// wildcard from assignment.
final int completelyNormalFinalField;

// The workaround for this problem specifying explicit type argument:
// {@code Foo.<@Nullable Void>.with(null)}
// TODO(b/372305346): Uncomment when fixed
// Foo<?> problematicFoo = Foo.with(null);

InitializationProblemWithNullLiteral() {
this.completelyNormalFinalField = 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,26 @@ class InnerClassWithSingleConstructor {

@KtProperty
int property() {
// To overcome the problem with Kotlin Compiler which complains that "i" is not
// initialized, J2KT renders the constructor as primary. The workaround does not work
// when there are two or more constructors.
// See: https://youtrack.jetbrains.com/issue/KT-65299
return i;
}
}
};

static final Object obj2 =
new Object() {
class InnerClassWithTwoConstructors {
final int i;

InnerClassWithTwoConstructors(int i) {
this.i = i;
}

InnerClassWithTwoConstructors(float i) {
this.i = (int) i;
}

@KtProperty
int property() {
return i;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ public static void testObservable(Observable<?> observable) {
}

public static <T extends Observable<?>> void testObservableParameterized(T observable) {
// TODO(b/261839232): "Expected Nothing" issue, cast to Observable<@Nullable Object> would help
// observable.addObserver(e -> {});
observable.addObserver(e -> {});
}

public static void testSuperWildcardObservable(SuperWildcardObservable<?> observable) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
* Copyright 2022 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 org.jspecify.annotations.NullMarked;

@NullMarked
class UnsatisfiedTypeBounds {
interface Foo<T extends Foo<T>> {
T get();
}

interface Command {}

static class FooCommand implements Foo<FooCommand>, Command {
@Override
public FooCommand get() {
return this;
}
}

static final class Helper<T> {}

// TODO(b/395578676): Uncomment when fixed.
// static <T> T methodWithTypeConstraints(Helper<T> helper, Foo<? extends T> foo) {
// return foo.get();
// }

// static void test() {
// Command command = methodWithTypeConstraints(new Helper<Command>(), new FooCommand());
// }
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package j2kt

import javaemul.lang.*
import kotlin.OptIn
import kotlin.String
import kotlin.Suppress
import kotlin.experimental.ExperimentalObjCName
import kotlin.native.ObjCName
Expand Down Expand Up @@ -79,3 +80,14 @@ open class AccidentalOverride {
abstract override fun abstractAccidentalOverride()
}
}

@ObjCName("J2ktJ2ktParent", exact = true)
interface Parent {
@ObjCName("test")
fun <T: String?> test(
@ObjCName("withNSString") t: T
)
}

@ObjCName("J2ktJ2ktChild", exact = true)
interface Child: Parent
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Generated by J2KT from "j2kt/AnonymousClassUninitializedFieldReference.java"

#import <Foundation/NSObjCRuntime.h>

@class J2ktJ2ktAnonymousClassUninitializedFieldReference;

NS_ASSUME_NONNULL_BEGIN

@compatibility_alias J2ktAnonymousClassUninitializedFieldReference J2ktJ2ktAnonymousClassUninitializedFieldReference;

NS_INLINE J2ktJ2ktAnonymousClassUninitializedFieldReference* create_J2ktAnonymousClassUninitializedFieldReference_initWithId_(id obj) {
return [[J2ktJ2ktAnonymousClassUninitializedFieldReference alloc] initWithId:obj];
}

NS_INLINE J2ktJ2ktAnonymousClassUninitializedFieldReference* new_J2ktAnonymousClassUninitializedFieldReference_initWithId_(id obj) {
return [[J2ktJ2ktAnonymousClassUninitializedFieldReference alloc] initWithId:obj];
}

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated from "j2ktnotpassing/AnonymousClassUninitializedFieldReference.java"
// Generated from "j2kt/AnonymousClassUninitializedFieldReference.java"
@file:OptIn(ExperimentalObjCName::class)
@file:Suppress(
"ALWAYS_NULL",
Expand All @@ -16,7 +16,7 @@
"VARIABLE_WITH_REDUNDANT_INITIALIZER",
"REDUNDANT_ELSE_IN_WHEN")

package j2ktnotpassing
package j2kt

import javaemul.lang.*
import kotlin.Any
Expand All @@ -25,7 +25,7 @@ import kotlin.Suppress
import kotlin.experimental.ExperimentalObjCName
import kotlin.native.ObjCName

@ObjCName("J2ktJ2ktnotpassingAnonymousClassUninitializedFieldReference", exact = true)
@ObjCName("J2ktJ2ktAnonymousClassUninitializedFieldReference", exact = true)
open class AnonymousClassUninitializedFieldReference {
private val obj: Any

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,16 @@ open class BoxOverloads {

@ObjCName("J2ktJ2ktBoxOverloads_Generic", exact = true)
interface Generic<T> {
@ObjCName("overload")
fun overload(
@ObjCName("withDouble") d: Double
)

@ObjCName("overload")
fun overload(
@ObjCName("withId") t: T
)

@ObjCName("comparableOverload")
fun comparableOverload(
@ObjCName("withDouble") d: Double
Expand Down
Loading

0 comments on commit 17f1290

Please sign in to comment.