2023-04-28 15:28:52 +00:00
|
|
|
|
use std::ops::Range;
|
|
|
|
|
|
2023-01-29 13:37:05 +00:00
|
|
|
|
use crate::errors;
|
2021-09-09 17:05:03 +00:00
|
|
|
|
use crate::lexer::unicode_chars::UNICODE_ARRAY;
|
2023-02-22 10:30:35 +00:00
|
|
|
|
use crate::make_unclosed_delims_error;
|
2021-06-14 12:56:49 +00:00
|
|
|
|
use rustc_ast::ast::{self, AttrStyle};
|
2022-04-26 15:40:14 +03:00
|
|
|
|
use rustc_ast::token::{self, CommentKind, Delimiter, Token, TokenKind};
|
2022-09-21 16:38:28 +10:00
|
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2021-11-04 23:31:42 +01:00
|
|
|
|
use rustc_ast::util::unicode::contains_text_flow_control_chars;
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
|
use rustc_errors::{codes::*, Applicability, DiagCtxt, DiagnosticBuilder, StashKey};
|
2023-04-28 15:28:52 +00:00
|
|
|
|
use rustc_lexer::unescape::{self, EscapeError, Mode};
|
2020-09-03 15:37:03 +02:00
|
|
|
|
use rustc_lexer::{Base, DocStyle, RawStrError};
|
2023-07-16 18:59:05 +00:00
|
|
|
|
use rustc_lexer::{Cursor, LiteralKind};
|
2021-08-19 11:40:00 -07:00
|
|
|
|
use rustc_session::lint::builtin::{
|
2021-11-01 10:39:43 +01:00
|
|
|
|
RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX, TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
|
2021-08-19 11:40:00 -07:00
|
|
|
|
};
|
2021-06-14 12:56:49 +00:00
|
|
|
|
use rustc_session::lint::BuiltinLintDiagnostics;
|
2020-01-11 15:03:15 +01:00
|
|
|
|
use rustc_session::parse::ParseSess;
|
2024-02-14 19:09:25 +11:00
|
|
|
|
use rustc_span::symbol::Symbol;
|
2021-06-14 20:38:57 +00:00
|
|
|
|
use rustc_span::{edition::Edition, BytePos, Pos, Span};
|
2014-05-21 16:57:31 -07:00
|
|
|
|
|
2023-01-26 11:02:19 +08:00
|
|
|
|
mod diagnostics;
|
2017-01-12 23:32:00 +00:00
|
|
|
|
mod tokentrees;
|
2019-10-11 12:32:18 +02:00
|
|
|
|
mod unescape_error_reporting;
|
2015-11-15 02:37:49 +05:30
|
|
|
|
mod unicode_chars;
|
2020-03-28 01:46:20 -04:00
|
|
|
|
|
2021-01-23 12:48:31 -08:00
|
|
|
|
use unescape_error_reporting::{emit_unescape_error, escaped_char};
|
2014-05-21 16:57:31 -07:00
|
|
|
|
|
2022-07-27 14:21:08 +10:00
|
|
|
|
// This type is used a lot. Make sure it doesn't unintentionally get bigger.
|
|
|
|
|
//
|
|
|
|
|
// This assertion is in this crate, rather than in `rustc_lexer`, because that
|
|
|
|
|
// crate cannot depend on `rustc_data_structures`.
|
|
|
|
|
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
|
2022-07-27 13:59:30 +10:00
|
|
|
|
rustc_data_structures::static_assert_size!(rustc_lexer::Token, 12);
|
2022-07-27 14:21:08 +10:00
|
|
|
|
|
2019-01-27 21:04:50 -08:00
|
|
|
|
#[derive(Clone, Debug)]
|
2023-02-21 14:51:19 +00:00
|
|
|
|
pub struct UnmatchedDelim {
|
2022-04-26 15:40:14 +03:00
|
|
|
|
pub expected_delim: Delimiter,
|
|
|
|
|
pub found_delim: Option<Delimiter>,
|
2019-01-27 21:04:50 -08:00
|
|
|
|
pub found_span: Span,
|
|
|
|
|
pub unclosed_span: Option<Span>,
|
|
|
|
|
pub candidate_span: Option<Span>,
|
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 14:21:07 +11:00
|
|
|
|
pub(crate) fn parse_token_trees<'sess, 'src>(
|
|
|
|
|
sess: &'sess ParseSess,
|
|
|
|
|
mut src: &'src str,
|
2022-09-21 15:18:36 +10:00
|
|
|
|
mut start_pos: BytePos,
|
2020-09-03 15:37:03 +02:00
|
|
|
|
override_span: Option<Span>,
|
2024-01-11 14:22:44 +11:00
|
|
|
|
) -> Result<TokenStream, Vec<DiagnosticBuilder<'sess>>> {
|
2022-09-21 15:18:36 +10:00
|
|
|
|
// Skip `#!`, if present.
|
|
|
|
|
if let Some(shebang_len) = rustc_lexer::strip_shebang(src) {
|
|
|
|
|
src = &src[shebang_len..];
|
|
|
|
|
start_pos = start_pos + BytePos::from_usize(shebang_len);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-26 09:18:23 +10:00
|
|
|
|
let cursor = Cursor::new(src);
|
2023-01-14 10:34:06 -08:00
|
|
|
|
let string_reader = StringReader {
|
|
|
|
|
sess,
|
|
|
|
|
start_pos,
|
|
|
|
|
pos: start_pos,
|
|
|
|
|
src,
|
|
|
|
|
cursor,
|
|
|
|
|
override_span,
|
|
|
|
|
nbsp_is_whitespace: false,
|
|
|
|
|
};
|
2023-10-13 22:43:48 +00:00
|
|
|
|
let (stream, res, unmatched_delims) =
|
2023-02-22 10:30:35 +00:00
|
|
|
|
tokentrees::TokenTreesReader::parse_all_token_trees(string_reader);
|
2023-10-13 22:43:48 +00:00
|
|
|
|
match res {
|
2024-01-05 16:38:04 +11:00
|
|
|
|
Ok(()) if unmatched_delims.is_empty() => Ok(stream),
|
2023-02-22 10:30:35 +00:00
|
|
|
|
_ => {
|
2023-04-09 17:35:02 -04:00
|
|
|
|
// Return error if there are unmatched delimiters or unclosed delimiters.
|
2023-02-22 10:30:35 +00:00
|
|
|
|
// We emit delimiter mismatch errors first, then emit the unclosing delimiter mismatch
|
|
|
|
|
// because the delimiter mismatch is more likely to be the root cause of error
|
|
|
|
|
|
|
|
|
|
let mut buffer = Vec::with_capacity(1);
|
|
|
|
|
for unmatched in unmatched_delims {
|
2023-11-21 20:07:32 +01:00
|
|
|
|
if let Some(err) = make_unclosed_delims_error(unmatched, sess) {
|
2024-01-11 14:22:44 +11:00
|
|
|
|
buffer.push(err);
|
2023-02-22 10:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2023-10-13 22:43:48 +00:00
|
|
|
|
if let Err(errs) = res {
|
|
|
|
|
// Add unclosing delimiter or diff marker errors
|
|
|
|
|
for err in errs {
|
2024-01-11 14:22:44 +11:00
|
|
|
|
buffer.push(err);
|
2023-10-13 22:43:48 +00:00
|
|
|
|
}
|
2023-02-22 10:30:35 +00:00
|
|
|
|
}
|
|
|
|
|
Err(buffer)
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-09-03 15:37:03 +02:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 14:21:07 +11:00
|
|
|
|
struct StringReader<'sess, 'src> {
|
|
|
|
|
sess: &'sess ParseSess,
|
2019-07-31 19:55:01 +03:00
|
|
|
|
/// Initial position, read-only.
|
|
|
|
|
start_pos: BytePos,
|
|
|
|
|
/// The absolute offset within the source_map of the current character.
|
2020-05-14 20:02:40 +02:00
|
|
|
|
pos: BytePos,
|
2019-07-31 19:55:01 +03:00
|
|
|
|
/// Source text to tokenize.
|
2024-01-11 14:21:07 +11:00
|
|
|
|
src: &'src str,
|
2022-09-26 09:18:23 +10:00
|
|
|
|
/// Cursor for getting lexer tokens.
|
2024-01-11 14:21:07 +11:00
|
|
|
|
cursor: Cursor<'src>,
|
2019-05-12 19:55:16 +03:00
|
|
|
|
override_span: Option<Span>,
|
2023-01-14 10:34:06 -08:00
|
|
|
|
/// When a "unknown start of token: \u{a0}" has already been emitted earlier
|
|
|
|
|
/// in this file, it's safe to treat further occurrences of the non-breaking
|
|
|
|
|
/// space character as whitespace.
|
|
|
|
|
nbsp_is_whitespace: bool,
|
2017-03-15 00:22:48 +00:00
|
|
|
|
}
|
|
|
|
|
|
2024-01-11 14:21:07 +11:00
|
|
|
|
impl<'sess, 'src> StringReader<'sess, 'src> {
|
|
|
|
|
pub fn dcx(&self) -> &'sess DiagCtxt {
|
2023-12-18 21:14:02 +11:00
|
|
|
|
&self.sess.dcx
|
|
|
|
|
}
|
|
|
|
|
|
2017-03-28 05:32:43 +00:00
|
|
|
|
fn mk_sp(&self, lo: BytePos, hi: BytePos) -> Span {
|
2019-08-11 01:44:55 +03:00
|
|
|
|
self.override_span.unwrap_or_else(|| Span::with_root_ctxt(lo, hi))
|
2017-03-28 05:32:43 +00:00
|
|
|
|
}
|
2018-08-12 15:43:51 +02:00
|
|
|
|
|
2022-09-21 16:38:28 +10:00
|
|
|
|
/// Returns the next token, paired with a bool indicating if the token was
|
|
|
|
|
/// preceded by whitespace.
|
|
|
|
|
fn next_token(&mut self) -> (Token, bool) {
|
|
|
|
|
let mut preceded_by_whitespace = false;
|
2023-01-07 16:33:05 +00:00
|
|
|
|
let mut swallow_next_invalid = 0;
|
2020-08-31 19:00:49 +02:00
|
|
|
|
// Skip trivial (whitespace & comments) tokens
|
|
|
|
|
loop {
|
2023-07-16 18:59:05 +00:00
|
|
|
|
let str_before = self.cursor.as_str();
|
2022-09-26 13:06:15 +10:00
|
|
|
|
let token = self.cursor.advance_token();
|
2020-08-31 19:00:49 +02:00
|
|
|
|
let start = self.pos;
|
2022-07-27 13:59:30 +10:00
|
|
|
|
self.pos = self.pos + BytePos(token.len);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
2020-08-31 19:00:49 +02:00
|
|
|
|
debug!("next_token: {:?}({:?})", token.kind, self.str_from(start));
|
2019-05-06 11:53:40 +03:00
|
|
|
|
|
2022-09-26 12:12:58 +10:00
|
|
|
|
// Now "cook" the token, converting the simple `rustc_lexer::TokenKind` enum into a
|
|
|
|
|
// rich `rustc_ast::TokenKind`. This turns strings into interned symbols and runs
|
|
|
|
|
// additional validation.
|
|
|
|
|
let kind = match token.kind {
|
|
|
|
|
rustc_lexer::TokenKind::LineComment { doc_style } => {
|
|
|
|
|
// Skip non-doc comments
|
|
|
|
|
let Some(doc_style) = doc_style else {
|
|
|
|
|
self.lint_unicode_text_flow(start);
|
|
|
|
|
preceded_by_whitespace = true;
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Opening delimiter of the length 3 is not included into the symbol.
|
|
|
|
|
let content_start = start + BytePos(3);
|
|
|
|
|
let content = self.str_from(content_start);
|
|
|
|
|
self.cook_doc_comment(content_start, content, CommentKind::Line, doc_style)
|
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::BlockComment { doc_style, terminated } => {
|
|
|
|
|
if !terminated {
|
|
|
|
|
self.report_unterminated_block_comment(start, doc_style);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Skip non-doc comments
|
|
|
|
|
let Some(doc_style) = doc_style else {
|
|
|
|
|
self.lint_unicode_text_flow(start);
|
|
|
|
|
preceded_by_whitespace = true;
|
|
|
|
|
continue;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Opening delimiter of the length 3 and closing delimiter of the length 2
|
|
|
|
|
// are not included into the symbol.
|
|
|
|
|
let content_start = start + BytePos(3);
|
|
|
|
|
let content_end = self.pos - BytePos(if terminated { 2 } else { 0 });
|
|
|
|
|
let content = self.str_from_to(content_start, content_end);
|
|
|
|
|
self.cook_doc_comment(content_start, content, CommentKind::Block, doc_style)
|
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::Whitespace => {
|
|
|
|
|
preceded_by_whitespace = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::Ident => {
|
2023-07-25 09:24:12 +00:00
|
|
|
|
self.ident(start)
|
2020-08-31 19:00:49 +02:00
|
|
|
|
}
|
2022-09-26 12:12:58 +10:00
|
|
|
|
rustc_lexer::TokenKind::RawIdent => {
|
|
|
|
|
let sym = nfc_normalize(self.str_from(start + BytePos(2)));
|
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
|
self.sess.symbol_gallery.insert(sym, span);
|
|
|
|
|
if !sym.can_be_raw() {
|
2023-12-18 21:14:02 +11:00
|
|
|
|
self.dcx().emit_err(errors::CannotBeRawIdent { span, ident: sym });
|
2022-09-26 12:12:58 +10:00
|
|
|
|
}
|
2023-03-14 12:16:19 +00:00
|
|
|
|
self.sess.raw_identifier_spans.push(span);
|
2022-09-26 12:12:58 +10:00
|
|
|
|
token::Ident(sym, true)
|
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::UnknownPrefix => {
|
|
|
|
|
self.report_unknown_prefix(start);
|
2023-07-25 09:24:12 +00:00
|
|
|
|
self.ident(start)
|
2022-09-26 12:12:58 +10:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::InvalidIdent
|
|
|
|
|
// Do not recover an identifier with emoji if the codepoint is a confusable
|
|
|
|
|
// with a recoverable substitution token, like `➖`.
|
|
|
|
|
if !UNICODE_ARRAY
|
|
|
|
|
.iter()
|
|
|
|
|
.any(|&(c, _, _)| {
|
|
|
|
|
let sym = self.str_from(start);
|
|
|
|
|
sym.chars().count() == 1 && c == sym.chars().next().unwrap()
|
|
|
|
|
}) =>
|
|
|
|
|
{
|
|
|
|
|
let sym = nfc_normalize(self.str_from(start));
|
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
|
self.sess.bad_unicode_identifiers.borrow_mut().entry(sym).or_default()
|
|
|
|
|
.push(span);
|
|
|
|
|
token::Ident(sym, false)
|
|
|
|
|
}
|
2023-07-16 18:59:05 +00:00
|
|
|
|
// split up (raw) c string literals to an ident and a string literal when edition < 2021.
|
|
|
|
|
rustc_lexer::TokenKind::Literal {
|
|
|
|
|
kind: kind @ (LiteralKind::CStr { .. } | LiteralKind::RawCStr { .. }),
|
|
|
|
|
suffix_start: _,
|
|
|
|
|
} if !self.mk_sp(start, self.pos).edition().at_least_rust_2021() => {
|
|
|
|
|
let prefix_len = match kind {
|
|
|
|
|
LiteralKind::CStr { .. } => 1,
|
|
|
|
|
LiteralKind::RawCStr { .. } => 2,
|
|
|
|
|
_ => unreachable!(),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// reset the state so that only the prefix ("c" or "cr")
|
|
|
|
|
// was consumed.
|
|
|
|
|
let lit_start = start + BytePos(prefix_len);
|
|
|
|
|
self.pos = lit_start;
|
|
|
|
|
self.cursor = Cursor::new(&str_before[prefix_len as usize..]);
|
|
|
|
|
|
|
|
|
|
self.report_unknown_prefix(start);
|
|
|
|
|
let prefix_span = self.mk_sp(start, lit_start);
|
2023-07-25 09:24:12 +00:00
|
|
|
|
return (Token::new(self.ident(start), prefix_span), preceded_by_whitespace);
|
2023-07-16 18:59:05 +00:00
|
|
|
|
}
|
2022-09-26 12:12:58 +10:00
|
|
|
|
rustc_lexer::TokenKind::Literal { kind, suffix_start } => {
|
|
|
|
|
let suffix_start = start + BytePos(suffix_start);
|
|
|
|
|
let (kind, symbol) = self.cook_lexer_literal(start, suffix_start, kind);
|
|
|
|
|
let suffix = if suffix_start < self.pos {
|
|
|
|
|
let string = self.str_from(suffix_start);
|
|
|
|
|
if string == "_" {
|
|
|
|
|
self.sess
|
2023-12-17 22:25:47 +11:00
|
|
|
|
.dcx
|
2023-04-10 16:04:14 +01:00
|
|
|
|
.emit_err(errors::UnderscoreLiteralSuffix { span: self.mk_sp(suffix_start, self.pos) });
|
2022-09-26 12:12:58 +10:00
|
|
|
|
None
|
|
|
|
|
} else {
|
|
|
|
|
Some(Symbol::intern(string))
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
token::Literal(token::Lit { kind, symbol, suffix })
|
|
|
|
|
}
|
2023-04-10 06:52:18 +00:00
|
|
|
|
rustc_lexer::TokenKind::Lifetime { starts_with_number } => {
|
2022-09-26 12:12:58 +10:00
|
|
|
|
// Include the leading `'` in the real identifier, for macro
|
|
|
|
|
// expansion purposes. See #12512 for the gory details of why
|
|
|
|
|
// this is necessary.
|
|
|
|
|
let lifetime_name = self.str_from(start);
|
|
|
|
|
if starts_with_number {
|
2022-09-01 18:48:09 +00:00
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
2024-01-03 16:00:29 +11:00
|
|
|
|
self.dcx().struct_err("lifetimes cannot start with a number")
|
2024-01-09 09:08:49 +11:00
|
|
|
|
.with_span(span)
|
2024-01-03 16:00:29 +11:00
|
|
|
|
.stash(span, StashKey::LifetimeIsChar);
|
2022-09-26 12:12:58 +10:00
|
|
|
|
}
|
|
|
|
|
let ident = Symbol::intern(lifetime_name);
|
|
|
|
|
token::Lifetime(ident)
|
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::Semi => token::Semi,
|
|
|
|
|
rustc_lexer::TokenKind::Comma => token::Comma,
|
|
|
|
|
rustc_lexer::TokenKind::Dot => token::Dot,
|
|
|
|
|
rustc_lexer::TokenKind::OpenParen => token::OpenDelim(Delimiter::Parenthesis),
|
|
|
|
|
rustc_lexer::TokenKind::CloseParen => token::CloseDelim(Delimiter::Parenthesis),
|
|
|
|
|
rustc_lexer::TokenKind::OpenBrace => token::OpenDelim(Delimiter::Brace),
|
|
|
|
|
rustc_lexer::TokenKind::CloseBrace => token::CloseDelim(Delimiter::Brace),
|
|
|
|
|
rustc_lexer::TokenKind::OpenBracket => token::OpenDelim(Delimiter::Bracket),
|
|
|
|
|
rustc_lexer::TokenKind::CloseBracket => token::CloseDelim(Delimiter::Bracket),
|
|
|
|
|
rustc_lexer::TokenKind::At => token::At,
|
|
|
|
|
rustc_lexer::TokenKind::Pound => token::Pound,
|
|
|
|
|
rustc_lexer::TokenKind::Tilde => token::Tilde,
|
|
|
|
|
rustc_lexer::TokenKind::Question => token::Question,
|
|
|
|
|
rustc_lexer::TokenKind::Colon => token::Colon,
|
|
|
|
|
rustc_lexer::TokenKind::Dollar => token::Dollar,
|
|
|
|
|
rustc_lexer::TokenKind::Eq => token::Eq,
|
|
|
|
|
rustc_lexer::TokenKind::Bang => token::Not,
|
|
|
|
|
rustc_lexer::TokenKind::Lt => token::Lt,
|
|
|
|
|
rustc_lexer::TokenKind::Gt => token::Gt,
|
|
|
|
|
rustc_lexer::TokenKind::Minus => token::BinOp(token::Minus),
|
|
|
|
|
rustc_lexer::TokenKind::And => token::BinOp(token::And),
|
|
|
|
|
rustc_lexer::TokenKind::Or => token::BinOp(token::Or),
|
|
|
|
|
rustc_lexer::TokenKind::Plus => token::BinOp(token::Plus),
|
|
|
|
|
rustc_lexer::TokenKind::Star => token::BinOp(token::Star),
|
|
|
|
|
rustc_lexer::TokenKind::Slash => token::BinOp(token::Slash),
|
|
|
|
|
rustc_lexer::TokenKind::Caret => token::BinOp(token::Caret),
|
|
|
|
|
rustc_lexer::TokenKind::Percent => token::BinOp(token::Percent),
|
|
|
|
|
|
|
|
|
|
rustc_lexer::TokenKind::Unknown | rustc_lexer::TokenKind::InvalidIdent => {
|
2023-01-07 16:33:05 +00:00
|
|
|
|
// Don't emit diagnostics for sequences of the same invalid token
|
|
|
|
|
if swallow_next_invalid > 0 {
|
|
|
|
|
swallow_next_invalid -= 1;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
let mut it = self.str_from_to_end(start).chars();
|
|
|
|
|
let c = it.next().unwrap();
|
2023-01-14 10:34:06 -08:00
|
|
|
|
if c == '\u{00a0}' {
|
|
|
|
|
// If an error has already been reported on non-breaking
|
|
|
|
|
// space characters earlier in the file, treat all
|
|
|
|
|
// subsequent occurrences as whitespace.
|
|
|
|
|
if self.nbsp_is_whitespace {
|
|
|
|
|
preceded_by_whitespace = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
self.nbsp_is_whitespace = true;
|
|
|
|
|
}
|
2023-01-07 16:33:05 +00:00
|
|
|
|
let repeats = it.take_while(|c1| *c1 == c).count();
|
2022-09-26 12:12:58 +10:00
|
|
|
|
// FIXME: the lexer could be used to turn the ASCII version of unicode
|
|
|
|
|
// homoglyphs, instead of keeping a table in `check_for_substitution`into the
|
|
|
|
|
// token. Ideally, this should be inside `rustc_lexer`. However, we should
|
|
|
|
|
// first remove compound tokens like `<<` from `rustc_lexer`, and then add
|
|
|
|
|
// fancier error recovery to it, as there will be less overall work to do this
|
|
|
|
|
// way.
|
2023-01-29 13:37:05 +00:00
|
|
|
|
let (token, sugg) = unicode_chars::check_for_substitution(self, start, c, repeats+1);
|
2023-12-18 21:14:02 +11:00
|
|
|
|
self.dcx().emit_err(errors::UnknownTokenStart {
|
2023-01-29 13:37:05 +00:00
|
|
|
|
span: self.mk_sp(start, self.pos + Pos::from_usize(repeats * c.len_utf8())),
|
|
|
|
|
escaped: escaped_char(c),
|
|
|
|
|
sugg,
|
|
|
|
|
null: if c == '\x00' {Some(errors::UnknownTokenNull)} else {None},
|
|
|
|
|
repeat: if repeats > 0 {
|
|
|
|
|
swallow_next_invalid = repeats;
|
|
|
|
|
Some(errors::UnknownTokenRepeat { repeats })
|
|
|
|
|
} else {None}
|
|
|
|
|
});
|
|
|
|
|
|
2022-09-26 12:12:58 +10:00
|
|
|
|
if let Some(token) = token {
|
|
|
|
|
token
|
|
|
|
|
} else {
|
|
|
|
|
preceded_by_whitespace = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
rustc_lexer::TokenKind::Eof => token::Eof,
|
|
|
|
|
};
|
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
|
return (Token::new(kind, span), preceded_by_whitespace);
|
2020-08-31 19:00:49 +02:00
|
|
|
|
}
|
2018-11-01 11:57:29 -05:00
|
|
|
|
}
|
|
|
|
|
|
2023-07-25 09:24:12 +00:00
|
|
|
|
fn ident(&self, start: BytePos) -> TokenKind {
|
|
|
|
|
let sym = nfc_normalize(self.str_from(start));
|
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
|
|
|
|
self.sess.symbol_gallery.insert(sym, span);
|
|
|
|
|
token::Ident(sym, false)
|
|
|
|
|
}
|
|
|
|
|
|
2021-08-19 11:40:00 -07:00
|
|
|
|
/// Detect usages of Unicode codepoints changing the direction of the text on screen and loudly
|
|
|
|
|
/// complain about it.
|
|
|
|
|
fn lint_unicode_text_flow(&self, start: BytePos) {
|
|
|
|
|
// Opening delimiter of the length 2 is not included into the comment text.
|
|
|
|
|
let content_start = start + BytePos(2);
|
|
|
|
|
let content = self.str_from(content_start);
|
2021-11-04 23:31:42 +01:00
|
|
|
|
if contains_text_flow_control_chars(content) {
|
2021-11-03 23:37:23 +01:00
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
2021-08-19 11:40:00 -07:00
|
|
|
|
self.sess.buffer_lint_with_diagnostic(
|
2023-11-21 20:07:32 +01:00
|
|
|
|
TEXT_DIRECTION_CODEPOINT_IN_COMMENT,
|
2021-08-19 11:40:00 -07:00
|
|
|
|
span,
|
|
|
|
|
ast::CRATE_NODE_ID,
|
|
|
|
|
"unicode codepoint changing visible direction of text present in comment",
|
|
|
|
|
BuiltinLintDiagnostics::UnicodeTextFlow(span, content.to_string()),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-17 18:43:35 +02:00
|
|
|
|
fn cook_doc_comment(
|
|
|
|
|
&self,
|
|
|
|
|
content_start: BytePos,
|
|
|
|
|
content: &str,
|
|
|
|
|
comment_kind: CommentKind,
|
|
|
|
|
doc_style: DocStyle,
|
|
|
|
|
) -> TokenKind {
|
2020-08-17 21:52:49 +02:00
|
|
|
|
if content.contains('\r') {
|
|
|
|
|
for (idx, _) in content.char_indices().filter(|&(_, c)| c == '\r') {
|
2023-01-29 13:37:05 +00:00
|
|
|
|
let span = self.mk_sp(
|
2020-08-17 21:52:49 +02:00
|
|
|
|
content_start + BytePos(idx as u32),
|
|
|
|
|
content_start + BytePos(idx as u32 + 1),
|
|
|
|
|
);
|
2023-01-29 13:37:05 +00:00
|
|
|
|
let block = matches!(comment_kind, CommentKind::Block);
|
2023-12-18 21:14:02 +11:00
|
|
|
|
self.dcx().emit_err(errors::CrDocComment { span, block });
|
2020-08-17 21:52:49 +02:00
|
|
|
|
}
|
2020-08-17 18:43:35 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let attr_style = match doc_style {
|
|
|
|
|
DocStyle::Outer => AttrStyle::Outer,
|
|
|
|
|
DocStyle::Inner => AttrStyle::Inner,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
token::DocComment(comment_kind, attr_style, Symbol::intern(content))
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
|
fn cook_lexer_literal(
|
|
|
|
|
&self,
|
|
|
|
|
start: BytePos,
|
2022-11-04 09:19:34 +11:00
|
|
|
|
end: BytePos,
|
2019-05-06 11:53:40 +03:00
|
|
|
|
kind: rustc_lexer::LiteralKind,
|
|
|
|
|
) -> (token::LitKind, Symbol) {
|
2022-11-04 09:19:34 +11:00
|
|
|
|
match kind {
|
2019-05-06 11:53:40 +03:00
|
|
|
|
rustc_lexer::LiteralKind::Char { terminated } => {
|
|
|
|
|
if !terminated {
|
2024-01-03 21:50:36 +11:00
|
|
|
|
self.dcx()
|
|
|
|
|
.struct_span_fatal(self.mk_sp(start, end), "unterminated character literal")
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
|
.with_code(E0762)
|
2024-01-03 21:50:36 +11:00
|
|
|
|
.emit()
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2024-01-24 15:24:58 +11:00
|
|
|
|
self.cook_unicode(token::Char, Mode::Char, start, end, 1, 1) // ' '
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::Byte { terminated } => {
|
|
|
|
|
if !terminated {
|
2024-01-03 21:50:36 +11:00
|
|
|
|
self.dcx()
|
|
|
|
|
.struct_span_fatal(
|
|
|
|
|
self.mk_sp(start + BytePos(1), end),
|
|
|
|
|
"unterminated byte constant",
|
|
|
|
|
)
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
|
.with_code(E0763)
|
2024-01-03 21:50:36 +11:00
|
|
|
|
.emit()
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2024-01-24 15:24:58 +11:00
|
|
|
|
self.cook_unicode(token::Byte, Mode::Byte, start, end, 2, 1) // b' '
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::Str { terminated } => {
|
|
|
|
|
if !terminated {
|
2024-01-03 21:50:36 +11:00
|
|
|
|
self.dcx()
|
|
|
|
|
.struct_span_fatal(
|
|
|
|
|
self.mk_sp(start, end),
|
|
|
|
|
"unterminated double quote string",
|
|
|
|
|
)
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
|
.with_code(E0765)
|
2024-01-03 21:50:36 +11:00
|
|
|
|
.emit()
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2024-01-24 15:24:58 +11:00
|
|
|
|
self.cook_unicode(token::Str, Mode::Str, start, end, 1, 1) // " "
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::ByteStr { terminated } => {
|
|
|
|
|
if !terminated {
|
2024-01-03 21:50:36 +11:00
|
|
|
|
self.dcx()
|
|
|
|
|
.struct_span_fatal(
|
|
|
|
|
self.mk_sp(start + BytePos(1), end),
|
|
|
|
|
"unterminated double quote byte string",
|
|
|
|
|
)
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
|
.with_code(E0766)
|
2024-01-03 21:50:36 +11:00
|
|
|
|
.emit()
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2024-01-24 15:24:58 +11:00
|
|
|
|
self.cook_unicode(token::ByteStr, Mode::ByteStr, start, end, 2, 1) // b" "
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2023-03-05 15:03:22 +00:00
|
|
|
|
rustc_lexer::LiteralKind::CStr { terminated } => {
|
|
|
|
|
if !terminated {
|
2024-01-03 21:50:36 +11:00
|
|
|
|
self.dcx()
|
|
|
|
|
.struct_span_fatal(
|
|
|
|
|
self.mk_sp(start + BytePos(1), end),
|
|
|
|
|
"unterminated C string",
|
|
|
|
|
)
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
|
.with_code(E0767)
|
2024-01-03 21:50:36 +11:00
|
|
|
|
.emit()
|
2023-03-05 15:03:22 +00:00
|
|
|
|
}
|
2024-01-24 15:24:58 +11:00
|
|
|
|
self.cook_mixed(token::CStr, Mode::CStr, start, end, 2, 1) // c" "
|
2023-03-05 15:03:22 +00:00
|
|
|
|
}
|
2022-07-27 13:59:30 +10:00
|
|
|
|
rustc_lexer::LiteralKind::RawStr { n_hashes } => {
|
|
|
|
|
if let Some(n_hashes) = n_hashes {
|
|
|
|
|
let n = u32::from(n_hashes);
|
2022-11-04 09:19:34 +11:00
|
|
|
|
let kind = token::StrRaw(n_hashes);
|
2024-01-24 15:24:58 +11:00
|
|
|
|
self.cook_unicode(kind, Mode::RawStr, start, end, 2 + n, 1 + n) // r##" "##
|
2022-07-27 13:59:30 +10:00
|
|
|
|
} else {
|
|
|
|
|
self.report_raw_str_error(start, 1);
|
|
|
|
|
}
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2022-07-27 13:59:30 +10:00
|
|
|
|
rustc_lexer::LiteralKind::RawByteStr { n_hashes } => {
|
|
|
|
|
if let Some(n_hashes) = n_hashes {
|
|
|
|
|
let n = u32::from(n_hashes);
|
2022-11-04 09:19:34 +11:00
|
|
|
|
let kind = token::ByteStrRaw(n_hashes);
|
2024-01-24 15:24:58 +11:00
|
|
|
|
self.cook_unicode(kind, Mode::RawByteStr, start, end, 3 + n, 1 + n) // br##" "##
|
2022-07-27 13:59:30 +10:00
|
|
|
|
} else {
|
|
|
|
|
self.report_raw_str_error(start, 2);
|
|
|
|
|
}
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2023-03-05 15:03:22 +00:00
|
|
|
|
rustc_lexer::LiteralKind::RawCStr { n_hashes } => {
|
|
|
|
|
if let Some(n_hashes) = n_hashes {
|
|
|
|
|
let n = u32::from(n_hashes);
|
|
|
|
|
let kind = token::CStrRaw(n_hashes);
|
2024-01-24 16:00:10 +11:00
|
|
|
|
self.cook_unicode(kind, Mode::RawCStr, start, end, 3 + n, 1 + n) // cr##" "##
|
2023-03-05 15:03:22 +00:00
|
|
|
|
} else {
|
|
|
|
|
self.report_raw_str_error(start, 2);
|
|
|
|
|
}
|
|
|
|
|
}
|
2019-05-06 11:53:40 +03:00
|
|
|
|
rustc_lexer::LiteralKind::Int { base, empty_int } => {
|
2024-02-14 19:09:25 +11:00
|
|
|
|
let mut kind = token::Integer;
|
2022-11-04 09:19:34 +11:00
|
|
|
|
if empty_int {
|
2023-01-29 13:37:05 +00:00
|
|
|
|
let span = self.mk_sp(start, end);
|
2024-02-14 20:12:05 +11:00
|
|
|
|
let guar = self.dcx().emit_err(errors::NoDigitsLiteral { span });
|
|
|
|
|
kind = token::Err(guar);
|
2024-02-14 19:09:25 +11:00
|
|
|
|
} else if matches!(base, Base::Binary | Base::Octal) {
|
|
|
|
|
let base = base as u32;
|
|
|
|
|
let s = self.str_from_to(start + BytePos(2), end);
|
|
|
|
|
for (idx, c) in s.char_indices() {
|
|
|
|
|
let span = self.mk_sp(
|
|
|
|
|
start + BytePos::from_usize(2 + idx),
|
|
|
|
|
start + BytePos::from_usize(2 + idx + c.len_utf8()),
|
|
|
|
|
);
|
|
|
|
|
if c != '_' && c.to_digit(base).is_none() {
|
2024-02-14 20:12:05 +11:00
|
|
|
|
let guar =
|
|
|
|
|
self.dcx().emit_err(errors::InvalidDigitLiteral { span, base });
|
|
|
|
|
kind = token::Err(guar);
|
2022-11-04 10:02:29 +11:00
|
|
|
|
}
|
|
|
|
|
}
|
2022-11-04 09:19:34 +11:00
|
|
|
|
}
|
2024-02-14 19:09:25 +11:00
|
|
|
|
(kind, self.symbol_from_to(start, end))
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
|
|
|
|
|
if empty_exponent {
|
2023-01-29 13:37:05 +00:00
|
|
|
|
let span = self.mk_sp(start, self.pos);
|
2023-12-18 21:14:02 +11:00
|
|
|
|
self.dcx().emit_err(errors::EmptyExponentFloat { span });
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2023-01-29 13:37:05 +00:00
|
|
|
|
let base = match base {
|
|
|
|
|
Base::Hexadecimal => Some("hexadecimal"),
|
|
|
|
|
Base::Octal => Some("octal"),
|
|
|
|
|
Base::Binary => Some("binary"),
|
|
|
|
|
_ => None,
|
|
|
|
|
};
|
|
|
|
|
if let Some(base) = base {
|
|
|
|
|
let span = self.mk_sp(start, end);
|
2023-12-18 21:14:02 +11:00
|
|
|
|
self.dcx().emit_err(errors::FloatLiteralUnsupportedBase { span, base });
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2022-11-04 09:19:34 +11:00
|
|
|
|
(token::Float, self.symbol_from_to(start, end))
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2022-11-04 09:19:34 +11:00
|
|
|
|
}
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
|
|
|
|
|
2018-05-09 12:49:39 +10:00
|
|
|
|
#[inline]
|
|
|
|
|
fn src_index(&self, pos: BytePos) -> usize {
|
2019-07-31 19:55:01 +03:00
|
|
|
|
(pos - self.start_pos).to_usize()
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 21:02:19 +03:00
|
|
|
|
/// Slice of the source text from `start` up to but excluding `self.pos`,
|
|
|
|
|
/// meaning the slice does not include the character `self.ch`.
|
2024-01-11 14:21:07 +11:00
|
|
|
|
fn str_from(&self, start: BytePos) -> &'src str {
|
2019-06-25 21:02:19 +03:00
|
|
|
|
self.str_from_to(start, self.pos)
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 21:37:25 +03:00
|
|
|
|
/// As symbol_from, with an explicit endpoint.
|
|
|
|
|
fn symbol_from_to(&self, start: BytePos, end: BytePos) -> Symbol {
|
2014-12-20 00:09:35 -08:00
|
|
|
|
debug!("taking an ident from {:?} to {:?}", start, end);
|
2019-06-25 21:02:19 +03:00
|
|
|
|
Symbol::intern(self.str_from_to(start, end))
|
2014-06-24 17:44:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2019-06-25 21:02:19 +03:00
|
|
|
|
/// Slice of the source text spanning from `start` up to but excluding `end`.
|
2024-01-11 14:21:07 +11:00
|
|
|
|
fn str_from_to(&self, start: BytePos, end: BytePos) -> &'src str {
|
2019-06-25 21:02:19 +03:00
|
|
|
|
&self.src[self.src_index(start)..self.src_index(end)]
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2023-01-07 16:33:05 +00:00
|
|
|
|
/// Slice of the source text spanning from `start` until the end
|
2024-01-11 14:21:07 +11:00
|
|
|
|
fn str_from_to_end(&self, start: BytePos) -> &'src str {
|
2023-01-07 16:33:05 +00:00
|
|
|
|
&self.src[self.src_index(start)..]
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 13:59:30 +10:00
|
|
|
|
fn report_raw_str_error(&self, start: BytePos, prefix_len: u32) -> ! {
|
|
|
|
|
match rustc_lexer::validate_raw_str(self.str_from(start), prefix_len) {
|
|
|
|
|
Err(RawStrError::InvalidStarter { bad_char }) => {
|
2020-05-29 17:37:16 +02:00
|
|
|
|
self.report_non_started_raw_string(start, bad_char)
|
|
|
|
|
}
|
2022-07-27 13:59:30 +10:00
|
|
|
|
Err(RawStrError::NoTerminator { expected, found, possible_terminator_offset }) => self
|
2020-05-29 17:37:16 +02:00
|
|
|
|
.report_unterminated_raw_string(start, expected, possible_terminator_offset, found),
|
2022-07-27 13:59:30 +10:00
|
|
|
|
Err(RawStrError::TooManyDelimiters { found }) => {
|
2020-05-29 17:37:16 +02:00
|
|
|
|
self.report_too_many_hashes(start, found)
|
2020-03-28 01:46:20 -04:00
|
|
|
|
}
|
2022-07-27 13:59:30 +10:00
|
|
|
|
Ok(()) => panic!("no error found for supposedly invalid raw string literal"),
|
2020-03-28 01:46:20 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-29 17:37:16 +02:00
|
|
|
|
fn report_non_started_raw_string(&self, start: BytePos, bad_char: char) -> ! {
|
2024-01-04 11:44:16 +11:00
|
|
|
|
self.sess
|
|
|
|
|
.dcx
|
|
|
|
|
.struct_span_fatal(
|
|
|
|
|
self.mk_sp(start, self.pos),
|
|
|
|
|
format!(
|
|
|
|
|
"found invalid character; only `#` is allowed in raw string delimitation: {}",
|
|
|
|
|
escaped_char(bad_char)
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.emit()
|
2014-05-21 16:57:31 -07:00
|
|
|
|
}
|
|
|
|
|
|
2020-03-28 01:46:20 -04:00
|
|
|
|
fn report_unterminated_raw_string(
|
|
|
|
|
&self,
|
|
|
|
|
start: BytePos,
|
2022-07-27 13:59:30 +10:00
|
|
|
|
n_hashes: u32,
|
|
|
|
|
possible_offset: Option<u32>,
|
|
|
|
|
found_terminators: u32,
|
2020-03-28 01:46:20 -04:00
|
|
|
|
) -> ! {
|
2024-01-03 21:50:36 +11:00
|
|
|
|
let mut err =
|
|
|
|
|
self.dcx().struct_span_fatal(self.mk_sp(start, start), "unterminated raw string");
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
|
err.code(E0748);
|
2019-05-06 11:53:40 +03:00
|
|
|
|
err.span_label(self.mk_sp(start, start), "unterminated raw string");
|
2014-07-02 09:39:48 -07:00
|
|
|
|
|
2019-05-06 11:53:40 +03:00
|
|
|
|
if n_hashes > 0 {
|
Restrict `From<S>` for `{D,Subd}iagnosticMessage`.
Currently a `{D,Subd}iagnosticMessage` can be created from any type that
impls `Into<String>`. That includes `&str`, `String`, and `Cow<'static,
str>`, which are reasonable. It also includes `&String`, which is pretty
weird, and results in many places making unnecessary allocations for
patterns like this:
```
self.fatal(&format!(...))
```
This creates a string with `format!`, takes a reference, passes the
reference to `fatal`, which does an `into()`, which clones the
reference, doing a second allocation. Two allocations for a single
string, bleh.
This commit changes the `From` impls so that you can only create a
`{D,Subd}iagnosticMessage` from `&str`, `String`, or `Cow<'static,
str>`. This requires changing all the places that currently create one
from a `&String`. Most of these are of the `&format!(...)` form
described above; each one removes an unnecessary static `&`, plus an
allocation when executed. There are also a few places where the existing
use of `&String` was more reasonable; these now just use `clone()` at
the call site.
As well as making the code nicer and more efficient, this is a step
towards possibly using `Cow<'static, str>` in
`{D,Subd}iagnosticMessage::{Str,Eager}`. That would require changing
the `From<&'a str>` impls to `From<&'static str>`, which is doable, but
I'm not yet sure if it's worthwhile.
2023-04-20 13:26:58 +10:00
|
|
|
|
err.note(format!(
|
2019-05-06 11:53:40 +03:00
|
|
|
|
"this raw string should be terminated with `\"{}`",
|
2022-07-27 13:59:30 +10:00
|
|
|
|
"#".repeat(n_hashes as usize)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
));
|
2019-04-25 11:48:25 +03:00
|
|
|
|
}
|
2014-07-02 09:39:48 -07:00
|
|
|
|
|
2020-03-28 01:46:20 -04:00
|
|
|
|
if let Some(possible_offset) = possible_offset {
|
2023-04-09 23:07:45 +02:00
|
|
|
|
let lo = start + BytePos(possible_offset);
|
|
|
|
|
let hi = lo + BytePos(found_terminators);
|
2020-03-29 11:12:48 -04:00
|
|
|
|
let span = self.mk_sp(lo, hi);
|
2020-03-28 01:46:20 -04:00
|
|
|
|
err.span_suggestion(
|
|
|
|
|
span,
|
2020-03-29 11:12:48 -04:00
|
|
|
|
"consider terminating the string here",
|
2022-07-27 13:59:30 +10:00
|
|
|
|
"#".repeat(n_hashes as usize),
|
2020-03-28 01:46:20 -04:00
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-09 16:11:28 -08:00
|
|
|
|
err.emit()
|
2014-07-02 09:39:48 -07:00
|
|
|
|
}
|
|
|
|
|
|
2022-04-14 03:22:02 +09:00
|
|
|
|
fn report_unterminated_block_comment(&self, start: BytePos, doc_style: Option<DocStyle>) {
|
|
|
|
|
let msg = match doc_style {
|
|
|
|
|
Some(_) => "unterminated block doc-comment",
|
|
|
|
|
None => "unterminated block comment",
|
|
|
|
|
};
|
|
|
|
|
let last_bpos = self.pos;
|
2024-01-03 21:50:36 +11:00
|
|
|
|
let mut err = self.dcx().struct_span_fatal(self.mk_sp(start, last_bpos), msg);
|
Stop using `String` for error codes.
Error codes are integers, but `String` is used everywhere to represent
them. Gross!
This commit introduces `ErrCode`, an integral newtype for error codes,
replacing `String`. It also introduces a constant for every error code,
e.g. `E0123`, and removes the `error_code!` macro. The constants are
imported wherever used with `use rustc_errors::codes::*`.
With the old code, we have three different ways to specify an error code
at a use point:
```
error_code!(E0123) // macro call
struct_span_code_err!(dcx, span, E0123, "msg"); // bare ident arg to macro call
\#[diag(name, code = "E0123")] // string
struct Diag;
```
With the new code, they all use the `E0123` constant.
```
E0123 // constant
struct_span_code_err!(dcx, span, E0123, "msg"); // constant
\#[diag(name, code = E0123)] // constant
struct Diag;
```
The commit also changes the structure of the error code definitions:
- `rustc_error_codes` now just defines a higher-order macro listing the
used error codes and nothing else.
- Because that's now the only thing in the `rustc_error_codes` crate, I
moved it into the `lib.rs` file and removed the `error_codes.rs` file.
- `rustc_errors` uses that macro to define everything, e.g. the error
code constants and the `DIAGNOSTIC_TABLES`. This is in its new
`codes.rs` file.
2024-01-14 10:57:07 +11:00
|
|
|
|
err.code(E0758);
|
2022-04-14 03:22:02 +09:00
|
|
|
|
let mut nested_block_comment_open_idxs = vec![];
|
|
|
|
|
let mut last_nested_block_comment_idxs = None;
|
2022-04-14 21:18:27 +09:00
|
|
|
|
let mut content_chars = self.str_from(start).char_indices().peekable();
|
2022-04-14 03:22:02 +09:00
|
|
|
|
|
2022-04-14 21:18:27 +09:00
|
|
|
|
while let Some((idx, current_char)) = content_chars.next() {
|
|
|
|
|
match content_chars.peek() {
|
|
|
|
|
Some((_, '*')) if current_char == '/' => {
|
|
|
|
|
nested_block_comment_open_idxs.push(idx);
|
|
|
|
|
}
|
|
|
|
|
Some((_, '/')) if current_char == '*' => {
|
|
|
|
|
last_nested_block_comment_idxs =
|
|
|
|
|
nested_block_comment_open_idxs.pop().map(|open_idx| (open_idx, idx));
|
|
|
|
|
}
|
|
|
|
|
_ => {}
|
|
|
|
|
};
|
2022-04-14 03:22:02 +09:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if let Some((nested_open_idx, nested_close_idx)) = last_nested_block_comment_idxs {
|
|
|
|
|
err.span_label(self.mk_sp(start, start + BytePos(2)), msg)
|
|
|
|
|
.span_label(
|
|
|
|
|
self.mk_sp(
|
2022-04-14 21:18:27 +09:00
|
|
|
|
start + BytePos(nested_open_idx as u32),
|
|
|
|
|
start + BytePos(nested_open_idx as u32 + 2),
|
2022-04-14 03:22:02 +09:00
|
|
|
|
),
|
|
|
|
|
"...as last nested comment starts here, maybe you want to close this instead?",
|
|
|
|
|
)
|
|
|
|
|
.span_label(
|
|
|
|
|
self.mk_sp(
|
2022-04-14 21:18:27 +09:00
|
|
|
|
start + BytePos(nested_close_idx as u32),
|
|
|
|
|
start + BytePos(nested_close_idx as u32 + 2),
|
2022-04-14 03:22:02 +09:00
|
|
|
|
),
|
2022-04-14 21:18:27 +09:00
|
|
|
|
"...and last nested comment terminates here.",
|
2022-04-14 03:22:02 +09:00
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
err.emit();
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-14 16:46:46 +00:00
|
|
|
|
// RFC 3101 introduced the idea of (reserved) prefixes. As of Rust 2021,
|
|
|
|
|
// using a (unknown) prefix is an error. In earlier editions, however, they
|
|
|
|
|
// only result in a (allowed by default) lint, and are treated as regular
|
|
|
|
|
// identifier tokens.
|
2021-06-14 16:50:20 +00:00
|
|
|
|
fn report_unknown_prefix(&self, start: BytePos) {
|
2021-06-14 12:56:49 +00:00
|
|
|
|
let prefix_span = self.mk_sp(start, self.pos);
|
2023-01-29 13:37:05 +00:00
|
|
|
|
let prefix = self.str_from_to(start, self.pos);
|
2021-06-14 12:56:49 +00:00
|
|
|
|
|
2021-06-14 20:38:57 +00:00
|
|
|
|
let expn_data = prefix_span.ctxt().outer_expn_data();
|
|
|
|
|
|
|
|
|
|
if expn_data.edition >= Edition::Edition2021 {
|
2021-06-14 16:48:07 +00:00
|
|
|
|
// In Rust 2021, this is a hard error.
|
2023-01-29 13:37:05 +00:00
|
|
|
|
let sugg = if prefix == "rb" {
|
|
|
|
|
Some(errors::UnknownPrefixSugg::UseBr(prefix_span))
|
2021-07-31 15:37:36 +02:00
|
|
|
|
} else if expn_data.is_root() {
|
2023-01-29 13:37:05 +00:00
|
|
|
|
Some(errors::UnknownPrefixSugg::Whitespace(prefix_span.shrink_to_hi()))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
2023-12-18 21:14:02 +11:00
|
|
|
|
self.dcx().emit_err(errors::UnknownPrefix { span: prefix_span, prefix, sugg });
|
2021-06-14 16:48:07 +00:00
|
|
|
|
} else {
|
|
|
|
|
// Before Rust 2021, only emit a lint for migration.
|
2021-06-14 12:56:49 +00:00
|
|
|
|
self.sess.buffer_lint_with_diagnostic(
|
2023-11-21 20:07:32 +01:00
|
|
|
|
RUST_2021_PREFIXES_INCOMPATIBLE_SYNTAX,
|
2021-06-14 12:56:49 +00:00
|
|
|
|
prefix_span,
|
|
|
|
|
ast::CRATE_NODE_ID,
|
2023-05-16 16:04:03 +10:00
|
|
|
|
format!("prefix `{prefix}` is unknown"),
|
2021-06-14 12:56:49 +00:00
|
|
|
|
BuiltinLintDiagnostics::ReservedPrefix(prefix_span),
|
|
|
|
|
);
|
2021-05-16 11:10:05 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-29 13:37:05 +00:00
|
|
|
|
fn report_too_many_hashes(&self, start: BytePos, num: u32) -> ! {
|
2023-12-18 21:14:02 +11:00
|
|
|
|
self.dcx().emit_fatal(errors::TooManyHashes { span: self.mk_sp(start, self.pos), num });
|
2014-07-02 09:39:48 -07:00
|
|
|
|
}
|
2019-04-25 11:48:25 +03:00
|
|
|
|
|
2023-04-28 15:28:52 +00:00
|
|
|
|
fn cook_common(
|
2021-01-23 12:48:31 -08:00
|
|
|
|
&self,
|
2024-02-14 20:29:31 +11:00
|
|
|
|
mut kind: token::LitKind,
|
2021-01-23 12:48:31 -08:00
|
|
|
|
mode: Mode,
|
2022-11-04 09:19:34 +11:00
|
|
|
|
start: BytePos,
|
|
|
|
|
end: BytePos,
|
2021-01-23 12:48:31 -08:00
|
|
|
|
prefix_len: u32,
|
|
|
|
|
postfix_len: u32,
|
2023-04-28 15:28:52 +00:00
|
|
|
|
unescape: fn(&str, Mode, &mut dyn FnMut(Range<usize>, Result<(), EscapeError>)),
|
2022-11-04 09:19:34 +11:00
|
|
|
|
) -> (token::LitKind, Symbol) {
|
|
|
|
|
let content_start = start + BytePos(prefix_len);
|
|
|
|
|
let content_end = end - BytePos(postfix_len);
|
2020-05-13 09:52:01 +02:00
|
|
|
|
let lit_content = self.str_from_to(content_start, content_end);
|
2023-04-28 15:28:52 +00:00
|
|
|
|
unescape(lit_content, mode, &mut |range, result| {
|
2020-05-13 09:52:01 +02:00
|
|
|
|
// Here we only check for errors. The actual unescaping is done later.
|
|
|
|
|
if let Err(err) = result {
|
2022-11-04 09:19:34 +11:00
|
|
|
|
let span_with_quotes = self.mk_sp(start, end);
|
2021-01-23 12:48:31 -08:00
|
|
|
|
let (start, end) = (range.start as u32, range.end as u32);
|
|
|
|
|
let lo = content_start + BytePos(start);
|
|
|
|
|
let hi = lo + BytePos(end - start);
|
|
|
|
|
let span = self.mk_sp(lo, hi);
|
2024-02-14 20:29:31 +11:00
|
|
|
|
let is_fatal = err.is_fatal();
|
2024-02-14 20:12:05 +11:00
|
|
|
|
if let Some(guar) = emit_unescape_error(
|
2023-12-18 22:21:37 +11:00
|
|
|
|
self.dcx(),
|
2020-05-13 09:52:01 +02:00
|
|
|
|
lit_content,
|
|
|
|
|
span_with_quotes,
|
2021-01-23 12:48:31 -08:00
|
|
|
|
span,
|
2020-05-13 09:52:01 +02:00
|
|
|
|
mode,
|
2019-06-25 21:02:19 +03:00
|
|
|
|
range,
|
2019-04-25 11:48:25 +03:00
|
|
|
|
err,
|
2024-02-14 20:29:31 +11:00
|
|
|
|
) {
|
|
|
|
|
assert!(is_fatal);
|
2024-02-14 20:12:05 +11:00
|
|
|
|
kind = token::Err(guar);
|
2024-02-14 20:29:31 +11:00
|
|
|
|
}
|
2019-06-25 21:02:19 +03:00
|
|
|
|
}
|
2020-05-13 09:52:01 +02:00
|
|
|
|
});
|
2022-10-10 13:40:56 +11:00
|
|
|
|
|
|
|
|
|
// We normally exclude the quotes for the symbol, but for errors we
|
|
|
|
|
// include it because it results in clearer error messages.
|
2024-02-14 20:12:05 +11:00
|
|
|
|
let sym = if !matches!(kind, token::Err(_)) {
|
|
|
|
|
Symbol::intern(lit_content)
|
2022-10-10 13:40:56 +11:00
|
|
|
|
} else {
|
2024-02-14 20:12:05 +11:00
|
|
|
|
self.symbol_from_to(start, end)
|
|
|
|
|
};
|
|
|
|
|
(kind, sym)
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2023-03-05 15:03:22 +00:00
|
|
|
|
|
2024-01-24 15:24:58 +11:00
|
|
|
|
fn cook_unicode(
|
2023-03-05 15:03:22 +00:00
|
|
|
|
&self,
|
|
|
|
|
kind: token::LitKind,
|
|
|
|
|
mode: Mode,
|
|
|
|
|
start: BytePos,
|
|
|
|
|
end: BytePos,
|
|
|
|
|
prefix_len: u32,
|
|
|
|
|
postfix_len: u32,
|
|
|
|
|
) -> (token::LitKind, Symbol) {
|
2023-04-28 15:28:52 +00:00
|
|
|
|
self.cook_common(kind, mode, start, end, prefix_len, postfix_len, |src, mode, callback| {
|
2024-01-24 15:24:58 +11:00
|
|
|
|
unescape::unescape_unicode(src, mode, &mut |span, result| {
|
2023-04-28 15:28:52 +00:00
|
|
|
|
callback(span, result.map(drop))
|
|
|
|
|
})
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-10-10 13:40:56 +11:00
|
|
|
|
|
2024-01-24 15:24:58 +11:00
|
|
|
|
fn cook_mixed(
|
2023-04-28 15:28:52 +00:00
|
|
|
|
&self,
|
|
|
|
|
kind: token::LitKind,
|
|
|
|
|
mode: Mode,
|
|
|
|
|
start: BytePos,
|
|
|
|
|
end: BytePos,
|
|
|
|
|
prefix_len: u32,
|
|
|
|
|
postfix_len: u32,
|
|
|
|
|
) -> (token::LitKind, Symbol) {
|
|
|
|
|
self.cook_common(kind, mode, start, end, prefix_len, postfix_len, |src, mode, callback| {
|
2024-01-24 15:24:58 +11:00
|
|
|
|
unescape::unescape_mixed(src, mode, &mut |span, result| {
|
2023-04-28 15:28:52 +00:00
|
|
|
|
callback(span, result.map(drop))
|
|
|
|
|
})
|
|
|
|
|
})
|
2019-05-06 11:53:40 +03:00
|
|
|
|
}
|
2016-01-03 11:14:09 +02:00
|
|
|
|
}
|
2019-12-29 19:50:43 +08:00
|
|
|
|
|
|
|
|
|
pub fn nfc_normalize(string: &str) -> Symbol {
|
|
|
|
|
use unicode_normalization::{is_nfc_quick, IsNormalized, UnicodeNormalization};
|
|
|
|
|
match is_nfc_quick(string.chars()) {
|
|
|
|
|
IsNormalized::Yes => Symbol::intern(string),
|
|
|
|
|
_ => {
|
|
|
|
|
let normalized_str: String = string.chars().nfc().collect();
|
|
|
|
|
Symbol::intern(&normalized_str)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|