diff options
| author | bors <bors@rust-lang.org> | 2018-12-19 12:49:32 +0000 |
|---|---|---|
| committer | bors <bors@rust-lang.org> | 2018-12-19 12:49:32 +0000 |
| commit | 6f839fbb0d407956777529b029fbb0d6cc6e4318 (patch) | |
| tree | 7efc541878cc49a26d67b9eb036d16657c6a5937 /src/libsyntax_ext | |
| parent | 74ebf026fe927ffa99d541479454f45791806802 (diff) | |
| parent | 1ba6ec4a327d978471cdac31d17d9202106b15bd (diff) | |
| download | rust-6f839fbb0d407956777529b029fbb0d6cc6e4318.tar.gz rust-6f839fbb0d407956777529b029fbb0d6cc6e4318.zip | |
Auto merge of #56977 - pietroalbini:rollup, r=pietroalbini
Rollup of 15 pull requests Successful merges: - #56363 (Defactored Bytes::read) - #56663 (Remove lifetime from Resolver) - #56689 (add a lint group for lints emitted by rustdoc) - #56772 (fix issue 54153 by not testing issue-18804 on Windows nor OS X.) - #56820 (format-related tweaks) - #56881 (Implement Eq, PartialEq and Hash for atomic::Ordering) - #56907 (Fix grammar in compiler error for array iterators) - #56908 (rustc: Don't ICE on usage of two new target features) - #56910 (Do not point at delim spans for complete correct blocks) - #56913 (Enable stack probes for UEFI images) - #56918 (Profiler: simplify total_duration, improve readability) - #56931 (Update release notes for Rust 1.31.1) - #56947 (Add targets thumbv7neon-linux-androideabi and thumbv7neon-unknown-linux-gnueabihf) - #56948 (Update LLVM submodule) - #56959 (Fix mobile menu rendering collision with tooltip.) Failed merges: - #56914 (Ignore ui/target-feature-gate on sparc, sparc64, powerpc, powerpc64 and powerpc64le) r? @ghost
Diffstat (limited to 'src/libsyntax_ext')
| -rw-r--r-- | src/libsyntax_ext/format.rs | 133 |
1 files changed, 52 insertions, 81 deletions
diff --git a/src/libsyntax_ext/format.rs b/src/libsyntax_ext/format.rs index 24108a30fdc..41799eede9e 100644 --- a/src/libsyntax_ext/format.rs +++ b/src/libsyntax_ext/format.rs @@ -158,28 +158,15 @@ fn parse_args(ecx: &mut ExtCtxt, } // accept trailing commas if named || (p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq)) { named = true; - let ident = match p.token { - token::Ident(i, _) => { - p.bump(); - i - } - _ if named => { - ecx.span_err( - p.span, - "expected ident, positional arguments cannot follow named arguments", - ); - return None; - } - _ => { - ecx.span_err( - p.span, - &format!( - "expected ident for named argument, found `{}`", - p.this_token_to_string() - ), - ); - return None; - } + let ident = if let token::Ident(i, _) = p.token { + p.bump(); + i + } else { + ecx.span_err( + p.span, + "expected ident, positional arguments cannot follow named arguments", + ); + return None; }; let name: &str = &ident.as_str(); @@ -286,11 +273,11 @@ impl<'a, 'b> Context<'a, 'b> { } else { MultiSpan::from_span(self.fmtsp) }; - let mut refs: Vec<_> = self + let refs_len = self.invalid_refs.len(); + let mut refs = self .invalid_refs .iter() - .map(|(r, pos)| (r.to_string(), self.arg_spans.get(*pos))) - .collect(); + .map(|(r, pos)| (r.to_string(), self.arg_spans.get(*pos))); if self.names.is_empty() && !numbered_position_args { e = self.ecx.mut_span_err( @@ -303,28 +290,24 @@ impl<'a, 'b> Context<'a, 'b> { ), ); } else { - let (arg_list, mut sp) = match refs.len() { - 1 => { - let (reg, pos) = refs.pop().unwrap(); - ( - format!("argument {}", reg), - MultiSpan::from_span(*pos.unwrap_or(&self.fmtsp)), - ) - } - _ => { - let pos = - MultiSpan::from_spans(refs.iter().map(|(_, p)| *p.unwrap()).collect()); - let mut refs: Vec<String> = refs.iter().map(|(s, _)| s.to_owned()).collect(); - let reg = refs.pop().unwrap(); - ( - format!( - "arguments {head} and {tail}", - tail = reg, - head = refs.join(", ") - ), - pos, - ) - } + let (arg_list, mut sp) = if refs_len == 1 { + let (reg, pos) = refs.next().unwrap(); + ( + format!("argument {}", reg), + MultiSpan::from_span(*pos.unwrap_or(&self.fmtsp)), + ) + } else { + let (mut refs, spans): (Vec<_>, Vec<_>) = refs.unzip(); + let pos = MultiSpan::from_spans(spans.into_iter().map(|s| *s.unwrap()).collect()); + let reg = refs.pop().unwrap(); + ( + format!( + "arguments {head} and {tail}", + head = refs.join(", "), + tail = reg, + ), + pos, + ) }; if !self.is_literal { sp = MultiSpan::from_span(self.fmtsp); @@ -353,33 +336,30 @@ impl<'a, 'b> Context<'a, 'b> { Placeholder(_) => { // record every (position, type) combination only once let ref mut seen_ty = self.arg_unique_types[arg]; - let i = match seen_ty.iter().position(|x| *x == ty) { - Some(i) => i, - None => { - let i = seen_ty.len(); - seen_ty.push(ty); - i - } - }; + let i = seen_ty.iter().position(|x| *x == ty).unwrap_or_else(|| { + let i = seen_ty.len(); + seen_ty.push(ty); + i + }); self.arg_types[arg].push(i); } Count => { - match self.count_positions.entry(arg) { - Entry::Vacant(e) => { - let i = self.count_positions_count; - e.insert(i); - self.count_args.push(Exact(arg)); - self.count_positions_count += 1; - } - Entry::Occupied(_) => {} + if let Entry::Vacant(e) = self.count_positions.entry(arg) { + let i = self.count_positions_count; + e.insert(i); + self.count_args.push(Exact(arg)); + self.count_positions_count += 1; } } } } Named(name) => { - let idx = match self.names.get(&name) { - Some(e) => *e, + match self.names.get(&name) { + Some(idx) => { + // Treat as positional arg. + self.verify_arg_type(Exact(*idx), ty) + } None => { let msg = format!("there is no argument named `{}`", name); let sp = if self.is_literal { @@ -389,11 +369,8 @@ impl<'a, 'b> Context<'a, 'b> { }; let mut err = self.ecx.struct_span_err(sp, &msg[..]); err.emit(); - return; } - }; - // Treat as positional arg. - self.verify_arg_type(Exact(idx), ty) + } } } } @@ -436,12 +413,10 @@ impl<'a, 'b> Context<'a, 'b> { parse::CountIs(i) => count("Is", Some(self.ecx.expr_usize(sp, i))), parse::CountIsParam(i) => { // This needs mapping too, as `i` is referring to a macro - // argument. - let i = match self.count_positions.get(&i) { - Some(&i) => i, - None => 0, // error already emitted elsewhere - }; - let i = i + self.count_args_index_offset; + // argument. If `i` is not found in `count_positions` then + // the error had already been emitted elsewhere. + let i = self.count_positions.get(&i).cloned().unwrap_or(0) + + self.count_args_index_offset; count("Param", Some(self.ecx.expr_usize(sp, i))) } parse::CountImplied => count("Implied", None), @@ -526,10 +501,7 @@ impl<'a, 'b> Context<'a, 'b> { }, }; - let fill = match arg.format.fill { - Some(c) => c, - None => ' ', - }; + let fill = arg.format.fill.unwrap_or(' '); if *arg != simple_arg || fill != ' ' { self.all_pieces_simple = false; @@ -828,8 +800,7 @@ pub fn expand_preparsed_format_args(ecx: &mut ExtCtxt, if !parser.errors.is_empty() { let err = parser.errors.remove(0); let sp = fmt.span.from_inner_byte_pos(err.start, err.end); - let mut e = ecx.struct_span_err(sp, &format!("invalid format string: {}", - err.description)); + let mut e = ecx.struct_span_err(sp, &format!("invalid format string: {}", err.description)); e.span_label(sp, err.label + " in format string"); if let Some(note) = err.note { e.note(¬e); |
