Skip to content

Commit f41bcbc

Browse files
authored
Rollup merge of rust-lang#147481 - hkBst:format-1, r=jackh726
format: some small cleanup Some small cleanup and some additional comments I did while trying to understand this code.
2 parents 6a4b600 + d4ecd71 commit f41bcbc

File tree

2 files changed

+19
-22
lines changed

2 files changed

+19
-22
lines changed

compiler/rustc_ast/src/token.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -881,11 +881,11 @@ impl Token {
881881
}
882882

883883
pub fn is_qpath_start(&self) -> bool {
884-
self == &Lt || self == &Shl
884+
matches!(self.kind, Lt | Shl)
885885
}
886886

887887
pub fn is_path_start(&self) -> bool {
888-
self == &PathSep
888+
self.kind == PathSep
889889
|| self.is_qpath_start()
890890
|| matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
891891
|| self.is_path_segment_keyword()

compiler/rustc_builtin_macros/src/format.rs

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,35 +69,26 @@ struct MacroInput {
6969
/// Ok((fmtstr, parsed arguments))
7070
/// ```
7171
fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, MacroInput> {
72-
let mut args = FormatArguments::new();
73-
7472
let mut p = ecx.new_parser_from_tts(tts);
7573

76-
if p.token == token::Eof {
77-
return Err(ecx.dcx().create_err(errors::FormatRequiresString { span: sp }));
78-
}
79-
80-
let first_token = &p.token;
81-
82-
let fmtstr = if let token::Literal(lit) = first_token.kind
83-
&& matches!(lit.kind, token::Str | token::StrRaw(_))
84-
{
74+
// parse the format string
75+
let fmtstr = match p.token.kind {
76+
token::Eof => return Err(ecx.dcx().create_err(errors::FormatRequiresString { span: sp })),
8577
// This allows us to properly handle cases when the first comma
8678
// after the format string is mistakenly replaced with any operator,
8779
// which cause the expression parser to eat too much tokens.
88-
p.parse_literal_maybe_minus()?
89-
} else {
80+
token::Literal(token::Lit { kind: token::Str | token::StrRaw(_), .. }) => {
81+
p.parse_literal_maybe_minus()?
82+
}
9083
// Otherwise, we fall back to the expression parser.
91-
p.parse_expr()?
84+
_ => p.parse_expr()?,
9285
};
9386

94-
// Only allow implicit captures to be used when the argument is a direct literal
95-
// instead of a macro expanding to one.
96-
let is_direct_literal = matches!(fmtstr.kind, ExprKind::Lit(_));
97-
87+
// parse comma FormatArgument pairs
88+
let mut args = FormatArguments::new();
9889
let mut first = true;
99-
10090
while p.token != token::Eof {
91+
// parse a comma, or else report an error
10192
if !p.eat(exp!(Comma)) {
10293
if first {
10394
p.clear_expected_token_types();
@@ -120,9 +111,11 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
120111
}
121112
}
122113
first = false;
114+
// accept a trailing comma
123115
if p.token == token::Eof {
124116
break;
125-
} // accept trailing commas
117+
}
118+
// parse a FormatArgument
126119
match p.token.ident() {
127120
Some((ident, _)) if p.look_ahead(1, |t| *t == token::Eq) => {
128121
p.bump();
@@ -156,6 +149,10 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
156149
}
157150
}
158151
}
152+
153+
// Only allow implicit captures for direct literals
154+
let is_direct_literal = matches!(fmtstr.kind, ExprKind::Lit(_));
155+
159156
Ok(MacroInput { fmtstr, args, is_direct_literal })
160157
}
161158

0 commit comments

Comments
 (0)