# Pitch: Unicode Equivalence for Swift Source

**URL:** <https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576>\
**Category:** Pitches\
**Tags:** compiler, unicode\
**Created:** [March 13, 2019, 9:17pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576 "2019-03-13T21:17:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 13, 2019, 9:17pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/1 "2019-03-13T21:17:26Z")

</div>

### Unicode is Swifty...

Swift claims to support Unicode source. Many languages aim to be encoding agnostic by only using ASCII in the grammar and not attempting to interpret anything beyond that in comments and string literals. But Swift has gone much further and defined its grammar in terms of Unicode. The [formal definitions of identifiers and operators](https://docs.swift.org/swift-book/ReferenceManual/zzSummaryOfTheGrammar.html) are each a screen full of Unicode scalar references and demonstrate clear and deliberate support for advanced Unicode concepts like combining characters. Swift’s documentation even goes so far as to encourage use of Unicode. Unicode characters appear in identifiers in _The Swift Programming Language_ already on page 3, “The Basics” under “[Naming Constants and Variables](https://docs.swift.org/swift-book/LanguageGuide/TheBasics.html#ID313)” and recur in examples throughout the book.

### ...but Swift has dropped the ⚾.

Swift proudly touts support for Unicode equivalence across the documentation for its `String` type, but the compiler does not actually consider it.

As @michelf noted recently:

> [@String Comparison for Identifiers](https://forums.swift.org/t/string-comparison-for-identifiers/21558/1):
>
> [T]he Swift compiler does not normalize the source, and in particular it will not do normalization before comparing identifiers. Otherwise these two `é` identifiers would clash:
> 
> ```swift
> let é = "é" // é written as one unicode scalar
> let é = "é" // é written as "e" with a combining accent
> 
> ```

It addition to allowing clashes, it fails to resolve references the developer expects to match:

```swift
let café = "Queen’s Lane Coffee House"
print(café) // Error: “Use of unresolved identifier 'café'”

```

In addition to being surprising, this means that wherever Unicode actually gets used, two developers with different input methods may have difficulty using each other’s code.

Due to quirks of tooling, a single developer may write functions he later has trouble calling himself: A developer might write most of his code locally with a decomposition‐based input method—such as most Korean keyboards—and he writes it in Xcode, which is normalization agnostic. But during review on GitHub, he detects a typo in a function name and quickly fixes it there. Since GitHub’s interface checks everything in in NFC, he now—unknowingly—has a composed form function name. After a while he pulls the change to his local machine. When he types the function name with his keyboard in order to call it somewhere, the compiler tells him it does not exist. Confused, he tries it in several places and eventually concludes that the declaration must be buggy, so he erases and retypes it only to be puzzled by the fact that it now magically works. (Alternatively he gives up, reverts the commit and starts over, refactoring the code to avoid what he perceives as a compiler glitch.)

Known tools that are not really compatible with Swift’s current behaviour include:

- **GitHub** UI: Commits made through the web interface are in NFC; but commits made by the command line are unaltered.
- **Swift Forums** : Copied and pasted text becomes NFC, but text entered directly is unaltered.

### Possible Solutions

Essentially the Swift compiler needs to respect Unicode equivalence when it compares tokens. Several concrete routes to accomplishing this exist:

- A) Have the compiler test tokens for equality the same way as the Swift `String` type does.

- B) Have the compiler normalize each token after tokenization, with the exception of string‐like literals.

- C) Have the compiler parse a normalized copy of the entire file.

- D) Have the compiler normalize the actual on‐disk file.

(I personally favour option C, because it is future proof, consistent, and predictable. A and B are also a good options with only minimal surprises.)

### Source Compatibility

Properly handling Unicode equivalence will break source that contains equivalent identifiers that the developer intended to be distinct, such as @michelf’s example near the top. An important question is whether that sort of thing is a bug that should ever have compiled in the first place—which is how I would qualify it.

Option C would also change the semantics of combinations like `let x = "écrire".unicodeScalars.first!`. Those oft assumed semantics were never actually promised, so it falls in a similar category to [SE‐0241](https://forums.swift.org/t/se-0241-explicit-encoded-offsets-for-string-indices/19929) about `encodedOffset`. It only breaks misuse, but there is a lot of misuse. So at the very least, migration aids should be considered.

### ABI Stability

...uh oh... Can someone more knowledgeable tell me whether name mangling and such already handles equivalence or if this whole pitch goes 💥 right here and Swift in Unicode is doomed for ever?

CC: @Michael_Ilseman

### Immediate Mitigation on a Per‐Project Basis

Projects which extensively use Unicode can defend against this to some degree by scripting normalization of all project files before build and at check in.† Then at least your project is internally consistent and the risk is much lower. But you will still run into issues if you try to import two packages which use opposite normalization forms. And SourceKit will still throw compiler errors at you while you type if your input method assumes the opposite normalization form to the rest of your package—though liberal use of autocomplete can reduce that somewhat.

* * *

_† For anyone interested, I tool I wrote, [Workspace](https://github.com/SDGGiesbrecht/Workspace#workspace), can concisely normalize packages: `$ workspace normalize`, though that feature is largely unadvertised because I never recognized its importance._

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 13, 2019, 9:22pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/2 "2019-03-13T21:22:06Z")

</div>

Technically normalizing unicode names could break ABI, but I think we should clearly do it anyway. I see the lack of normalization as a longstanding bug in the compiler.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 13, 2019, 9:31pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/3 "2019-03-13T21:31:23Z")

</div>

...hmm...

It will not affect the ABI of anything ASCII, since that is static under all normalization. At least the API surface area of the standard, core and system libraries restricts itself to ASCII at the moment, right?. Are there internals that don’t? If not, then this would be theoretically ABI‐breaking, but break nothing in practice, since nothing that is declared ABI‐stable uses the affected functionality?

Only thinking out loud. None of those are facts I am absolutely sure of.

---

<div class="post-metadata">

**Author:** ![jrose](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/jrose/32/408_2.png) [@jrose](https://forums.swift.org/u/jrose)\
**Post date:** [March 13, 2019, 9:43pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/4 "2019-03-13T21:43:40Z")

</div>

Normalizing Unicode names could lose user data in NSCoding archives. :-(

I _do_ think we should normalize for typo-correction purposes, but I don't think it's worth slowing down the compiler for something that most users will not encounter anyway.

EDIT: a programming language is by nature a parseable format, and while it's a parseable format _for humans_ I feel like it's valid to be stricter about it than text. We don't want to start accepting U+037E GREEK QUESTION MARK as a statement delimiter even though its canonical form is a semicolon.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 13, 2019, 9:47pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/5 "2019-03-13T21:47:04Z")

</div>

Right, we can fix this because none of the core libraries are using identifiers with multiple representations. Note that there are several different normalizations we could use; ideally we would use the most compact, but it might be more prudent to use a normalization that matches what Xcode (sorry for the platform bias, but it probably needs to be Xcode) currently outputs.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 13, 2019, 9:50pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/6 "2019-03-13T21:50:59Z")

</div>

ASCII sequences are always canonical, so I wouldn't expect this to significantly slow down the compiler; we can very cheaply remember during identifier-lexing whether we saw a non-ASCII character, and we can put redundant entries in the identifier table for non-canonical strings.

I agree that we don't necessarily have to normalize non-identifiers during lexing, although I'm not sure this would really be particularly problematic.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [March 13, 2019, 10:14pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/7 "2019-03-13T22:14:08Z")

</div>

This has been discussed extensively in a few prior threads. I would encourage you to read through them to get a sense of where things stand.

In short, there is a thorough set of rules already laid out in UAX#31 on how to normalize identifiers in programming languages. Several of us have written several versions of a proposal to adopt it, but each time it has failed because of issues with emoji. Recent versions of Unicode now have more robust classifications for emoji, so the proposal can be resurrected with better luck now, probably. No need to start from scratch; feel free to build on the work that we’ve already done.

All of this applies only to identifiers. Literals should _never_ be messed with by the compiler. That are, after all, supposed to be _literals_.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 13, 2019, 10:48pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/8 "2019-03-13T22:48:56Z")

</div>

Thanks. I had search, but I guess I picked the wrong search terms. Knowing there must be some threads and being more persistent allowed me to find several. I’ll post links here in chronological order for others who land here. (I actually haven’t read the threads yet. I will now.)

> [@Unicode identifiers & operators](https://forums.swift.org/t/unicode-identifiers-operators/4044):
>
> \*TL;DR:\* Swift 4 Stage 1 seeks to prioritize "Source stability features". Most source-breaking changes were done with in Swift 3; however, the categorization of Unicode characters into identifiers & operators was never thoroughly discussed on swift-evolution. This seems like it might be our last chance, and I think there are some big improvements to be had. I've gathered some information+thoughts into an early-stage pitch / pre-proposal. It doesn't really have a conclusion, so I'm hoping …

> [@\[Proposal\] Refining Identifier and Operator Symbology](https://forums.swift.org/t/proposal-refining-identifier-and-operator-symbology/4304):
>
> Dear Swift-Evolution community, A few of us have been preparing a proposal to refine the definitions of identifiers & operators. This includes some changes to the permitted Unicode characters. The latest (perhaps final?) draft is available here: We'd welcome your initial thoughts, and will probably submit a PR soon to the swift-evolution repo for a formal review. Full text follows below. —Jacob Bandes-Storch, Xiaodi Wu, Erica Sadun, Jonathan Shapiro Refining Identifier and Operator Sym…

> [@\[Draft\] Refining identifier and operator symbology (take 2)](https://forums.swift.org/t/draft-refining-identifier-and-operator-symbology-take-2/5210):
>
> As Stage 2 of Swift 4 evolution starts now, I'd like to share a revised proposal in draft form. It proposes a source-breaking change for \*rationalizing\* which characters are permitted in identifiers and which in operators. It's justified for this phase of Swift 4 because: - Existing grammar, in permitting invisible characters without security-minded restrictions, can be \*actively harmful.\* - A rationalized approach is \*superior\* to the current approach: by referencing Unicode standards, …

> [@A path forward on rationalizing unicode identifiers and operators](https://forums.swift.org/t/a-path-forward-on-rationalizing-unicode-identifiers-and-operators/6735):
>
> The core team recently met to discuss PR609 - Refining identifier and operator symbology: The proposal correctly observes that the partitioning of unicode codepoints into identifiers and operators is a mess in some cases. It really is an outright bug for slightly_smiling_face to be an identifier, but frowning to be an operator. That said, the proposal itself is complicated and is defined in terms of a bunch of unicode classes that may evolve in the “wrong way for Swift” in the future. T…

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 13, 2019, 11:37pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/9 "2019-03-13T23:37:08Z")

</div>

While the first one started with the same premise, the other threads seem to be much wider in scope and become largely about other issues like which characters should be operators and which should be identifier. None of that matters to me. I only care about the compiler correctly recognizing matching tokens. But I defer to those who have already done much heavier lifting. Thanks for the hard work, @xwu.

---

<div class="post-metadata">

**Author:** ![michelf](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/michelf/32/1180_2.png) [@michelf](https://forums.swift.org/u/michelf)\
**Post date:** [March 14, 2019, 12:57am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/10 "2019-03-14T00:57:14Z")

</div>

If we don't normalize and then we add a reflection APIs, we'll have to deal with the possibility of two distinct properties having names that are equal to each other when it comes to String equality. But then maybe backward compatibility requires it.

Here's an idea to sort-of deprecate non-normalized identifiers without forbidding them outright: make the compiler normalize all identifiers except for those in ``backticks``. This should make sure any desire for a weird normalization is written in a way that'll make people suspicious that something is going on. I believe this should be relatively easy to implement. The migrator could also automatically detect and migrate those symbols to whatever the developer chooses.

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 14, 2019, 12:59am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/11 "2019-03-14T00:59:44Z")

</div>

We would welcome a fix for the normalization-of-names issue. The rules about what exactly is an operator are a completely separate issue and should not block a normalization fix, which as you say is ABI-affecting and therefore should be fixed ASAP before there's a bunch of code relying on libraries with stable ABIs using inconsistent normalizations.

As a general matter, ABI issues can be fixed, you just have to be aware of the practical impact of the fix.

---

<div class="post-metadata">

**Author:** ![xwu](https://avatars.discourse-cdn.com/v4/letter/x/b487fb/32.png) [@xwu](https://forums.swift.org/u/xwu)\
**Post date:** [March 14, 2019, 1:49am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/12 "2019-03-14T01:49:52Z")

</div>

Since APIs that differ only in normalization form would arguably be a bug, would you consider that narrow change alone to require a proposal?

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 14, 2019, 2:24am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/13 "2019-03-14T02:24:32Z")

</div>

I wouldn't, but I can raise that question to the rest of the Core Team.

---

<div class="post-metadata">

**Author:** ![jawbroken](https://avatars.discourse-cdn.com/v4/letter/j/cc9497/32.png) [@jawbroken](https://forums.swift.org/u/jawbroken)\
**Post date:** [March 14, 2019, 3:22am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/14 "2019-03-14T03:22:49Z")

</div>

A targeted fix here just for identifiers would be great. I have no interest in normalising anything else, and would consider it a bug if string literals were normalised.

---

<div class="post-metadata">

**Author:** ![Chris\_Lattner3](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/chris_lattner3/32/348_2.png) [@Chris\_Lattner3](https://forums.swift.org/u/Chris_Lattner3)\
**Post date:** [March 17, 2019, 4:10am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/15 "2019-03-17T04:10:04Z")

</div>

> [@John\_McCall](#):
>
> Technically normalizing unicode names could break ABI, but I think we should clearly do it anyway. I see the lack of normalization as a longstanding bug in the compiler.

+1, completely agreed.

-Chris

---

<div class="post-metadata">

**Author:** ![Panajev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/panajev/32/1227_2.png) [@Panajev](https://forums.swift.org/u/Panajev)\
**Post date:** [March 17, 2019, 7:43am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/16 "2019-03-17T07:43:27Z")

</div>

Important enough to delay Swift 5.0 and iOS 12.2? It seems to me as the release date of these two should be open to s delsy to fit in this ABI breaking change you really really do not want people to rely on.  
Would be quite puzzled if that cannot happen (there are business driven decisions behind the next iOS point release, but getting the ABI right at the start seems to be a worthy goal).

---

<div class="post-metadata">

**Author:** ![Panajev](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/panajev/32/1227_2.png) [@Panajev](https://forums.swift.org/u/Panajev)\
**Post date:** [March 17, 2019, 7:50am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/17 "2019-03-17T07:50:56Z")

</div>

> [@SDGGiesbrecht](#):
>
> Swift claims to support Unicode source. Many languages aim to be encoding agnostic by only using ASCII in the grammar and not attempting to interpret anything beyond that in comments and string literals. But Swift has gone much further and defined its grammar in terms of Unicode

On the other side I fear this is ultimately a mistake and that although essentially ASCII only grammar seems not inclusive it does provide a simple easy to parse and easy to share solution: a lingua franca does have some advantages as Anglocentric as that may sound, but that is quite off topic on a boat already sailed :).

---

<div class="post-metadata">

**Author:** ![John\_McCall](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/john_mccall/32/1351_2.png) [@John\_McCall](https://forums.swift.org/u/John_McCall)\
**Post date:** [March 17, 2019, 5:41pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/18 "2019-03-17T17:41:00Z")

</div>

No, we cannot delay Swift releases over a relatively minor bug that we have no reason to think that we cannot fix in a future release.

---

<div class="post-metadata">

**Author:** ![dhoepfl](https://avatars.discourse-cdn.com/v4/letter/d/b5ac83/32.png) [@dhoepfl](https://forums.swift.org/u/dhoepfl)\
**Post date:** [March 21, 2019, 11:37am UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/19 "2019-03-21T11:37:34Z")

</div>

I, too, think that this is a bug and that the `backticks`-form should preserve encoding. I also see this bug as non-critical, so the following is more for later reference:

I think, due to character duplication (“Å” (U+00C5) vs. “Å” (U+212B)), the compiler has to use NFC internally. These characters differ in NFC, but are the same in NFD. Code points that do not exist in NFD should be disallowed outside `identifiers`.

It might be necessary to allow `\u{…}` inside backticked identifiers to allow NFD users to use all identifiers. Otherwise anyone using a NFD editor had no way of using a module that uses any of those characters if the module was written using NFC.

---

<div class="post-metadata">

**Author:** ![SDGGiesbrecht](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/sdggiesbrecht/32/4768_2.png) [@SDGGiesbrecht](https://forums.swift.org/u/SDGGiesbrecht)\
**Post date:** [March 21, 2019, 7:38pm UTC](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576/20 "2019-03-21T19:38:12Z")

</div>

> [@dhoepfl](#):
>
> (“Å” (U+00C5) vs. “Å” (U+212B) These characters differ in NFC, but are the same in NFD.

They are the same in either.

NFC always does NFD first†—decomposing and reordering (NFD) before it recomposes in a possibly different way.

In this case NFD transforms both into U+0041 + U+030A. NFC then recombines that into U+00C5.

Maybe you are confusing it with this fact: The first time NFD or NFC is performed it will “lose information” by removing the “distinction” between the ångström unit and the scandinavian letter Å. The ångström will never reappear in NF‐anything. But after initial normalization there is only one NFC representation for any NFD and vice versa, so no information is lost no matter how many times data gets converted back and forth.

* * *

_†Implementations may skip or refactor this step for performance reasons when it is known that doing so will not alter the result._

[Next page](https://forums.swift.org/t/pitch-unicode-equivalence-for-swift-source/21576.md?page=2)
