Skip to content

Commit 67029ea

Browse files
committed
parse_format optimize import use
1 parent 5f01e12 commit 67029ea

File tree

7 files changed

+33
-38
lines changed

7 files changed

+33
-38
lines changed

compiler/rustc_builtin_macros/src/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -651,7 +651,7 @@ fn expand_preparsed_asm(
651651
.map(|span| template_span.from_inner(InnerSpan::new(span.start, span.end)));
652652
for piece in unverified_pieces {
653653
match piece {
654-
parse::Piece::String(s) => {
654+
parse::Piece::Lit(s) => {
655655
template.push(ast::InlineAsmTemplatePiece::String(s.to_string().into()))
656656
}
657657
parse::Piece::NextArgument(arg) => {

compiler/rustc_builtin_macros/src/format.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ fn make_format_args(
405405

406406
for piece in &pieces {
407407
match *piece {
408-
parse::Piece::String(s) => {
408+
parse::Piece::Lit(s) => {
409409
unfinished_literal.push_str(s);
410410
}
411411
parse::Piece::NextArgument(box parse::Argument { position, position_span, format }) => {

compiler/rustc_parse_format/src/lib.rs

+17-24
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,8 @@
1616
#![warn(unreachable_pub)]
1717
// tidy-alphabetical-end
1818

19-
use std::{iter, str, string};
20-
2119
pub use Alignment::*;
2220
pub use Count::*;
23-
pub use Piece::*;
2421
pub use Position::*;
2522
use rustc_lexer::unescape;
2623

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

207204
pub struct ParseError {
208-
pub description: string::String,
209-
pub note: Option<string::String>,
210-
pub label: string::String,
205+
pub description: String,
206+
pub note: Option<String>,
207+
pub label: String,
211208
pub span: InnerSpan,
212-
pub secondary_label: Option<(string::String, InnerSpan)>,
209+
pub secondary_label: Option<(String, InnerSpan)>,
213210
pub suggestion: Suggestion,
214211
}
215212

@@ -225,7 +222,7 @@ pub enum Suggestion {
225222
/// `format!("{foo:?#}")` -> `format!("{foo:#?}")`
226223
/// `format!("{foo:?x}")` -> `format!("{foo:x?}")`
227224
/// `format!("{foo:?X}")` -> `format!("{foo:X?}")`
228-
ReorderFormatParameter(InnerSpan, string::String),
225+
ReorderFormatParameter(InnerSpan, String),
229226
}
230227

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

281-
Some(String(self.string(pos + 1)))
278+
Some(Piece::Lit(self.string(pos + 1)))
282279
} else {
283280
let arg = self.argument(lbrace_end);
284281
if let Some(rbrace_pos) = self.consume_closing_brace(&arg) {
@@ -299,13 +296,13 @@ impl<'a> Iterator for Parser<'a> {
299296
_ => self.suggest_positional_arg_instead_of_captured_arg(arg),
300297
}
301298
}
302-
Some(NextArgument(Box::new(arg)))
299+
Some(Piece::NextArgument(Box::new(arg)))
303300
}
304301
}
305302
'}' => {
306303
self.cur.next();
307304
if self.consume('}') {
308-
Some(String(self.string(pos + 1)))
305+
Some(Piece::Lit(self.string(pos + 1)))
309306
} else {
310307
let err_pos = self.to_span_index(pos);
311308
self.err_with_note(
@@ -317,7 +314,7 @@ impl<'a> Iterator for Parser<'a> {
317314
None
318315
}
319316
}
320-
_ => Some(String(self.string(pos))),
317+
_ => Some(Piece::Lit(self.string(pos))),
321318
}
322319
} else {
323320
if self.is_source_literal {
@@ -336,7 +333,7 @@ impl<'a> Parser<'a> {
336333
pub fn new(
337334
s: &'a str,
338335
style: Option<usize>,
339-
snippet: Option<string::String>,
336+
snippet: Option<String>,
340337
append_newline: bool,
341338
mode: ParseMode,
342339
) -> Parser<'a> {
@@ -366,7 +363,7 @@ impl<'a> Parser<'a> {
366363
/// Notifies of an error. The message doesn't actually need to be of type
367364
/// String, but I think it does when this eventually uses conditions so it
368365
/// might as well start using it now.
369-
fn err<S1: Into<string::String>, S2: Into<string::String>>(
366+
fn err<S1: Into<String>, S2: Into<String>>(
370367
&mut self,
371368
description: S1,
372369
label: S2,
@@ -385,11 +382,7 @@ impl<'a> Parser<'a> {
385382
/// Notifies of an error. The message doesn't actually need to be of type
386383
/// String, but I think it does when this eventually uses conditions so it
387384
/// might as well start using it now.
388-
fn err_with_note<
389-
S1: Into<string::String>,
390-
S2: Into<string::String>,
391-
S3: Into<string::String>,
392-
>(
385+
fn err_with_note<S1: Into<String>, S2: Into<String>, S3: Into<String>>(
393386
&mut self,
394387
description: S1,
395388
label: S2,
@@ -974,7 +967,7 @@ impl<'a> Parser<'a> {
974967
/// in order to properly synthesise the intra-string `Span`s for error diagnostics.
975968
fn find_width_map_from_snippet(
976969
input: &str,
977-
snippet: Option<string::String>,
970+
snippet: Option<String>,
978971
str_style: Option<usize>,
979972
) -> InputStringKind {
980973
let snippet = match snippet {
@@ -1089,8 +1082,8 @@ fn find_width_map_from_snippet(
10891082
InputStringKind::Literal { width_mappings }
10901083
}
10911084

1092-
fn unescape_string(string: &str) -> Option<string::String> {
1093-
let mut buf = string::String::new();
1085+
fn unescape_string(string: &str) -> Option<String> {
1086+
let mut buf = String::new();
10941087
let mut ok = true;
10951088
unescape::unescape_unicode(string, unescape::Mode::Str, &mut |_, unescaped_char| {
10961089
match unescaped_char {

compiler/rustc_parse_format/src/tests.rs

+10-8
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use Piece::*;
2+
13
use super::*;
24

35
#[track_caller]
@@ -32,12 +34,12 @@ fn musterr(s: &str) {
3234

3335
#[test]
3436
fn simple() {
35-
same("asdf", &[String("asdf")]);
36-
same("a{{b", &[String("a"), String("{b")]);
37-
same("a}}b", &[String("a"), String("}b")]);
38-
same("a}}", &[String("a"), String("}")]);
39-
same("}}", &[String("}")]);
40-
same("\\}}", &[String("\\"), String("}")]);
37+
same("asdf", &[Lit("asdf")]);
38+
same("a{{b", &[Lit("a"), Lit("{b")]);
39+
same("a}}b", &[Lit("a"), Lit("}b")]);
40+
same("a}}", &[Lit("a"), Lit("}")]);
41+
same("}}", &[Lit("}")]);
42+
same("\\}}", &[Lit("\\"), Lit("}")]);
4143
}
4244

4345
#[test]
@@ -370,7 +372,7 @@ fn format_flags() {
370372
#[test]
371373
fn format_mixture() {
372374
same("abcd {3:x} efg", &[
373-
String("abcd "),
375+
Lit("abcd "),
374376
NextArgument(Box::new(Argument {
375377
position: ArgumentIs(3),
376378
position_span: InnerSpan { start: 7, end: 8 },
@@ -390,7 +392,7 @@ fn format_mixture() {
390392
ty_span: None,
391393
},
392394
})),
393-
String(" efg"),
395+
Lit(" efg"),
394396
]);
395397
}
396398
#[test]

compiler/rustc_trait_selection/src/error_reporting/traits/on_unimplemented.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -799,7 +799,7 @@ impl<'tcx> OnUnimplementedFormatString {
799799
let mut result = Ok(());
800800
for token in &mut parser {
801801
match token {
802-
Piece::String(_) => (), // Normal string, no need to check it
802+
Piece::Lit(_) => (), // Normal string, no need to check it
803803
Piece::NextArgument(a) => {
804804
let format_spec = a.format;
805805
if self.is_diagnostic_namespace_variant
@@ -946,7 +946,7 @@ impl<'tcx> OnUnimplementedFormatString {
946946
let item_context = (options.get(&sym::ItemContext)).unwrap_or(&empty_string);
947947
let constructed_message = (&mut parser)
948948
.map(|p| match p {
949-
Piece::String(s) => s.to_owned(),
949+
Piece::Lit(s) => s.to_owned(),
950950
Piece::NextArgument(a) => match a.position {
951951
Position::ArgumentNamed(arg) => {
952952
let s = Symbol::intern(arg);

src/tools/rust-analyzer/crates/hir-def/src/body/lower/asm.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ impl ExprCollector<'_> {
229229
};
230230
for piece in unverified_pieces {
231231
match piece {
232-
rustc_parse_format::Piece::String(_) => {}
232+
rustc_parse_format::Piece::Lit(_) => {}
233233
rustc_parse_format::Piece::NextArgument(arg) => {
234234
// let span = arg_spans.next();
235235

src/tools/rust-analyzer/crates/hir-def/src/hir/format_args.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ pub(crate) fn parse(
287287

288288
for piece in pieces {
289289
match piece {
290-
parse::Piece::String(s) => {
290+
parse::Piece::Lit(s) => {
291291
unfinished_literal.push_str(s);
292292
}
293293
parse::Piece::NextArgument(arg) => {

0 commit comments

Comments
 (0)