Skip to content

Commit

Permalink
Normalize identifiers in librustc_parse.
Browse files Browse the repository at this point in the history
  • Loading branch information
crlf0710 committed Nov 23, 2019
1 parent d902539 commit d5edcb4
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
8 changes: 6 additions & 2 deletions Cargo.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3753,6 +3753,7 @@ dependencies = [
"smallvec 1.0.0",
"syntax",
"syntax_pos",
"unicode-normalization",
]

[[package]]
Expand Down Expand Up @@ -4935,9 +4936,12 @@ dependencies = [

[[package]]
name = "unicode-normalization"
version = "0.1.7"
version = "0.1.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a0180bc61fc5a987082bfa111f4cc95c4caff7f9799f3e46df09163a937aa25"
checksum = "b561e267b2326bb4cebfc0ef9e68355c7abe6c6f522aeac2f5bf95d56c59bdcf"
dependencies = [
"smallvec 1.0.0",
]

[[package]]
name = "unicode-segmentation"
Expand Down
1 change: 1 addition & 0 deletions src/librustc_parse/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ rustc_lexer = { path = "../librustc_lexer" }
rustc_target = { path = "../librustc_target" }
smallvec = { version = "1.0", features = ["union", "may_dangle"] }
rustc_error_codes = { path = "../librustc_error_codes" }
unicode-normalization = "0.1.11"
17 changes: 15 additions & 2 deletions src/librustc_parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,7 @@ impl<'a> StringReader<'a> {
if is_raw_ident {
ident_start = ident_start + BytePos(2);
}
// FIXME: perform NFKC normalization here. (Issue #2253)
let sym = self.symbol_from(ident_start);
let sym = self.nfc_symbol_from(ident_start);
if is_raw_ident {
let span = self.mk_sp(start, self.pos);
if !sym.can_be_raw() {
Expand Down Expand Up @@ -466,6 +465,20 @@ impl<'a> StringReader<'a> {
Symbol::intern(self.str_from_to(start, end))
}

/// As symbol_from, with the text normalized into Unicode NFC form.
fn nfc_symbol_from(&self, start: BytePos) -> Symbol {
use unicode_normalization::{is_nfc_quick, IsNormalized, UnicodeNormalization};
debug!("taking an normalized ident from {:?} to {:?}", start, self.pos);
let sym = self.str_from(start);
match is_nfc_quick(sym.chars()) {
IsNormalized::Yes => Symbol::intern(sym),
_ => {
let sym_str: String = sym.chars().nfc().collect();
Symbol::intern(&sym_str)
}
}
}

/// Slice of the source text spanning from `start` up to but excluding `end`.
fn str_from_to(&self, start: BytePos, end: BytePos) -> &str
{
Expand Down

0 comments on commit d5edcb4

Please sign in to comment.