Skip to content

Commit 0bcddfe

Browse files
committed
Normalize identifiers in librustc_parse.
1 parent b13d65a commit 0bcddfe

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

Cargo.lock

+6-2
Original file line numberDiff line numberDiff line change
@@ -3696,6 +3696,7 @@ dependencies = [
36963696
"smallvec 1.0.0",
36973697
"syntax",
36983698
"syntax_pos",
3699+
"unicode-normalization",
36993700
]
37003701

37013702
[[package]]
@@ -4913,9 +4914,12 @@ dependencies = [
49134914

49144915
[[package]]
49154916
name = "unicode-normalization"
4916-
version = "0.1.7"
4917+
version = "0.1.11"
49174918
source = "registry+https://github.com/rust-lang/crates.io-index"
4918-
checksum = "6a0180bc61fc5a987082bfa111f4cc95c4caff7f9799f3e46df09163a937aa25"
4919+
checksum = "b561e267b2326bb4cebfc0ef9e68355c7abe6c6f522aeac2f5bf95d56c59bdcf"
4920+
dependencies = [
4921+
"smallvec 1.0.0",
4922+
]
49194923

49204924
[[package]]
49214925
name = "unicode-segmentation"

src/librustc_parse/Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -20,3 +20,4 @@ rustc_error_codes = { path = "../librustc_error_codes" }
2020
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
2121
syntax_pos = { path = "../libsyntax_pos" }
2222
syntax = { path = "../libsyntax" }
23+
unicode-normalization = "0.1.11"

src/librustc_parse/lexer/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -220,8 +220,7 @@ impl<'a> StringReader<'a> {
220220
if is_raw_ident {
221221
ident_start = ident_start + BytePos(2);
222222
}
223-
// FIXME: perform NFKC normalization here. (Issue #2253)
224-
let sym = self.symbol_from(ident_start);
223+
let sym = self.nfc_symbol_from(ident_start);
225224
if is_raw_ident {
226225
let span = self.mk_sp(start, self.pos);
227226
if !sym.can_be_raw() {
@@ -470,6 +469,20 @@ impl<'a> StringReader<'a> {
470469
Symbol::intern(self.str_from_to(start, end))
471470
}
472471

472+
/// As symbol_from, with the text normalized into Unicode NFC form.
473+
fn nfc_symbol_from(&self, start: BytePos) -> Symbol {
474+
use unicode_normalization::{is_nfc_quick, IsNormalized, UnicodeNormalization};
475+
debug!("taking an normalized ident from {:?} to {:?}", start, self.pos);
476+
let sym = self.str_from(start);
477+
match is_nfc_quick(sym.chars()) {
478+
IsNormalized::Yes => Symbol::intern(sym),
479+
_ => {
480+
let sym_str: String = sym.chars().nfc().collect();
481+
Symbol::intern(&sym_str)
482+
}
483+
}
484+
}
485+
473486
/// Slice of the source text spanning from `start` up to but excluding `end`.
474487
fn str_from_to(&self, start: BytePos, end: BytePos) -> &str {
475488
&self.src[self.src_index(start)..self.src_index(end)]

0 commit comments

Comments
 (0)