Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions compiler/rustc_ast/src/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,11 +881,11 @@ impl Token {
}

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

pub fn is_path_start(&self) -> bool {
self == &PathSep
self.kind == PathSep
|| self.is_qpath_start()
|| matches!(self.is_metavar_seq(), Some(MetaVarKind::Path))
|| self.is_path_segment_keyword()
Expand Down
37 changes: 17 additions & 20 deletions compiler/rustc_builtin_macros/src/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,35 +69,26 @@ struct MacroInput {
/// Ok((fmtstr, parsed arguments))
/// ```
fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, MacroInput> {
let mut args = FormatArguments::new();

let mut p = ecx.new_parser_from_tts(tts);

if p.token == token::Eof {
return Err(ecx.dcx().create_err(errors::FormatRequiresString { span: sp }));
}

let first_token = &p.token;

let fmtstr = if let token::Literal(lit) = first_token.kind
&& matches!(lit.kind, token::Str | token::StrRaw(_))
{
// parse the format string
let fmtstr = match p.token.kind {
token::Eof => return Err(ecx.dcx().create_err(errors::FormatRequiresString { span: sp })),
// This allows us to properly handle cases when the first comma
// after the format string is mistakenly replaced with any operator,
// which cause the expression parser to eat too much tokens.
p.parse_literal_maybe_minus()?
} else {
token::Literal(token::Lit { kind: token::Str | token::StrRaw(_), .. }) => {
p.parse_literal_maybe_minus()?
}
// Otherwise, we fall back to the expression parser.
p.parse_expr()?
_ => p.parse_expr()?,
};

// Only allow implicit captures to be used when the argument is a direct literal
// instead of a macro expanding to one.
let is_direct_literal = matches!(fmtstr.kind, ExprKind::Lit(_));

// parse comma FormatArgument pairs
let mut args = FormatArguments::new();
let mut first = true;

while p.token != token::Eof {
// parse a comma, or else report an error
if !p.eat(exp!(Comma)) {
if first {
p.clear_expected_token_types();
Expand All @@ -120,9 +111,11 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
}
}
first = false;
// accept a trailing comma
if p.token == token::Eof {
break;
} // accept trailing commas
}
// parse a FormatArgument
match p.token.ident() {
Some((ident, _)) if p.look_ahead(1, |t| *t == token::Eq) => {
p.bump();
Expand Down Expand Up @@ -156,6 +149,10 @@ fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a,
}
}
}

// Only allow implicit captures for direct literals
let is_direct_literal = matches!(fmtstr.kind, ExprKind::Lit(_));

Ok(MacroInput { fmtstr, args, is_direct_literal })
}

Expand Down
Loading