From 6b1e11e92c2a490bf532138b9dc844485c421305 Mon Sep 17 00:00:00 2001 From: Googler Date: Wed, 24 Jan 2024 17:33:57 -0800 Subject: [PATCH] [J2KT] Fix import resolver to include member types from super classes, to match Kotlin semantic. PiperOrigin-RevId: 601284853 --- .../j2cl/transpiler/backend/kotlin/Names.kt | 6 ++- .../readable/java/j2kt/ImportResolution.java | 10 +++++ .../j2kt/output_kt/ImportResolution.kt.txt | 8 ++++ .../java/j2ktnotpassing/ImportResolution.java | 33 -------------- .../ImportResolution+J2ObjCCompat.h.txt | 11 ----- .../output_kt/ImportResolution.kt.txt | 45 ------------------- 6 files changed, 23 insertions(+), 90 deletions(-) delete mode 100644 transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/ImportResolution.java delete mode 100644 transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/output_kt/ImportResolution+J2ObjCCompat.h.txt delete mode 100644 transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/output_kt/ImportResolution.kt.txt diff --git a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt index 4b47def48d..22bd6f8103 100644 --- a/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt +++ b/transpiler/java/com/google/j2cl/transpiler/backend/kotlin/Names.kt @@ -38,7 +38,11 @@ internal val TypeDeclaration.memberTypeNameMap: Map /** A map of local member names used in this type. */ internal val TypeDeclaration.localTypeNameMap: Map - get() = memberTypeNameMap.plus(nameMapEntry) + get() = superTypesMemberNameMap.plus(memberTypeNameMap).plus(nameMapEntry) + +/** A map of local names from super type members. */ +internal val TypeDeclaration.superTypesMemberNameMap: Map + get() = superTypeDeclaration?.run { superTypesMemberNameMap.plus(memberTypeNameMap) } ?: mapOf() /** A map of local names used in this type. */ internal val Type.localTypeNameMap: Map diff --git a/transpiler/javatests/com/google/j2cl/readable/java/j2kt/ImportResolution.java b/transpiler/javatests/com/google/j2cl/readable/java/j2kt/ImportResolution.java index a7bdc23b60..c6e731533c 100644 --- a/transpiler/javatests/com/google/j2cl/readable/java/j2kt/ImportResolution.java +++ b/transpiler/javatests/com/google/j2cl/readable/java/j2kt/ImportResolution.java @@ -39,6 +39,11 @@ void testLocal(String string) { } static class Child extends Parent { + @Override + void testJavaLang(java.lang.String string) { + string.trim(); + } + @Override void testLocal(String string) { string.local(); @@ -46,6 +51,11 @@ void testLocal(String string) { } static class GrandChild extends Child { + @Override + void testJavaLang(java.lang.String string) { + string.trim(); + } + @Override void testLocal(String string) { string.local(); diff --git a/transpiler/javatests/com/google/j2cl/readable/java/j2kt/output_kt/ImportResolution.kt.txt b/transpiler/javatests/com/google/j2cl/readable/java/j2kt/output_kt/ImportResolution.kt.txt index 552c69be35..6d8960b6aa 100644 --- a/transpiler/javatests/com/google/j2cl/readable/java/j2kt/output_kt/ImportResolution.kt.txt +++ b/transpiler/javatests/com/google/j2cl/readable/java/j2kt/output_kt/ImportResolution.kt.txt @@ -49,12 +49,20 @@ open class ImportResolution { } open class Child internal constructor(): ImportResolution.Parent() { + override fun testJavaLang_pp_j2kt(string: kotlin.String?) { + string!!.java_trim() + } + override fun testLocal_pp_j2kt(string: ImportResolution.Parent.String?) { string!!.local_pp_j2kt() } } open class GrandChild internal constructor(): ImportResolution.Child() { + override fun testJavaLang_pp_j2kt(string: kotlin.String?) { + string!!.java_trim() + } + override fun testLocal_pp_j2kt(string: ImportResolution.Parent.String?) { string!!.local_pp_j2kt() } diff --git a/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/ImportResolution.java b/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/ImportResolution.java deleted file mode 100644 index b0375bee83..0000000000 --- a/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/ImportResolution.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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; - -public class ImportResolution { - static class Parent { - class String {} - } - - static class Child extends Parent { - void testJavaLang(java.lang.String string) { - string.trim(); - } - } - - static class GrandChild extends Parent { - void testJavaLang(java.lang.String string) { - string.trim(); - } - } -} diff --git a/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/output_kt/ImportResolution+J2ObjCCompat.h.txt b/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/output_kt/ImportResolution+J2ObjCCompat.h.txt deleted file mode 100644 index 08f6d361ab..0000000000 --- a/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/output_kt/ImportResolution+J2ObjCCompat.h.txt +++ /dev/null @@ -1,11 +0,0 @@ -// Generated by J2KT from "j2ktnotpassing/ImportResolution.java" - -#import - -@class J2ktJ2ktnotpassingImportResolution; - -NS_ASSUME_NONNULL_BEGIN - -@compatibility_alias J2ktnotpassingImportResolution J2ktJ2ktnotpassingImportResolution; - -NS_ASSUME_NONNULL_END diff --git a/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/output_kt/ImportResolution.kt.txt b/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/output_kt/ImportResolution.kt.txt deleted file mode 100644 index edc2b9ee78..0000000000 --- a/transpiler/javatests/com/google/j2cl/readable/java/j2ktnotpassing/output_kt/ImportResolution.kt.txt +++ /dev/null @@ -1,45 +0,0 @@ -// Generated from "j2ktnotpassing/ImportResolution.java" -@file:OptIn(ExperimentalObjCName::class) -@file:Suppress( - "ALWAYS_NULL", - "PARAMETER_NAME_CHANGED_ON_OVERRIDE", - "REPEATED_BOUND", - "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") - -package j2ktnotpassing - -import javaemul.lang.* -import kotlin.OptIn -import kotlin.String -import kotlin.Suppress -import kotlin.experimental.ExperimentalObjCName -import kotlin.native.ObjCName - -@ObjCName("J2ktJ2ktnotpassingImportResolution", exact = true) -open class ImportResolution { - open class Parent internal constructor() { - open inner class String internal constructor() {} - } - - open class Child internal constructor(): ImportResolution.Parent() { - internal open fun testJavaLang_pp_j2ktnotpassing(string: String?) { - string!!.java_trim() - } - } - - open class GrandChild internal constructor(): ImportResolution.Parent() { - internal open fun testJavaLang_pp_j2ktnotpassing(string: String?) { - string!!.java_trim() - } - } -}