]> git.lizzy.rs Git - rust.git/blob - src/librustc_parse/lexer/mod.rs
Auto merge of #68241 - ecstatic-morse:unified-dataflow-impls, r=pnkfelix
[rust.git] / src / librustc_parse / lexer / mod.rs
1 use rustc_data_structures::sync::Lrc;
2 use rustc_errors::{DiagnosticBuilder, FatalError};
3 use rustc_lexer::unescape;
4 use rustc_lexer::Base;
5 use rustc_session::parse::ParseSess;
6 use rustc_span::symbol::{sym, Symbol};
7 use rustc_span::{BytePos, Pos, Span};
8 use syntax::token::{self, Token, TokenKind};
9 use syntax::util::comments;
10
11 use log::debug;
12 use std::char;
13 use std::convert::TryInto;
14
15 mod tokentrees;
16 mod unescape_error_reporting;
17 mod unicode_chars;
18 use unescape_error_reporting::{emit_unescape_error, push_escaped_char};
19
20 #[derive(Clone, Debug)]
21 pub struct UnmatchedBrace {
22     pub expected_delim: token::DelimToken,
23     pub found_delim: Option<token::DelimToken>,
24     pub found_span: Span,
25     pub unclosed_span: Option<Span>,
26     pub candidate_span: Option<Span>,
27 }
28
29 pub struct StringReader<'a> {
30     sess: &'a ParseSess,
31     /// Initial position, read-only.
32     start_pos: BytePos,
33     /// The absolute offset within the source_map of the current character.
34     // FIXME(#64197): `pub` is needed by tests for now.
35     pub pos: BytePos,
36     /// Stop reading src at this index.
37     end_src_index: usize,
38     /// Source text to tokenize.
39     src: Lrc<String>,
40     override_span: Option<Span>,
41 }
42
43 impl<'a> StringReader<'a> {
44     pub fn new(
45         sess: &'a ParseSess,
46         source_file: Lrc<rustc_span::SourceFile>,
47         override_span: Option<Span>,
48     ) -> Self {
49         if source_file.src.is_none() {
50             sess.span_diagnostic
51                 .bug(&format!("cannot lex `source_file` without source: {}", source_file.name));
52         }
53
54         let src = (*source_file.src.as_ref().unwrap()).clone();
55
56         StringReader {
57             sess,
58             start_pos: source_file.start_pos,
59             pos: source_file.start_pos,
60             end_src_index: src.len(),
61             src,
62             override_span,
63         }
64     }
65
66     pub fn retokenize(sess: &'a ParseSess, mut span: Span) -> Self {
67         let begin = sess.source_map().lookup_byte_offset(span.lo());
68         let end = sess.source_map().lookup_byte_offset(span.hi());
69
70         // Make the range zero-length if the span is invalid.
71         if begin.sf.start_pos != end.sf.start_pos {
72             span = span.shrink_to_lo();
73         }
74
75         let mut sr = StringReader::new(sess, begin.sf, None);
76
77         // Seek the lexer to the right byte range.
78         sr.end_src_index = sr.src_index(span.hi());
79
80         sr
81     }
82
83     fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
84         self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
85     }
86
87     /// Returns the next token, including trivia like whitespace or comments.
88     ///
89     /// `Err(())` means that some errors were encountered, which can be
90     /// retrieved using `buffer_fatal_errors`.
91     pub fn next_token(&mut self) -> Token {
92         let start_src_index = self.src_index(self.pos);
93         let text: &str = &self.src[start_src_index..self.end_src_index];
94
95         if text.is_empty() {
96             let span = self.mk_sp(self.pos, self.pos);
97             return Token::new(token::Eof, span);
98         }
99
100         {
101             let is_beginning_of_file = self.pos == self.start_pos;
102             if is_beginning_of_file {
103                 if let Some(shebang_len) = rustc_lexer::strip_shebang(text) {
104                     let start = self.pos;
105                     self.pos = self.pos + BytePos::from_usize(shebang_len);
106
107                     let sym = self.symbol_from(start + BytePos::from_usize("#!".len()));
108                     let kind = token::Shebang(sym);
109
110                     let span = self.mk_sp(start, self.pos);
111                     return Token::new(kind, span);
112                 }
113             }
114         }
115
116         let token = rustc_lexer::first_token(text);
117
118         let start = self.pos;
119         self.pos = self.pos + BytePos::from_usize(token.len);
120
121         debug!("try_next_token: {:?}({:?})", token.kind, self.str_from(start));
122
123         // This could use `?`, but that makes code significantly (10-20%) slower.
124         // https://github.com/rust-lang/rust/issues/37939
125         let kind = self.cook_lexer_token(token.kind, start);
126
127         let span = self.mk_sp(start, self.pos);
128         Token::new(kind, span)
129     }
130
131     /// Report a fatal lexical error with a given span.
132     fn fatal_span(&self, sp: Span, m: &str) -> FatalError {
133         self.sess.span_diagnostic.span_fatal(sp, m)
134     }
135
136     /// Report a lexical error with a given span.
137     fn err_span(&self, sp: Span, m: &str) {
138         self.sess.span_diagnostic.struct_span_err(sp, m).emit();
139     }
140
141     /// Report a fatal error spanning [`from_pos`, `to_pos`).
142     fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> FatalError {
143         self.fatal_span(self.mk_sp(from_pos, to_pos), m)
144     }
145
146     /// Report a lexical error spanning [`from_pos`, `to_pos`).
147     fn err_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
148         self.err_span(self.mk_sp(from_pos, to_pos), m)
149     }
150
151     fn struct_span_fatal(
152         &self,
153         from_pos: BytePos,
154         to_pos: BytePos,
155         m: &str,
156     ) -> DiagnosticBuilder<'a> {
157         self.sess.span_diagnostic.struct_span_fatal(self.mk_sp(from_pos, to_pos), m)
158     }
159
160     fn struct_fatal_span_char(
161         &self,
162         from_pos: BytePos,
163         to_pos: BytePos,
164         m: &str,
165         c: char,
166     ) -> DiagnosticBuilder<'a> {
167         let mut m = m.to_string();
168         m.push_str(": ");
169         push_escaped_char(&mut m, c);
170
171         self.sess.span_diagnostic.struct_span_fatal(self.mk_sp(from_pos, to_pos), &m[..])
172     }
173
174     /// Turns simple `rustc_lexer::TokenKind` enum into a rich
175     /// `libsyntax::TokenKind`. This turns strings into interned
176     /// symbols and runs additional validation.
177     fn cook_lexer_token(&self, token: rustc_lexer::TokenKind, start: BytePos) -> TokenKind {
178         match token {
179             rustc_lexer::TokenKind::LineComment => {
180                 let string = self.str_from(start);
181                 // comments with only more "/"s are not doc comments
182                 let tok = if comments::is_line_doc_comment(string) {
183                     self.forbid_bare_cr(start, string, "bare CR not allowed in doc-comment");
184                     token::DocComment(Symbol::intern(string))
185                 } else {
186                     token::Comment
187                 };
188
189                 tok
190             }
191             rustc_lexer::TokenKind::BlockComment { terminated } => {
192                 let string = self.str_from(start);
193                 // block comments starting with "/**" or "/*!" are doc-comments
194                 // but comments with only "*"s between two "/"s are not
195                 let is_doc_comment = comments::is_block_doc_comment(string);
196
197                 if !terminated {
198                     let msg = if is_doc_comment {
199                         "unterminated block doc-comment"
200                     } else {
201                         "unterminated block comment"
202                     };
203                     let last_bpos = self.pos;
204                     self.fatal_span_(start, last_bpos, msg).raise();
205                 }
206
207                 let tok = if is_doc_comment {
208                     self.forbid_bare_cr(start, string, "bare CR not allowed in block doc-comment");
209                     token::DocComment(Symbol::intern(string))
210                 } else {
211                     token::Comment
212                 };
213
214                 tok
215             }
216             rustc_lexer::TokenKind::Whitespace => token::Whitespace,
217             rustc_lexer::TokenKind::Ident | rustc_lexer::TokenKind::RawIdent => {
218                 let is_raw_ident = token == rustc_lexer::TokenKind::RawIdent;
219                 let mut ident_start = start;
220                 if is_raw_ident {
221                     ident_start = ident_start + BytePos(2);
222                 }
223                 let sym = nfc_normalize(self.str_from(ident_start));
224                 if is_raw_ident {
225                     let span = self.mk_sp(start, self.pos);
226                     if !sym.can_be_raw() {
227                         self.err_span(span, &format!("`{}` cannot be a raw identifier", sym));
228                     }
229                     self.sess.raw_identifier_spans.borrow_mut().push(span);
230                 }
231                 token::Ident(sym, is_raw_ident)
232             }
233             rustc_lexer::TokenKind::Literal { kind, suffix_start } => {
234                 let suffix_start = start + BytePos(suffix_start as u32);
235                 let (kind, symbol) = self.cook_lexer_literal(start, suffix_start, kind);
236                 let suffix = if suffix_start < self.pos {
237                     let string = self.str_from(suffix_start);
238                     if string == "_" {
239                         self.sess
240                             .span_diagnostic
241                             .struct_span_warn(
242                                 self.mk_sp(suffix_start, self.pos),
243                                 "underscore literal suffix is not allowed",
244                             )
245                             .warn(
246                                 "this was previously accepted by the compiler but is \
247                                    being phased out; it will become a hard error in \
248                                    a future release!",
249                             )
250                             .note(
251                                 "see issue #42326 \
252                                  <https://github.com/rust-lang/rust/issues/42326> \
253                                  for more information",
254                             )
255                             .emit();
256                         None
257                     } else {
258                         Some(Symbol::intern(string))
259                     }
260                 } else {
261                     None
262                 };
263                 token::Literal(token::Lit { kind, symbol, suffix })
264             }
265             rustc_lexer::TokenKind::Lifetime { starts_with_number } => {
266                 // Include the leading `'` in the real identifier, for macro
267                 // expansion purposes. See #12512 for the gory details of why
268                 // this is necessary.
269                 let lifetime_name = self.str_from(start);
270                 if starts_with_number {
271                     self.err_span_(start, self.pos, "lifetimes cannot start with a number");
272                 }
273                 let ident = Symbol::intern(lifetime_name);
274                 token::Lifetime(ident)
275             }
276             rustc_lexer::TokenKind::Semi => token::Semi,
277             rustc_lexer::TokenKind::Comma => token::Comma,
278             rustc_lexer::TokenKind::Dot => token::Dot,
279             rustc_lexer::TokenKind::OpenParen => token::OpenDelim(token::Paren),
280             rustc_lexer::TokenKind::CloseParen => token::CloseDelim(token::Paren),
281             rustc_lexer::TokenKind::OpenBrace => token::OpenDelim(token::Brace),
282             rustc_lexer::TokenKind::CloseBrace => token::CloseDelim(token::Brace),
283             rustc_lexer::TokenKind::OpenBracket => token::OpenDelim(token::Bracket),
284             rustc_lexer::TokenKind::CloseBracket => token::CloseDelim(token::Bracket),
285             rustc_lexer::TokenKind::At => token::At,
286             rustc_lexer::TokenKind::Pound => token::Pound,
287             rustc_lexer::TokenKind::Tilde => token::Tilde,
288             rustc_lexer::TokenKind::Question => token::Question,
289             rustc_lexer::TokenKind::Colon => token::Colon,
290             rustc_lexer::TokenKind::Dollar => token::Dollar,
291             rustc_lexer::TokenKind::Eq => token::Eq,
292             rustc_lexer::TokenKind::Not => token::Not,
293             rustc_lexer::TokenKind::Lt => token::Lt,
294             rustc_lexer::TokenKind::Gt => token::Gt,
295             rustc_lexer::TokenKind::Minus => token::BinOp(token::Minus),
296             rustc_lexer::TokenKind::And => token::BinOp(token::And),
297             rustc_lexer::TokenKind::Or => token::BinOp(token::Or),
298             rustc_lexer::TokenKind::Plus => token::BinOp(token::Plus),
299             rustc_lexer::TokenKind::Star => token::BinOp(token::Star),
300             rustc_lexer::TokenKind::Slash => token::BinOp(token::Slash),
301             rustc_lexer::TokenKind::Caret => token::BinOp(token::Caret),
302             rustc_lexer::TokenKind::Percent => token::BinOp(token::Percent),
303
304             rustc_lexer::TokenKind::Unknown => {
305                 let c = self.str_from(start).chars().next().unwrap();
306                 let mut err =
307                     self.struct_fatal_span_char(start, self.pos, "unknown start of token", c);
308                 // FIXME: the lexer could be used to turn the ASCII version of unicode homoglyphs,
309                 // instead of keeping a table in `check_for_substitution`into the token. Ideally,
310                 // this should be inside `rustc_lexer`. However, we should first remove compound
311                 // tokens like `<<` from `rustc_lexer`, and then add fancier error recovery to it,
312                 // as there will be less overall work to do this way.
313                 let token = unicode_chars::check_for_substitution(self, start, c, &mut err)
314                     .unwrap_or_else(|| token::Unknown(self.symbol_from(start)));
315                 err.emit();
316                 token
317             }
318         }
319     }
320
321     fn cook_lexer_literal(
322         &self,
323         start: BytePos,
324         suffix_start: BytePos,
325         kind: rustc_lexer::LiteralKind,
326     ) -> (token::LitKind, Symbol) {
327         match kind {
328             rustc_lexer::LiteralKind::Char { terminated } => {
329                 if !terminated {
330                     self.fatal_span_(start, suffix_start, "unterminated character literal".into())
331                         .raise()
332                 }
333                 let content_start = start + BytePos(1);
334                 let content_end = suffix_start - BytePos(1);
335                 self.validate_char_escape(content_start, content_end);
336                 let id = self.symbol_from_to(content_start, content_end);
337                 (token::Char, id)
338             }
339             rustc_lexer::LiteralKind::Byte { terminated } => {
340                 if !terminated {
341                     self.fatal_span_(
342                         start + BytePos(1),
343                         suffix_start,
344                         "unterminated byte constant".into(),
345                     )
346                     .raise()
347                 }
348                 let content_start = start + BytePos(2);
349                 let content_end = suffix_start - BytePos(1);
350                 self.validate_byte_escape(content_start, content_end);
351                 let id = self.symbol_from_to(content_start, content_end);
352                 (token::Byte, id)
353             }
354             rustc_lexer::LiteralKind::Str { terminated } => {
355                 if !terminated {
356                     self.fatal_span_(start, suffix_start, "unterminated double quote string".into())
357                         .raise()
358                 }
359                 let content_start = start + BytePos(1);
360                 let content_end = suffix_start - BytePos(1);
361                 self.validate_str_escape(content_start, content_end);
362                 let id = self.symbol_from_to(content_start, content_end);
363                 (token::Str, id)
364             }
365             rustc_lexer::LiteralKind::ByteStr { terminated } => {
366                 if !terminated {
367                     self.fatal_span_(
368                         start + BytePos(1),
369                         suffix_start,
370                         "unterminated double quote byte string".into(),
371                     )
372                     .raise()
373                 }
374                 let content_start = start + BytePos(2);
375                 let content_end = suffix_start - BytePos(1);
376                 self.validate_byte_str_escape(content_start, content_end);
377                 let id = self.symbol_from_to(content_start, content_end);
378                 (token::ByteStr, id)
379             }
380             rustc_lexer::LiteralKind::RawStr { n_hashes, started, terminated } => {
381                 if !started {
382                     self.report_non_started_raw_string(start);
383                 }
384                 if !terminated {
385                     self.report_unterminated_raw_string(start, n_hashes)
386                 }
387                 let n_hashes: u16 = self.restrict_n_hashes(start, n_hashes);
388                 let n = u32::from(n_hashes);
389                 let content_start = start + BytePos(2 + n);
390                 let content_end = suffix_start - BytePos(1 + n);
391                 self.validate_raw_str_escape(content_start, content_end);
392                 let id = self.symbol_from_to(content_start, content_end);
393                 (token::StrRaw(n_hashes), id)
394             }
395             rustc_lexer::LiteralKind::RawByteStr { n_hashes, started, terminated } => {
396                 if !started {
397                     self.report_non_started_raw_string(start);
398                 }
399                 if !terminated {
400                     self.report_unterminated_raw_string(start, n_hashes)
401                 }
402                 let n_hashes: u16 = self.restrict_n_hashes(start, n_hashes);
403                 let n = u32::from(n_hashes);
404                 let content_start = start + BytePos(3 + n);
405                 let content_end = suffix_start - BytePos(1 + n);
406                 self.validate_raw_byte_str_escape(content_start, content_end);
407                 let id = self.symbol_from_to(content_start, content_end);
408                 (token::ByteStrRaw(n_hashes), id)
409             }
410             rustc_lexer::LiteralKind::Int { base, empty_int } => {
411                 if empty_int {
412                     self.err_span_(start, suffix_start, "no valid digits found for number");
413                     (token::Integer, sym::integer(0))
414                 } else {
415                     self.validate_int_literal(base, start, suffix_start);
416                     (token::Integer, self.symbol_from_to(start, suffix_start))
417                 }
418             }
419             rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
420                 if empty_exponent {
421                     let mut err = self.struct_span_fatal(
422                         start,
423                         self.pos,
424                         "expected at least one digit in exponent",
425                     );
426                     err.emit();
427                 }
428
429                 match base {
430                     Base::Hexadecimal => self.err_span_(
431                         start,
432                         suffix_start,
433                         "hexadecimal float literal is not supported",
434                     ),
435                     Base::Octal => {
436                         self.err_span_(start, suffix_start, "octal float literal is not supported")
437                     }
438                     Base::Binary => {
439                         self.err_span_(start, suffix_start, "binary float literal is not supported")
440                     }
441                     _ => (),
442                 }
443
444                 let id = self.symbol_from_to(start, suffix_start);
445                 (token::Float, id)
446             }
447         }
448     }
449
450     #[inline]
451     fn src_index(&self, pos: BytePos) -> usize {
452         (pos - self.start_pos).to_usize()
453     }
454
455     /// Slice of the source text from `start` up to but excluding `self.pos`,
456     /// meaning the slice does not include the character `self.ch`.
457     fn str_from(&self, start: BytePos) -> &str {
458         self.str_from_to(start, self.pos)
459     }
460
461     /// Creates a Symbol from a given offset to the current offset.
462     fn symbol_from(&self, start: BytePos) -> Symbol {
463         debug!("taking an ident from {:?} to {:?}", start, self.pos);
464         Symbol::intern(self.str_from(start))
465     }
466
467     /// As symbol_from, with an explicit endpoint.
468     fn symbol_from_to(&self, start: BytePos, end: BytePos) -> Symbol {
469         debug!("taking an ident from {:?} to {:?}", start, end);
470         Symbol::intern(self.str_from_to(start, end))
471     }
472
473     /// Slice of the source text spanning from `start` up to but excluding `end`.
474     fn str_from_to(&self, start: BytePos, end: BytePos) -> &str {
475         &self.src[self.src_index(start)..self.src_index(end)]
476     }
477
478     fn forbid_bare_cr(&self, start: BytePos, s: &str, errmsg: &str) {
479         let mut idx = 0;
480         loop {
481             idx = match s[idx..].find('\r') {
482                 None => break,
483                 Some(it) => idx + it + 1,
484             };
485             self.err_span_(start + BytePos(idx as u32 - 1), start + BytePos(idx as u32), errmsg);
486         }
487     }
488
489     fn report_non_started_raw_string(&self, start: BytePos) -> ! {
490         let bad_char = self.str_from(start).chars().last().unwrap();
491         self.struct_fatal_span_char(
492             start,
493             self.pos,
494             "found invalid character; only `#` is allowed \
495                  in raw string delimitation",
496             bad_char,
497         )
498         .emit();
499         FatalError.raise()
500     }
501
502     fn report_unterminated_raw_string(&self, start: BytePos, n_hashes: usize) -> ! {
503         let mut err = self.struct_span_fatal(start, start, "unterminated raw string");
504         err.span_label(self.mk_sp(start, start), "unterminated raw string");
505
506         if n_hashes > 0 {
507             err.note(&format!(
508                 "this raw string should be terminated with `\"{}`",
509                 "#".repeat(n_hashes as usize)
510             ));
511         }
512
513         err.emit();
514         FatalError.raise()
515     }
516
517     fn restrict_n_hashes(&self, start: BytePos, n_hashes: usize) -> u16 {
518         match n_hashes.try_into() {
519             Ok(n_hashes) => n_hashes,
520             Err(_) => {
521                 self.fatal_span_(
522                     start,
523                     self.pos,
524                     "too many `#` symbols: raw strings may be \
525                                   delimited by up to 65535 `#` symbols",
526                 )
527                 .raise();
528             }
529         }
530     }
531
532     fn validate_char_escape(&self, content_start: BytePos, content_end: BytePos) {
533         let lit = self.str_from_to(content_start, content_end);
534         if let Err((off, err)) = unescape::unescape_char(lit) {
535             emit_unescape_error(
536                 &self.sess.span_diagnostic,
537                 lit,
538                 self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
539                 unescape::Mode::Char,
540                 0..off,
541                 err,
542             )
543         }
544     }
545
546     fn validate_byte_escape(&self, content_start: BytePos, content_end: BytePos) {
547         let lit = self.str_from_to(content_start, content_end);
548         if let Err((off, err)) = unescape::unescape_byte(lit) {
549             emit_unescape_error(
550                 &self.sess.span_diagnostic,
551                 lit,
552                 self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
553                 unescape::Mode::Byte,
554                 0..off,
555                 err,
556             )
557         }
558     }
559
560     fn validate_str_escape(&self, content_start: BytePos, content_end: BytePos) {
561         let lit = self.str_from_to(content_start, content_end);
562         unescape::unescape_str(lit, &mut |range, c| {
563             if let Err(err) = c {
564                 emit_unescape_error(
565                     &self.sess.span_diagnostic,
566                     lit,
567                     self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
568                     unescape::Mode::Str,
569                     range,
570                     err,
571                 )
572             }
573         })
574     }
575
576     fn validate_raw_str_escape(&self, content_start: BytePos, content_end: BytePos) {
577         let lit = self.str_from_to(content_start, content_end);
578         unescape::unescape_raw_str(lit, &mut |range, c| {
579             if let Err(err) = c {
580                 emit_unescape_error(
581                     &self.sess.span_diagnostic,
582                     lit,
583                     self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
584                     unescape::Mode::Str,
585                     range,
586                     err,
587                 )
588             }
589         })
590     }
591
592     fn validate_raw_byte_str_escape(&self, content_start: BytePos, content_end: BytePos) {
593         let lit = self.str_from_to(content_start, content_end);
594         unescape::unescape_raw_byte_str(lit, &mut |range, c| {
595             if let Err(err) = c {
596                 emit_unescape_error(
597                     &self.sess.span_diagnostic,
598                     lit,
599                     self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
600                     unescape::Mode::ByteStr,
601                     range,
602                     err,
603                 )
604             }
605         })
606     }
607
608     fn validate_byte_str_escape(&self, content_start: BytePos, content_end: BytePos) {
609         let lit = self.str_from_to(content_start, content_end);
610         unescape::unescape_byte_str(lit, &mut |range, c| {
611             if let Err(err) = c {
612                 emit_unescape_error(
613                     &self.sess.span_diagnostic,
614                     lit,
615                     self.mk_sp(content_start - BytePos(1), content_end + BytePos(1)),
616                     unescape::Mode::ByteStr,
617                     range,
618                     err,
619                 )
620             }
621         })
622     }
623
624     fn validate_int_literal(&self, base: Base, content_start: BytePos, content_end: BytePos) {
625         let base = match base {
626             Base::Binary => 2,
627             Base::Octal => 8,
628             _ => return,
629         };
630         let s = self.str_from_to(content_start + BytePos(2), content_end);
631         for (idx, c) in s.char_indices() {
632             let idx = idx as u32;
633             if c != '_' && c.to_digit(base).is_none() {
634                 let lo = content_start + BytePos(2 + idx);
635                 let hi = content_start + BytePos(2 + idx + c.len_utf8() as u32);
636                 self.err_span_(lo, hi, &format!("invalid digit for a base {} literal", base));
637             }
638         }
639     }
640 }
641
642 pub fn nfc_normalize(string: &str) -> Symbol {
643     use unicode_normalization::{is_nfc_quick, IsNormalized, UnicodeNormalization};
644     match is_nfc_quick(string.chars()) {
645         IsNormalized::Yes => Symbol::intern(string),
646         _ => {
647             let normalized_str: String = string.chars().nfc().collect();
648             Symbol::intern(&normalized_str)
649         }
650     }
651 }