1 use crate::lexer::unicode_chars::UNICODE_ARRAY;
2 use rustc_ast::ast::{self, AttrStyle};
3 use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind};
4 use rustc_ast::tokenstream::TokenStream;
5 use rustc_ast::util::unicode::contains_text_flow_control_chars;
7 error_code, Applicability, DiagnosticBuilder, ErrorGuaranteed, PResult, StashKey,
9 use rustc_lexer::unescape::{self, Mode};
10 use rustc_lexer::Cursor;
11 use rustc_lexer::{Base, DocStyle, RawStrError};
12 use rustc_session::lint::builtin::{
13 RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX, TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
15 use rustc_session::lint::BuiltinLintDiagnostics;
16 use rustc_session::parse::ParseSess;
17 use rustc_span::symbol::{sym, Symbol};
18 use rustc_span::{edition::Edition, BytePos, Pos, Span};
21 mod unescape_error_reporting;
24 use unescape_error_reporting::{emit_unescape_error, escaped_char};
26 // This type is used a lot. Make sure it doesn't unintentionally get bigger.
28 // This assertion is in this crate, rather than in `rustc_lexer`, because that
29 // crate cannot depend on `rustc_data_structures`.
30 #[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
31 rustc_data_structures::static_assert_size!(rustc_lexer::Token, 12);
33 #[derive(Clone, Debug)]
34 pub struct UnmatchedBrace {
35 pub expected_delim: Delimiter,
36 pub found_delim: Option<Delimiter>,
38 pub unclosed_span: Option<Span>,
39 pub candidate_span: Option<Span>,
42 pub(crate) fn parse_token_trees<'a>(
45 mut start_pos: BytePos,
46 override_span: Option<Span>,
47 ) -> (PResult<'a, TokenStream>, Vec<UnmatchedBrace>) {
48 // Skip `#!`, if present.
49 if let Some(shebang_len) = rustc_lexer::strip_shebang(src) {
50 src = &src[shebang_len..];
51 start_pos = start_pos + BytePos::from_usize(shebang_len);
54 let cursor = Cursor::new(src);
56 StringReader { sess, start_pos, pos: start_pos, src, cursor, override_span };
57 tokentrees::TokenTreesReader::parse_all_token_trees(string_reader)
60 struct StringReader<'a> {
62 /// Initial position, read-only.
64 /// The absolute offset within the source_map of the current character.
66 /// Source text to tokenize.
68 /// Cursor for getting lexer tokens.
70 override_span: Option<Span>,
73 impl<'a> StringReader<'a> {
74 fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
75 self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
78 /// Returns the next token, paired with a bool indicating if the token was
79 /// preceded by whitespace.
80 fn next_token(&mut self) -> (Token, bool) {
81 let mut preceded_by_whitespace = false;
83 // Skip trivial (whitespace & comments) tokens
85 let token = self.cursor.advance_token();
87 self.pos = self.pos + BytePos(token.len);
89 debug!("next_token: {:?}({:?})", token.kind, self.str_from(start));
91 // Now "cook" the token, converting the simple `rustc_lexer::TokenKind` enum into a
92 // rich `rustc_ast::TokenKind`. This turns strings into interned symbols and runs
93 // additional validation.
94 let kind = match token.kind {
95 rustc_lexer::TokenKind::LineComment { doc_style } => {
96 // Skip non-doc comments
97 let Some(doc_style) = doc_style else {
98 self.lint_unicode_text_flow(start);
99 preceded_by_whitespace = true;
103 // Opening delimiter of the length 3 is not included into the symbol.
104 let content_start = start + BytePos(3);
105 let content = self.str_from(content_start);
106 self.cook_doc_comment(content_start, content, CommentKind::Line, doc_style)
108 rustc_lexer::TokenKind::BlockComment { doc_style, terminated } => {
110 self.report_unterminated_block_comment(start, doc_style);
113 // Skip non-doc comments
114 let Some(doc_style) = doc_style else {
115 self.lint_unicode_text_flow(start);
116 preceded_by_whitespace = true;
120 // Opening delimiter of the length 3 and closing delimiter of the length 2
121 // are not included into the symbol.
122 let content_start = start + BytePos(3);
123 let content_end = self.pos - BytePos(if terminated { 2 } else { 0 });
124 let content = self.str_from_to(content_start, content_end);
125 self.cook_doc_comment(content_start, content, CommentKind::Block, doc_style)
127 rustc_lexer::TokenKind::Whitespace => {
128 preceded_by_whitespace = true;
131 rustc_lexer::TokenKind::Ident => {
132 let sym = nfc_normalize(self.str_from(start));
133 let span = self.mk_sp(start, self.pos);
134 self.sess.symbol_gallery.insert(sym, span);
135 token::Ident(sym, false)
137 rustc_lexer::TokenKind::RawIdent => {
138 let sym = nfc_normalize(self.str_from(start + BytePos(2)));
139 let span = self.mk_sp(start, self.pos);
140 self.sess.symbol_gallery.insert(sym, span);
141 if !sym.can_be_raw() {
142 self.err_span(span, &format!("`{}` cannot be a raw identifier", sym));
144 self.sess.raw_identifier_spans.borrow_mut().push(span);
145 token::Ident(sym, true)
147 rustc_lexer::TokenKind::UnknownPrefix => {
148 self.report_unknown_prefix(start);
149 let sym = nfc_normalize(self.str_from(start));
150 let span = self.mk_sp(start, self.pos);
151 self.sess.symbol_gallery.insert(sym, span);
152 token::Ident(sym, false)
154 rustc_lexer::TokenKind::InvalidIdent
155 // Do not recover an identifier with emoji if the codepoint is a confusable
156 // with a recoverable substitution token, like `➖`.
160 let sym = self.str_from(start);
161 sym.chars().count() == 1 && c == sym.chars().next().unwrap()
164 let sym = nfc_normalize(self.str_from(start));
165 let span = self.mk_sp(start, self.pos);
166 self.sess.bad_unicode_identifiers.borrow_mut().entry(sym).or_default()
168 token::Ident(sym, false)
170 rustc_lexer::TokenKind::Literal { kind, suffix_start } => {
171 let suffix_start = start + BytePos(suffix_start);
172 let (kind, symbol) = self.cook_lexer_literal(start, suffix_start, kind);
173 let suffix = if suffix_start < self.pos {
174 let string = self.str_from(suffix_start);
179 self.mk_sp(suffix_start, self.pos),
180 "underscore literal suffix is not allowed",
183 "this was previously accepted by the compiler but is \
184 being phased out; it will become a hard error in \
189 <https://github.com/rust-lang/rust/issues/42326> \
190 for more information",
195 Some(Symbol::intern(string))
200 token::Literal(token::Lit { kind, symbol, suffix })
202 rustc_lexer::TokenKind::Lifetime { starts_with_number } => {
203 // Include the leading `'` in the real identifier, for macro
204 // expansion purposes. See #12512 for the gory details of why
205 // this is necessary.
206 let lifetime_name = self.str_from(start);
207 if starts_with_number {
208 let span = self.mk_sp(start, self.pos);
209 let mut diag = self.sess.struct_err("lifetimes cannot start with a number");
211 diag.stash(span, StashKey::LifetimeIsChar);
213 let ident = Symbol::intern(lifetime_name);
214 token::Lifetime(ident)
216 rustc_lexer::TokenKind::Semi => token::Semi,
217 rustc_lexer::TokenKind::Comma => token::Comma,
218 rustc_lexer::TokenKind::Dot => token::Dot,
219 rustc_lexer::TokenKind::OpenParen => token::OpenDelim(Delimiter::Parenthesis),
220 rustc_lexer::TokenKind::CloseParen => token::CloseDelim(Delimiter::Parenthesis),
221 rustc_lexer::TokenKind::OpenBrace => token::OpenDelim(Delimiter::Brace),
222 rustc_lexer::TokenKind::CloseBrace => token::CloseDelim(Delimiter::Brace),
223 rustc_lexer::TokenKind::OpenBracket => token::OpenDelim(Delimiter::Bracket),
224 rustc_lexer::TokenKind::CloseBracket => token::CloseDelim(Delimiter::Bracket),
225 rustc_lexer::TokenKind::At => token::At,
226 rustc_lexer::TokenKind::Pound => token::Pound,
227 rustc_lexer::TokenKind::Tilde => token::Tilde,
228 rustc_lexer::TokenKind::Question => token::Question,
229 rustc_lexer::TokenKind::Colon => token::Colon,
230 rustc_lexer::TokenKind::Dollar => token::Dollar,
231 rustc_lexer::TokenKind::Eq => token::Eq,
232 rustc_lexer::TokenKind::Bang => token::Not,
233 rustc_lexer::TokenKind::Lt => token::Lt,
234 rustc_lexer::TokenKind::Gt => token::Gt,
235 rustc_lexer::TokenKind::Minus => token::BinOp(token::Minus),
236 rustc_lexer::TokenKind::And => token::BinOp(token::And),
237 rustc_lexer::TokenKind::Or => token::BinOp(token::Or),
238 rustc_lexer::TokenKind::Plus => token::BinOp(token::Plus),
239 rustc_lexer::TokenKind::Star => token::BinOp(token::Star),
240 rustc_lexer::TokenKind::Slash => token::BinOp(token::Slash),
241 rustc_lexer::TokenKind::Caret => token::BinOp(token::Caret),
242 rustc_lexer::TokenKind::Percent => token::BinOp(token::Percent),
244 rustc_lexer::TokenKind::Unknown | rustc_lexer::TokenKind::InvalidIdent => {
245 let c = self.str_from(start).chars().next().unwrap();
247 self.struct_err_span_char(start, self.pos, "unknown start of token", c);
248 // FIXME: the lexer could be used to turn the ASCII version of unicode
249 // homoglyphs, instead of keeping a table in `check_for_substitution`into the
250 // token. Ideally, this should be inside `rustc_lexer`. However, we should
251 // first remove compound tokens like `<<` from `rustc_lexer`, and then add
252 // fancier error recovery to it, as there will be less overall work to do this
254 let token = unicode_chars::check_for_substitution(self, start, c, &mut err);
256 err.help("source files must contain UTF-8 encoded text, unexpected null bytes might occur when a different encoding is used");
259 if let Some(token) = token {
262 preceded_by_whitespace = true;
266 rustc_lexer::TokenKind::Eof => token::Eof,
268 let span = self.mk_sp(start, self.pos);
269 return (Token::new(kind, span), preceded_by_whitespace);
273 /// Report a fatal lexical error with a given span.
274 fn fatal_span(&self, sp: Span, m: &str) -> ! {
275 self.sess.span_diagnostic.span_fatal(sp, m)
278 /// Report a lexical error with a given span.
279 fn err_span(&self, sp: Span, m: &str) {
280 self.sess.span_diagnostic.struct_span_err(sp, m).emit();
283 /// Report a fatal error spanning [`from_pos`, `to_pos`).
284 fn fatal_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> ! {
285 self.fatal_span(self.mk_sp(from_pos, to_pos), m)
288 /// Report a lexical error spanning [`from_pos`, `to_pos`).
289 fn err_span_(&self, from_pos: BytePos, to_pos: BytePos, m: &str) {
290 self.err_span(self.mk_sp(from_pos, to_pos), m)
293 fn struct_fatal_span_char(
299 ) -> DiagnosticBuilder<'a, !> {
302 .struct_span_fatal(self.mk_sp(from_pos, to_pos), &format!("{}: {}", m, escaped_char(c)))
305 fn struct_err_span_char(
311 ) -> DiagnosticBuilder<'a, ErrorGuaranteed> {
314 .struct_span_err(self.mk_sp(from_pos, to_pos), &format!("{}: {}", m, escaped_char(c)))
317 /// Detect usages of Unicode codepoints changing the direction of the text on screen and loudly
318 /// complain about it.
319 fn lint_unicode_text_flow(&self, start: BytePos) {
320 // Opening delimiter of the length 2 is not included into the comment text.
321 let content_start = start + BytePos(2);
322 let content = self.str_from(content_start);
323 if contains_text_flow_control_chars(content) {
324 let span = self.mk_sp(start, self.pos);
325 self.sess.buffer_lint_with_diagnostic(
326 &TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
329 "unicode codepoint changing visible direction of text present in comment",
330 BuiltinLintDiagnostics::UnicodeTextFlow(span, content.to_string()),
337 content_start: BytePos,
339 comment_kind: CommentKind,
342 if content.contains('\r') {
343 for (idx, _) in content.char_indices().filter(|&(_, c)| c == '\r') {
345 content_start + BytePos(idx as u32),
346 content_start + BytePos(idx as u32 + 1),
348 CommentKind::Line => "bare CR not allowed in doc-comment",
349 CommentKind::Block => "bare CR not allowed in block doc-comment",
355 let attr_style = match doc_style {
356 DocStyle::Outer => AttrStyle::Outer,
357 DocStyle::Inner => AttrStyle::Inner,
360 token::DocComment(comment_kind, attr_style, Symbol::intern(content))
363 fn cook_lexer_literal(
367 kind: rustc_lexer::LiteralKind,
368 ) -> (token::LitKind, Symbol) {
370 rustc_lexer::LiteralKind::Char { terminated } => {
372 self.sess.span_diagnostic.span_fatal_with_code(
373 self.mk_sp(start, end),
374 "unterminated character literal",
378 self.cook_quoted(token::Char, Mode::Char, start, end, 1, 1) // ' '
380 rustc_lexer::LiteralKind::Byte { terminated } => {
382 self.sess.span_diagnostic.span_fatal_with_code(
383 self.mk_sp(start + BytePos(1), end),
384 "unterminated byte constant",
388 self.cook_quoted(token::Byte, Mode::Byte, start, end, 2, 1) // b' '
390 rustc_lexer::LiteralKind::Str { terminated } => {
392 self.sess.span_diagnostic.span_fatal_with_code(
393 self.mk_sp(start, end),
394 "unterminated double quote string",
398 self.cook_quoted(token::Str, Mode::Str, start, end, 1, 1) // " "
400 rustc_lexer::LiteralKind::ByteStr { terminated } => {
402 self.sess.span_diagnostic.span_fatal_with_code(
403 self.mk_sp(start + BytePos(1), end),
404 "unterminated double quote byte string",
408 self.cook_quoted(token::ByteStr, Mode::ByteStr, start, end, 2, 1) // b" "
410 rustc_lexer::LiteralKind::RawStr { n_hashes } => {
411 if let Some(n_hashes) = n_hashes {
412 let n = u32::from(n_hashes);
413 let kind = token::StrRaw(n_hashes);
414 self.cook_quoted(kind, Mode::RawStr, start, end, 2 + n, 1 + n) // r##" "##
416 self.report_raw_str_error(start, 1);
419 rustc_lexer::LiteralKind::RawByteStr { n_hashes } => {
420 if let Some(n_hashes) = n_hashes {
421 let n = u32::from(n_hashes);
422 let kind = token::ByteStrRaw(n_hashes);
423 self.cook_quoted(kind, Mode::RawByteStr, start, end, 3 + n, 1 + n) // br##" "##
425 self.report_raw_str_error(start, 2);
428 rustc_lexer::LiteralKind::Int { base, empty_int } => {
432 .struct_span_err_with_code(
433 self.mk_sp(start, end),
434 "no valid digits found for number",
438 (token::Integer, sym::integer(0))
440 self.validate_int_literal(base, start, end);
441 (token::Integer, self.symbol_from_to(start, end))
444 rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
446 self.err_span_(start, self.pos, "expected at least one digit in exponent");
449 Base::Hexadecimal => {
450 self.err_span_(start, end, "hexadecimal float literal is not supported")
453 self.err_span_(start, end, "octal float literal is not supported")
456 self.err_span_(start, end, "binary float literal is not supported")
460 (token::Float, self.symbol_from_to(start, end))
466 fn src_index(&self, pos: BytePos) -> usize {
467 (pos - self.start_pos).to_usize()
470 /// Slice of the source text from `start` up to but excluding `self.pos`,
471 /// meaning the slice does not include the character `self.ch`.
472 fn str_from(&self, start: BytePos) -> &str {
473 self.str_from_to(start, self.pos)
476 /// As symbol_from, with an explicit endpoint.
477 fn symbol_from_to(&self, start: BytePos, end: BytePos) -> Symbol {
478 debug!("taking an ident from {:?} to {:?}", start, end);
479 Symbol::intern(self.str_from_to(start, end))
482 /// Slice of the source text spanning from `start` up to but excluding `end`.
483 fn str_from_to(&self, start: BytePos, end: BytePos) -> &str {
484 &self.src[self.src_index(start)..self.src_index(end)]
487 fn report_raw_str_error(&self, start: BytePos, prefix_len: u32) -> ! {
488 match rustc_lexer::validate_raw_str(self.str_from(start), prefix_len) {
489 Err(RawStrError::InvalidStarter { bad_char }) => {
490 self.report_non_started_raw_string(start, bad_char)
492 Err(RawStrError::NoTerminator { expected, found, possible_terminator_offset }) => self
493 .report_unterminated_raw_string(start, expected, possible_terminator_offset, found),
494 Err(RawStrError::TooManyDelimiters { found }) => {
495 self.report_too_many_hashes(start, found)
497 Ok(()) => panic!("no error found for supposedly invalid raw string literal"),
501 fn report_non_started_raw_string(&self, start: BytePos, bad_char: char) -> ! {
502 self.struct_fatal_span_char(
505 "found invalid character; only `#` is allowed in raw string delimitation",
511 fn report_unterminated_raw_string(
515 possible_offset: Option<u32>,
516 found_terminators: u32,
518 let mut err = self.sess.span_diagnostic.struct_span_fatal_with_code(
519 self.mk_sp(start, start),
520 "unterminated raw string",
524 err.span_label(self.mk_sp(start, start), "unterminated raw string");
528 "this raw string should be terminated with `\"{}`",
529 "#".repeat(n_hashes as usize)
533 if let Some(possible_offset) = possible_offset {
534 let lo = start + BytePos(possible_offset as u32);
535 let hi = lo + BytePos(found_terminators as u32);
536 let span = self.mk_sp(lo, hi);
539 "consider terminating the string here",
540 "#".repeat(n_hashes as usize),
541 Applicability::MaybeIncorrect,
548 fn report_unterminated_block_comment(&self, start: BytePos, doc_style: Option<DocStyle>) {
549 let msg = match doc_style {
550 Some(_) => "unterminated block doc-comment",
551 None => "unterminated block comment",
553 let last_bpos = self.pos;
554 let mut err = self.sess.span_diagnostic.struct_span_fatal_with_code(
555 self.mk_sp(start, last_bpos),
559 let mut nested_block_comment_open_idxs = vec![];
560 let mut last_nested_block_comment_idxs = None;
561 let mut content_chars = self.str_from(start).char_indices().peekable();
563 while let Some((idx, current_char)) = content_chars.next() {
564 match content_chars.peek() {
565 Some((_, '*')) if current_char == '/' => {
566 nested_block_comment_open_idxs.push(idx);
568 Some((_, '/')) if current_char == '*' => {
569 last_nested_block_comment_idxs =
570 nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
576 if let Some((nested_open_idx, nested_close_idx)) = last_nested_block_comment_idxs {
577 err.span_label(self.mk_sp(start, start + BytePos(2)), msg)
580 start + BytePos(nested_open_idx as u32),
581 start + BytePos(nested_open_idx as u32 + 2),
583 "...as last nested comment starts here, maybe you want to close this instead?",
587 start + BytePos(nested_close_idx as u32),
588 start + BytePos(nested_close_idx as u32 + 2),
590 "...and last nested comment terminates here.",
597 // RFC 3101 introduced the idea of (reserved) prefixes. As of Rust 2021,
598 // using a (unknown) prefix is an error. In earlier editions, however, they
599 // only result in a (allowed by default) lint, and are treated as regular
600 // identifier tokens.
601 fn report_unknown_prefix(&self, start: BytePos) {
602 let prefix_span = self.mk_sp(start, self.pos);
603 let prefix_str = self.str_from_to(start, self.pos);
604 let msg = format!("prefix `{}` is unknown", prefix_str);
606 let expn_data = prefix_span.ctxt().outer_expn_data();
608 if expn_data.edition >= Edition::Edition2021 {
609 // In Rust 2021, this is a hard error.
610 let mut err = self.sess.span_diagnostic.struct_span_err(prefix_span, &msg);
611 err.span_label(prefix_span, "unknown prefix");
612 if prefix_str == "rb" {
613 err.span_suggestion_verbose(
615 "use `br` for a raw byte string",
617 Applicability::MaybeIncorrect,
619 } else if expn_data.is_root() {
620 err.span_suggestion_verbose(
621 prefix_span.shrink_to_hi(),
622 "consider inserting whitespace here",
624 Applicability::MaybeIncorrect,
627 err.note("prefixed identifiers and literals are reserved since Rust 2021");
630 // Before Rust 2021, only emit a lint for migration.
631 self.sess.buffer_lint_with_diagnostic(
632 &RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
636 BuiltinLintDiagnostics::ReservedPrefix(prefix_span),
641 fn report_too_many_hashes(&self, start: BytePos, found: u32) -> ! {
646 "too many `#` symbols: raw strings may be delimited \
647 by up to 255 `#` symbols, but found {}",
655 kind: token::LitKind,
661 ) -> (token::LitKind, Symbol) {
662 let content_start = start + BytePos(prefix_len);
663 let content_end = end - BytePos(postfix_len);
664 let lit_content = self.str_from_to(content_start, content_end);
665 unescape::unescape_literal(lit_content, mode, &mut |range, result| {
666 // Here we only check for errors. The actual unescaping is done later.
667 if let Err(err) = result {
668 let span_with_quotes = self.mk_sp(start, end);
669 let (start, end) = (range.start as u32, range.end as u32);
670 let lo = content_start + BytePos(start);
671 let hi = lo + BytePos(end - start);
672 let span = self.mk_sp(lo, hi);
674 &self.sess.span_diagnostic,
684 (kind, Symbol::intern(lit_content))
687 fn validate_int_literal(&self, base: Base, content_start: BytePos, content_end: BytePos) {
688 let base = match base {
693 let s = self.str_from_to(content_start + BytePos(2), content_end);
694 for (idx, c) in s.char_indices() {
695 let idx = idx as u32;
696 if c != '_' && c.to_digit(base).is_none() {
697 let lo = content_start + BytePos(2 + idx);
698 let hi = content_start + BytePos(2 + idx + c.len_utf8() as u32);
699 self.err_span_(lo, hi, &format!("invalid digit for a base {} literal", base));
705 pub fn nfc_normalize(string: &str) -> Symbol {
706 use unicode_normalization::{is_nfc_quick, IsNormalized, UnicodeNormalization};
707 match is_nfc_quick(string.chars()) {
708 IsNormalized::Yes => Symbol::intern(string),
710 let normalized_str: String = string.chars().nfc().collect();
711 Symbol::intern(&normalized_str)