Skip to content

Commit

Permalink
[J2KT] Add LambdaParameterTypeInference readable to prevent regress…
Browse files Browse the repository at this point in the history
…ion in case unnecessary projection casts are inserted around lambdas.

PiperOrigin-RevId: 702685974
  • Loading branch information
Googler authored and copybara-github committed Dec 4, 2024
1 parent fef79c9 commit 8dc0be1
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* 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.Nullable;

@Nullable
public class LambdaParameterTypeInference {
public interface Supplier<T extends @Nullable Object> {
T get();
}

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

public static <T extends @Nullable Object> Consumer<T> wrap(Consumer<T> consumer) {
throw new RuntimeException();
}

public static <T extends @Nullable Object> void add(
Supplier<T> supplier, Consumer<? super T> consumer) {
throw new RuntimeException();
}

// J2KT inserts nullability- and generic- related casts in case of type mismatch, using
// projections if necessary, which can potentially affect type inference in lambdas. This test
// covers a case which would cause [CANNOT_INFER_PARAMETER_TYPE] compilation error in Kotlin if
// unnecessary cast was inserted for "wrap()" call.
public static <T extends @Nullable Object> void test(Supplier<? extends T> supplier) {
add(supplier, wrap(x -> {}));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Generated by J2KT from "j2kt/LambdaParameterTypeInference.java"

#import <Foundation/NSObjCRuntime.h>

@class J2ktJ2ktLambdaParameterTypeInference;
@class J2ktJ2ktLambdaParameterTypeInferenceCompanion;

@protocol J2ktJ2ktLambdaParameterTypeInference_Consumer;
@protocol J2ktJ2ktLambdaParameterTypeInference_Supplier;

NS_ASSUME_NONNULL_BEGIN

@compatibility_alias J2ktLambdaParameterTypeInference J2ktJ2ktLambdaParameterTypeInference;

@compatibility_alias J2ktLambdaParameterTypeInferenceCompanion J2ktJ2ktLambdaParameterTypeInferenceCompanion;

NS_INLINE id<J2ktJ2ktLambdaParameterTypeInference_Consumer> _Nullable J2ktLambdaParameterTypeInference_wrapWithJ2ktLambdaParameterTypeInference_Consumer_(id<J2ktJ2ktLambdaParameterTypeInference_Consumer> _Nullable consumer) {
return [J2ktJ2ktLambdaParameterTypeInferenceCompanion.shared wrapWithJ2ktLambdaParameterTypeInference_Consumer:consumer];
}

NS_INLINE void J2ktLambdaParameterTypeInference_addWithJ2ktLambdaParameterTypeInference_Supplier_withJ2ktLambdaParameterTypeInference_Consumer_(id<J2ktJ2ktLambdaParameterTypeInference_Supplier> _Nullable supplier, id<J2ktJ2ktLambdaParameterTypeInference_Consumer> _Nullable consumer) {
[J2ktJ2ktLambdaParameterTypeInferenceCompanion.shared addWithJ2ktLambdaParameterTypeInference_Supplier:supplier withJ2ktLambdaParameterTypeInference_Consumer:consumer];
}

NS_INLINE void J2ktLambdaParameterTypeInference_testWithJ2ktLambdaParameterTypeInference_Supplier_(id<J2ktJ2ktLambdaParameterTypeInference_Supplier> _Nullable supplier) {
[J2ktJ2ktLambdaParameterTypeInferenceCompanion.shared testWithJ2ktLambdaParameterTypeInference_Supplier:supplier];
}

#define J2ktLambdaParameterTypeInference_Supplier J2ktJ2ktLambdaParameterTypeInference_Supplier

#define J2ktLambdaParameterTypeInference_Consumer J2ktJ2ktLambdaParameterTypeInference_Consumer

NS_ASSUME_NONNULL_END
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// Generated from "j2kt/LambdaParameterTypeInference.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 kotlin.OptIn
import kotlin.Suppress
import kotlin.experimental.ExperimentalObjCName
import kotlin.jvm.JvmStatic
import kotlin.native.ObjCName

@ObjCName("J2ktJ2ktLambdaParameterTypeInference", exact = true)
open class LambdaParameterTypeInference {
@ObjCName("J2ktJ2ktLambdaParameterTypeInferenceCompanion", exact = true)
companion object {
@JvmStatic
@ObjCName("wrap")
fun <T> wrap(
@ObjCName("withJ2ktLambdaParameterTypeInference_Consumer") consumer: LambdaParameterTypeInference.Consumer<T>?
): LambdaParameterTypeInference.Consumer<T>? {
throw RuntimeException()
}

@JvmStatic
@ObjCName("add")
fun <T> add(
@ObjCName("withJ2ktLambdaParameterTypeInference_Supplier") supplier: LambdaParameterTypeInference.Supplier<T>?,
@ObjCName("withJ2ktLambdaParameterTypeInference_Consumer") consumer: LambdaParameterTypeInference.Consumer<in T>?
) {
throw RuntimeException()
}

@JvmStatic
@ObjCName("test")
fun <T> test(
@ObjCName("withJ2ktLambdaParameterTypeInference_Supplier") supplier: LambdaParameterTypeInference.Supplier<out T>?
) {
LambdaParameterTypeInference.add(supplier, LambdaParameterTypeInference.wrap(LambdaParameterTypeInference.Consumer { x /* capture#1-of out T */ ->
}))
}
}

@ObjCName("J2ktJ2ktLambdaParameterTypeInference_Supplier", exact = true)
fun interface Supplier<T> {
@ObjCName("get")
fun get(): T
}

@ObjCName("J2ktJ2ktLambdaParameterTypeInference_Consumer", exact = true)
fun interface Consumer<T> {
@ObjCName("accept")
fun accept(
@ObjCName("withId") t: T
)
}
}

0 comments on commit 8dc0be1

Please sign in to comment.