@@ -102,6 +102,142 @@ struct JNINestedTypesTests {
102102 )
103103 }
104104
105+ @ Test ( " Import: shadowed nested type name " )
106+ func shadowedNestedTypeName_java ( ) throws {
107+ // A nested type whose name shadows a type of the same name in an outer
108+ // scope must resolve to the innermost declaration. Before the fix,
109+ // `Outer.Inner.kind` was resolving to `Outer.Kind` rather than
110+ // `Outer.Inner.Kind`, but only when the shadowing type was wrapped in a
111+ // conditional-compilation `#if` block; see the `_ifConfig` variant below.
112+ try assertOutput (
113+ input : """
114+ public struct Outer: Sendable, Codable {
115+ public enum Kind: Sendable, Codable {
116+ case alpha
117+ case beta
118+ }
119+
120+ public struct Inner: Sendable, Codable {
121+ public enum Kind: Sendable, Codable {
122+ case one
123+ case two
124+ }
125+
126+ public var kind: Kind // Outer.Inner.Kind
127+ }
128+
129+ public var kind: Kind // Outer.Kind
130+ public var inner: Inner
131+ }
132+ """ ,
133+ . jni ,
134+ . java ,
135+ detectChunkByInitialLines : 1 ,
136+ expectedChunks : [
137+ // The `Inner.kind` property must reference `Outer.Inner.Kind`, not `Outer.Kind`.
138+ """
139+ public Outer.Inner.Kind getKind(SwiftArena swiftArena) {
140+ return Outer.Inner.Kind.wrapMemoryAddressUnsafe(Outer.Inner.$getKind(this.$memoryAddress()), swiftArena);
141+ """ ,
142+ """
143+ public void setKind(Outer.Inner.Kind newValue) {
144+ Outer.Inner.$setKind(newValue.$memoryAddress(), this.$memoryAddress());
145+ """ ,
146+ // The outer `kind` must reference `Outer.Kind`.
147+ """
148+ public Outer.Kind getKind(SwiftArena swiftArena) {
149+ return Outer.Kind.wrapMemoryAddressUnsafe(Outer.$getKind(this.$memoryAddress()), swiftArena);
150+ """ ,
151+ ]
152+ )
153+ }
154+
155+ @ Test ( " Import: shadowed nested type name inside #if " )
156+ func shadowedNestedTypeName_ifConfig_java ( ) throws {
157+ try assertOutput (
158+ input : """
159+ public struct Outer {
160+ #if SOME_FLAG
161+ public enum Kind {
162+ case alphaX
163+ }
164+ #else
165+ public enum Kind {
166+ case alpha
167+ case beta
168+ }
169+ #endif
170+
171+ public struct Inner {
172+ #if SOME_FLAG
173+ public enum Kind {
174+ case oneX
175+ }
176+ #else
177+ public enum Kind {
178+ case one
179+ case two
180+ }
181+ #endif
182+
183+ public var kind: Kind // Outer.Inner.Kind
184+ }
185+
186+ public var kind: Kind // Outer.Kind
187+ public var inner: Inner
188+ }
189+ """ ,
190+ . jni ,
191+ . java ,
192+ detectChunkByInitialLines : 1 ,
193+ expectedChunks : [
194+ """
195+ public Outer.Inner.Kind getKind(SwiftArena swiftArena) {
196+ return Outer.Inner.Kind.wrapMemoryAddressUnsafe(Outer.Inner.$getKind(this.$memoryAddress()), swiftArena);
197+ """ ,
198+ """
199+ public void setKind(Outer.Inner.Kind newValue) {
200+ Outer.Inner.$setKind(newValue.$memoryAddress(), this.$memoryAddress());
201+ """ ,
202+ ]
203+ )
204+ }
205+
206+ @ Test ( " Import: shadowed nested type name in deeper nesting " )
207+ func shadowedNestedTypeName_deep_java ( ) throws {
208+ try assertOutput (
209+ input : """
210+ public struct DiscordChannel {
211+ public struct Kind {
212+ public init() {}
213+ }
214+
215+ public struct Message {
216+ public struct MessageReference {
217+ public struct Kind {
218+ public init() {}
219+ }
220+ public var kind: Kind
221+ }
222+ }
223+ }
224+ """ ,
225+ . jni ,
226+ . java ,
227+ detectChunkByInitialLines : 1 ,
228+ expectedChunks : [
229+ // The `MessageReference.kind` property must reference the deeply nested
230+ // `DiscordChannel.Message.MessageReference.Kind`, not `DiscordChannel.Kind`.
231+ """
232+ public DiscordChannel.Message.MessageReference.Kind getKind(SwiftArena swiftArena) {
233+ """ ,
234+ """
235+ public void setKind(DiscordChannel.Message.MessageReference.Kind newValue) {
236+ """ ,
237+ ]
238+ )
239+ }
240+
105241 @ Test ( " Import: nested in enum " )
106242 func nestedEnums_java ( ) throws {
107243 try assertOutput (
0 commit comments