Use Swift-identifier escaping for file-derived bridge function names - #263
Merged
marcprux merged 1 commit intoJul 24, 2026
Merged
Conversation
marcprux
reviewed
Jul 24, 2026
| self.compactMap { ch -> String in | ||
| if ch == "_" || (ch.isASCII && (ch.isLetter || ch.isNumber)) { | ||
| return String(ch) | ||
| } else if let utf16 = ch.utf16.first { |
Member
There was a problem hiding this comment.
Don't we need to translate all the codepoints instead of just the first one?
Contributor
Author
There was a problem hiding this comment.
Yea I think you're right let me see
Follow-up to skiptools#262. That change escaped the Swift `@_cdecl` function name derived from a source file name via `cdeclEscaped`, but `cdeclEscaped` is a JNI-symbol escaper, not a Swift-identifier escaper — the two have different rules, so reusing it leaves two cases wrong: - `.` is preserved by `cdeclEscaped` (it is a separator callers handle), but a `.` is invalid in a Swift identifier. A file such as `Model.generated.swift` still produces an uncompilable declaration: func Model.generatedKt_Swift_i(...) // '.' is not valid - `_` is valid in a Swift identifier but `cdeclEscaped` mangles it to `_1`, so `Model_Extensions.swift` needlessly yields `Model_1ExtensionsKt_Swift_i` on the Swift side. Add a dedicated `swiftIdentifierEscaped` that leaves ASCII alphanumerics and `_` untouched and hex-escapes everything else, and apply it to the Swift function name only; the JNI symbol keeps `cdeclEscaped`. The `+` case from skiptools#262 is unchanged (both escapers map `+` to `_0002b`). Also corrects the `cdeclEscaped` doc comment, which claimed `.` -> `_` while the code preserves `.`. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
vincentborko
force-pushed
the
pr/escape-filename-swift-identifier
branch
from
July 24, 2026 15:10
781dcd8 to
1b80c85
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Follow-up to #262 (which closed #63). That change escaped the Swift
@_cdeclfunction name derived from a source file name viacdeclEscaped— butcdeclEscapedis a JNI-symbol escaper, not a Swift-identifier escaper. The two have different rules, and reusing the JNI one leaves two cases wrong:.is still broken.cdeclEscapedpreserves.(it treats it as a separator that callers replace), but.is invalid in a Swift identifier. A file likeModel.generated.swift(a common SwiftGen/Sourcery pattern) still produces an uncompilable declaration:_is needlessly mangled._is valid in a Swift identifier, butcdeclEscapedmangles it to_1. SoModel_Extensions.swiftyieldsModel_1ExtensionsKt_Swift_ion the Swift side, where a plainModel_ExtensionsKt_Swift_iis correct and cleaner.Fix
Add a dedicated
swiftIdentifierEscapedthat leaves ASCII alphanumerics and_untouched and hex-escapes everything else, and apply it to the Swift function name only. The JNI symbol keepscdeclEscaped(unchanged). The+case from #262 is unaffected — both escapers map+to_0002b, so the existing test passes as-is.Also corrects the
cdeclEscapeddoc comment, which claimed. and / -> _while the code actually preserves..Bridging decision
No bridging-surface change. Both escapers are pure string transforms; the JNI symbol (linkage) is untouched, only the Swift-side identifier derivation is corrected.
Testing
swift test(native XCTest + transpiled) — full SkipSyntax suite green (925 tests).+test (testBridgeFunctionNameEscapesInvalidFileNameCharacters) unchanged and passing.testBridgeFunctionNamePreservesUnderscoreInFileName, which pins the_case: the Swift function name keeps_, while the JNI symbol still mangles it to_1. Verified the test genuinely pins the fix by revertingtypeNametocdeclEscaped— the test then fails withModel_1ExtensionsKt_Swift_i.