Skip to content

Commit

Permalink
parse_format optimize import use
Browse files Browse the repository at this point in the history
  • Loading branch information
hkBst committed Jan 23, 2025
1 parent 5f01e12 commit 08d7220
Showing 1 changed file with 17 additions and 24 deletions.
41 changes: 17 additions & 24 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@
#![warn(unreachable_pub)]
// tidy-alphabetical-end

use std::{iter, str, string};

pub use Alignment::*;
pub use Count::*;
pub use Piece::*;
pub use Position::*;
use rustc_lexer::unescape;

Expand Down Expand Up @@ -86,7 +83,7 @@ impl InnerOffset {
#[derive(Clone, Debug, PartialEq)]
pub enum Piece<'a> {
/// A literal string which should directly be emitted
String(&'a str),
Lit(&'a str),
/// This describes that formatting should process the next argument (as
/// specified inside) for emission.
NextArgument(Box<Argument<'a>>),
Expand Down Expand Up @@ -205,11 +202,11 @@ pub enum Count<'a> {
}

pub struct ParseError {
pub description: string::String,
pub note: Option<string::String>,
pub label: string::String,
pub description: String,
pub note: Option<String>,
pub label: String,
pub span: InnerSpan,
pub secondary_label: Option<(string::String, InnerSpan)>,
pub secondary_label: Option<(String, InnerSpan)>,
pub suggestion: Suggestion,
}

Expand All @@ -225,7 +222,7 @@ pub enum Suggestion {
/// `format!("{foo:?#}")` -> `format!("{foo:#?}")`
/// `format!("{foo:?x}")` -> `format!("{foo:x?}")`
/// `format!("{foo:?X}")` -> `format!("{foo:X?}")`
ReorderFormatParameter(InnerSpan, string::String),
ReorderFormatParameter(InnerSpan, String),
}

/// The parser structure for interpreting the input format string. This is
Expand All @@ -237,7 +234,7 @@ pub enum Suggestion {
pub struct Parser<'a> {
mode: ParseMode,
input: &'a str,
cur: iter::Peekable<str::CharIndices<'a>>,
cur: std::iter::Peekable<std::str::CharIndices<'a>>,
/// Error messages accumulated during parsing
pub errors: Vec<ParseError>,
/// Current position of implicit positional argument pointer
Expand Down Expand Up @@ -278,7 +275,7 @@ impl<'a> Iterator for Parser<'a> {
if self.consume('{') {
self.last_opening_brace = curr_last_brace;

Some(String(self.string(pos + 1)))
Some(Piece::Lit(self.string(pos + 1)))
} else {
let arg = self.argument(lbrace_end);
if let Some(rbrace_pos) = self.consume_closing_brace(&arg) {
Expand All @@ -299,13 +296,13 @@ impl<'a> Iterator for Parser<'a> {
_ => self.suggest_positional_arg_instead_of_captured_arg(arg),
}
}
Some(NextArgument(Box::new(arg)))
Some(Piece::NextArgument(Box::new(arg)))
}
}
'}' => {
self.cur.next();
if self.consume('}') {
Some(String(self.string(pos + 1)))
Some(Piece::Lit(self.string(pos + 1)))
} else {
let err_pos = self.to_span_index(pos);
self.err_with_note(
Expand All @@ -317,7 +314,7 @@ impl<'a> Iterator for Parser<'a> {
None
}
}
_ => Some(String(self.string(pos))),
_ => Some(Piece::Lit(self.string(pos))),
}
} else {
if self.is_source_literal {
Expand All @@ -336,7 +333,7 @@ impl<'a> Parser<'a> {
pub fn new(
s: &'a str,
style: Option<usize>,
snippet: Option<string::String>,
snippet: Option<String>,
append_newline: bool,
mode: ParseMode,
) -> Parser<'a> {
Expand Down Expand Up @@ -366,7 +363,7 @@ impl<'a> Parser<'a> {
/// Notifies of an error. The message doesn't actually need to be of type
/// String, but I think it does when this eventually uses conditions so it
/// might as well start using it now.
fn err<S1: Into<string::String>, S2: Into<string::String>>(
fn err<S1: Into<String>, S2: Into<String>>(
&mut self,
description: S1,
label: S2,
Expand All @@ -385,11 +382,7 @@ impl<'a> Parser<'a> {
/// Notifies of an error. The message doesn't actually need to be of type
/// String, but I think it does when this eventually uses conditions so it
/// might as well start using it now.
fn err_with_note<
S1: Into<string::String>,
S2: Into<string::String>,
S3: Into<string::String>,
>(
fn err_with_note<S1: Into<String>, S2: Into<String>, S3: Into<String>>(
&mut self,
description: S1,
label: S2,
Expand Down Expand Up @@ -974,7 +967,7 @@ impl<'a> Parser<'a> {
/// in order to properly synthesise the intra-string `Span`s for error diagnostics.
fn find_width_map_from_snippet(
input: &str,
snippet: Option<string::String>,
snippet: Option<String>,
str_style: Option<usize>,
) -> InputStringKind {
let snippet = match snippet {
Expand Down Expand Up @@ -1089,8 +1082,8 @@ fn find_width_map_from_snippet(
InputStringKind::Literal { width_mappings }
}

fn unescape_string(string: &str) -> Option<string::String> {
let mut buf = string::String::new();
fn unescape_string(string: &str) -> Option<String> {
let mut buf = String::new();
let mut ok = true;
unescape::unescape_unicode(string, unescape::Mode::Str, &mut |_, unescaped_char| {
match unescaped_char {
Expand Down

0 comments on commit 08d7220

Please sign in to comment.