1
Fork 0

Rollup merge of #135943 - hkBst:opt_imports, r=estebank

Rename `Piece::String` to `Piece::Lit`

This renames Piece::String to Piece::Lit to avoid shadowing std::string::String and removes "pub use Piece::*;".
This commit is contained in:
León Orell Valerian Liehr 2025-01-29 03:12:19 +01:00 committed by GitHub
commit dbb092b671
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 33 additions and 38 deletions

View file

@ -651,7 +651,7 @@ fn expand_preparsed_asm(
.map(|span| template_span.from_inner(InnerSpan::new(span.start, span.end))); .map(|span| template_span.from_inner(InnerSpan::new(span.start, span.end)));
for piece in unverified_pieces { for piece in unverified_pieces {
match piece { match piece {
parse::Piece::String(s) => { parse::Piece::Lit(s) => {
template.push(ast::InlineAsmTemplatePiece::String(s.to_string().into())) template.push(ast::InlineAsmTemplatePiece::String(s.to_string().into()))
} }
parse::Piece::NextArgument(arg) => { parse::Piece::NextArgument(arg) => {

View file

@ -406,7 +406,7 @@ fn make_format_args(
for piece in &pieces { for piece in &pieces {
match *piece { match *piece {
parse::Piece::String(s) => { parse::Piece::Lit(s) => {
unfinished_literal.push_str(s); unfinished_literal.push_str(s);
} }
parse::Piece::NextArgument(box parse::Argument { position, position_span, format }) => { parse::Piece::NextArgument(box parse::Argument { position, position_span, format }) => {

View file

@ -16,11 +16,8 @@
#![warn(unreachable_pub)] #![warn(unreachable_pub)]
// tidy-alphabetical-end // tidy-alphabetical-end
use std::{iter, str, string};
pub use Alignment::*; pub use Alignment::*;
pub use Count::*; pub use Count::*;
pub use Piece::*;
pub use Position::*; pub use Position::*;
use rustc_lexer::unescape; use rustc_lexer::unescape;
@ -86,7 +83,7 @@ impl InnerOffset {
#[derive(Clone, Debug, PartialEq)] #[derive(Clone, Debug, PartialEq)]
pub enum Piece<'a> { pub enum Piece<'a> {
/// A literal string which should directly be emitted /// A literal string which should directly be emitted
String(&'a str), Lit(&'a str),
/// This describes that formatting should process the next argument (as /// This describes that formatting should process the next argument (as
/// specified inside) for emission. /// specified inside) for emission.
NextArgument(Box<Argument<'a>>), NextArgument(Box<Argument<'a>>),
@ -205,11 +202,11 @@ pub enum Count<'a> {
} }
pub struct ParseError { pub struct ParseError {
pub description: string::String, pub description: String,
pub note: Option<string::String>, pub note: Option<String>,
pub label: string::String, pub label: String,
pub span: InnerSpan, pub span: InnerSpan,
pub secondary_label: Option<(string::String, InnerSpan)>, pub secondary_label: Option<(String, InnerSpan)>,
pub suggestion: Suggestion, pub suggestion: Suggestion,
} }
@ -225,7 +222,7 @@ pub enum Suggestion {
/// `format!("{foo:?#}")` -> `format!("{foo:#?}")` /// `format!("{foo:?#}")` -> `format!("{foo:#?}")`
/// `format!("{foo:?x}")` -> `format!("{foo:x?}")` /// `format!("{foo:?x}")` -> `format!("{foo:x?}")`
/// `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 /// The parser structure for interpreting the input format string. This is
@ -237,7 +234,7 @@ pub enum Suggestion {
pub struct Parser<'a> { pub struct Parser<'a> {
mode: ParseMode, mode: ParseMode,
input: &'a str, input: &'a str,
cur: iter::Peekable<str::CharIndices<'a>>, cur: std::iter::Peekable<std::str::CharIndices<'a>>,
/// Error messages accumulated during parsing /// Error messages accumulated during parsing
pub errors: Vec<ParseError>, pub errors: Vec<ParseError>,
/// Current position of implicit positional argument pointer /// Current position of implicit positional argument pointer
@ -278,7 +275,7 @@ impl<'a> Iterator for Parser<'a> {
if self.consume('{') { if self.consume('{') {
self.last_opening_brace = curr_last_brace; self.last_opening_brace = curr_last_brace;
Some(String(self.string(pos + 1))) Some(Piece::Lit(self.string(pos + 1)))
} else { } else {
let arg = self.argument(lbrace_end); let arg = self.argument(lbrace_end);
if let Some(rbrace_pos) = self.consume_closing_brace(&arg) { if let Some(rbrace_pos) = self.consume_closing_brace(&arg) {
@ -299,13 +296,13 @@ impl<'a> Iterator for Parser<'a> {
_ => self.suggest_positional_arg_instead_of_captured_arg(arg), _ => self.suggest_positional_arg_instead_of_captured_arg(arg),
} }
} }
Some(NextArgument(Box::new(arg))) Some(Piece::NextArgument(Box::new(arg)))
} }
} }
'}' => { '}' => {
self.cur.next(); self.cur.next();
if self.consume('}') { if self.consume('}') {
Some(String(self.string(pos + 1))) Some(Piece::Lit(self.string(pos + 1)))
} else { } else {
let err_pos = self.to_span_index(pos); let err_pos = self.to_span_index(pos);
self.err_with_note( self.err_with_note(
@ -317,7 +314,7 @@ impl<'a> Iterator for Parser<'a> {
None None
} }
} }
_ => Some(String(self.string(pos))), _ => Some(Piece::Lit(self.string(pos))),
} }
} else { } else {
if self.is_source_literal { if self.is_source_literal {
@ -336,7 +333,7 @@ impl<'a> Parser<'a> {
pub fn new( pub fn new(
s: &'a str, s: &'a str,
style: Option<usize>, style: Option<usize>,
snippet: Option<string::String>, snippet: Option<String>,
append_newline: bool, append_newline: bool,
mode: ParseMode, mode: ParseMode,
) -> Parser<'a> { ) -> Parser<'a> {
@ -366,7 +363,7 @@ impl<'a> Parser<'a> {
/// Notifies of an error. The message doesn't actually need to be of type /// 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 /// String, but I think it does when this eventually uses conditions so it
/// might as well start using it now. /// 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, &mut self,
description: S1, description: S1,
label: S2, label: S2,
@ -385,11 +382,7 @@ impl<'a> Parser<'a> {
/// Notifies of an error. The message doesn't actually need to be of type /// 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 /// String, but I think it does when this eventually uses conditions so it
/// might as well start using it now. /// might as well start using it now.
fn err_with_note< fn err_with_note<S1: Into<String>, S2: Into<String>, S3: Into<String>>(
S1: Into<string::String>,
S2: Into<string::String>,
S3: Into<string::String>,
>(
&mut self, &mut self,
description: S1, description: S1,
label: S2, label: S2,
@ -968,7 +961,7 @@ impl<'a> Parser<'a> {
/// in order to properly synthesise the intra-string `Span`s for error diagnostics. /// in order to properly synthesise the intra-string `Span`s for error diagnostics.
fn find_width_map_from_snippet( fn find_width_map_from_snippet(
input: &str, input: &str,
snippet: Option<string::String>, snippet: Option<String>,
str_style: Option<usize>, str_style: Option<usize>,
) -> InputStringKind { ) -> InputStringKind {
let snippet = match snippet { let snippet = match snippet {
@ -1083,8 +1076,8 @@ fn find_width_map_from_snippet(
InputStringKind::Literal { width_mappings } InputStringKind::Literal { width_mappings }
} }
fn unescape_string(string: &str) -> Option<string::String> { fn unescape_string(string: &str) -> Option<String> {
let mut buf = string::String::new(); let mut buf = String::new();
let mut ok = true; let mut ok = true;
unescape::unescape_unicode(string, unescape::Mode::Str, &mut |_, unescaped_char| { unescape::unescape_unicode(string, unescape::Mode::Str, &mut |_, unescaped_char| {
match unescaped_char { match unescaped_char {

View file

@ -1,3 +1,5 @@
use Piece::*;
use super::*; use super::*;
#[track_caller] #[track_caller]
@ -32,12 +34,12 @@ fn musterr(s: &str) {
#[test] #[test]
fn simple() { fn simple() {
same("asdf", &[String("asdf")]); same("asdf", &[Lit("asdf")]);
same("a{{b", &[String("a"), String("{b")]); same("a{{b", &[Lit("a"), Lit("{b")]);
same("a}}b", &[String("a"), String("}b")]); same("a}}b", &[Lit("a"), Lit("}b")]);
same("a}}", &[String("a"), String("}")]); same("a}}", &[Lit("a"), Lit("}")]);
same("}}", &[String("}")]); same("}}", &[Lit("}")]);
same("\\}}", &[String("\\"), String("}")]); same("\\}}", &[Lit("\\"), Lit("}")]);
} }
#[test] #[test]
@ -370,7 +372,7 @@ fn format_flags() {
#[test] #[test]
fn format_mixture() { fn format_mixture() {
same("abcd {3:x} efg", &[ same("abcd {3:x} efg", &[
String("abcd "), Lit("abcd "),
NextArgument(Box::new(Argument { NextArgument(Box::new(Argument {
position: ArgumentIs(3), position: ArgumentIs(3),
position_span: InnerSpan { start: 7, end: 8 }, position_span: InnerSpan { start: 7, end: 8 },
@ -390,7 +392,7 @@ fn format_mixture() {
ty_span: None, ty_span: None,
}, },
})), })),
String(" efg"), Lit(" efg"),
]); ]);
} }
#[test] #[test]

View file

@ -799,7 +799,7 @@ impl<'tcx> OnUnimplementedFormatString {
let mut result = Ok(()); let mut result = Ok(());
for token in &mut parser { for token in &mut parser {
match token { match token {
Piece::String(_) => (), // Normal string, no need to check it Piece::Lit(_) => (), // Normal string, no need to check it
Piece::NextArgument(a) => { Piece::NextArgument(a) => {
let format_spec = a.format; let format_spec = a.format;
if self.is_diagnostic_namespace_variant if self.is_diagnostic_namespace_variant
@ -950,7 +950,7 @@ impl<'tcx> OnUnimplementedFormatString {
let item_context = (options.get(&sym::ItemContext)).unwrap_or(&empty_string); let item_context = (options.get(&sym::ItemContext)).unwrap_or(&empty_string);
let constructed_message = (&mut parser) let constructed_message = (&mut parser)
.map(|p| match p { .map(|p| match p {
Piece::String(s) => s.to_owned(), Piece::Lit(s) => s.to_owned(),
Piece::NextArgument(a) => match a.position { Piece::NextArgument(a) => match a.position {
Position::ArgumentNamed(arg) => { Position::ArgumentNamed(arg) => {
let s = Symbol::intern(arg); let s = Symbol::intern(arg);

View file

@ -229,7 +229,7 @@ impl ExprCollector<'_> {
}; };
for piece in unverified_pieces { for piece in unverified_pieces {
match piece { match piece {
rustc_parse_format::Piece::String(_) => {} rustc_parse_format::Piece::Lit(_) => {}
rustc_parse_format::Piece::NextArgument(arg) => { rustc_parse_format::Piece::NextArgument(arg) => {
// let span = arg_spans.next(); // let span = arg_spans.next();

View file

@ -287,7 +287,7 @@ pub(crate) fn parse(
for piece in pieces { for piece in pieces {
match piece { match piece {
parse::Piece::String(s) => { parse::Piece::Lit(s) => {
unfinished_literal.push_str(s); unfinished_literal.push_str(s);
} }
parse::Piece::NextArgument(arg) => { parse::Piece::NextArgument(arg) => {