Skip to content

Commit 3a7632f

Browse files
committed
Improve nested names handling even in #if scopes
This also resolves #822 while at it
1 parent c235a0f commit 3a7632f

2 files changed

Lines changed: 167 additions & 5 deletions

File tree

‎Sources/SwiftExtract/SwiftTypes/SwiftParsedModuleSymbolTableBuilder.swift‎

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,37 @@ extension SwiftParsedModuleSymbolTableBuilder {
174174
parent: SwiftNominalTypeDeclaration
175175
) {
176176
for member in node.members {
177-
// Find any nested types within this nominal type and add them.
178-
if let nominalMember = member.decl.asNominal {
179-
self.handle(sourceFilePath: sourceFilePath, nominalTypeDecl: nominalMember, parent: parent)
180-
} else if let typeAliasMember = member.decl.as(TypeAliasDeclSyntax.self) {
181-
self.handle(sourceFilePath: sourceFilePath, typeAliasDecl: typeAliasMember, parent: parent)
177+
self.handle(sourceFilePath: sourceFilePath, memberDecl: member.decl, parent: parent)
178+
}
179+
}
180+
181+
/// Add a nested type declaration to the symbol table.
182+
package mutating func handle(
183+
sourceFilePath: String,
184+
memberDecl decl: DeclSyntax,
185+
parent: SwiftNominalTypeDeclaration
186+
) {
187+
if let nominalMember = decl.asNominal {
188+
self.handle(sourceFilePath: sourceFilePath, nominalTypeDecl: nominalMember, parent: parent)
189+
} else if let typeAliasMember = decl.as(TypeAliasDeclSyntax.self) {
190+
self.handle(sourceFilePath: sourceFilePath, typeAliasDecl: typeAliasMember, parent: parent)
191+
} else if let ifConfigNode = decl.as(IfConfigDeclSyntax.self) {
192+
let (clause, _) = ifConfigNode.activeClause(in: buildConfig)
193+
if let clause, let elements = clause.elements {
194+
switch elements {
195+
case .decls(let memberBlock):
196+
for memberItem in memberBlock {
197+
self.handle(sourceFilePath: sourceFilePath, memberDecl: memberItem.decl, parent: parent)
198+
}
199+
case .statements(let codeBlock):
200+
for codeItem in codeBlock {
201+
if let declNode = codeItem.item.as(DeclSyntax.self) {
202+
self.handle(sourceFilePath: sourceFilePath, memberDecl: declNode, parent: parent)
203+
}
204+
}
205+
default:
206+
break
207+
}
182208
}
183209
}
184210
}

‎Tests/JExtractSwiftTests/JNI/JNINestedTypesTests.swift‎

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)