Skip to content

Commit bc5963d

Browse files
authored
Rollup merge of rust-lang#67702 - crlf0710:normalize_ident2, r=petrochenkov
Add symbol normalization for proc_macro_server. Follow up for rust-lang#66670, finishing the first bullet point in rust-lang#55467. r? @petrochenkov
2 parents 89fbed9 + 8f84d9e commit bc5963d

File tree

2 files changed

+14
-15
lines changed

2 files changed

+14
-15
lines changed

Diff for: src/librustc_expand/proc_macro_server.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use crate::base::ExtCtxt;
22

3+
use rustc_parse::lexer::nfc_normalize;
34
use rustc_parse::{nt_to_tokenstream, parse_stream_from_source_str};
45
use syntax::ast;
56
use syntax::print::pprust;
@@ -327,6 +328,7 @@ impl Ident {
327328
}
328329
}
329330
fn new(sym: Symbol, is_raw: bool, span: Span) -> Ident {
331+
let sym = nfc_normalize(&sym.as_str());
330332
let string = sym.as_str();
331333
if !Self::is_valid(&string) {
332334
panic!("`{:?}` is not a valid identifier", string)

Diff for: src/librustc_parse/lexer/mod.rs

+12-15
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl<'a> StringReader<'a> {
220220
if is_raw_ident {
221221
ident_start = ident_start + BytePos(2);
222222
}
223-
let sym = self.nfc_symbol_from(ident_start);
223+
let sym = nfc_normalize(self.str_from(ident_start));
224224
if is_raw_ident {
225225
let span = self.mk_sp(start, self.pos);
226226
if !sym.can_be_raw() {
@@ -469,20 +469,6 @@ impl<'a> StringReader<'a> {
469469
Symbol::intern(self.str_from_to(start, end))
470470
}
471471

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-
486472
/// Slice of the source text spanning from `start` up to but excluding `end`.
487473
fn str_from_to(&self, start: BytePos, end: BytePos) -> &str {
488474
&self.src[self.src_index(start)..self.src_index(end)]
@@ -651,3 +637,14 @@ impl<'a> StringReader<'a> {
651637
}
652638
}
653639
}
640+
641+
pub fn nfc_normalize(string: &str) -> Symbol {
642+
use unicode_normalization::{is_nfc_quick, IsNormalized, UnicodeNormalization};
643+
match is_nfc_quick(string.chars()) {
644+
IsNormalized::Yes => Symbol::intern(string),
645+
_ => {
646+
let normalized_str: String = string.chars().nfc().collect();
647+
Symbol::intern(&normalized_str)
648+
}
649+
}
650+
}

0 commit comments

Comments
 (0)