1use diagnostics::make_errors_for_mismatched_closing_delims;
2use rustc_ast::ast::{self, AttrStyle};
3use rustc_ast::token::{self, CommentKind, Delimiter, IdentIsRaw, Token, TokenKind};
4use rustc_ast::tokenstream::TokenStream;
5use rustc_ast::util::unicode::{TEXT_FLOW_CONTROL_CHARS, contains_text_flow_control_chars};
6use rustc_errors::codes::*;
7use rustc_errors::{Applicability, Diag, DiagCtxtHandle, StashKey};
8use rustc_lexer::{
9 Base, Cursor, DocStyle, FrontmatterAllowed, LiteralKind, RawStrError, is_horizontal_whitespace,
10};
11use rustc_literal_escaper::{EscapeError, Mode, check_for_errors};
12use rustc_session::lint::BuiltinLintDiag;
13use rustc_session::lint::builtin::{
14 RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX, RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX,
15 TEXT_DIRECTION_CODEPOINT_IN_COMMENT, TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
16};
17use rustc_session::parse::ParseSess;
18use rustc_span::{BytePos, Pos, Span, Symbol, sym};
19use tracing::debug;
20
21use crate::errors;
22use crate::lexer::diagnostics::TokenTreeDiagInfo;
23use crate::lexer::unicode_chars::UNICODE_ARRAY;
24
25mod diagnostics;
26mod tokentrees;
27mod unescape_error_reporting;
28mod unicode_chars;
29
30use unescape_error_reporting::{emit_unescape_error, escaped_char};
31
32#[cfg(target_pointer_width = "64")]
37const _: [(); 12] = [(); ::std::mem::size_of::<rustc_lexer::Token>()];rustc_data_structures::static_assert_size!(rustc_lexer::Token, 12);
38
39const INVISIBLE_CHARACTERS: [char; 8] = [
40 '\u{200b}', '\u{200c}', '\u{2060}', '\u{2061}', '\u{2062}', '\u{00ad}', '\u{034f}', '\u{061c}',
41];
42
43#[derive(#[automatically_derived]
impl ::core::clone::Clone for UnmatchedDelim {
#[inline]
fn clone(&self) -> UnmatchedDelim {
UnmatchedDelim {
found_delim: ::core::clone::Clone::clone(&self.found_delim),
found_span: ::core::clone::Clone::clone(&self.found_span),
unclosed_span: ::core::clone::Clone::clone(&self.unclosed_span),
candidate_span: ::core::clone::Clone::clone(&self.candidate_span),
}
}
}Clone, #[automatically_derived]
impl ::core::fmt::Debug for UnmatchedDelim {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field4_finish(f,
"UnmatchedDelim", "found_delim", &self.found_delim, "found_span",
&self.found_span, "unclosed_span", &self.unclosed_span,
"candidate_span", &&self.candidate_span)
}
}Debug)]
44pub(crate) struct UnmatchedDelim {
45 pub found_delim: Option<Delimiter>,
46 pub found_span: Span,
47 pub unclosed_span: Option<Span>,
48 pub candidate_span: Option<Span>,
49}
50
51pub enum StripTokens {
53 ShebangAndFrontmatter,
55 Shebang,
60 Nothing,
65}
66
67pub(crate) fn lex_token_trees<'psess, 'src>(
68 psess: &'psess ParseSess,
69 mut src: &'src str,
70 mut start_pos: BytePos,
71 override_span: Option<Span>,
72 strip_tokens: StripTokens,
73) -> Result<TokenStream, Vec<Diag<'psess>>> {
74 match strip_tokens {
75 StripTokens::Shebang | StripTokens::ShebangAndFrontmatter => {
76 if let Some(shebang_len) = rustc_lexer::strip_shebang(src) {
77 src = &src[shebang_len..];
78 start_pos = start_pos + BytePos::from_usize(shebang_len);
79 }
80 }
81 StripTokens::Nothing => {}
82 }
83
84 let frontmatter_allowed = match strip_tokens {
85 StripTokens::ShebangAndFrontmatter => FrontmatterAllowed::Yes,
86 StripTokens::Shebang | StripTokens::Nothing => FrontmatterAllowed::No,
87 };
88
89 let cursor = Cursor::new(src, frontmatter_allowed);
90 let mut lexer = Lexer {
91 psess,
92 start_pos,
93 pos: start_pos,
94 src,
95 cursor,
96 override_span,
97 nbsp_is_whitespace: false,
98 last_lifetime: None,
99 token: Token::dummy(),
100 diag_info: TokenTreeDiagInfo::default(),
101 };
102 let res = lexer.lex_token_trees(false);
103
104 let mut unmatched_closing_delims: Vec<_> =
105 make_errors_for_mismatched_closing_delims(&lexer.diag_info.unmatched_delims, psess);
106
107 match res {
108 Ok((_open_spacing, stream)) => {
109 if unmatched_closing_delims.is_empty() {
110 Ok(stream)
111 } else {
112 Err(unmatched_closing_delims)
114 }
115 }
116 Err(errs) => {
117 unmatched_closing_delims.extend(errs);
120 Err(unmatched_closing_delims)
121 }
122 }
123}
124
125struct Lexer<'psess, 'src> {
126 psess: &'psess ParseSess,
127 start_pos: BytePos,
129 pos: BytePos,
131 src: &'src str,
133 cursor: Cursor<'src>,
135 override_span: Option<Span>,
136 nbsp_is_whitespace: bool,
140
141 last_lifetime: Option<Span>,
144
145 token: Token,
147
148 diag_info: TokenTreeDiagInfo,
149}
150
151impl<'psess, 'src> Lexer<'psess, 'src> {
152 fn dcx(&self) -> DiagCtxtHandle<'psess> {
153 self.psess.dcx()
154 }
155
156 fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
157 self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
158 }
159
160 fn next_token_from_cursor(&mut self) -> (Token, bool) {
163 let mut preceded_by_whitespace = false;
164 let mut swallow_next_invalid = 0;
165 loop {
167 let str_before = self.cursor.as_str();
168 let token = self.cursor.advance_token();
169 let start = self.pos;
170 self.pos = self.pos + BytePos(token.len);
171
172 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/lexer/mod.rs:172",
"rustc_parse::lexer", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/lexer/mod.rs"),
::tracing_core::__macro_support::Option::Some(172u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::lexer"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("next_token: {0:?}({1:?})",
token.kind, self.str_from(start)) as &dyn Value))])
});
} else { ; }
};debug!("next_token: {:?}({:?})", token.kind, self.str_from(start));
173
174 if let rustc_lexer::TokenKind::Semi
175 | rustc_lexer::TokenKind::LineComment { .. }
176 | rustc_lexer::TokenKind::BlockComment { .. }
177 | rustc_lexer::TokenKind::CloseParen
178 | rustc_lexer::TokenKind::CloseBrace
179 | rustc_lexer::TokenKind::CloseBracket = token.kind
180 {
181 self.last_lifetime = None;
184 }
185
186 let kind = match token.kind {
190 rustc_lexer::TokenKind::LineComment { doc_style } => {
191 let Some(doc_style) = doc_style else {
193 self.lint_unicode_text_flow(start);
194 preceded_by_whitespace = true;
195 continue;
196 };
197
198 let content_start = start + BytePos(3);
200 let content = self.str_from(content_start);
201 self.lint_doc_comment_unicode_text_flow(start, content);
202 self.cook_doc_comment(content_start, content, CommentKind::Line, doc_style)
203 }
204 rustc_lexer::TokenKind::BlockComment { doc_style, terminated } => {
205 if !terminated {
206 self.report_unterminated_block_comment(start, doc_style);
207 }
208
209 let Some(doc_style) = doc_style else {
211 self.lint_unicode_text_flow(start);
212 preceded_by_whitespace = true;
213 continue;
214 };
215
216 let content_start = start + BytePos(3);
219 let content_end = self.pos - BytePos(if terminated { 2 } else { 0 });
220 let content = self.str_from_to(content_start, content_end);
221 self.lint_doc_comment_unicode_text_flow(start, content);
222 self.cook_doc_comment(content_start, content, CommentKind::Block, doc_style)
223 }
224 rustc_lexer::TokenKind::Frontmatter { has_invalid_preceding_whitespace, invalid_infostring } => {
225 self.validate_frontmatter(start, has_invalid_preceding_whitespace, invalid_infostring);
226 preceded_by_whitespace = true;
227 continue;
228 }
229 rustc_lexer::TokenKind::Whitespace => {
230 preceded_by_whitespace = true;
231 continue;
232 }
233 rustc_lexer::TokenKind::Ident => self.ident(start),
234 rustc_lexer::TokenKind::RawIdent => {
235 let sym = nfc_normalize(self.str_from(start + BytePos(2)));
236 let span = self.mk_sp(start, self.pos);
237 self.psess.symbol_gallery.insert(sym, span);
238 if !sym.can_be_raw() {
239 self.dcx().emit_err(errors::CannotBeRawIdent { span, ident: sym });
240 }
241 self.psess.raw_identifier_spans.push(span);
242 token::Ident(sym, IdentIsRaw::Yes)
243 }
244 rustc_lexer::TokenKind::UnknownPrefix => {
245 self.report_unknown_prefix(start);
246 self.ident(start)
247 }
248 rustc_lexer::TokenKind::UnknownPrefixLifetime => {
249 self.report_unknown_prefix(start);
250 let lifetime_name = self.str_from(start);
254 self.last_lifetime = Some(self.mk_sp(start, start + BytePos(1)));
255 let ident = Symbol::intern(lifetime_name);
256 token::Lifetime(ident, IdentIsRaw::No)
257 }
258 rustc_lexer::TokenKind::InvalidIdent
259 if !UNICODE_ARRAY.iter().any(|&(c, _, _)| {
262 let sym = self.str_from(start);
263 sym.chars().count() == 1 && c == sym.chars().next().unwrap()
264 }) =>
265 {
266 let sym = nfc_normalize(self.str_from(start));
267 let span = self.mk_sp(start, self.pos);
268 self.psess
269 .bad_unicode_identifiers
270 .borrow_mut()
271 .entry(sym)
272 .or_default()
273 .push(span);
274 token::Ident(sym, IdentIsRaw::No)
275 }
276 rustc_lexer::TokenKind::Literal {
279 kind: kind @ (LiteralKind::CStr { .. } | LiteralKind::RawCStr { .. }),
280 suffix_start: _,
281 } if !self.mk_sp(start, self.pos).edition().at_least_rust_2021() => {
282 let prefix_len = match kind {
283 LiteralKind::CStr { .. } => 1,
284 LiteralKind::RawCStr { .. } => 2,
285 _ => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
286 };
287
288 let lit_start = start + BytePos(prefix_len);
291 self.pos = lit_start;
292 self.cursor = Cursor::new(&str_before[prefix_len as usize..], FrontmatterAllowed::No);
293 self.report_unknown_prefix(start);
294 let prefix_span = self.mk_sp(start, lit_start);
295 return (Token::new(self.ident(start), prefix_span), preceded_by_whitespace);
296 }
297 rustc_lexer::TokenKind::GuardedStrPrefix => {
298 self.maybe_report_guarded_str(start, str_before)
299 }
300 rustc_lexer::TokenKind::Literal { kind, suffix_start } => {
301 let suffix_start = start + BytePos(suffix_start);
302 let (kind, symbol) = self.cook_lexer_literal(start, suffix_start, kind);
303 let suffix = if suffix_start < self.pos {
304 let string = self.str_from(suffix_start);
305 if string == "_" {
306 self.dcx().emit_err(errors::UnderscoreLiteralSuffix {
307 span: self.mk_sp(suffix_start, self.pos),
308 });
309 None
310 } else {
311 Some(Symbol::intern(string))
312 }
313 } else {
314 None
315 };
316 self.lint_literal_unicode_text_flow(symbol, kind, self.mk_sp(start, self.pos), "literal");
317 token::Literal(token::Lit { kind, symbol, suffix })
318 }
319 rustc_lexer::TokenKind::Lifetime { starts_with_number } => {
320 let lifetime_name = nfc_normalize(self.str_from(start));
324 self.last_lifetime = Some(self.mk_sp(start, start + BytePos(1)));
325 if starts_with_number {
326 let span = self.mk_sp(start, self.pos);
327 self.dcx()
328 .struct_err("lifetimes cannot start with a number")
329 .with_span(span)
330 .stash(span, StashKey::LifetimeIsChar);
331 }
332 token::Lifetime(lifetime_name, IdentIsRaw::No)
333 }
334 rustc_lexer::TokenKind::RawLifetime => {
335 self.last_lifetime = Some(self.mk_sp(start, start + BytePos(1)));
336
337 let ident_start = start + BytePos(3);
338 let prefix_span = self.mk_sp(start, ident_start);
339
340 if prefix_span.at_least_rust_2021() {
341 if self.cursor.as_str().starts_with('\'') {
347 let lit_span = self.mk_sp(start, self.pos + BytePos(1));
348 let contents = self.str_from_to(start + BytePos(1), self.pos);
349 emit_unescape_error(
350 self.dcx(),
351 contents,
352 lit_span,
353 lit_span,
354 Mode::Char,
355 0..contents.len(),
356 EscapeError::MoreThanOneChar,
357 )
358 .expect("expected error");
359 }
360
361 let span = self.mk_sp(start, self.pos);
362
363 let lifetime_name_without_tick =
364 Symbol::intern(&self.str_from(ident_start));
365 if !lifetime_name_without_tick.can_be_raw() {
366 self.dcx().emit_err(
367 errors::CannotBeRawLifetime {
368 span,
369 ident: lifetime_name_without_tick
370 }
371 );
372 }
373
374 let mut lifetime_name =
376 String::with_capacity(lifetime_name_without_tick.as_str().len() + 1);
377 lifetime_name.push('\'');
378 lifetime_name += lifetime_name_without_tick.as_str();
379 let sym = nfc_normalize(&lifetime_name);
380
381 self.psess.raw_identifier_spans.push(span);
383
384 token::Lifetime(sym, IdentIsRaw::Yes)
385 } else {
386 self.psess.buffer_lint(
388 RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
389 prefix_span,
390 ast::CRATE_NODE_ID,
391 BuiltinLintDiag::RawPrefix(prefix_span),
392 );
393
394 let lt_start = start + BytePos(2);
396 self.pos = lt_start;
397 self.cursor = Cursor::new(&str_before[2 as usize..], FrontmatterAllowed::No);
398
399 let lifetime_name = nfc_normalize(self.str_from(start));
400 token::Lifetime(lifetime_name, IdentIsRaw::No)
401 }
402 }
403 rustc_lexer::TokenKind::Semi => token::Semi,
404 rustc_lexer::TokenKind::Comma => token::Comma,
405 rustc_lexer::TokenKind::Dot => token::Dot,
406 rustc_lexer::TokenKind::OpenParen => token::OpenParen,
407 rustc_lexer::TokenKind::CloseParen => token::CloseParen,
408 rustc_lexer::TokenKind::OpenBrace => token::OpenBrace,
409 rustc_lexer::TokenKind::CloseBrace => token::CloseBrace,
410 rustc_lexer::TokenKind::OpenBracket => token::OpenBracket,
411 rustc_lexer::TokenKind::CloseBracket => token::CloseBracket,
412 rustc_lexer::TokenKind::At => token::At,
413 rustc_lexer::TokenKind::Pound => token::Pound,
414 rustc_lexer::TokenKind::Tilde => token::Tilde,
415 rustc_lexer::TokenKind::Question => token::Question,
416 rustc_lexer::TokenKind::Colon => token::Colon,
417 rustc_lexer::TokenKind::Dollar => token::Dollar,
418 rustc_lexer::TokenKind::Eq => token::Eq,
419 rustc_lexer::TokenKind::Bang => token::Bang,
420 rustc_lexer::TokenKind::Lt => token::Lt,
421 rustc_lexer::TokenKind::Gt => token::Gt,
422 rustc_lexer::TokenKind::Minus => token::Minus,
423 rustc_lexer::TokenKind::And => token::And,
424 rustc_lexer::TokenKind::Or => token::Or,
425 rustc_lexer::TokenKind::Plus => token::Plus,
426 rustc_lexer::TokenKind::Star => token::Star,
427 rustc_lexer::TokenKind::Slash => token::Slash,
428 rustc_lexer::TokenKind::Caret => token::Caret,
429 rustc_lexer::TokenKind::Percent => token::Percent,
430
431 rustc_lexer::TokenKind::Unknown | rustc_lexer::TokenKind::InvalidIdent => {
432 if swallow_next_invalid > 0 {
434 swallow_next_invalid -= 1;
435 continue;
436 }
437 let mut it = self.str_from_to_end(start).chars();
438 let c = it.next().unwrap();
439 if c == '\u{00a0}' {
440 if self.nbsp_is_whitespace {
444 preceded_by_whitespace = true;
445 continue;
446 }
447 self.nbsp_is_whitespace = true;
448 }
449 let repeats = it.take_while(|c1| *c1 == c).count();
450 let (token, sugg) =
457 unicode_chars::check_for_substitution(self, start, c, repeats + 1);
458 self.dcx().emit_err(errors::UnknownTokenStart {
459 span: self.mk_sp(start, self.pos + Pos::from_usize(repeats * c.len_utf8())),
460 escaped: escaped_char(c),
461 sugg,
462 null: if c == '\x00' { Some(errors::UnknownTokenNull) } else { None },
463 invisible: if INVISIBLE_CHARACTERS.contains(&c) { Some(errors::InvisibleCharacter) } else { None },
464 repeat: if repeats > 0 {
465 swallow_next_invalid = repeats;
466 Some(errors::UnknownTokenRepeat { repeats })
467 } else {
468 None
469 },
470 });
471
472 if let Some(token) = token {
473 token
474 } else {
475 preceded_by_whitespace = true;
476 continue;
477 }
478 }
479 rustc_lexer::TokenKind::Eof => token::Eof,
480 };
481 let span = self.mk_sp(start, self.pos);
482 return (Token::new(kind, span), preceded_by_whitespace);
483 }
484 }
485
486 fn ident(&self, start: BytePos) -> TokenKind {
487 let sym = nfc_normalize(self.str_from(start));
488 let span = self.mk_sp(start, self.pos);
489 self.psess.symbol_gallery.insert(sym, span);
490 token::Ident(sym, IdentIsRaw::No)
491 }
492
493 fn lint_unicode_text_flow(&self, start: BytePos) {
496 let content_start = start + BytePos(2);
498 let content = self.str_from(content_start);
499 if contains_text_flow_control_chars(content) {
500 let span = self.mk_sp(start, self.pos);
501 self.psess.buffer_lint(
502 TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
503 span,
504 ast::CRATE_NODE_ID,
505 BuiltinLintDiag::UnicodeTextFlow(span, content.to_string()),
506 );
507 }
508 }
509
510 fn lint_doc_comment_unicode_text_flow(&mut self, start: BytePos, content: &str) {
511 if contains_text_flow_control_chars(content) {
512 self.report_text_direction_codepoint(
513 content,
514 self.mk_sp(start, self.pos),
515 0,
516 false,
517 "doc comment",
518 );
519 }
520 }
521
522 fn lint_literal_unicode_text_flow(
523 &mut self,
524 text: Symbol,
525 lit_kind: token::LitKind,
526 span: Span,
527 label: &'static str,
528 ) {
529 if !contains_text_flow_control_chars(text.as_str()) {
530 return;
531 }
532 let (padding, point_at_inner_spans) = match lit_kind {
533 token::LitKind::Str | token::LitKind::Char => (1, true),
535 token::LitKind::CStr => (2, true),
537 token::LitKind::StrRaw(n) => (n as u32 + 2, true),
539 token::LitKind::CStrRaw(n) => (n as u32 + 3, true),
541 token::LitKind::Err(_) => return,
543 _ => (0, false),
545 };
546 self.report_text_direction_codepoint(
547 text.as_str(),
548 span,
549 padding,
550 point_at_inner_spans,
551 label,
552 );
553 }
554
555 fn report_text_direction_codepoint(
556 &self,
557 text: &str,
558 span: Span,
559 padding: u32,
560 point_at_inner_spans: bool,
561 label: &str,
562 ) {
563 let spans: Vec<_> = text
565 .char_indices()
566 .filter_map(|(i, c)| {
567 TEXT_FLOW_CONTROL_CHARS.contains(&c).then(|| {
568 let lo = span.lo() + BytePos(i as u32 + padding);
569 (c, span.with_lo(lo).with_hi(lo + BytePos(c.len_utf8() as u32)))
570 })
571 })
572 .collect();
573
574 let label = label.to_string();
575 let count = spans.len();
576 let labels = point_at_inner_spans
577 .then_some(errors::HiddenUnicodeCodepointsDiagLabels { spans: spans.clone() });
578 let sub = if point_at_inner_spans && !spans.is_empty() {
579 errors::HiddenUnicodeCodepointsDiagSub::Escape { spans }
580 } else {
581 errors::HiddenUnicodeCodepointsDiagSub::NoEscape { spans }
582 };
583
584 self.psess.buffer_lint(
585 TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
586 span,
587 ast::CRATE_NODE_ID,
588 errors::HiddenUnicodeCodepointsDiag { label, count, span_label: span, labels, sub },
589 );
590 }
591
592 fn validate_frontmatter(
593 &self,
594 start: BytePos,
595 has_invalid_preceding_whitespace: bool,
596 invalid_infostring: bool,
597 ) {
598 let s = self.str_from(start);
599 let real_start = s.find("---").unwrap();
600 let frontmatter_opening_pos = BytePos(real_start as u32) + start;
601 let real_s = &s[real_start..];
602 let within = real_s.trim_start_matches('-');
603 let len_opening = real_s.len() - within.len();
604
605 let frontmatter_opening_end_pos = frontmatter_opening_pos + BytePos(len_opening as u32);
606 if has_invalid_preceding_whitespace {
607 let line_start =
608 BytePos(s[..real_start].rfind("\n").map_or(0, |i| i as u32 + 1)) + start;
609 let span = self.mk_sp(line_start, frontmatter_opening_end_pos);
610 let label_span = self.mk_sp(line_start, frontmatter_opening_pos);
611 self.dcx().emit_err(errors::FrontmatterInvalidOpeningPrecedingWhitespace {
612 span,
613 note_span: label_span,
614 });
615 }
616
617 let line_end = real_s.find('\n').unwrap_or(real_s.len());
618 if invalid_infostring {
619 let span = self.mk_sp(
620 frontmatter_opening_end_pos,
621 frontmatter_opening_pos + BytePos(line_end as u32),
622 );
623 self.dcx().emit_err(errors::FrontmatterInvalidInfostring { span });
624 }
625
626 let last_line_start = real_s.rfind('\n').map_or(line_end, |i| i + 1);
627
628 let content = &real_s[line_end..last_line_start];
629 if let Some(cr_offset) = content.find('\r') {
630 let cr_pos = start + BytePos((real_start + line_end + cr_offset) as u32);
631 let span = self.mk_sp(cr_pos, cr_pos + BytePos(1 as u32));
632 self.dcx().emit_err(errors::BareCrFrontmatter { span });
633 }
634
635 let last_line = &real_s[last_line_start..];
636 let last_line_trimmed = last_line.trim_start_matches(is_horizontal_whitespace);
637 let last_line_start_pos = frontmatter_opening_pos + BytePos(last_line_start as u32);
638
639 let frontmatter_span = self.mk_sp(frontmatter_opening_pos, self.pos);
640 self.psess.gated_spans.gate(sym::frontmatter, frontmatter_span);
641
642 if !last_line_trimmed.starts_with("---") {
643 let label_span = self.mk_sp(frontmatter_opening_pos, frontmatter_opening_end_pos);
644 self.dcx().emit_err(errors::FrontmatterUnclosed {
645 span: frontmatter_span,
646 note_span: label_span,
647 });
648 return;
649 }
650
651 if last_line_trimmed.len() != last_line.len() {
652 let line_end = last_line_start_pos + BytePos(last_line.len() as u32);
653 let span = self.mk_sp(last_line_start_pos, line_end);
654 let whitespace_end =
655 last_line_start_pos + BytePos((last_line.len() - last_line_trimmed.len()) as u32);
656 let label_span = self.mk_sp(last_line_start_pos, whitespace_end);
657 self.dcx().emit_err(errors::FrontmatterInvalidClosingPrecedingWhitespace {
658 span,
659 note_span: label_span,
660 });
661 }
662
663 let rest = last_line_trimmed.trim_start_matches('-');
664 let len_close = last_line_trimmed.len() - rest.len();
665 if len_close != len_opening {
666 let span = self.mk_sp(frontmatter_opening_pos, self.pos);
667 let opening = self.mk_sp(frontmatter_opening_pos, frontmatter_opening_end_pos);
668 let last_line_close_pos = last_line_start_pos + BytePos(len_close as u32);
669 let close = self.mk_sp(last_line_start_pos, last_line_close_pos);
670 self.dcx().emit_err(errors::FrontmatterLengthMismatch {
671 span,
672 opening,
673 close,
674 len_opening,
675 len_close,
676 });
677 }
678
679 if u8::try_from(len_opening).is_err() {
681 self.dcx().emit_err(errors::FrontmatterTooManyDashes { len_opening });
682 }
683
684 if !rest.trim_matches(is_horizontal_whitespace).is_empty() {
685 let span = self.mk_sp(last_line_start_pos, self.pos);
686 self.dcx().emit_err(errors::FrontmatterExtraCharactersAfterClose { span });
687 }
688 }
689
690 fn cook_doc_comment(
691 &self,
692 content_start: BytePos,
693 content: &str,
694 comment_kind: CommentKind,
695 doc_style: DocStyle,
696 ) -> TokenKind {
697 if content.contains('\r') {
698 for (idx, _) in content.char_indices().filter(|&(_, c)| c == '\r') {
699 let span = self.mk_sp(
700 content_start + BytePos(idx as u32),
701 content_start + BytePos(idx as u32 + 1),
702 );
703 let block = #[allow(non_exhaustive_omitted_patterns)] match comment_kind {
CommentKind::Block => true,
_ => false,
}matches!(comment_kind, CommentKind::Block);
704 self.dcx().emit_err(errors::CrDocComment { span, block });
705 }
706 }
707
708 let attr_style = match doc_style {
709 DocStyle::Outer => AttrStyle::Outer,
710 DocStyle::Inner => AttrStyle::Inner,
711 };
712
713 token::DocComment(comment_kind, attr_style, Symbol::intern(content))
714 }
715
716 fn cook_lexer_literal(
717 &self,
718 start: BytePos,
719 end: BytePos,
720 kind: rustc_lexer::LiteralKind,
721 ) -> (token::LitKind, Symbol) {
722 match kind {
723 rustc_lexer::LiteralKind::Char { terminated } => {
724 if !terminated {
725 let mut err = self
726 .dcx()
727 .struct_span_fatal(self.mk_sp(start, end), "unterminated character literal")
728 .with_code(E0762);
729 if let Some(lt_sp) = self.last_lifetime {
730 err.multipart_suggestion(
731 "if you meant to write a string literal, use double quotes",
732 <[_]>::into_vec(::alloc::boxed::box_new([(lt_sp, "\"".to_string()),
(self.mk_sp(start, start + BytePos(1)), "\"".to_string())]))vec![
733 (lt_sp, "\"".to_string()),
734 (self.mk_sp(start, start + BytePos(1)), "\"".to_string()),
735 ],
736 Applicability::MaybeIncorrect,
737 );
738 }
739 err.emit()
740 }
741 self.cook_quoted(token::Char, Mode::Char, start, end, 1, 1) }
743 rustc_lexer::LiteralKind::Byte { terminated } => {
744 if !terminated {
745 self.dcx()
746 .struct_span_fatal(
747 self.mk_sp(start + BytePos(1), end),
748 "unterminated byte constant",
749 )
750 .with_code(E0763)
751 .emit()
752 }
753 self.cook_quoted(token::Byte, Mode::Byte, start, end, 2, 1) }
755 rustc_lexer::LiteralKind::Str { terminated } => {
756 if !terminated {
757 self.dcx()
758 .struct_span_fatal(
759 self.mk_sp(start, end),
760 "unterminated double quote string",
761 )
762 .with_code(E0765)
763 .emit()
764 }
765 self.cook_quoted(token::Str, Mode::Str, start, end, 1, 1) }
767 rustc_lexer::LiteralKind::ByteStr { terminated } => {
768 if !terminated {
769 self.dcx()
770 .struct_span_fatal(
771 self.mk_sp(start + BytePos(1), end),
772 "unterminated double quote byte string",
773 )
774 .with_code(E0766)
775 .emit()
776 }
777 self.cook_quoted(token::ByteStr, Mode::ByteStr, start, end, 2, 1)
778 }
780 rustc_lexer::LiteralKind::CStr { terminated } => {
781 if !terminated {
782 self.dcx()
783 .struct_span_fatal(
784 self.mk_sp(start + BytePos(1), end),
785 "unterminated C string",
786 )
787 .with_code(E0767)
788 .emit()
789 }
790 self.cook_quoted(token::CStr, Mode::CStr, start, end, 2, 1) }
792 rustc_lexer::LiteralKind::RawStr { n_hashes } => {
793 if let Some(n_hashes) = n_hashes {
794 let n = u32::from(n_hashes);
795 let kind = token::StrRaw(n_hashes);
796 self.cook_quoted(kind, Mode::RawStr, start, end, 2 + n, 1 + n)
797 } else {
799 self.report_raw_str_error(start, 1);
800 }
801 }
802 rustc_lexer::LiteralKind::RawByteStr { n_hashes } => {
803 if let Some(n_hashes) = n_hashes {
804 let n = u32::from(n_hashes);
805 let kind = token::ByteStrRaw(n_hashes);
806 self.cook_quoted(kind, Mode::RawByteStr, start, end, 3 + n, 1 + n)
807 } else {
809 self.report_raw_str_error(start, 2);
810 }
811 }
812 rustc_lexer::LiteralKind::RawCStr { n_hashes } => {
813 if let Some(n_hashes) = n_hashes {
814 let n = u32::from(n_hashes);
815 let kind = token::CStrRaw(n_hashes);
816 self.cook_quoted(kind, Mode::RawCStr, start, end, 3 + n, 1 + n)
817 } else {
819 self.report_raw_str_error(start, 2);
820 }
821 }
822 rustc_lexer::LiteralKind::Int { base, empty_int } => {
823 let mut kind = token::Integer;
824 if empty_int {
825 let span = self.mk_sp(start, end);
826 let guar = self.dcx().emit_err(errors::NoDigitsLiteral { span });
827 kind = token::Err(guar);
828 } else if #[allow(non_exhaustive_omitted_patterns)] match base {
Base::Binary | Base::Octal => true,
_ => false,
}matches!(base, Base::Binary | Base::Octal) {
829 let base = base as u32;
830 let s = self.str_from_to(start + BytePos(2), end);
831 for (idx, c) in s.char_indices() {
832 let span = self.mk_sp(
833 start + BytePos::from_usize(2 + idx),
834 start + BytePos::from_usize(2 + idx + c.len_utf8()),
835 );
836 if c != '_' && c.to_digit(base).is_none() {
837 let guar =
838 self.dcx().emit_err(errors::InvalidDigitLiteral { span, base });
839 kind = token::Err(guar);
840 }
841 }
842 }
843 (kind, self.symbol_from_to(start, end))
844 }
845 rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
846 let mut kind = token::Float;
847 if empty_exponent {
848 let span = self.mk_sp(start, self.pos);
849 let guar = self.dcx().emit_err(errors::EmptyExponentFloat { span });
850 kind = token::Err(guar);
851 }
852 let base = match base {
853 Base::Hexadecimal => Some("hexadecimal"),
854 Base::Octal => Some("octal"),
855 Base::Binary => Some("binary"),
856 _ => None,
857 };
858 if let Some(base) = base {
859 let span = self.mk_sp(start, end);
860 let guar =
861 self.dcx().emit_err(errors::FloatLiteralUnsupportedBase { span, base });
862 kind = token::Err(guar)
863 }
864 (kind, self.symbol_from_to(start, end))
865 }
866 }
867 }
868
869 #[inline]
870 fn src_index(&self, pos: BytePos) -> usize {
871 (pos - self.start_pos).to_usize()
872 }
873
874 fn str_from(&self, start: BytePos) -> &'src str {
877 self.str_from_to(start, self.pos)
878 }
879
880 fn symbol_from_to(&self, start: BytePos, end: BytePos) -> Symbol {
882 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_parse/src/lexer/mod.rs:882",
"rustc_parse::lexer", ::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_parse/src/lexer/mod.rs"),
::tracing_core::__macro_support::Option::Some(882u32),
::tracing_core::__macro_support::Option::Some("rustc_parse::lexer"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("taking an ident from {0:?} to {1:?}",
start, end) as &dyn Value))])
});
} else { ; }
};debug!("taking an ident from {:?} to {:?}", start, end);
883 Symbol::intern(self.str_from_to(start, end))
884 }
885
886 fn str_from_to(&self, start: BytePos, end: BytePos) -> &'src str {
888 &self.src[self.src_index(start)..self.src_index(end)]
889 }
890
891 fn str_from_to_end(&self, start: BytePos) -> &'src str {
893 &self.src[self.src_index(start)..]
894 }
895
896 fn report_raw_str_error(&self, start: BytePos, prefix_len: u32) -> ! {
897 match rustc_lexer::validate_raw_str(self.str_from(start), prefix_len) {
898 Err(RawStrError::InvalidStarter { bad_char }) => {
899 self.report_non_started_raw_string(start, bad_char)
900 }
901 Err(RawStrError::NoTerminator { expected, found, possible_terminator_offset }) => self
902 .report_unterminated_raw_string(start, expected, possible_terminator_offset, found),
903 Err(RawStrError::TooManyDelimiters { found }) => {
904 self.report_too_many_hashes(start, found)
905 }
906 Ok(()) => {
::core::panicking::panic_fmt(format_args!("no error found for supposedly invalid raw string literal"));
}panic!("no error found for supposedly invalid raw string literal"),
907 }
908 }
909
910 fn report_non_started_raw_string(&self, start: BytePos, bad_char: char) -> ! {
911 self.dcx()
912 .struct_span_fatal(
913 self.mk_sp(start, self.pos),
914 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("found invalid character; only `#` is allowed in raw string delimitation: {0}",
escaped_char(bad_char)))
})format!(
915 "found invalid character; only `#` is allowed in raw string delimitation: {}",
916 escaped_char(bad_char)
917 ),
918 )
919 .emit()
920 }
921
922 fn report_unterminated_raw_string(
923 &self,
924 start: BytePos,
925 n_hashes: u32,
926 possible_offset: Option<u32>,
927 found_terminators: u32,
928 ) -> ! {
929 let mut err =
930 self.dcx().struct_span_fatal(self.mk_sp(start, start), "unterminated raw string");
931 err.code(E0748);
932 err.span_label(self.mk_sp(start, start), "unterminated raw string");
933
934 if n_hashes > 0 {
935 err.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this raw string should be terminated with `\"{0}`",
"#".repeat(n_hashes as usize)))
})format!(
936 "this raw string should be terminated with `\"{}`",
937 "#".repeat(n_hashes as usize)
938 ));
939 }
940
941 if let Some(possible_offset) = possible_offset {
942 let lo = start + BytePos(possible_offset);
943 let hi = lo + BytePos(found_terminators);
944 let span = self.mk_sp(lo, hi);
945 err.span_suggestion(
946 span,
947 "consider terminating the string here",
948 "#".repeat(n_hashes as usize),
949 Applicability::MaybeIncorrect,
950 );
951 }
952
953 err.emit()
954 }
955
956 fn report_unterminated_block_comment(&self, start: BytePos, doc_style: Option<DocStyle>) {
957 let msg = match doc_style {
958 Some(_) => "unterminated block doc-comment",
959 None => "unterminated block comment",
960 };
961 let last_bpos = self.pos;
962 let mut err = self.dcx().struct_span_fatal(self.mk_sp(start, last_bpos), msg);
963 err.code(E0758);
964 let mut nested_block_comment_open_idxs = ::alloc::vec::Vec::new()vec![];
965 let mut last_nested_block_comment_idxs = None;
966 let mut content_chars = self.str_from(start).char_indices().peekable();
967
968 while let Some((idx, current_char)) = content_chars.next() {
969 match content_chars.peek() {
970 Some((_, '*')) if current_char == '/' => {
971 nested_block_comment_open_idxs.push(idx);
972 }
973 Some((_, '/')) if current_char == '*' => {
974 last_nested_block_comment_idxs =
975 nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
976 }
977 _ => {}
978 };
979 }
980
981 if let Some((nested_open_idx, nested_close_idx)) = last_nested_block_comment_idxs {
982 err.span_label(self.mk_sp(start, start + BytePos(2)), msg)
983 .span_label(
984 self.mk_sp(
985 start + BytePos(nested_open_idx as u32),
986 start + BytePos(nested_open_idx as u32 + 2),
987 ),
988 "...as last nested comment starts here, maybe you want to close this instead?",
989 )
990 .span_label(
991 self.mk_sp(
992 start + BytePos(nested_close_idx as u32),
993 start + BytePos(nested_close_idx as u32 + 2),
994 ),
995 "...and last nested comment terminates here.",
996 );
997 }
998
999 err.emit();
1000 }
1001
1002 fn report_unknown_prefix(&self, start: BytePos) {
1007 let prefix_span = self.mk_sp(start, self.pos);
1008 let prefix = self.str_from_to(start, self.pos);
1009 let expn_data = prefix_span.ctxt().outer_expn_data();
1010
1011 if expn_data.edition.at_least_rust_2021() {
1012 let sugg = if prefix == "rb" {
1014 Some(errors::UnknownPrefixSugg::UseBr(prefix_span))
1015 } else if prefix == "rc" {
1016 Some(errors::UnknownPrefixSugg::UseCr(prefix_span))
1017 } else if expn_data.is_root() {
1018 if self.cursor.first() == '\''
1019 && let Some(start) = self.last_lifetime
1020 && self.cursor.third() != '\''
1021 && let end = self.mk_sp(self.pos, self.pos + BytePos(1))
1022 && !self.psess.source_map().is_multiline(start.until(end))
1023 {
1024 Some(errors::UnknownPrefixSugg::MeantStr { start, end })
1028 } else {
1029 Some(errors::UnknownPrefixSugg::Whitespace(prefix_span.shrink_to_hi()))
1030 }
1031 } else {
1032 None
1033 };
1034 self.dcx().emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
1035 } else {
1036 self.psess.buffer_lint(
1038 RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
1039 prefix_span,
1040 ast::CRATE_NODE_ID,
1041 BuiltinLintDiag::ReservedPrefix(prefix_span, prefix.to_string()),
1042 );
1043 }
1044 }
1045
1046 fn maybe_report_guarded_str(&mut self, start: BytePos, str_before: &'src str) -> TokenKind {
1053 let span = self.mk_sp(start, self.pos);
1054 let edition2024 = span.edition().at_least_rust_2024();
1055
1056 let space_pos = start + BytePos(1);
1057 let space_span = self.mk_sp(space_pos, space_pos);
1058
1059 let mut cursor = Cursor::new(str_before, FrontmatterAllowed::No);
1060
1061 let (is_string, span, unterminated) = match cursor.guarded_double_quoted_string() {
1062 Some(rustc_lexer::GuardedStr { n_hashes, terminated, token_len }) => {
1063 let end = start + BytePos(token_len);
1064 let span = self.mk_sp(start, end);
1065 let str_start = start + BytePos(n_hashes);
1066
1067 if edition2024 {
1068 self.cursor = cursor;
1069 self.pos = end;
1070 }
1071
1072 let unterminated = if terminated { None } else { Some(str_start) };
1073
1074 (true, span, unterminated)
1075 }
1076 None => {
1077 if true {
match (&self.str_from_to(start, start + BytePos(2)), &"##") {
(left_val, right_val) => {
if !(*left_val == *right_val) {
let kind = ::core::panicking::AssertKind::Eq;
::core::panicking::assert_failed(kind, &*left_val,
&*right_val, ::core::option::Option::None);
}
}
};
};debug_assert_eq!(self.str_from_to(start, start + BytePos(2)), "##");
1079
1080 (false, span, None)
1081 }
1082 };
1083 if edition2024 {
1084 if let Some(str_start) = unterminated {
1085 self.dcx()
1087 .struct_span_fatal(
1088 self.mk_sp(str_start, self.pos),
1089 "unterminated double quote string",
1090 )
1091 .with_code(E0765)
1092 .emit()
1093 }
1094
1095 let sugg = if span.from_expansion() {
1096 None
1097 } else {
1098 Some(errors::GuardedStringSugg(space_span))
1099 };
1100
1101 let err = if is_string {
1103 self.dcx().emit_err(errors::ReservedString { span, sugg })
1104 } else {
1105 self.dcx().emit_err(errors::ReservedMultihash { span, sugg })
1106 };
1107
1108 token::Literal(token::Lit {
1109 kind: token::Err(err),
1110 symbol: self.symbol_from_to(start, self.pos),
1111 suffix: None,
1112 })
1113 } else {
1114 self.psess.buffer_lint(
1116 RUST_2024_GUARDED_STRING_INCOMPATIBLE_SYNTAX,
1117 span,
1118 ast::CRATE_NODE_ID,
1119 BuiltinLintDiag::ReservedString { is_string, suggestion: space_span },
1120 );
1121
1122 self.pos = start + BytePos(1);
1125 self.cursor = Cursor::new(&str_before[1..], FrontmatterAllowed::No);
1126 token::Pound
1127 }
1128 }
1129
1130 fn report_too_many_hashes(&self, start: BytePos, num: u32) -> ! {
1131 self.dcx().emit_fatal(errors::TooManyHashes { span: self.mk_sp(start, self.pos), num });
1132 }
1133
1134 fn cook_quoted(
1135 &self,
1136 mut kind: token::LitKind,
1137 mode: Mode,
1138 start: BytePos,
1139 end: BytePos,
1140 prefix_len: u32,
1141 postfix_len: u32,
1142 ) -> (token::LitKind, Symbol) {
1143 let content_start = start + BytePos(prefix_len);
1144 let content_end = end - BytePos(postfix_len);
1145 let lit_content = self.str_from_to(content_start, content_end);
1146 check_for_errors(lit_content, mode, |range, err| {
1147 let span_with_quotes = self.mk_sp(start, end);
1148 let (start, end) = (range.start as u32, range.end as u32);
1149 let lo = content_start + BytePos(start);
1150 let hi = lo + BytePos(end - start);
1151 let span = self.mk_sp(lo, hi);
1152 let is_fatal = err.is_fatal();
1153 if let Some(guar) = emit_unescape_error(
1154 self.dcx(),
1155 lit_content,
1156 span_with_quotes,
1157 span,
1158 mode,
1159 range,
1160 err,
1161 ) {
1162 if !is_fatal { ::core::panicking::panic("assertion failed: is_fatal") };assert!(is_fatal);
1163 kind = token::Err(guar);
1164 }
1165 });
1166
1167 let sym = if !#[allow(non_exhaustive_omitted_patterns)] match kind {
token::Err(_) => true,
_ => false,
}matches!(kind, token::Err(_)) {
1170 Symbol::intern(lit_content)
1171 } else {
1172 self.symbol_from_to(start, end)
1173 };
1174 (kind, sym)
1175 }
1176}
1177
1178pub fn nfc_normalize(string: &str) -> Symbol {
1179 use unicode_normalization::{IsNormalized, UnicodeNormalization, is_nfc_quick};
1180 match is_nfc_quick(string.chars()) {
1181 IsNormalized::Yes => Symbol::intern(string),
1182 _ => {
1183 let normalized_str: String = string.chars().nfc().collect();
1184 Symbol::intern(&normalized_str)
1185 }
1186 }
1187}