Skip to main content

rustc_builtin_macros/
format.rs

1use std::ops::Range;
2
3use parse::Position::ArgumentNamed;
4use rustc_ast::tokenstream::TokenStream;
5use rustc_ast::{
6    Expr, ExprKind, FormatAlignment, FormatArgPosition, FormatArgPositionKind, FormatArgs,
7    FormatArgsPiece, FormatArgument, FormatArgumentKind, FormatArguments, FormatCount,
8    FormatDebugHex, FormatOptions, FormatPlaceholder, FormatSign, FormatTrait, Recovered, StmtKind,
9    token,
10};
11use rustc_data_structures::fx::FxHashSet;
12use rustc_errors::{
13    Applicability, BufferedEarlyLint, DecorateDiagCompat, Diag, Diagnostic, MultiSpan, PResult,
14    SingleLabelManySpans, listify, pluralize,
15};
16use rustc_expand::base::*;
17use rustc_lint_defs::LintId;
18use rustc_lint_defs::builtin::NAMED_ARGUMENTS_USED_POSITIONALLY;
19use rustc_parse::exp;
20use rustc_parse_format as parse;
21use rustc_span::{BytePos, ErrorGuaranteed, Ident, InnerSpan, Span, Symbol};
22
23use crate::diagnostics;
24use crate::util::{ExprToSpannedString, expr_to_spanned_string};
25
26// The format_args!() macro is expanded in three steps:
27//  1. First, `parse_args` will parse the `(literal, arg, arg, name=arg, name=arg)` syntax,
28//     but doesn't parse the template (the literal) itself.
29//  2. Second, `make_format_args` will parse the template, the format options, resolve argument references,
30//     produce diagnostics, and turn the whole thing into a `FormatArgs` AST node.
31//  3. Much later, in AST lowering (rustc_ast_lowering), that `FormatArgs` structure will be turned
32//     into the expression of type `core::fmt::Arguments`.
33
34// See rustc_ast/src/format.rs for the FormatArgs structure and glossary.
35
36// Only used in parse_args and report_invalid_references,
37// to indicate how a referred argument was used.
38#[derive(#[automatically_derived]
impl ::core::clone::Clone for PositionUsedAs {
    #[inline]
    fn clone(&self) -> PositionUsedAs {
        let _: ::core::clone::AssertParamIsClone<Option<Span>>;
        *self
    }
}Clone, #[automatically_derived]
impl ::core::marker::Copy for PositionUsedAs { }Copy, #[automatically_derived]
impl ::core::fmt::Debug for PositionUsedAs {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        match self {
            PositionUsedAs::Placeholder(__self_0) =>
                ::core::fmt::Formatter::debug_tuple_field1_finish(f,
                    "Placeholder", &__self_0),
            PositionUsedAs::Precision =>
                ::core::fmt::Formatter::write_str(f, "Precision"),
            PositionUsedAs::Width =>
                ::core::fmt::Formatter::write_str(f, "Width"),
        }
    }
}Debug, #[automatically_derived]
impl ::core::cmp::PartialEq for PositionUsedAs {
    #[inline]
    fn eq(&self, other: &PositionUsedAs) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr &&
            match (self, other) {
                (PositionUsedAs::Placeholder(__self_0),
                    PositionUsedAs::Placeholder(__arg1_0)) =>
                    __self_0 == __arg1_0,
                _ => true,
            }
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for PositionUsedAs {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {
        let _: ::core::cmp::AssertParamIsEq<Option<Span>>;
    }
}Eq)]
39enum PositionUsedAs {
40    Placeholder(Option<Span>),
41    Precision,
42    Width,
43}
44use PositionUsedAs::*;
45
46#[derive(#[automatically_derived]
impl ::core::fmt::Debug for MacroInput {
    #[inline]
    fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
        ::core::fmt::Formatter::debug_struct_field3_finish(f, "MacroInput",
            "fmtstr", &self.fmtstr, "args", &self.args, "is_direct_literal",
            &&self.is_direct_literal)
    }
}Debug)]
47struct MacroInput {
48    fmtstr: Box<Expr>,
49    args: FormatArguments,
50    /// Whether the first argument was a string literal or a result from eager macro expansion.
51    /// If it's not a string literal, we disallow implicit argument capturing.
52    ///
53    /// This does not correspond to whether we can treat spans to the literal normally, as the whole
54    /// invocation might be the result of another macro expansion, in which case this flag may still be true.
55    ///
56    /// See [RFC 2795] for more information.
57    ///
58    /// [RFC 2795]: https://rust-lang.github.io/rfcs/2795-format-args-implicit-identifiers.html#macro-hygiene
59    is_direct_literal: bool,
60}
61
62/// Parses the arguments from the given list of tokens, returning the diagnostic
63/// if there's a parse error so we can continue parsing other format!
64/// expressions.
65///
66/// If parsing succeeds, the return value is:
67///
68/// ```text
69/// Ok((fmtstr, parsed arguments))
70/// ```
71fn parse_args<'a>(ecx: &ExtCtxt<'a>, sp: Span, tts: TokenStream) -> PResult<'a, MacroInput> {
72    let mut p = ecx.new_parser_from_tts(tts);
73
74    // parse the format string
75    let fmtstr = match p.token.kind {
76        token::Eof => {
77            return Err(ecx.dcx().create_err(diagnostics::FormatRequiresString { span: sp }));
78        }
79        // This allows us to properly handle cases when the first comma
80        // after the format string is mistakenly replaced with any operator,
81        // which cause the expression parser to eat too much tokens.
82        token::Literal(token::Lit { kind: token::Str | token::StrRaw(_), .. }) => {
83            p.parse_literal_maybe_minus()?
84        }
85        // Otherwise, we fall back to the expression parser.
86        _ => p.parse_expr()?,
87    };
88
89    // parse comma FormatArgument pairs
90    let mut args = FormatArguments::new();
91    let mut first = true;
92    while p.token != token::Eof {
93        // parse a comma, or else report an error
94        if !p.eat(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: ::rustc_parse::parser::token_type::TokenType::Comma,
}exp!(Comma)) {
95            if first {
96                p.clear_expected_token_types();
97            }
98
99            match p.expect(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Comma,
    token_type: ::rustc_parse::parser::token_type::TokenType::Comma,
}exp!(Comma)) {
100                Err(err) => {
101                    if token::TokenKind::Comma.similar_tokens().contains(&p.token.kind) {
102                        // If a similar token is found, then it may be a typo. We
103                        // consider it as a comma, and continue parsing.
104                        err.emit();
105                        p.bump();
106                    } else {
107                        // Otherwise stop the parsing and return the error.
108                        return Err(err);
109                    }
110                }
111                Ok(Recovered::Yes(_)) => (),
112                Ok(Recovered::No) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
113            }
114        }
115        first = false;
116        // accept a trailing comma
117        if p.token == token::Eof {
118            break;
119        }
120        // parse a FormatArgument
121        match p.token.ident() {
122            Some((ident, _)) if p.look_ahead(1, |t| *t == token::Eq) => {
123                p.bump();
124                p.expect(::rustc_parse::parser::token_type::ExpTokenPair {
    tok: rustc_ast::token::Eq,
    token_type: ::rustc_parse::parser::token_type::TokenType::Eq,
}exp!(Eq))?;
125                let expr = p.parse_expr()?;
126                if let Some((_, prev)) = args.by_name(ident.name) {
127                    ecx.dcx().emit_err(diagnostics::FormatDuplicateArg {
128                        span: ident.span,
129                        prev: prev.kind.ident().unwrap().span,
130                        duplicate: ident.span,
131                        ident,
132                    });
133                    continue;
134                }
135                args.add(FormatArgument { kind: FormatArgumentKind::Named(ident), expr });
136            }
137            _ => {
138                let expr = p.parse_expr()?;
139                if !args.named_args().is_empty() {
140                    return Err(ecx.dcx().create_err(diagnostics::PositionalAfterNamed {
141                        span: expr.span,
142                        args: args
143                            .named_args()
144                            .iter()
145                            .filter_map(|a| a.kind.ident().map(|ident| (a, ident)))
146                            .map(|(arg, n)| n.span.to(arg.expr.span))
147                            .collect(),
148                    }));
149                }
150                args.add(FormatArgument { kind: FormatArgumentKind::Normal, expr });
151            }
152        }
153    }
154
155    // Only allow implicit captures for direct literals
156    let is_direct_literal = #[allow(non_exhaustive_omitted_patterns)] match fmtstr.kind {
    ExprKind::Lit(_) => true,
    _ => false,
}matches!(fmtstr.kind, ExprKind::Lit(_));
157
158    Ok(MacroInput { fmtstr, args, is_direct_literal })
159}
160
161fn make_format_args(
162    ecx: &mut ExtCtxt<'_>,
163    input: MacroInput,
164    append_newline: bool,
165    macro_span: Span,
166) -> ExpandResult<Result<FormatArgs, ErrorGuaranteed>, ()> {
167    let msg = "format argument must be a string literal";
168    let unexpanded_fmt_span = input.fmtstr.span;
169
170    let MacroInput { fmtstr: efmt, mut args, is_direct_literal } = input;
171
172    let ExprToSpannedString {
173        symbol: fmt_str,
174        span: fmt_span,
175        style: fmt_style,
176        uncooked_symbol: uncooked_fmt_str,
177    } = {
178        // Extract snippet so that we can check cases `{}`, `{:?}` and `{:#?}` and emit help for
179        // them later.
180        let snippet = if let ExprKind::Block(b, None) = &efmt.kind
181            && b.stmts.len() <= 1
182        {
183            Some(ecx.sess.source_map().span_to_snippet(unexpanded_fmt_span))
184        } else {
185            None
186        };
187
188        let ExpandResult::Ready(mac) = expr_to_spanned_string(ecx, efmt.clone(), msg) else {
189            return ExpandResult::Retry(());
190        };
191        match mac {
192            Ok(mut fmt) if append_newline => {
193                fmt.symbol = Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}\n", fmt.symbol))
    })format!("{}\n", fmt.symbol));
194                fmt
195            }
196            Ok(fmt) => fmt,
197            Err(err) => {
198                let guar = match err {
199                    Ok((mut err, suggested)) => {
200                        if !suggested {
201                            if let ExprKind::Block(block, None) = &efmt.kind
202                                && let [stmt] = block.stmts.as_slice()
203                                && let StmtKind::Expr(expr) = &stmt.kind
204                                && let ExprKind::Path(None, path) = &expr.kind
205                                && path.segments.len() == 1
206                                && path.segments[0].args.is_none()
207                            {
208                                err.multipart_suggestion(
209                                    "quote your inlined format argument to use as string literal",
210                                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(unexpanded_fmt_span.shrink_to_hi(), "\"".to_string()),
                (unexpanded_fmt_span.shrink_to_lo(), "\"".to_string())]))vec![
211                                        (unexpanded_fmt_span.shrink_to_hi(), "\"".to_string()),
212                                        (unexpanded_fmt_span.shrink_to_lo(), "\"".to_string()),
213                                    ],
214                                    Applicability::MaybeIncorrect,
215                                );
216                            } else {
217                                // `{}` or `()`
218                                let should_suggest = |kind: &ExprKind| -> bool {
219                                    match kind {
220                                        ExprKind::Block(b, None) if b.stmts.is_empty() => true,
221                                        ExprKind::Tup(v) if v.is_empty() => true,
222                                        _ => false,
223                                    }
224                                };
225
226                                let mut sugg_fmt = String::new();
227                                for kind in std::iter::once(&efmt.kind)
228                                    .chain(args.explicit_args().into_iter().map(|a| &a.expr.kind))
229                                {
230                                    sugg_fmt.push_str(if should_suggest(kind) {
231                                        "{:?} "
232                                    } else {
233                                        "{} "
234                                    });
235                                }
236                                sugg_fmt = sugg_fmt.trim_end().to_string();
237                                err.span_suggestion_verbose(
238                                    unexpanded_fmt_span.shrink_to_lo(),
239                                    "you might be missing a string literal to format with",
240                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\"{0}\", ", sugg_fmt))
    })format!("\"{sugg_fmt}\", "),
241                                    Applicability::MaybeIncorrect,
242                                );
243
244                                if let Some(Ok(snippet)) = snippet.as_ref() {
245                                    match snippet.as_str() {
246                                        "{}" | "{:?}" | "{:#?}" => {
247                                            err.span_suggestion_verbose(
248                                                unexpanded_fmt_span,
249                                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might want to enclose `{0}` with `\"\"`",
                snippet))
    })format!("you might want to enclose `{snippet}` with `\"\"`"),
250                                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\"{0}\"", snippet))
    })format!("\"{snippet}\""),
251                                                Applicability::MaybeIncorrect,
252                                            );
253                                        }
254                                        _ => {}
255                                    };
256                                }
257                            }
258                        }
259                        err.emit()
260                    }
261                    Err(guar) => guar,
262                };
263                return ExpandResult::Ready(Err(guar));
264            }
265        }
266    };
267
268    let str_style = match fmt_style {
269        rustc_ast::StrStyle::Cooked => None,
270        rustc_ast::StrStyle::Raw(raw) => Some(raw as usize),
271    };
272
273    let fmt_str = fmt_str.as_str(); // for the suggestions below
274    let fmt_snippet = ecx.source_map().span_to_snippet(unexpanded_fmt_span).ok();
275    let mut parser = parse::Parser::new(
276        fmt_str,
277        str_style,
278        fmt_snippet,
279        append_newline,
280        parse::ParseMode::Format,
281    );
282
283    let mut pieces = Vec::new();
284    while let Some(piece) = parser.next() {
285        if !parser.errors.is_empty() {
286            break;
287        } else {
288            pieces.push(piece);
289        }
290    }
291
292    let is_source_literal = parser.is_source_literal;
293
294    if !parser.errors.is_empty() {
295        let err = parser.errors.remove(0);
296        let sp = if is_source_literal {
297            fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end))
298        } else {
299            // The format string could be another macro invocation, e.g.:
300            //     format!(concat!("abc", "{}"), 4);
301            // However, `err.span` is an inner span relative to the *result* of
302            // the macro invocation, which is why we would get a nonsensical
303            // result calling `fmt_span.from_inner(err.span)` as above, and
304            // might even end up inside a multibyte character (issue #86085).
305            // Therefore, we conservatively report the error for the entire
306            // argument span here.
307            fmt_span
308        };
309        let mut e = diagnostics::InvalidFormatString {
310            span: sp,
311            note_: None,
312            label_: None,
313            sugg_: None,
314            desc: err.description,
315            label1: err.label,
316        };
317        if let Some(note) = err.note {
318            e.note_ = Some(diagnostics::InvalidFormatStringNote { note });
319        }
320        if let Some((label, span)) = err.secondary_label
321            && is_source_literal
322        {
323            e.label_ = Some(diagnostics::InvalidFormatStringLabel {
324                span: fmt_span.from_inner(InnerSpan::new(span.start, span.end)),
325                label,
326            });
327        }
328        match err.suggestion {
329            parse::Suggestion::None => {}
330            parse::Suggestion::UsePositional => {
331                let captured_arg_span =
332                    fmt_span.from_inner(InnerSpan::new(err.span.start, err.span.end));
333                if let Ok(arg) = ecx.source_map().span_to_snippet(captured_arg_span) {
334                    let span = match args.unnamed_args().last() {
335                        Some(arg) => arg.expr.span,
336                        None => fmt_span,
337                    };
338                    e.sugg_ = Some(diagnostics::InvalidFormatStringSuggestion::UsePositional {
339                        captured: captured_arg_span,
340                        len: args.unnamed_args().len().to_string(),
341                        span: span.shrink_to_hi(),
342                        arg,
343                    });
344                }
345            }
346            parse::Suggestion::RemoveRawIdent(span) => {
347                if is_source_literal {
348                    let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
349                    e.sugg_ =
350                        Some(diagnostics::InvalidFormatStringSuggestion::RemoveRawIdent { span })
351                }
352            }
353            parse::Suggestion::ReorderFormatParameter(span, replacement) => {
354                let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
355                e.sugg_ =
356                    Some(diagnostics::InvalidFormatStringSuggestion::ReorderFormatParameter {
357                        span,
358                        replacement,
359                    });
360            }
361            parse::Suggestion::AddMissingColon(span) => {
362                let span = fmt_span.from_inner(InnerSpan::new(span.start, span.end));
363                e.sugg_ =
364                    Some(diagnostics::InvalidFormatStringSuggestion::AddMissingColon { span });
365            }
366            parse::Suggestion::UseRustDebugPrintingMacro => {
367                // This targets `println!("{=}", x);` and `println!("{0=}", x);`
368                if let [arg] = args.all_args() {
369                    let expr_span = arg.expr.span;
370                    if let Ok(expr_snippet) = ecx.source_map().span_to_snippet(expr_span) {
371                        let replacement = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}!({1})", "dbg", expr_snippet))
    })format!("{}!({})", "dbg", expr_snippet);
372
373                        let call_span = macro_span.source_callsite();
374                        e.sugg_ = Some(
375                            diagnostics::InvalidFormatStringSuggestion::UseRustDebugPrintingMacro {
376                                macro_span: call_span,
377                                replacement,
378                            },
379                        );
380                    }
381                }
382            }
383        }
384        let guar = ecx.dcx().emit_err(e);
385        return ExpandResult::Ready(Err(guar));
386    }
387
388    let to_span = |inner_span: Range<usize>| {
389        is_source_literal.then(|| {
390            fmt_span.from_inner(InnerSpan { start: inner_span.start, end: inner_span.end })
391        })
392    };
393
394    let mut used = ::alloc::vec::from_elem(false, args.explicit_args().len())vec![false; args.explicit_args().len()];
395    let mut invalid_refs = Vec::new();
396    let mut numeric_references_to_named_arg = Vec::new();
397
398    enum ArgRef<'a> {
399        Index(usize),
400        Name(&'a str, Option<Span>),
401    }
402    use ArgRef::*;
403
404    let mut unnamed_arg_after_named_arg = false;
405
406    let mut lookup_arg = |arg: ArgRef<'_>,
407                          span: Option<Span>,
408                          used_as: PositionUsedAs,
409                          kind: FormatArgPositionKind|
410     -> FormatArgPosition {
411        let index = match arg {
412            Index(index) => {
413                if let Some(arg) = args.by_index(index) {
414                    used[index] = true;
415                    if arg.kind.ident().is_some() {
416                        // This was a named argument, but it was used as a positional argument.
417                        numeric_references_to_named_arg.push((index, span, used_as));
418                    }
419                    Ok(index)
420                } else {
421                    // Doesn't exist as an explicit argument.
422                    invalid_refs.push((index, span, used_as, kind));
423                    Err(index)
424                }
425            }
426            Name(name, span) => {
427                let name = Symbol::intern(name);
428                if let Some((index, _)) = args.by_name(name) {
429                    // Name found in `args`, so we resolve it to its index.
430                    if index < args.explicit_args().len() {
431                        // Mark it as used, if it was an explicit argument.
432                        used[index] = true;
433                    }
434                    Ok(index)
435                } else {
436                    // Name not found in `args`, so we add it as an implicitly captured argument.
437                    let span = span.unwrap_or(fmt_span);
438                    let ident = Ident::new(name, span);
439                    let expr = if is_direct_literal {
440                        ecx.expr_ident(span, ident)
441                    } else {
442                        // For the moment capturing variables from format strings expanded from macros is
443                        // disabled (see RFC #2795)
444                        let guar = ecx.dcx().emit_err(diagnostics::FormatNoArgNamed { span, name });
445                        unnamed_arg_after_named_arg = true;
446                        DummyResult::raw_expr(span, Some(guar))
447                    };
448                    Ok(args.add(FormatArgument { kind: FormatArgumentKind::Captured(ident), expr }))
449                }
450            }
451        };
452        FormatArgPosition { index, kind, span }
453    };
454
455    let mut template = Vec::new();
456    let mut unfinished_literal = String::new();
457    let mut placeholder_index = 0;
458
459    for piece in &pieces {
460        match piece.clone() {
461            parse::Piece::Lit(s) => {
462                unfinished_literal.push_str(s);
463            }
464            parse::Piece::NextArgument(parse::Argument { position, position_span, format }) => {
465                if !unfinished_literal.is_empty() {
466                    template.push(FormatArgsPiece::Literal(Symbol::intern(&unfinished_literal)));
467                    unfinished_literal.clear();
468                }
469
470                let span =
471                    parser.arg_places.get(placeholder_index).and_then(|s| to_span(s.clone()));
472                placeholder_index += 1;
473
474                let position_span = to_span(position_span);
475                let argument = match position {
476                    parse::ArgumentImplicitlyIs(i) => lookup_arg(
477                        Index(i),
478                        position_span,
479                        Placeholder(span),
480                        FormatArgPositionKind::Implicit,
481                    ),
482                    parse::ArgumentIs(i) => lookup_arg(
483                        Index(i),
484                        position_span,
485                        Placeholder(span),
486                        FormatArgPositionKind::Number,
487                    ),
488                    parse::ArgumentNamed(name) => lookup_arg(
489                        Name(name, position_span),
490                        position_span,
491                        Placeholder(span),
492                        FormatArgPositionKind::Named,
493                    ),
494                };
495
496                let alignment = match format.align {
497                    parse::AlignUnknown => None,
498                    parse::AlignLeft => Some(FormatAlignment::Left),
499                    parse::AlignRight => Some(FormatAlignment::Right),
500                    parse::AlignCenter => Some(FormatAlignment::Center),
501                };
502
503                let format_trait = match format.ty {
504                    "" => FormatTrait::Display,
505                    "?" => FormatTrait::Debug,
506                    "e" => FormatTrait::LowerExp,
507                    "E" => FormatTrait::UpperExp,
508                    "o" => FormatTrait::Octal,
509                    "p" => FormatTrait::Pointer,
510                    "b" => FormatTrait::Binary,
511                    "x" => FormatTrait::LowerHex,
512                    "X" => FormatTrait::UpperHex,
513                    _ => {
514                        invalid_placeholder_type_error(ecx, format.ty, format.ty_span, fmt_span);
515                        FormatTrait::Display
516                    }
517                };
518
519                let precision_span = format.precision_span.and_then(to_span);
520                let precision = match format.precision {
521                    parse::CountIs(n) => Some(FormatCount::Literal(n)),
522                    parse::CountIsName(name, name_span) => Some(FormatCount::Argument(lookup_arg(
523                        Name(name, to_span(name_span)),
524                        precision_span,
525                        Precision,
526                        FormatArgPositionKind::Named,
527                    ))),
528                    parse::CountIsParam(i) => Some(FormatCount::Argument(lookup_arg(
529                        Index(i),
530                        precision_span,
531                        Precision,
532                        FormatArgPositionKind::Number,
533                    ))),
534                    parse::CountIsStar(i) => Some(FormatCount::Argument(lookup_arg(
535                        Index(i),
536                        precision_span,
537                        Precision,
538                        FormatArgPositionKind::Implicit,
539                    ))),
540                    parse::CountImplied => None,
541                };
542
543                let width_span = format.width_span.and_then(to_span);
544                let width = match format.width {
545                    parse::CountIs(n) => Some(FormatCount::Literal(n)),
546                    parse::CountIsName(name, name_span) => Some(FormatCount::Argument(lookup_arg(
547                        Name(name, to_span(name_span)),
548                        width_span,
549                        Width,
550                        FormatArgPositionKind::Named,
551                    ))),
552                    parse::CountIsParam(i) => Some(FormatCount::Argument(lookup_arg(
553                        Index(i),
554                        width_span,
555                        Width,
556                        FormatArgPositionKind::Number,
557                    ))),
558                    parse::CountIsStar(_) => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
559                    parse::CountImplied => None,
560                };
561
562                template.push(FormatArgsPiece::Placeholder(FormatPlaceholder {
563                    argument,
564                    span,
565                    format_trait,
566                    format_options: FormatOptions {
567                        fill: format.fill,
568                        alignment,
569                        sign: format.sign.map(|s| match s {
570                            parse::Sign::Plus => FormatSign::Plus,
571                            parse::Sign::Minus => FormatSign::Minus,
572                        }),
573                        alternate: format.alternate,
574                        zero_pad: format.zero_pad,
575                        debug_hex: format.debug_hex.map(|s| match s {
576                            parse::DebugHex::Lower => FormatDebugHex::Lower,
577                            parse::DebugHex::Upper => FormatDebugHex::Upper,
578                        }),
579                        precision,
580                        width,
581                    },
582                }));
583            }
584        }
585    }
586
587    if !unfinished_literal.is_empty() {
588        template.push(FormatArgsPiece::Literal(Symbol::intern(&unfinished_literal)));
589    }
590
591    if !invalid_refs.is_empty() {
592        report_invalid_references(ecx, &invalid_refs, &template, fmt_span, &args, parser);
593    }
594
595    let unused = used
596        .iter()
597        .enumerate()
598        .filter(|&(_, used)| !used)
599        .map(|(i, _)| {
600            let named = #[allow(non_exhaustive_omitted_patterns)] match args.explicit_args()[i].kind {
    FormatArgumentKind::Named(_) => true,
    _ => false,
}matches!(args.explicit_args()[i].kind, FormatArgumentKind::Named(_));
601            (args.explicit_args()[i].expr.span, named)
602        })
603        .collect::<Vec<_>>();
604
605    let has_unused = !unused.is_empty();
606    if has_unused {
607        // If there's a lot of unused arguments,
608        // let's check if this format arguments looks like another syntax (printf / shell).
609        let detect_foreign_fmt = unused.len() > args.explicit_args().len() / 2;
610        let foreign_fmt_str =
611            if append_newline { fmt_str.strip_suffix('\n').unwrap_or(fmt_str) } else { fmt_str };
612        report_missing_placeholders(
613            ecx,
614            unused,
615            &used,
616            &args,
617            &pieces,
618            &invalid_refs,
619            detect_foreign_fmt,
620            str_style,
621            foreign_fmt_str,
622            uncooked_fmt_str.1.as_str(),
623            fmt_span,
624        );
625    }
626
627    // Only check for unused named argument names if there are no other errors to avoid causing
628    // too much noise in output errors, such as when a named argument is entirely unused.
629    if invalid_refs.is_empty() && !has_unused && !unnamed_arg_after_named_arg {
630        for &(index, span, used_as) in &numeric_references_to_named_arg {
631            let (position_sp_to_replace, position_sp_for_msg) = match used_as {
632                Placeholder(pspan) => (span, pspan),
633                Precision => {
634                    // Strip the leading `.` for precision.
635                    let span = span.map(|span| span.with_lo(span.lo() + BytePos(1)));
636                    (span, span)
637                }
638                Width => (span, span),
639            };
640            let arg_name = args.explicit_args()[index].kind.ident().unwrap();
641            ecx.buffered_early_lint.push(BufferedEarlyLint {
642                span: Some(arg_name.span.into()),
643                node_id: rustc_ast::CRATE_NODE_ID,
644                lint_id: LintId::of(NAMED_ARGUMENTS_USED_POSITIONALLY),
645                diagnostic: DecorateDiagCompat(Box::new(move |dcx, level, sess| {
646                    let (suggestion, name) =
647                        if let Some(positional_arg_to_replace) = position_sp_to_replace {
648                            let mut name = arg_name.name.to_string();
649                            let is_formatting_arg = #[allow(non_exhaustive_omitted_patterns)] match used_as {
    Width | Precision => true,
    _ => false,
}matches!(used_as, Width | Precision);
650                            if is_formatting_arg {
651                                name.push('$')
652                            };
653                            let span_to_replace = if let Ok(positional_arg_content) = sess
654                                .downcast_ref::<rustc_session::Session>()
655                                .expect("expected a `Session`")
656                                .source_map()
657                                .span_to_snippet(positional_arg_to_replace)
658                                && positional_arg_content.starts_with(':')
659                            {
660                                positional_arg_to_replace.shrink_to_lo()
661                            } else {
662                                positional_arg_to_replace
663                            };
664                            (Some(span_to_replace), name)
665                        } else {
666                            (None, String::new())
667                        };
668
669                    diagnostics::NamedArgumentUsedPositionally {
670                        named_arg_sp: arg_name.span,
671                        position_label_sp: position_sp_for_msg,
672                        suggestion,
673                        name,
674                        named_arg_name: arg_name.name.to_string(),
675                    }
676                    .into_diag(dcx, level)
677                })),
678            });
679        }
680    }
681
682    ExpandResult::Ready(Ok(FormatArgs {
683        span: fmt_span,
684        template,
685        arguments: args,
686        uncooked_fmt_str,
687        is_source_literal,
688    }))
689}
690
691fn invalid_placeholder_type_error(
692    ecx: &ExtCtxt<'_>,
693    ty: &str,
694    ty_span: Option<Range<usize>>,
695    fmt_span: Span,
696) {
697    let sp = ty_span.map(|sp| fmt_span.from_inner(InnerSpan::new(sp.start, sp.end)));
698    let suggs = if let Some(sp) = sp {
699        [
700            ("", "Display"),
701            ("?", "Debug"),
702            ("e", "LowerExp"),
703            ("E", "UpperExp"),
704            ("o", "Octal"),
705            ("p", "Pointer"),
706            ("b", "Binary"),
707            ("x", "LowerHex"),
708            ("X", "UpperHex"),
709        ]
710        .into_iter()
711        .map(|(fmt, trait_name)| diagnostics::FormatUnknownTraitSugg { span: sp, fmt, trait_name })
712        .collect()
713    } else {
714        ::alloc::vec::Vec::new()vec![]
715    };
716    ecx.dcx().emit_err(diagnostics::FormatUnknownTrait { span: sp.unwrap_or(fmt_span), ty, suggs });
717}
718
719fn report_missing_placeholders(
720    ecx: &ExtCtxt<'_>,
721    unused: Vec<(Span, bool)>,
722    used: &[bool],
723    args: &FormatArguments,
724    pieces: &[parse::Piece<'_>],
725    invalid_refs: &[(usize, Option<Span>, PositionUsedAs, FormatArgPositionKind)],
726    detect_foreign_fmt: bool,
727    str_style: Option<usize>,
728    fmt_str: &str,
729    uncooked_fmt_str: &str,
730    fmt_span: Span,
731) {
732    let mut diag = if let &[(span, named)] = &unused[..] {
733        ecx.dcx().create_err(diagnostics::FormatUnusedArg { span, named })
734    } else {
735        let unused_labels = unused
736            .iter()
737            .map(|&(span, named)| diagnostics::FormatUnusedArg { span, named })
738            .collect();
739        let unused_spans = unused.iter().map(|&(span, _)| span).collect();
740        ecx.dcx().create_err(diagnostics::FormatUnusedArgs {
741            fmt: fmt_span,
742            unused: unused_spans,
743            unused_labels,
744        })
745    };
746
747    let placeholders = pieces
748        .iter()
749        .filter_map(|piece| {
750            if let parse::Piece::NextArgument(argument) = piece
751                && let ArgumentNamed(binding) = argument.position
752            {
753                let span = fmt_span.from_inner(InnerSpan::new(
754                    argument.position_span.start,
755                    argument.position_span.end,
756                ));
757                Some((span, binding))
758            } else {
759                None
760            }
761        })
762        .collect::<Vec<_>>();
763
764    if !placeholders.is_empty() {
765        if let Some(new_diag) = report_redundant_format_arguments(ecx, args, used, placeholders) {
766            diag.cancel();
767            new_diag.emit();
768            return;
769        }
770    }
771
772    // Used to ensure we only report translations for *one* kind of foreign format.
773    let mut found_foreign = false;
774
775    // Decide if we want to look for foreign formatting directives.
776    if detect_foreign_fmt {
777        use super::format_foreign as foreign;
778
779        // The set of foreign substitutions we've explained. This prevents spamming the user
780        // with `%d should be written as {}` over and over again.
781        let mut explained = FxHashSet::default();
782
783        macro_rules! check_foreign {
784            ($kind:ident) => {{
785                let mut show_doc_note = false;
786
787                let mut suggestions = vec![];
788                // account for `"` and account for raw strings `r#`
789                let padding = str_style.map(|i| i + 2).unwrap_or(1);
790                for sub in foreign::$kind::iter_subs(fmt_str, padding) {
791                    let (trn, success) = match sub.translate() {
792                        Ok(trn) => (trn, true),
793                        Err(Some(msg)) => (msg, false),
794
795                        // If it has no translation, don't call it out specifically.
796                        _ => continue,
797                    };
798
799                    let pos = sub.position();
800                    if !explained.insert(sub.to_string()) {
801                        continue;
802                    }
803
804                    if !found_foreign {
805                        found_foreign = true;
806                        show_doc_note = true;
807                    }
808
809                    let sp = fmt_span.from_inner(pos);
810
811                    if success {
812                        suggestions.push((sp, trn));
813                    } else {
814                        diag.span_note(
815                            sp,
816                            format!("format specifiers use curly braces, and {}", trn),
817                        );
818                    }
819                }
820
821                if show_doc_note {
822                    diag.note(concat!(
823                        stringify!($kind),
824                        " formatting is not supported; see the documentation for `std::fmt`",
825                    ));
826                }
827                if suggestions.len() > 0 {
828                    diag.multipart_suggestion(
829                        "format specifiers use curly braces",
830                        suggestions,
831                        Applicability::MachineApplicable,
832                    );
833                }
834            }};
835        }
836
837        {
    let mut show_doc_note = false;
    let mut suggestions = ::alloc::vec::Vec::new();
    let padding = str_style.map(|i| i + 2).unwrap_or(1);
    for sub in foreign::printf::iter_subs(fmt_str, padding) {
        let (trn, success) =
            match sub.translate() {
                Ok(trn) => (trn, true),
                Err(Some(msg)) => (msg, false),
                _ => continue,
            };
        let pos = sub.position();
        if !explained.insert(sub.to_string()) { continue; }
        if !found_foreign { found_foreign = true; show_doc_note = true; }
        let sp = fmt_span.from_inner(pos);
        if success {
            suggestions.push((sp, trn));
        } else {
            diag.span_note(sp,
                ::alloc::__export::must_use({
                        ::alloc::fmt::format(format_args!("format specifiers use curly braces, and {0}",
                                trn))
                    }));
        }
    }
    if show_doc_note {
        diag.note("printf formatting is not supported; see the documentation for `std::fmt`");
    }
    if suggestions.len() > 0 {
        diag.multipart_suggestion("format specifiers use curly braces",
            suggestions, Applicability::MachineApplicable);
    }
};check_foreign!(printf);
838        if !found_foreign {
839            {
    let mut show_doc_note = false;
    let mut suggestions = ::alloc::vec::Vec::new();
    let padding = str_style.map(|i| i + 2).unwrap_or(1);
    for sub in foreign::shell::iter_subs(fmt_str, padding) {
        let (trn, success) =
            match sub.translate() {
                Ok(trn) => (trn, true),
                Err(Some(msg)) => (msg, false),
                _ => continue,
            };
        let pos = sub.position();
        if !explained.insert(sub.to_string()) { continue; }
        if !found_foreign { found_foreign = true; show_doc_note = true; }
        let sp = fmt_span.from_inner(pos);
        if success {
            suggestions.push((sp, trn));
        } else {
            diag.span_note(sp,
                ::alloc::__export::must_use({
                        ::alloc::fmt::format(format_args!("format specifiers use curly braces, and {0}",
                                trn))
                    }));
        }
    }
    if show_doc_note {
        diag.note("shell formatting is not supported; see the documentation for `std::fmt`");
    }
    if suggestions.len() > 0 {
        diag.multipart_suggestion("format specifiers use curly braces",
            suggestions, Applicability::MachineApplicable);
    }
};check_foreign!(shell);
840        }
841    }
842    if !found_foreign && unused.len() == 1 {
843        diag.span_label(fmt_span, "formatting specifier missing");
844    }
845
846    if !found_foreign && invalid_refs.is_empty() {
847        // Show example if user didn't use any format specifiers
848        let show_example = !used.contains(&true);
849
850        if !show_example {
851            if unused.len() > 1 {
852                diag.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider adding {0} format specifiers",
                unused.len()))
    })format!("consider adding {} format specifiers", unused.len()));
853            }
854        } else {
855            let msg = if unused.len() == 1 {
856                "a format specifier".to_string()
857            } else {
858                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} format specifiers",
                unused.len()))
    })format!("{} format specifiers", unused.len())
859            };
860
861            let sugg = match str_style {
862                None => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\"{0}{1}\"", uncooked_fmt_str,
                "{}".repeat(unused.len())))
    })format!("\"{}{}\"", uncooked_fmt_str, "{}".repeat(unused.len())),
863                Some(n_hashes) => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("r{0}\"{2}{1}\"{0}",
                "#".repeat(n_hashes), "{}".repeat(unused.len()),
                uncooked_fmt_str))
    })format!(
864                    "r{hashes}\"{uncooked_fmt_str}{fmt_specifiers}\"{hashes}",
865                    hashes = "#".repeat(n_hashes),
866                    fmt_specifiers = "{}".repeat(unused.len())
867                ),
868            };
869            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("format specifiers use curly braces, consider adding {0}",
                msg))
    })format!("format specifiers use curly braces, consider adding {msg}");
870
871            diag.span_suggestion_verbose(fmt_span, msg, sugg, Applicability::MaybeIncorrect);
872        }
873    }
874
875    diag.emit();
876}
877
878/// This function detects and reports unused format!() arguments that are
879/// redundant due to implicit captures (e.g. `format!("{x}", x)`).
880fn report_redundant_format_arguments<'a>(
881    ecx: &ExtCtxt<'a>,
882    args: &FormatArguments,
883    used: &[bool],
884    placeholders: Vec<(Span, &str)>,
885) -> Option<Diag<'a>> {
886    let mut fmt_arg_indices = ::alloc::vec::Vec::new()vec![];
887    let mut args_spans = ::alloc::vec::Vec::new()vec![];
888    let mut fmt_spans = ::alloc::vec::Vec::new()vec![];
889
890    for (i, unnamed_arg) in args.unnamed_args().iter().enumerate().rev() {
891        let Some(ty) = unnamed_arg.expr.to_ty() else { continue };
892        let Some(argument_binding) = ty.kind.is_simple_path() else { continue };
893        let argument_binding = argument_binding.as_str();
894
895        if used[i] {
896            continue;
897        }
898
899        let matching_placeholders = placeholders
900            .iter()
901            .filter(|(_, inline_binding)| argument_binding == *inline_binding)
902            .map(|(span, _)| span)
903            .collect::<Vec<_>>();
904
905        if !matching_placeholders.is_empty() {
906            fmt_arg_indices.push(i);
907            args_spans.push(unnamed_arg.expr.span);
908            for span in &matching_placeholders {
909                if fmt_spans.contains(*span) {
910                    continue;
911                }
912                fmt_spans.push(**span);
913            }
914        }
915    }
916
917    if !args_spans.is_empty() {
918        let multispan = MultiSpan::from(fmt_spans);
919        let mut suggestion_spans = ::alloc::vec::Vec::new()vec![];
920
921        for (arg_span, fmt_arg_idx) in args_spans.iter().zip(fmt_arg_indices.iter()) {
922            let span = if fmt_arg_idx + 1 == args.explicit_args().len() {
923                *arg_span
924            } else {
925                arg_span.until(args.explicit_args()[*fmt_arg_idx + 1].expr.span)
926            };
927
928            suggestion_spans.push(span);
929        }
930
931        let sugg = if args.named_args().len() == 0 {
932            Some(diagnostics::FormatRedundantArgsSugg { spans: suggestion_spans })
933        } else {
934            None
935        };
936
937        return Some(ecx.dcx().create_err(diagnostics::FormatRedundantArgs {
938            n: args_spans.len(),
939            span: MultiSpan::from(args_spans),
940            note: multispan,
941            sugg,
942        }));
943    }
944
945    None
946}
947
948/// Handle invalid references to positional arguments. Output different
949/// errors for the case where all arguments are positional and for when
950/// there are named arguments or numbered positional arguments in the
951/// format string.
952fn report_invalid_references(
953    ecx: &ExtCtxt<'_>,
954    invalid_refs: &[(usize, Option<Span>, PositionUsedAs, FormatArgPositionKind)],
955    template: &[FormatArgsPiece],
956    fmt_span: Span,
957    args: &FormatArguments,
958    parser: parse::Parser<'_>,
959) {
960    let num_args_desc = match args.explicit_args().len() {
961        0 => "no arguments were given".to_string(),
962        1 => "there is 1 argument".to_string(),
963        n => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("there are {0} arguments", n))
    })format!("there are {n} arguments"),
964    };
965
966    let mut e;
967
968    if template.iter().all(|piece| match piece {
969        FormatArgsPiece::Placeholder(FormatPlaceholder {
970            argument: FormatArgPosition { kind: FormatArgPositionKind::Number, .. },
971            ..
972        }) => false,
973        FormatArgsPiece::Placeholder(FormatPlaceholder {
974            format_options:
975                FormatOptions {
976                    precision:
977                        Some(FormatCount::Argument(FormatArgPosition {
978                            kind: FormatArgPositionKind::Number,
979                            ..
980                        })),
981                    ..
982                }
983                | FormatOptions {
984                    width:
985                        Some(FormatCount::Argument(FormatArgPosition {
986                            kind: FormatArgPositionKind::Number,
987                            ..
988                        })),
989                    ..
990                },
991            ..
992        }) => false,
993        _ => true,
994    }) {
995        // There are no numeric positions.
996        // Collect all the implicit positions:
997        let mut spans = Vec::new();
998        let mut num_placeholders = 0;
999        let mut has_white_space_only_missing_arg = false;
1000        for piece in template {
1001            let mut placeholder = None;
1002            // `{arg:.*}`
1003            if let FormatArgsPiece::Placeholder(FormatPlaceholder {
1004                format_options:
1005                    FormatOptions {
1006                        precision:
1007                            Some(FormatCount::Argument(FormatArgPosition {
1008                                span,
1009                                kind: FormatArgPositionKind::Implicit,
1010                                ..
1011                            })),
1012                        ..
1013                    },
1014                ..
1015            }) = piece
1016            {
1017                placeholder = *span;
1018                num_placeholders += 1;
1019            }
1020            // `{}`
1021            if let FormatArgsPiece::Placeholder(FormatPlaceholder {
1022                argument: FormatArgPosition { kind: FormatArgPositionKind::Implicit, index, .. },
1023                span,
1024                ..
1025            }) = piece
1026            {
1027                placeholder = *span;
1028                num_placeholders += 1;
1029                //  Check whether there's any non-space whitespace in the placeholder. If so, we should emit a note suggesting an escaping `{`.
1030                if index.is_err()
1031                    && let Some(span) = span
1032                    && let Ok(snippet) = ecx.source_map().span_to_snippet(*span)
1033                    && snippet.chars().any(|c| c.is_whitespace() && c != ' ')
1034                {
1035                    has_white_space_only_missing_arg = true;
1036                }
1037            }
1038            // For `{:.*}`, we only push one span.
1039            spans.extend(placeholder);
1040        }
1041        let span = if spans.is_empty() {
1042            MultiSpan::from_span(fmt_span)
1043        } else {
1044            MultiSpan::from_spans(spans)
1045        };
1046        e = ecx.dcx().create_err(diagnostics::FormatPositionalMismatch {
1047            span,
1048            n: num_placeholders,
1049            desc: num_args_desc,
1050            highlight: SingleLabelManySpans {
1051                spans: args.explicit_args().iter().map(|arg| arg.expr.span).collect(),
1052                label: "",
1053            },
1054        });
1055        // Point out `{:.*}` placeholders: those take an extra argument.
1056        let mut has_precision_star = false;
1057        for piece in template {
1058            if let FormatArgsPiece::Placeholder(FormatPlaceholder {
1059                format_options:
1060                    FormatOptions {
1061                        precision:
1062                            Some(FormatCount::Argument(FormatArgPosition {
1063                                index,
1064                                span: Some(span),
1065                                kind: FormatArgPositionKind::Implicit,
1066                                ..
1067                            })),
1068                        ..
1069                    },
1070                ..
1071            }) = piece
1072            {
1073                let (Ok(index) | Err(index)) = index;
1074                has_precision_star = true;
1075                e.span_label(
1076                    *span,
1077                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this precision flag adds an extra required argument at position {0}, which is why there {1} expected",
                index,
                if num_placeholders == 1 {
                    "is 1 argument".to_string()
                } else {
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("are {0} arguments",
                                    num_placeholders))
                        })
                }))
    })format!(
1078                        "this precision flag adds an extra required argument at position {}, which is why there {} expected",
1079                        index,
1080                        if num_placeholders == 1 {
1081                            "is 1 argument".to_string()
1082                        } else {
1083                            format!("are {num_placeholders} arguments")
1084                        },
1085                    ),
1086                );
1087            }
1088        }
1089        if has_precision_star {
1090            e.note("positional arguments are zero-based");
1091        }
1092        if has_white_space_only_missing_arg {
1093            e.note("if you intended to print `{`, you can escape it with `{{`");
1094        }
1095    } else {
1096        let mut indexes: Vec<_> = invalid_refs.iter().map(|&(index, _, _, _)| index).collect();
1097        // Avoid `invalid reference to positional arguments 7 and 7 (there is 1 argument)`
1098        // for `println!("{7:7$}", 1);`
1099        indexes.sort();
1100        indexes.dedup();
1101        let span: MultiSpan = if !parser.is_source_literal || parser.arg_places.is_empty() {
1102            MultiSpan::from_span(fmt_span)
1103        } else {
1104            MultiSpan::from_spans(invalid_refs.iter().filter_map(|&(_, span, _, _)| span).collect())
1105        };
1106        let arg_list = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("argument{0} {1}",
                if indexes.len() == 1 { "" } else { "s" },
                listify(&indexes,
                        |i: &usize| i.to_string()).unwrap_or_default()))
    })format!(
1107            "argument{} {}",
1108            pluralize!(indexes.len()),
1109            listify(&indexes, |i: &usize| i.to_string()).unwrap_or_default()
1110        );
1111        e = ecx.dcx().struct_span_err(
1112            span,
1113            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("invalid reference to positional {0} ({1})",
                arg_list, num_args_desc))
    })format!("invalid reference to positional {arg_list} ({num_args_desc})"),
1114        );
1115        e.note("positional arguments are zero-based");
1116    }
1117
1118    if template.iter().any(|piece| match piece {
1119        FormatArgsPiece::Placeholder(FormatPlaceholder { format_options: f, .. }) => {
1120            *f != FormatOptions::default()
1121        }
1122        _ => false,
1123    }) {
1124        e.note("for information about formatting flags, visit https://doc.rust-lang.org/std/fmt/index.html");
1125    }
1126
1127    e.emit();
1128}
1129
1130fn expand_format_args_impl<'cx>(
1131    ecx: &'cx mut ExtCtxt<'_>,
1132    mut sp: Span,
1133    tts: TokenStream,
1134    nl: bool,
1135) -> MacroExpanderResult<'cx> {
1136    sp = ecx.with_def_site_ctxt(sp);
1137    ExpandResult::Ready(match parse_args(ecx, sp, tts) {
1138        Ok(input) => {
1139            let ExpandResult::Ready(mac) = make_format_args(ecx, input, nl, sp) else {
1140                return ExpandResult::Retry(());
1141            };
1142            match mac {
1143                Ok(format_args) => {
1144                    MacEager::expr(ecx.expr(sp, ExprKind::FormatArgs(Box::new(format_args))))
1145                }
1146                Err(guar) => MacEager::expr(DummyResult::raw_expr(sp, Some(guar))),
1147            }
1148        }
1149        Err(err) => {
1150            let guar = err.emit();
1151            DummyResult::any(sp, guar)
1152        }
1153    })
1154}
1155
1156pub(crate) fn expand_format_args<'cx>(
1157    ecx: &'cx mut ExtCtxt<'_>,
1158    sp: Span,
1159    tts: TokenStream,
1160) -> MacroExpanderResult<'cx> {
1161    expand_format_args_impl(ecx, sp, tts, false)
1162}
1163
1164pub(crate) fn expand_format_args_nl<'cx>(
1165    ecx: &'cx mut ExtCtxt<'_>,
1166    sp: Span,
1167    tts: TokenStream,
1168) -> MacroExpanderResult<'cx> {
1169    expand_format_args_impl(ecx, sp, tts, true)
1170}