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