Skip to content

Commit 29aa466

Browse files
authored
Rollup merge of rust-lang#56820 - ljedrz:format_tweaks, r=alexcrichton
format-related tweaks - remove an unreachable condition - inline one-liners related to `parse_expr` (called in succession) - refactor `report_invalid_references` - refactor `verify_arg_type` - minor stylistic improvements
2 parents 6e1cc22 + 959313a commit 29aa466

File tree

2 files changed

+55
-81
lines changed

2 files changed

+55
-81
lines changed

src/libsyntax/parse/parser.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3062,6 +3062,7 @@ impl<'a> Parser<'a> {
30623062
///
30633063
/// This parses an expression accounting for associativity and precedence of the operators in
30643064
/// the expression.
3065+
#[inline]
30653066
fn parse_assoc_expr(&mut self,
30663067
already_parsed_attrs: Option<ThinVec<Attribute>>)
30673068
-> PResult<'a, P<Expr>> {
@@ -3722,6 +3723,7 @@ impl<'a> Parser<'a> {
37223723
}
37233724

37243725
/// Parse an expression
3726+
#[inline]
37253727
pub fn parse_expr(&mut self) -> PResult<'a, P<Expr>> {
37263728
self.parse_expr_res(Restrictions::empty(), None)
37273729
}
@@ -3741,6 +3743,7 @@ impl<'a> Parser<'a> {
37413743
}
37423744

37433745
/// Parse an expression, subject to the given restrictions
3746+
#[inline]
37443747
fn parse_expr_res(&mut self, r: Restrictions,
37453748
already_parsed_attrs: Option<ThinVec<Attribute>>)
37463749
-> PResult<'a, P<Expr>> {

src/libsyntax_ext/format.rs

Lines changed: 52 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -158,28 +158,15 @@ fn parse_args(ecx: &mut ExtCtxt,
158158
} // accept trailing commas
159159
if named || (p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq)) {
160160
named = true;
161-
let ident = match p.token {
162-
token::Ident(i, _) => {
163-
p.bump();
164-
i
165-
}
166-
_ if named => {
167-
ecx.span_err(
168-
p.span,
169-
"expected ident, positional arguments cannot follow named arguments",
170-
);
171-
return None;
172-
}
173-
_ => {
174-
ecx.span_err(
175-
p.span,
176-
&format!(
177-
"expected ident for named argument, found `{}`",
178-
p.this_token_to_string()
179-
),
180-
);
181-
return None;
182-
}
161+
let ident = if let token::Ident(i, _) = p.token {
162+
p.bump();
163+
i
164+
} else {
165+
ecx.span_err(
166+
p.span,
167+
"expected ident, positional arguments cannot follow named arguments",
168+
);
169+
return None;
183170
};
184171
let name: &str = &ident.as_str();
185172

@@ -286,11 +273,11 @@ impl<'a, 'b> Context<'a, 'b> {
286273
} else {
287274
MultiSpan::from_span(self.fmtsp)
288275
};
289-
let mut refs: Vec<_> = self
276+
let refs_len = self.invalid_refs.len();
277+
let mut refs = self
290278
.invalid_refs
291279
.iter()
292-
.map(|(r, pos)| (r.to_string(), self.arg_spans.get(*pos)))
293-
.collect();
280+
.map(|(r, pos)| (r.to_string(), self.arg_spans.get(*pos)));
294281

295282
if self.names.is_empty() && !numbered_position_args {
296283
e = self.ecx.mut_span_err(
@@ -303,28 +290,24 @@ impl<'a, 'b> Context<'a, 'b> {
303290
),
304291
);
305292
} else {
306-
let (arg_list, mut sp) = match refs.len() {
307-
1 => {
308-
let (reg, pos) = refs.pop().unwrap();
309-
(
310-
format!("argument {}", reg),
311-
MultiSpan::from_span(*pos.unwrap_or(&self.fmtsp)),
312-
)
313-
}
314-
_ => {
315-
let pos =
316-
MultiSpan::from_spans(refs.iter().map(|(_, p)| *p.unwrap()).collect());
317-
let mut refs: Vec<String> = refs.iter().map(|(s, _)| s.to_owned()).collect();
318-
let reg = refs.pop().unwrap();
319-
(
320-
format!(
321-
"arguments {head} and {tail}",
322-
tail = reg,
323-
head = refs.join(", ")
324-
),
325-
pos,
326-
)
327-
}
293+
let (arg_list, mut sp) = if refs_len == 1 {
294+
let (reg, pos) = refs.next().unwrap();
295+
(
296+
format!("argument {}", reg),
297+
MultiSpan::from_span(*pos.unwrap_or(&self.fmtsp)),
298+
)
299+
} else {
300+
let (mut refs, spans): (Vec<_>, Vec<_>) = refs.unzip();
301+
let pos = MultiSpan::from_spans(spans.into_iter().map(|s| *s.unwrap()).collect());
302+
let reg = refs.pop().unwrap();
303+
(
304+
format!(
305+
"arguments {head} and {tail}",
306+
head = refs.join(", "),
307+
tail = reg,
308+
),
309+
pos,
310+
)
328311
};
329312
if !self.is_literal {
330313
sp = MultiSpan::from_span(self.fmtsp);
@@ -353,33 +336,30 @@ impl<'a, 'b> Context<'a, 'b> {
353336
Placeholder(_) => {
354337
// record every (position, type) combination only once
355338
let ref mut seen_ty = self.arg_unique_types[arg];
356-
let i = match seen_ty.iter().position(|x| *x == ty) {
357-
Some(i) => i,
358-
None => {
359-
let i = seen_ty.len();
360-
seen_ty.push(ty);
361-
i
362-
}
363-
};
339+
let i = seen_ty.iter().position(|x| *x == ty).unwrap_or_else(|| {
340+
let i = seen_ty.len();
341+
seen_ty.push(ty);
342+
i
343+
});
364344
self.arg_types[arg].push(i);
365345
}
366346
Count => {
367-
match self.count_positions.entry(arg) {
368-
Entry::Vacant(e) => {
369-
let i = self.count_positions_count;
370-
e.insert(i);
371-
self.count_args.push(Exact(arg));
372-
self.count_positions_count += 1;
373-
}
374-
Entry::Occupied(_) => {}
347+
if let Entry::Vacant(e) = self.count_positions.entry(arg) {
348+
let i = self.count_positions_count;
349+
e.insert(i);
350+
self.count_args.push(Exact(arg));
351+
self.count_positions_count += 1;
375352
}
376353
}
377354
}
378355
}
379356

380357
Named(name) => {
381-
let idx = match self.names.get(&name) {
382-
Some(e) => *e,
358+
match self.names.get(&name) {
359+
Some(idx) => {
360+
// Treat as positional arg.
361+
self.verify_arg_type(Exact(*idx), ty)
362+
}
383363
None => {
384364
let msg = format!("there is no argument named `{}`", name);
385365
let sp = if self.is_literal {
@@ -389,11 +369,8 @@ impl<'a, 'b> Context<'a, 'b> {
389369
};
390370
let mut err = self.ecx.struct_span_err(sp, &msg[..]);
391371
err.emit();
392-
return;
393372
}
394-
};
395-
// Treat as positional arg.
396-
self.verify_arg_type(Exact(idx), ty)
373+
}
397374
}
398375
}
399376
}
@@ -436,12 +413,10 @@ impl<'a, 'b> Context<'a, 'b> {
436413
parse::CountIs(i) => count("Is", Some(self.ecx.expr_usize(sp, i))),
437414
parse::CountIsParam(i) => {
438415
// This needs mapping too, as `i` is referring to a macro
439-
// argument.
440-
let i = match self.count_positions.get(&i) {
441-
Some(&i) => i,
442-
None => 0, // error already emitted elsewhere
443-
};
444-
let i = i + self.count_args_index_offset;
416+
// argument. If `i` is not found in `count_positions` then
417+
// the error had already been emitted elsewhere.
418+
let i = self.count_positions.get(&i).cloned().unwrap_or(0)
419+
+ self.count_args_index_offset;
445420
count("Param", Some(self.ecx.expr_usize(sp, i)))
446421
}
447422
parse::CountImplied => count("Implied", None),
@@ -526,10 +501,7 @@ impl<'a, 'b> Context<'a, 'b> {
526501
},
527502
};
528503

529-
let fill = match arg.format.fill {
530-
Some(c) => c,
531-
None => ' ',
532-
};
504+
let fill = arg.format.fill.unwrap_or(' ');
533505

534506
if *arg != simple_arg || fill != ' ' {
535507
self.all_pieces_simple = false;
@@ -828,8 +800,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt,
828800
if !parser.errors.is_empty() {
829801
let err = parser.errors.remove(0);
830802
let sp = fmt.span.from_inner_byte_pos(err.start, err.end);
831-
let mut e = ecx.struct_span_err(sp, &format!("invalid format string: {}",
832-
err.description));
803+
let mut e = ecx.struct_span_err(sp, &format!("invalid format string: {}", err.description));
833804
e.span_label(sp, err.label + " in format string");
834805
if let Some(note) = err.note {
835806
e.note(&note);

0 commit comments

Comments
 (0)