]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/format.rs
Detect bare blocks with type ascription that were meant to be a `struct` literal
[rust.git] / compiler / rustc_builtin_macros / src / format.rs
1 use ArgumentType::*;
2 use Position::*;
3
4 use rustc_ast as ast;
5 use rustc_ast::ptr::P;
6 use rustc_ast::tokenstream::TokenStream;
7 use rustc_ast::{token, BlockCheckMode, UnsafeSource};
8 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
9 use rustc_errors::{pluralize, Applicability, DiagnosticBuilder};
10 use rustc_expand::base::{self, *};
11 use rustc_parse_format as parse;
12 use rustc_span::symbol::{sym, Ident, Symbol};
13 use rustc_span::{MultiSpan, Span};
14
15 use std::borrow::Cow;
16 use std::collections::hash_map::Entry;
17
18 #[derive(PartialEq)]
19 enum ArgumentType {
20     Placeholder(&'static str),
21     Count,
22 }
23
24 enum Position {
25     Exact(usize),
26     Named(Symbol),
27 }
28
29 struct Context<'a, 'b> {
30     ecx: &'a mut ExtCtxt<'b>,
31     /// The macro's call site. References to unstable formatting internals must
32     /// use this span to pass the stability checker.
33     macsp: Span,
34     /// The span of the format string literal.
35     fmtsp: Span,
36
37     /// List of parsed argument expressions.
38     /// Named expressions are resolved early, and are appended to the end of
39     /// argument expressions.
40     ///
41     /// Example showing the various data structures in motion:
42     ///
43     /// * Original: `"{foo:o} {:o} {foo:x} {0:x} {1:o} {:x} {1:x} {0:o}"`
44     /// * Implicit argument resolution: `"{foo:o} {0:o} {foo:x} {0:x} {1:o} {1:x} {1:x} {0:o}"`
45     /// * Name resolution: `"{2:o} {0:o} {2:x} {0:x} {1:o} {1:x} {1:x} {0:o}"`
46     /// * `arg_types` (in JSON): `[[0, 1, 0], [0, 1, 1], [0, 1]]`
47     /// * `arg_unique_types` (in simplified JSON): `[["o", "x"], ["o", "x"], ["o", "x"]]`
48     /// * `names` (in JSON): `{"foo": 2}`
49     args: Vec<P<ast::Expr>>,
50     /// Placeholder slot numbers indexed by argument.
51     arg_types: Vec<Vec<usize>>,
52     /// Unique format specs seen for each argument.
53     arg_unique_types: Vec<Vec<ArgumentType>>,
54     /// Map from named arguments to their resolved indices.
55     names: FxHashMap<Symbol, usize>,
56
57     /// The latest consecutive literal strings, or empty if there weren't any.
58     literal: String,
59
60     /// Collection of the compiled `rt::Argument` structures
61     pieces: Vec<P<ast::Expr>>,
62     /// Collection of string literals
63     str_pieces: Vec<P<ast::Expr>>,
64     /// Stays `true` if all formatting parameters are default (as in "{}{}").
65     all_pieces_simple: bool,
66
67     /// Mapping between positional argument references and indices into the
68     /// final generated static argument array. We record the starting indices
69     /// corresponding to each positional argument, and number of references
70     /// consumed so far for each argument, to facilitate correct `Position`
71     /// mapping in `build_piece`. In effect this can be seen as a "flattened"
72     /// version of `arg_unique_types`.
73     ///
74     /// Again with the example described above in docstring for `args`:
75     ///
76     /// * `arg_index_map` (in JSON): `[[0, 1, 0], [2, 3, 3], [4, 5]]`
77     arg_index_map: Vec<Vec<usize>>,
78
79     /// Starting offset of count argument slots.
80     count_args_index_offset: usize,
81
82     /// Count argument slots and tracking data structures.
83     /// Count arguments are separately tracked for de-duplication in case
84     /// multiple references are made to one argument. For example, in this
85     /// format string:
86     ///
87     /// * Original: `"{:.*} {:.foo$} {1:.*} {:.0$}"`
88     /// * Implicit argument resolution: `"{1:.0$} {2:.foo$} {1:.3$} {4:.0$}"`
89     /// * Name resolution: `"{1:.0$} {2:.5$} {1:.3$} {4:.0$}"`
90     /// * `count_positions` (in JSON): `{0: 0, 5: 1, 3: 2}`
91     /// * `count_args`: `vec![Exact(0), Exact(5), Exact(3)]`
92     count_args: Vec<Position>,
93     /// Relative slot numbers for count arguments.
94     count_positions: FxHashMap<usize, usize>,
95     /// Number of count slots assigned.
96     count_positions_count: usize,
97
98     /// Current position of the implicit positional arg pointer, as if it
99     /// still existed in this phase of processing.
100     /// Used only for `all_pieces_simple` tracking in `build_piece`.
101     curarg: usize,
102     /// Current piece being evaluated, used for error reporting.
103     curpiece: usize,
104     /// Keep track of invalid references to positional arguments.
105     invalid_refs: Vec<(usize, usize)>,
106     /// Spans of all the formatting arguments, in order.
107     arg_spans: Vec<Span>,
108     /// All the formatting arguments that have formatting flags set, in order for diagnostics.
109     arg_with_formatting: Vec<parse::FormatSpec<'a>>,
110
111     /// Whether this format string came from a string literal, as opposed to a macro.
112     is_literal: bool,
113 }
114
115 /// Parses the arguments from the given list of tokens, returning the diagnostic
116 /// if there's a parse error so we can continue parsing other format!
117 /// expressions.
118 ///
119 /// If parsing succeeds, the return value is:
120 ///
121 /// ```text
122 /// Some((fmtstr, parsed arguments, index map for named arguments))
123 /// ```
124 fn parse_args<'a>(
125     ecx: &mut ExtCtxt<'a>,
126     sp: Span,
127     tts: TokenStream,
128 ) -> Result<(P<ast::Expr>, Vec<P<ast::Expr>>, FxHashMap<Symbol, usize>), DiagnosticBuilder<'a>> {
129     let mut args = Vec::<P<ast::Expr>>::new();
130     let mut names = FxHashMap::<Symbol, usize>::default();
131
132     let mut p = ecx.new_parser_from_tts(tts);
133
134     if p.token == token::Eof {
135         return Err(ecx.struct_span_err(sp, "requires at least a format string argument"));
136     }
137
138     let first_token = &p.token;
139     let fmtstr = match first_token.kind {
140         token::TokenKind::Literal(token::Lit {
141             kind: token::LitKind::Str | token::LitKind::StrRaw(_),
142             ..
143         }) => {
144             // If the first token is a string literal, then a format expression
145             // is constructed from it.
146             //
147             // This allows us to properly handle cases when the first comma
148             // after the format string is mistakenly replaced with any operator,
149             // which cause the expression parser to eat too much tokens.
150             p.parse_literal_maybe_minus()?
151         }
152         _ => {
153             // Otherwise, we fall back to the expression parser.
154             p.parse_expr()?
155         }
156     };
157
158     let mut first = true;
159     let mut named = false;
160
161     while p.token != token::Eof {
162         if !p.eat(&token::Comma) {
163             if first {
164                 p.clear_expected_tokens();
165             }
166
167             // `Parser::expect` tries to recover using the
168             // `Parser::unexpected_try_recover` function. This function is able
169             // to recover if the expected token is a closing delimiter.
170             //
171             // As `,` is not a closing delimiter, it will always return an `Err`
172             // variant.
173             let mut err = p.expect(&token::Comma).unwrap_err();
174
175             match token::TokenKind::Comma.similar_tokens() {
176                 Some(tks) if tks.contains(&p.token.kind) => {
177                     // If a similar token is found, then it may be a typo. We
178                     // consider it as a comma, and continue parsing.
179                     err.emit();
180                     p.bump();
181                 }
182                 // Otherwise stop the parsing and return the error.
183                 _ => return Err(err),
184             }
185         }
186         first = false;
187         if p.token == token::Eof {
188             break;
189         } // accept trailing commas
190         match p.token.ident() {
191             Some((ident, _)) if p.look_ahead(1, |t| *t == token::Eq) => {
192                 named = true;
193                 p.bump();
194                 p.expect(&token::Eq)?;
195                 let e = p.parse_expr()?;
196                 if let Some(prev) = names.get(&ident.name) {
197                     ecx.struct_span_err(e.span, &format!("duplicate argument named `{}`", ident))
198                         .span_label(args[*prev].span, "previously here")
199                         .span_label(e.span, "duplicate argument")
200                         .emit();
201                     continue;
202                 }
203
204                 // Resolve names into slots early.
205                 // Since all the positional args are already seen at this point
206                 // if the input is valid, we can simply append to the positional
207                 // args. And remember the names.
208                 let slot = args.len();
209                 names.insert(ident.name, slot);
210                 args.push(e);
211             }
212             _ => {
213                 let e = p.parse_expr()?;
214                 if named {
215                     let mut err = ecx.struct_span_err(
216                         e.span,
217                         "positional arguments cannot follow named arguments",
218                     );
219                     err.span_label(e.span, "positional arguments must be before named arguments");
220                     for pos in names.values() {
221                         err.span_label(args[*pos].span, "named argument");
222                     }
223                     err.emit();
224                 }
225                 args.push(e);
226             }
227         }
228     }
229     Ok((fmtstr, args, names))
230 }
231
232 impl<'a, 'b> Context<'a, 'b> {
233     fn resolve_name_inplace(&self, p: &mut parse::Piece<'_>) {
234         // NOTE: the `unwrap_or` branch is needed in case of invalid format
235         // arguments, e.g., `format_args!("{foo}")`.
236         let lookup = |s: Symbol| *self.names.get(&s).unwrap_or(&0);
237
238         match *p {
239             parse::String(_) => {}
240             parse::NextArgument(ref mut arg) => {
241                 if let parse::ArgumentNamed(s) = arg.position {
242                     arg.position = parse::ArgumentIs(lookup(s));
243                 }
244                 if let parse::CountIsName(s) = arg.format.width {
245                     arg.format.width = parse::CountIsParam(lookup(s));
246                 }
247                 if let parse::CountIsName(s) = arg.format.precision {
248                     arg.format.precision = parse::CountIsParam(lookup(s));
249                 }
250             }
251         }
252     }
253
254     /// Verifies one piece of a parse string, and remembers it if valid.
255     /// All errors are not emitted as fatal so we can continue giving errors
256     /// about this and possibly other format strings.
257     fn verify_piece(&mut self, p: &parse::Piece<'_>) {
258         match *p {
259             parse::String(..) => {}
260             parse::NextArgument(ref arg) => {
261                 // width/precision first, if they have implicit positional
262                 // parameters it makes more sense to consume them first.
263                 self.verify_count(arg.format.width);
264                 self.verify_count(arg.format.precision);
265
266                 // argument second, if it's an implicit positional parameter
267                 // it's written second, so it should come after width/precision.
268                 let pos = match arg.position {
269                     parse::ArgumentIs(i) | parse::ArgumentImplicitlyIs(i) => Exact(i),
270                     parse::ArgumentNamed(s) => Named(s),
271                 };
272
273                 let ty = Placeholder(match arg.format.ty {
274                     "" => "Display",
275                     "?" => "Debug",
276                     "e" => "LowerExp",
277                     "E" => "UpperExp",
278                     "o" => "Octal",
279                     "p" => "Pointer",
280                     "b" => "Binary",
281                     "x" => "LowerHex",
282                     "X" => "UpperHex",
283                     _ => {
284                         let fmtsp = self.fmtsp;
285                         let sp = arg.format.ty_span.map(|sp| fmtsp.from_inner(sp));
286                         let mut err = self.ecx.struct_span_err(
287                             sp.unwrap_or(fmtsp),
288                             &format!("unknown format trait `{}`", arg.format.ty),
289                         );
290                         err.note(
291                             "the only appropriate formatting traits are:\n\
292                                 - ``, which uses the `Display` trait\n\
293                                 - `?`, which uses the `Debug` trait\n\
294                                 - `e`, which uses the `LowerExp` trait\n\
295                                 - `E`, which uses the `UpperExp` trait\n\
296                                 - `o`, which uses the `Octal` trait\n\
297                                 - `p`, which uses the `Pointer` trait\n\
298                                 - `b`, which uses the `Binary` trait\n\
299                                 - `x`, which uses the `LowerHex` trait\n\
300                                 - `X`, which uses the `UpperHex` trait",
301                         );
302                         if let Some(sp) = sp {
303                             for (fmt, name) in &[
304                                 ("", "Display"),
305                                 ("?", "Debug"),
306                                 ("e", "LowerExp"),
307                                 ("E", "UpperExp"),
308                                 ("o", "Octal"),
309                                 ("p", "Pointer"),
310                                 ("b", "Binary"),
311                                 ("x", "LowerHex"),
312                                 ("X", "UpperHex"),
313                             ] {
314                                 // FIXME: rustfix (`run-rustfix`) fails to apply suggestions.
315                                 // > "Cannot replace slice of data that was already replaced"
316                                 err.tool_only_span_suggestion(
317                                     sp,
318                                     &format!("use the `{}` trait", name),
319                                     (*fmt).to_string(),
320                                     Applicability::MaybeIncorrect,
321                                 );
322                             }
323                         }
324                         err.emit();
325                         "<invalid>"
326                     }
327                 });
328                 self.verify_arg_type(pos, ty);
329                 self.curpiece += 1;
330             }
331         }
332     }
333
334     fn verify_count(&mut self, c: parse::Count) {
335         match c {
336             parse::CountImplied | parse::CountIs(..) => {}
337             parse::CountIsParam(i) => {
338                 self.verify_arg_type(Exact(i), Count);
339             }
340             parse::CountIsName(s) => {
341                 self.verify_arg_type(Named(s), Count);
342             }
343         }
344     }
345
346     fn describe_num_args(&self) -> Cow<'_, str> {
347         match self.args.len() {
348             0 => "no arguments were given".into(),
349             1 => "there is 1 argument".into(),
350             x => format!("there are {} arguments", x).into(),
351         }
352     }
353
354     /// Handle invalid references to positional arguments. Output different
355     /// errors for the case where all arguments are positional and for when
356     /// there are named arguments or numbered positional arguments in the
357     /// format string.
358     fn report_invalid_references(&self, numbered_position_args: bool) {
359         let mut e;
360         let sp = if !self.arg_spans.is_empty() {
361             // Point at the formatting arguments.
362             MultiSpan::from_spans(self.arg_spans.clone())
363         } else {
364             MultiSpan::from_span(self.fmtsp)
365         };
366         let refs =
367             self.invalid_refs.iter().map(|(r, pos)| (r.to_string(), self.arg_spans.get(*pos)));
368
369         let mut zero_based_note = false;
370
371         let count = self.pieces.len()
372             + self.arg_with_formatting.iter().filter(|fmt| fmt.precision_span.is_some()).count();
373         if self.names.is_empty() && !numbered_position_args && count != self.args.len() {
374             e = self.ecx.struct_span_err(
375                 sp,
376                 &format!(
377                     "{} positional argument{} in format string, but {}",
378                     count,
379                     pluralize!(count),
380                     self.describe_num_args(),
381                 ),
382             );
383             for arg in &self.args {
384                 // Point at the arguments that will be formatted.
385                 e.span_label(arg.span, "");
386             }
387         } else {
388             let (mut refs, spans): (Vec<_>, Vec<_>) = refs.unzip();
389             // Avoid `invalid reference to positional arguments 7 and 7 (there is 1 argument)`
390             // for `println!("{7:7$}", 1);`
391             refs.sort();
392             refs.dedup();
393             let spans: Vec<_> = spans.into_iter().filter_map(|sp| sp.copied()).collect();
394             let sp = if self.arg_spans.is_empty() || spans.is_empty() {
395                 MultiSpan::from_span(self.fmtsp)
396             } else {
397                 MultiSpan::from_spans(spans)
398             };
399             let arg_list = if refs.len() == 1 {
400                 format!("argument {}", refs[0])
401             } else {
402                 let reg = refs.pop().unwrap();
403                 format!("arguments {head} and {tail}", head = refs.join(", "), tail = reg)
404             };
405
406             e = self.ecx.struct_span_err(
407                 sp,
408                 &format!(
409                     "invalid reference to positional {} ({})",
410                     arg_list,
411                     self.describe_num_args()
412                 ),
413             );
414             zero_based_note = true;
415         };
416
417         for fmt in &self.arg_with_formatting {
418             if let Some(span) = fmt.precision_span {
419                 let span = self.fmtsp.from_inner(span);
420                 match fmt.precision {
421                     parse::CountIsParam(pos) if pos > self.args.len() => {
422                         e.span_label(
423                             span,
424                             &format!(
425                                 "this precision flag expects an `usize` argument at position {}, \
426                              but {}",
427                                 pos,
428                                 self.describe_num_args(),
429                             ),
430                         );
431                         zero_based_note = true;
432                     }
433                     parse::CountIsParam(pos) => {
434                         let count = self.pieces.len()
435                             + self
436                                 .arg_with_formatting
437                                 .iter()
438                                 .filter(|fmt| fmt.precision_span.is_some())
439                                 .count();
440                         e.span_label(span, &format!(
441                             "this precision flag adds an extra required argument at position {}, \
442                              which is why there {} expected",
443                             pos,
444                             if count == 1 {
445                                 "is 1 argument".to_string()
446                             } else {
447                                 format!("are {} arguments", count)
448                             },
449                         ));
450                         if let Some(arg) = self.args.get(pos) {
451                             e.span_label(
452                                 arg.span,
453                                 "this parameter corresponds to the precision flag",
454                             );
455                         }
456                         zero_based_note = true;
457                     }
458                     _ => {}
459                 }
460             }
461             if let Some(span) = fmt.width_span {
462                 let span = self.fmtsp.from_inner(span);
463                 match fmt.width {
464                     parse::CountIsParam(pos) if pos > self.args.len() => {
465                         e.span_label(
466                             span,
467                             &format!(
468                                 "this width flag expects an `usize` argument at position {}, \
469                              but {}",
470                                 pos,
471                                 self.describe_num_args(),
472                             ),
473                         );
474                         zero_based_note = true;
475                     }
476                     _ => {}
477                 }
478             }
479         }
480         if zero_based_note {
481             e.note("positional arguments are zero-based");
482         }
483         if !self.arg_with_formatting.is_empty() {
484             e.note(
485                 "for information about formatting flags, visit \
486                     https://doc.rust-lang.org/std/fmt/index.html",
487             );
488         }
489
490         e.emit();
491     }
492
493     /// Actually verifies and tracks a given format placeholder
494     /// (a.k.a. argument).
495     fn verify_arg_type(&mut self, arg: Position, ty: ArgumentType) {
496         match arg {
497             Exact(arg) => {
498                 if self.args.len() <= arg {
499                     self.invalid_refs.push((arg, self.curpiece));
500                     return;
501                 }
502                 match ty {
503                     Placeholder(_) => {
504                         // record every (position, type) combination only once
505                         let seen_ty = &mut self.arg_unique_types[arg];
506                         let i = seen_ty.iter().position(|x| *x == ty).unwrap_or_else(|| {
507                             let i = seen_ty.len();
508                             seen_ty.push(ty);
509                             i
510                         });
511                         self.arg_types[arg].push(i);
512                     }
513                     Count => {
514                         if let Entry::Vacant(e) = self.count_positions.entry(arg) {
515                             let i = self.count_positions_count;
516                             e.insert(i);
517                             self.count_args.push(Exact(arg));
518                             self.count_positions_count += 1;
519                         }
520                     }
521                 }
522             }
523
524             Named(name) => {
525                 match self.names.get(&name) {
526                     Some(&idx) => {
527                         // Treat as positional arg.
528                         self.verify_arg_type(Exact(idx), ty)
529                     }
530                     None => {
531                         let capture_feature_enabled = self
532                             .ecx
533                             .ecfg
534                             .features
535                             .map_or(false, |features| features.format_args_capture);
536
537                         // For the moment capturing variables from format strings expanded from macros is
538                         // disabled (see RFC #2795)
539                         let can_capture = capture_feature_enabled && self.is_literal;
540
541                         if can_capture {
542                             // Treat this name as a variable to capture from the surrounding scope
543                             let idx = self.args.len();
544                             self.arg_types.push(Vec::new());
545                             self.arg_unique_types.push(Vec::new());
546                             let span = if self.is_literal {
547                                 *self.arg_spans.get(self.curpiece).unwrap_or(&self.fmtsp)
548                             } else {
549                                 self.fmtsp
550                             };
551                             self.args.push(self.ecx.expr_ident(span, Ident::new(name, span)));
552                             self.names.insert(name, idx);
553                             self.verify_arg_type(Exact(idx), ty)
554                         } else {
555                             let msg = format!("there is no argument named `{}`", name);
556                             let sp = if self.is_literal {
557                                 *self.arg_spans.get(self.curpiece).unwrap_or(&self.fmtsp)
558                             } else {
559                                 self.fmtsp
560                             };
561                             let mut err = self.ecx.struct_span_err(sp, &msg[..]);
562
563                             if capture_feature_enabled && !self.is_literal {
564                                 err.note(&format!(
565                                     "did you intend to capture a variable `{}` from \
566                                      the surrounding scope?",
567                                     name
568                                 ));
569                                 err.note(
570                                     "to avoid ambiguity, `format_args!` cannot capture variables \
571                                      when the format string is expanded from a macro",
572                                 );
573                             } else if self.ecx.parse_sess().unstable_features.is_nightly_build() {
574                                 err.help(&format!(
575                                     "if you intended to capture `{}` from the surrounding scope, add \
576                                      `#![feature(format_args_capture)]` to the crate attributes",
577                                     name
578                                 ));
579                             }
580
581                             err.emit();
582                         }
583                     }
584                 }
585             }
586         }
587     }
588
589     /// Builds the mapping between format placeholders and argument objects.
590     fn build_index_map(&mut self) {
591         // NOTE: Keep the ordering the same as `into_expr`'s expansion would do!
592         let args_len = self.args.len();
593         self.arg_index_map.reserve(args_len);
594
595         let mut sofar = 0usize;
596
597         // Map the arguments
598         for i in 0..args_len {
599             let arg_types = &self.arg_types[i];
600             let arg_offsets = arg_types.iter().map(|offset| sofar + *offset).collect::<Vec<_>>();
601             self.arg_index_map.push(arg_offsets);
602             sofar += self.arg_unique_types[i].len();
603         }
604
605         // Record starting index for counts, which appear just after arguments
606         self.count_args_index_offset = sofar;
607     }
608
609     fn rtpath(ecx: &ExtCtxt<'_>, s: Symbol) -> Vec<Ident> {
610         ecx.std_path(&[sym::fmt, sym::rt, sym::v1, s])
611     }
612
613     fn build_count(&self, c: parse::Count) -> P<ast::Expr> {
614         let sp = self.macsp;
615         let count = |c, arg| {
616             let mut path = Context::rtpath(self.ecx, sym::Count);
617             path.push(Ident::new(c, sp));
618             match arg {
619                 Some(arg) => self.ecx.expr_call_global(sp, path, vec![arg]),
620                 None => self.ecx.expr_path(self.ecx.path_global(sp, path)),
621             }
622         };
623         match c {
624             parse::CountIs(i) => count(sym::Is, Some(self.ecx.expr_usize(sp, i))),
625             parse::CountIsParam(i) => {
626                 // This needs mapping too, as `i` is referring to a macro
627                 // argument. If `i` is not found in `count_positions` then
628                 // the error had already been emitted elsewhere.
629                 let i = self.count_positions.get(&i).cloned().unwrap_or(0)
630                     + self.count_args_index_offset;
631                 count(sym::Param, Some(self.ecx.expr_usize(sp, i)))
632             }
633             parse::CountImplied => count(sym::Implied, None),
634             // should never be the case, names are already resolved
635             parse::CountIsName(_) => panic!("should never happen"),
636         }
637     }
638
639     /// Build a literal expression from the accumulated string literals
640     fn build_literal_string(&mut self) -> P<ast::Expr> {
641         let sp = self.fmtsp;
642         let s = Symbol::intern(&self.literal);
643         self.literal.clear();
644         self.ecx.expr_str(sp, s)
645     }
646
647     /// Builds a static `rt::Argument` from a `parse::Piece` or append
648     /// to the `literal` string.
649     fn build_piece(
650         &mut self,
651         piece: &parse::Piece<'a>,
652         arg_index_consumed: &mut Vec<usize>,
653     ) -> Option<P<ast::Expr>> {
654         let sp = self.macsp;
655         match *piece {
656             parse::String(s) => {
657                 self.literal.push_str(s);
658                 None
659             }
660             parse::NextArgument(ref arg) => {
661                 // Build the position
662                 let pos = {
663                     match arg.position {
664                         parse::ArgumentIs(i) | parse::ArgumentImplicitlyIs(i) => {
665                             // Map to index in final generated argument array
666                             // in case of multiple types specified
667                             let arg_idx = match arg_index_consumed.get_mut(i) {
668                                 None => 0, // error already emitted elsewhere
669                                 Some(offset) => {
670                                     let idx_map = &self.arg_index_map[i];
671                                     // unwrap_or branch: error already emitted elsewhere
672                                     let arg_idx = *idx_map.get(*offset).unwrap_or(&0);
673                                     *offset += 1;
674                                     arg_idx
675                                 }
676                             };
677                             self.ecx.expr_usize(sp, arg_idx)
678                         }
679
680                         // should never be the case, because names are already
681                         // resolved.
682                         parse::ArgumentNamed(_) => panic!("should never happen"),
683                     }
684                 };
685
686                 let simple_arg = parse::Argument {
687                     position: {
688                         // We don't have ArgumentNext any more, so we have to
689                         // track the current argument ourselves.
690                         let i = self.curarg;
691                         self.curarg += 1;
692                         parse::ArgumentIs(i)
693                     },
694                     format: parse::FormatSpec {
695                         fill: arg.format.fill,
696                         align: parse::AlignUnknown,
697                         flags: 0,
698                         precision: parse::CountImplied,
699                         precision_span: None,
700                         width: parse::CountImplied,
701                         width_span: None,
702                         ty: arg.format.ty,
703                         ty_span: arg.format.ty_span,
704                     },
705                 };
706
707                 let fill = arg.format.fill.unwrap_or(' ');
708
709                 let pos_simple = arg.position.index() == simple_arg.position.index();
710
711                 if arg.format.precision_span.is_some() || arg.format.width_span.is_some() {
712                     self.arg_with_formatting.push(arg.format);
713                 }
714                 if !pos_simple || arg.format != simple_arg.format || fill != ' ' {
715                     self.all_pieces_simple = false;
716                 }
717
718                 // Build the format
719                 let fill = self.ecx.expr_lit(sp, ast::LitKind::Char(fill));
720                 let align = |name| {
721                     let mut p = Context::rtpath(self.ecx, sym::Alignment);
722                     p.push(Ident::new(name, sp));
723                     self.ecx.path_global(sp, p)
724                 };
725                 let align = match arg.format.align {
726                     parse::AlignLeft => align(sym::Left),
727                     parse::AlignRight => align(sym::Right),
728                     parse::AlignCenter => align(sym::Center),
729                     parse::AlignUnknown => align(sym::Unknown),
730                 };
731                 let align = self.ecx.expr_path(align);
732                 let flags = self.ecx.expr_u32(sp, arg.format.flags);
733                 let prec = self.build_count(arg.format.precision);
734                 let width = self.build_count(arg.format.width);
735                 let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, sym::FormatSpec));
736                 let fmt = self.ecx.expr_struct(
737                     sp,
738                     path,
739                     vec![
740                         self.ecx.field_imm(sp, Ident::new(sym::fill, sp), fill),
741                         self.ecx.field_imm(sp, Ident::new(sym::align, sp), align),
742                         self.ecx.field_imm(sp, Ident::new(sym::flags, sp), flags),
743                         self.ecx.field_imm(sp, Ident::new(sym::precision, sp), prec),
744                         self.ecx.field_imm(sp, Ident::new(sym::width, sp), width),
745                     ],
746                 );
747
748                 let path = self.ecx.path_global(sp, Context::rtpath(self.ecx, sym::Argument));
749                 Some(self.ecx.expr_struct(
750                     sp,
751                     path,
752                     vec![
753                         self.ecx.field_imm(sp, Ident::new(sym::position, sp), pos),
754                         self.ecx.field_imm(sp, Ident::new(sym::format, sp), fmt),
755                     ],
756                 ))
757             }
758         }
759     }
760
761     /// Actually builds the expression which the format_args! block will be
762     /// expanded to.
763     fn into_expr(self) -> P<ast::Expr> {
764         let mut locals =
765             Vec::with_capacity((0..self.args.len()).map(|i| self.arg_unique_types[i].len()).sum());
766         let mut counts = Vec::with_capacity(self.count_args.len());
767         let mut pats = Vec::with_capacity(self.args.len());
768         let mut heads = Vec::with_capacity(self.args.len());
769
770         let names_pos: Vec<_> = (0..self.args.len())
771             .map(|i| Ident::from_str_and_span(&format!("arg{}", i), self.macsp))
772             .collect();
773
774         // First, build up the static array which will become our precompiled
775         // format "string"
776         let pieces = self.ecx.expr_vec_slice(self.fmtsp, self.str_pieces);
777
778         // Before consuming the expressions, we have to remember spans for
779         // count arguments as they are now generated separate from other
780         // arguments, hence have no access to the `P<ast::Expr>`'s.
781         let spans_pos: Vec<_> = self.args.iter().map(|e| e.span).collect();
782
783         // Right now there is a bug such that for the expression:
784         //      foo(bar(&1))
785         // the lifetime of `1` doesn't outlast the call to `bar`, so it's not
786         // valid for the call to `foo`. To work around this all arguments to the
787         // format! string are shoved into locals. Furthermore, we shove the address
788         // of each variable because we don't want to move out of the arguments
789         // passed to this function.
790         for (i, e) in self.args.into_iter().enumerate() {
791             let name = names_pos[i];
792             let span = self.ecx.with_def_site_ctxt(e.span);
793             pats.push(self.ecx.pat_ident(span, name));
794             for arg_ty in self.arg_unique_types[i].iter() {
795                 locals.push(Context::format_arg(self.ecx, self.macsp, e.span, arg_ty, name));
796             }
797             heads.push(self.ecx.expr_addr_of(e.span, e));
798         }
799         for pos in self.count_args {
800             let index = match pos {
801                 Exact(i) => i,
802                 _ => panic!("should never happen"),
803             };
804             let name = names_pos[index];
805             let span = spans_pos[index];
806             counts.push(Context::format_arg(self.ecx, self.macsp, span, &Count, name));
807         }
808
809         // Now create a vector containing all the arguments
810         let args = locals.into_iter().chain(counts.into_iter());
811
812         let args_array = self.ecx.expr_vec(self.macsp, args.collect());
813
814         // Constructs an AST equivalent to:
815         //
816         //      match (&arg0, &arg1) {
817         //          (tmp0, tmp1) => args_array
818         //      }
819         //
820         // It was:
821         //
822         //      let tmp0 = &arg0;
823         //      let tmp1 = &arg1;
824         //      args_array
825         //
826         // Because of #11585 the new temporary lifetime rule, the enclosing
827         // statements for these temporaries become the let's themselves.
828         // If one or more of them are RefCell's, RefCell borrow() will also
829         // end there; they don't last long enough for args_array to use them.
830         // The match expression solves the scope problem.
831         //
832         // Note, it may also very well be transformed to:
833         //
834         //      match arg0 {
835         //          ref tmp0 => {
836         //              match arg1 => {
837         //                  ref tmp1 => args_array } } }
838         //
839         // But the nested match expression is proved to perform not as well
840         // as series of let's; the first approach does.
841         let args_match = {
842             let pat = self.ecx.pat_tuple(self.macsp, pats);
843             let arm = self.ecx.arm(self.macsp, pat, args_array);
844             let head = self.ecx.expr(self.macsp, ast::ExprKind::Tup(heads));
845             self.ecx.expr_match(self.macsp, head, vec![arm])
846         };
847
848         let ident = Ident::from_str_and_span("args", self.macsp);
849         let args_slice = self.ecx.expr_ident(self.macsp, ident);
850
851         // Now create the fmt::Arguments struct with all our locals we created.
852         let (fn_name, fn_args) = if self.all_pieces_simple {
853             ("new_v1", vec![pieces, args_slice])
854         } else {
855             // Build up the static array which will store our precompiled
856             // nonstandard placeholders, if there are any.
857             let fmt = self.ecx.expr_vec_slice(self.macsp, self.pieces);
858
859             ("new_v1_formatted", vec![pieces, args_slice, fmt])
860         };
861
862         let path = self.ecx.std_path(&[sym::fmt, sym::Arguments, Symbol::intern(fn_name)]);
863         let arguments = self.ecx.expr_call_global(self.macsp, path, fn_args);
864         let body = self.ecx.expr_block(P(ast::Block {
865             stmts: vec![self.ecx.stmt_expr(arguments)],
866             id: ast::DUMMY_NODE_ID,
867             rules: BlockCheckMode::Unsafe(UnsafeSource::CompilerGenerated),
868             span: self.macsp,
869             tokens: None,
870             could_be_bare_literal: false,
871         }));
872
873         let ident = Ident::from_str_and_span("args", self.macsp);
874         let binding_mode = ast::BindingMode::ByRef(ast::Mutability::Not);
875         let pat = self.ecx.pat_ident_binding_mode(self.macsp, ident, binding_mode);
876         let arm = self.ecx.arm(self.macsp, pat, body);
877         self.ecx.expr_match(self.macsp, args_match, vec![arm])
878     }
879
880     fn format_arg(
881         ecx: &ExtCtxt<'_>,
882         macsp: Span,
883         mut sp: Span,
884         ty: &ArgumentType,
885         arg: Ident,
886     ) -> P<ast::Expr> {
887         sp = ecx.with_def_site_ctxt(sp);
888         let arg = ecx.expr_ident(sp, arg);
889         let trait_ = match *ty {
890             Placeholder(trait_) if trait_ == "<invalid>" => return DummyResult::raw_expr(sp, true),
891             Placeholder(trait_) => trait_,
892             Count => {
893                 let path = ecx.std_path(&[sym::fmt, sym::ArgumentV1, sym::from_usize]);
894                 return ecx.expr_call_global(macsp, path, vec![arg]);
895             }
896         };
897
898         let path = ecx.std_path(&[sym::fmt, Symbol::intern(trait_), sym::fmt]);
899         let format_fn = ecx.path_global(sp, path);
900         let path = ecx.std_path(&[sym::fmt, sym::ArgumentV1, sym::new]);
901         ecx.expr_call_global(macsp, path, vec![arg, ecx.expr_path(format_fn)])
902     }
903 }
904
905 fn expand_format_args_impl<'cx>(
906     ecx: &'cx mut ExtCtxt<'_>,
907     mut sp: Span,
908     tts: TokenStream,
909     nl: bool,
910 ) -> Box<dyn base::MacResult + 'cx> {
911     sp = ecx.with_def_site_ctxt(sp);
912     match parse_args(ecx, sp, tts) {
913         Ok((efmt, args, names)) => {
914             MacEager::expr(expand_preparsed_format_args(ecx, sp, efmt, args, names, nl))
915         }
916         Err(mut err) => {
917             err.emit();
918             DummyResult::any(sp)
919         }
920     }
921 }
922
923 pub fn expand_format_args<'cx>(
924     ecx: &'cx mut ExtCtxt<'_>,
925     sp: Span,
926     tts: TokenStream,
927 ) -> Box<dyn base::MacResult + 'cx> {
928     expand_format_args_impl(ecx, sp, tts, false)
929 }
930
931 pub fn expand_format_args_nl<'cx>(
932     ecx: &'cx mut ExtCtxt<'_>,
933     sp: Span,
934     tts: TokenStream,
935 ) -> Box<dyn base::MacResult + 'cx> {
936     expand_format_args_impl(ecx, sp, tts, true)
937 }
938
939 /// Take the various parts of `format_args!(efmt, args..., name=names...)`
940 /// and construct the appropriate formatting expression.
941 pub fn expand_preparsed_format_args(
942     ecx: &mut ExtCtxt<'_>,
943     sp: Span,
944     efmt: P<ast::Expr>,
945     args: Vec<P<ast::Expr>>,
946     names: FxHashMap<Symbol, usize>,
947     append_newline: bool,
948 ) -> P<ast::Expr> {
949     // NOTE: this verbose way of initializing `Vec<Vec<ArgumentType>>` is because
950     // `ArgumentType` does not derive `Clone`.
951     let arg_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect();
952     let arg_unique_types: Vec<_> = (0..args.len()).map(|_| Vec::new()).collect();
953
954     let mut macsp = ecx.call_site();
955     macsp = ecx.with_def_site_ctxt(macsp);
956
957     let msg = "format argument must be a string literal";
958     let fmt_sp = efmt.span;
959     let efmt_kind_is_lit: bool = matches!(efmt.kind, ast::ExprKind::Lit(_));
960     let (fmt_str, fmt_style, fmt_span) = match expr_to_spanned_string(ecx, efmt, msg) {
961         Ok(mut fmt) if append_newline => {
962             fmt.0 = Symbol::intern(&format!("{}\n", fmt.0));
963             fmt
964         }
965         Ok(fmt) => fmt,
966         Err(err) => {
967             if let Some(mut err) = err {
968                 let sugg_fmt = match args.len() {
969                     0 => "{}".to_string(),
970                     _ => format!("{}{{}}", "{} ".repeat(args.len())),
971                 };
972                 err.span_suggestion(
973                     fmt_sp.shrink_to_lo(),
974                     "you might be missing a string literal to format with",
975                     format!("\"{}\", ", sugg_fmt),
976                     Applicability::MaybeIncorrect,
977                 );
978                 err.emit();
979             }
980             return DummyResult::raw_expr(sp, true);
981         }
982     };
983
984     let str_style = match fmt_style {
985         ast::StrStyle::Cooked => None,
986         ast::StrStyle::Raw(raw) => Some(raw as usize),
987     };
988
989     let fmt_str = &fmt_str.as_str(); // for the suggestions below
990     let fmt_snippet = ecx.source_map().span_to_snippet(fmt_sp).ok();
991     let mut parser = parse::Parser::new(
992         fmt_str,
993         str_style,
994         fmt_snippet,
995         append_newline,
996         parse::ParseMode::Format,
997     );
998
999     let mut unverified_pieces = Vec::new();
1000     while let Some(piece) = parser.next() {
1001         if !parser.errors.is_empty() {
1002             break;
1003         } else {
1004             unverified_pieces.push(piece);
1005         }
1006     }
1007
1008     if !parser.errors.is_empty() {
1009         let err = parser.errors.remove(0);
1010         let sp = if efmt_kind_is_lit {
1011             fmt_span.from_inner(err.span)
1012         } else {
1013             // The format string could be another macro invocation, e.g.:
1014             //     format!(concat!("abc", "{}"), 4);
1015             // However, `err.span` is an inner span relative to the *result* of
1016             // the macro invocation, which is why we would get a nonsensical
1017             // result calling `fmt_span.from_inner(err.span)` as above, and
1018             // might even end up inside a multibyte character (issue #86085).
1019             // Therefore, we conservatively report the error for the entire
1020             // argument span here.
1021             fmt_span
1022         };
1023         let mut e = ecx.struct_span_err(sp, &format!("invalid format string: {}", err.description));
1024         e.span_label(sp, err.label + " in format string");
1025         if let Some(note) = err.note {
1026             e.note(&note);
1027         }
1028         if let Some((label, span)) = err.secondary_label {
1029             let sp = fmt_span.from_inner(span);
1030             e.span_label(sp, label);
1031         }
1032         e.emit();
1033         return DummyResult::raw_expr(sp, true);
1034     }
1035
1036     let arg_spans = parser.arg_places.iter().map(|span| fmt_span.from_inner(*span)).collect();
1037
1038     let named_pos: FxHashSet<usize> = names.values().cloned().collect();
1039
1040     let mut cx = Context {
1041         ecx,
1042         args,
1043         arg_types,
1044         arg_unique_types,
1045         names,
1046         curarg: 0,
1047         curpiece: 0,
1048         arg_index_map: Vec::new(),
1049         count_args: Vec::new(),
1050         count_positions: FxHashMap::default(),
1051         count_positions_count: 0,
1052         count_args_index_offset: 0,
1053         literal: String::new(),
1054         pieces: Vec::with_capacity(unverified_pieces.len()),
1055         str_pieces: Vec::with_capacity(unverified_pieces.len()),
1056         all_pieces_simple: true,
1057         macsp,
1058         fmtsp: fmt_span,
1059         invalid_refs: Vec::new(),
1060         arg_spans,
1061         arg_with_formatting: Vec::new(),
1062         is_literal: parser.is_literal,
1063     };
1064
1065     // This needs to happen *after* the Parser has consumed all pieces to create all the spans
1066     let pieces = unverified_pieces
1067         .into_iter()
1068         .map(|mut piece| {
1069             cx.verify_piece(&piece);
1070             cx.resolve_name_inplace(&mut piece);
1071             piece
1072         })
1073         .collect::<Vec<_>>();
1074
1075     let numbered_position_args = pieces.iter().any(|arg: &parse::Piece<'_>| match *arg {
1076         parse::String(_) => false,
1077         parse::NextArgument(arg) => matches!(arg.position, parse::Position::ArgumentIs(_)),
1078     });
1079
1080     cx.build_index_map();
1081
1082     let mut arg_index_consumed = vec![0usize; cx.arg_index_map.len()];
1083
1084     for piece in pieces {
1085         if let Some(piece) = cx.build_piece(&piece, &mut arg_index_consumed) {
1086             let s = cx.build_literal_string();
1087             cx.str_pieces.push(s);
1088             cx.pieces.push(piece);
1089         }
1090     }
1091
1092     if !cx.literal.is_empty() {
1093         let s = cx.build_literal_string();
1094         cx.str_pieces.push(s);
1095     }
1096
1097     if !cx.invalid_refs.is_empty() {
1098         cx.report_invalid_references(numbered_position_args);
1099     }
1100
1101     // Make sure that all arguments were used and all arguments have types.
1102     let errs = cx
1103         .arg_types
1104         .iter()
1105         .enumerate()
1106         .filter(|(i, ty)| ty.is_empty() && !cx.count_positions.contains_key(&i))
1107         .map(|(i, _)| {
1108             let msg = if named_pos.contains(&i) {
1109                 // named argument
1110                 "named argument never used"
1111             } else {
1112                 // positional argument
1113                 "argument never used"
1114             };
1115             (cx.args[i].span, msg)
1116         })
1117         .collect::<Vec<_>>();
1118
1119     let errs_len = errs.len();
1120     if !errs.is_empty() {
1121         let args_used = cx.arg_types.len() - errs_len;
1122         let args_unused = errs_len;
1123
1124         let mut diag = {
1125             if let [(sp, msg)] = &errs[..] {
1126                 let mut diag = cx.ecx.struct_span_err(*sp, *msg);
1127                 diag.span_label(*sp, *msg);
1128                 diag
1129             } else {
1130                 let mut diag = cx.ecx.struct_span_err(
1131                     errs.iter().map(|&(sp, _)| sp).collect::<Vec<Span>>(),
1132                     "multiple unused formatting arguments",
1133                 );
1134                 diag.span_label(cx.fmtsp, "multiple missing formatting specifiers");
1135                 for (sp, msg) in errs {
1136                     diag.span_label(sp, msg);
1137                 }
1138                 diag
1139             }
1140         };
1141
1142         // Used to ensure we only report translations for *one* kind of foreign format.
1143         let mut found_foreign = false;
1144         // Decide if we want to look for foreign formatting directives.
1145         if args_used < args_unused {
1146             use super::format_foreign as foreign;
1147
1148             // The set of foreign substitutions we've explained.  This prevents spamming the user
1149             // with `%d should be written as {}` over and over again.
1150             let mut explained = FxHashSet::default();
1151
1152             macro_rules! check_foreign {
1153                 ($kind:ident) => {{
1154                     let mut show_doc_note = false;
1155
1156                     let mut suggestions = vec![];
1157                     // account for `"` and account for raw strings `r#`
1158                     let padding = str_style.map(|i| i + 2).unwrap_or(1);
1159                     for sub in foreign::$kind::iter_subs(fmt_str, padding) {
1160                         let trn = match sub.translate() {
1161                             Some(trn) => trn,
1162
1163                             // If it has no translation, don't call it out specifically.
1164                             None => continue,
1165                         };
1166
1167                         let pos = sub.position();
1168                         let sub = String::from(sub.as_str());
1169                         if explained.contains(&sub) {
1170                             continue;
1171                         }
1172                         explained.insert(sub.clone());
1173
1174                         if !found_foreign {
1175                             found_foreign = true;
1176                             show_doc_note = true;
1177                         }
1178
1179                         if let Some(inner_sp) = pos {
1180                             let sp = fmt_sp.from_inner(inner_sp);
1181                             suggestions.push((sp, trn));
1182                         } else {
1183                             diag.help(&format!("`{}` should be written as `{}`", sub, trn));
1184                         }
1185                     }
1186
1187                     if show_doc_note {
1188                         diag.note(concat!(
1189                             stringify!($kind),
1190                             " formatting not supported; see the documentation for `std::fmt`",
1191                         ));
1192                     }
1193                     if suggestions.len() > 0 {
1194                         diag.multipart_suggestion(
1195                             "format specifiers use curly braces",
1196                             suggestions,
1197                             Applicability::MachineApplicable,
1198                         );
1199                     }
1200                 }};
1201             }
1202
1203             check_foreign!(printf);
1204             if !found_foreign {
1205                 check_foreign!(shell);
1206             }
1207         }
1208         if !found_foreign && errs_len == 1 {
1209             diag.span_label(cx.fmtsp, "formatting specifier missing");
1210         }
1211
1212         diag.emit();
1213     }
1214
1215     cx.into_expr()
1216 }