Skip to content

Commit 3fb5265

Browse files
Rollup merge of #151474 - nnethercote:circ-deps, r=davidtwco
Minor structural improvements Some small cleanups. r? @davidtwco
2 parents ee753e8 + b33fdb2 commit 3fb5265

File tree

6 files changed

+205
-211
lines changed

6 files changed

+205
-211
lines changed

compiler/rustc_lexer/src/cursor.rs

Lines changed: 0 additions & 125 deletions
This file was deleted.

compiler/rustc_lexer/src/lib.rs

Lines changed: 125 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,15 +25,13 @@
2525
#![deny(unstable_features)]
2626
// tidy-alphabetical-end
2727

28-
mod cursor;
29-
3028
#[cfg(test)]
3129
mod tests;
3230

31+
use std::str::Chars;
32+
3333
use LiteralKind::*;
3434
use TokenKind::*;
35-
use cursor::EOF_CHAR;
36-
pub use cursor::{Cursor, FrontmatterAllowed};
3735
pub use unicode_ident::UNICODE_VERSION;
3836
use unicode_properties::UnicodeEmoji;
3937

@@ -407,7 +405,129 @@ pub fn is_ident(string: &str) -> bool {
407405
}
408406
}
409407

410-
impl Cursor<'_> {
408+
pub enum FrontmatterAllowed {
409+
Yes,
410+
No,
411+
}
412+
413+
/// Peekable iterator over a char sequence.
414+
///
415+
/// Next characters can be peeked via `first` method,
416+
/// and position can be shifted forward via `bump` method.
417+
pub struct Cursor<'a> {
418+
len_remaining: usize,
419+
/// Iterator over chars. Slightly faster than a &str.
420+
chars: Chars<'a>,
421+
pub(crate) frontmatter_allowed: FrontmatterAllowed,
422+
#[cfg(debug_assertions)]
423+
prev: char,
424+
}
425+
426+
const EOF_CHAR: char = '\0';
427+
428+
impl<'a> Cursor<'a> {
429+
pub fn new(input: &'a str, frontmatter_allowed: FrontmatterAllowed) -> Cursor<'a> {
430+
Cursor {
431+
len_remaining: input.len(),
432+
chars: input.chars(),
433+
frontmatter_allowed,
434+
#[cfg(debug_assertions)]
435+
prev: EOF_CHAR,
436+
}
437+
}
438+
439+
pub fn as_str(&self) -> &'a str {
440+
self.chars.as_str()
441+
}
442+
443+
/// Returns the last eaten symbol (or `'\0'` in release builds).
444+
/// (For debug assertions only.)
445+
pub(crate) fn prev(&self) -> char {
446+
#[cfg(debug_assertions)]
447+
{
448+
self.prev
449+
}
450+
451+
#[cfg(not(debug_assertions))]
452+
{
453+
EOF_CHAR
454+
}
455+
}
456+
457+
/// Peeks the next symbol from the input stream without consuming it.
458+
/// If requested position doesn't exist, `EOF_CHAR` is returned.
459+
/// However, getting `EOF_CHAR` doesn't always mean actual end of file,
460+
/// it should be checked with `is_eof` method.
461+
pub fn first(&self) -> char {
462+
// `.next()` optimizes better than `.nth(0)`
463+
self.chars.clone().next().unwrap_or(EOF_CHAR)
464+
}
465+
466+
/// Peeks the second symbol from the input stream without consuming it.
467+
pub(crate) fn second(&self) -> char {
468+
// `.next()` optimizes better than `.nth(1)`
469+
let mut iter = self.chars.clone();
470+
iter.next();
471+
iter.next().unwrap_or(EOF_CHAR)
472+
}
473+
474+
/// Peeks the third symbol from the input stream without consuming it.
475+
pub fn third(&self) -> char {
476+
// `.next()` optimizes better than `.nth(2)`
477+
let mut iter = self.chars.clone();
478+
iter.next();
479+
iter.next();
480+
iter.next().unwrap_or(EOF_CHAR)
481+
}
482+
483+
/// Checks if there is nothing more to consume.
484+
pub(crate) fn is_eof(&self) -> bool {
485+
self.chars.as_str().is_empty()
486+
}
487+
488+
/// Returns amount of already consumed symbols.
489+
pub(crate) fn pos_within_token(&self) -> u32 {
490+
(self.len_remaining - self.chars.as_str().len()) as u32
491+
}
492+
493+
/// Resets the number of bytes consumed to 0.
494+
pub(crate) fn reset_pos_within_token(&mut self) {
495+
self.len_remaining = self.chars.as_str().len();
496+
}
497+
498+
/// Moves to the next character.
499+
pub(crate) fn bump(&mut self) -> Option<char> {
500+
let c = self.chars.next()?;
501+
502+
#[cfg(debug_assertions)]
503+
{
504+
self.prev = c;
505+
}
506+
507+
Some(c)
508+
}
509+
510+
/// Moves to a substring by a number of bytes.
511+
pub(crate) fn bump_bytes(&mut self, n: usize) {
512+
self.chars = self.as_str()[n..].chars();
513+
}
514+
515+
/// Eats symbols while predicate returns true or until the end of file is reached.
516+
pub(crate) fn eat_while(&mut self, mut predicate: impl FnMut(char) -> bool) {
517+
// It was tried making optimized version of this for eg. line comments, but
518+
// LLVM can inline all of this and compile it down to fast iteration over bytes.
519+
while predicate(self.first()) && !self.is_eof() {
520+
self.bump();
521+
}
522+
}
523+
524+
pub(crate) fn eat_until(&mut self, byte: u8) {
525+
self.chars = match memchr::memchr(byte, self.as_str().as_bytes()) {
526+
Some(index) => self.as_str()[index..].chars(),
527+
None => "".chars(),
528+
}
529+
}
530+
411531
/// Parses a token from the input string.
412532
pub fn advance_token(&mut self) -> Token {
413533
let Some(first_char) = self.bump() else {

compiler/rustc_parse/src/errors.rs

Lines changed: 62 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use std::borrow::Cow;
44
use std::path::PathBuf;
55

6-
use rustc_ast::token::Token;
6+
use rustc_ast::token::{self, InvisibleOrigin, MetaVarKind, Token};
77
use rustc_ast::util::parser::ExprPrecedence;
88
use rustc_ast::{Path, Visibility};
99
use rustc_errors::codes::*;
@@ -17,7 +17,6 @@ use rustc_span::edition::{Edition, LATEST_STABLE_EDITION};
1717
use rustc_span::{Ident, Span, Symbol};
1818

1919
use crate::fluent_generated as fluent;
20-
use crate::parser::{ForbiddenLetReason, TokenDescription};
2120

2221
#[derive(Diagnostic)]
2322
#[diag(parse_maybe_report_ambiguous_plus)]
@@ -3710,3 +3709,64 @@ pub(crate) struct StructLiteralWithoutPathLate {
37103709
#[suggestion(applicability = "has-placeholders", code = "/* Type */ ", style = "verbose")]
37113710
pub suggestion_span: Span,
37123711
}
3712+
3713+
/// Used to forbid `let` expressions in certain syntactic locations.
3714+
#[derive(Clone, Copy, Subdiagnostic)]
3715+
pub(crate) enum ForbiddenLetReason {
3716+
/// `let` is not valid and the source environment is not important
3717+
OtherForbidden,
3718+
/// A let chain with the `||` operator
3719+
#[note(parse_not_supported_or)]
3720+
NotSupportedOr(#[primary_span] Span),
3721+
/// A let chain with invalid parentheses
3722+
///
3723+
/// For example, `let 1 = 1 && (expr && expr)` is allowed
3724+
/// but `(let 1 = 1 && (let 1 = 1 && (let 1 = 1))) && let a = 1` is not
3725+
#[note(parse_not_supported_parentheses)]
3726+
NotSupportedParentheses(#[primary_span] Span),
3727+
}
3728+
3729+
#[derive(Debug, rustc_macros::Subdiagnostic)]
3730+
#[suggestion(
3731+
parse_misspelled_kw,
3732+
applicability = "machine-applicable",
3733+
code = "{similar_kw}",
3734+
style = "verbose"
3735+
)]
3736+
pub(crate) struct MisspelledKw {
3737+
// We use a String here because `Symbol::into_diag_arg` calls `Symbol::to_ident_string`, which
3738+
// prefix the keyword with a `r#` because it aims to print the symbol as an identifier.
3739+
pub similar_kw: String,
3740+
#[primary_span]
3741+
pub span: Span,
3742+
pub is_incorrect_case: bool,
3743+
}
3744+
3745+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
3746+
pub(super) enum TokenDescription {
3747+
ReservedIdentifier,
3748+
Keyword,
3749+
ReservedKeyword,
3750+
DocComment,
3751+
3752+
// Expanded metavariables are wrapped in invisible delimiters which aren't
3753+
// pretty-printed. In error messages we must handle these specially
3754+
// otherwise we get confusing things in messages like "expected `(`, found
3755+
// ``". It's better to say e.g. "expected `(`, found type metavariable".
3756+
MetaVar(MetaVarKind),
3757+
}
3758+
3759+
impl TokenDescription {
3760+
pub(super) fn from_token(token: &Token) -> Option<Self> {
3761+
match token.kind {
3762+
_ if token.is_special_ident() => Some(TokenDescription::ReservedIdentifier),
3763+
_ if token.is_used_keyword() => Some(TokenDescription::Keyword),
3764+
_ if token.is_unused_keyword() => Some(TokenDescription::ReservedKeyword),
3765+
token::DocComment(..) => Some(TokenDescription::DocComment),
3766+
token::OpenInvisible(InvisibleOrigin::MetaVar(kind)) => {
3767+
Some(TokenDescription::MetaVar(kind))
3768+
}
3769+
_ => None,
3770+
}
3771+
}
3772+
}

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@ use crate::errors::{
3535
ExpectedIdentifier, ExpectedSemi, ExpectedSemiSugg, GenericParamsWithoutAngleBrackets,
3636
GenericParamsWithoutAngleBracketsSugg, HelpIdentifierStartsWithNumber, HelpUseLatestEdition,
3737
InInTypo, IncorrectAwait, IncorrectSemicolon, IncorrectUseOfAwait, IncorrectUseOfUse,
38-
PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg, SelfParamNotFirst,
39-
StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg, SuggAddMissingLetStmt,
40-
SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator, TernaryOperatorSuggestion,
41-
UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
38+
MisspelledKw, PatternMethodParamWithoutBody, QuestionMarkInType, QuestionMarkInTypeSugg,
39+
SelfParamNotFirst, StructLiteralBodyWithoutPath, StructLiteralBodyWithoutPathSugg,
40+
SuggAddMissingLetStmt, SuggEscapeIdentifier, SuggRemoveComma, TernaryOperator,
41+
TernaryOperatorSuggestion, UnexpectedConstInGenericParam, UnexpectedConstParamDeclaration,
4242
UnexpectedConstParamDeclarationSugg, UnmatchedAngleBrackets, UseEqInstead, WrapType,
4343
};
4444
use crate::parser::FnContext;
@@ -212,22 +212,6 @@ impl std::fmt::Display for UnaryFixity {
212212
}
213213
}
214214

215-
#[derive(Debug, rustc_macros::Subdiagnostic)]
216-
#[suggestion(
217-
parse_misspelled_kw,
218-
applicability = "machine-applicable",
219-
code = "{similar_kw}",
220-
style = "verbose"
221-
)]
222-
struct MisspelledKw {
223-
// We use a String here because `Symbol::into_diag_arg` calls `Symbol::to_ident_string`, which
224-
// prefix the keyword with a `r#` because it aims to print the symbol as an identifier.
225-
similar_kw: String,
226-
#[primary_span]
227-
span: Span,
228-
is_incorrect_case: bool,
229-
}
230-
231215
/// Checks if the given `lookup` identifier is similar to any keyword symbol in `candidates`.
232216
///
233217
/// This is a specialized version of [`Symbol::find_similar`] that constructs an error when a

0 commit comments

Comments
 (0)