From 0a4f4e87e04b88d5269003d7868f33cd818e1ba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Esteban=20K=C3=BCber?= Date: Sat, 8 Aug 2020 20:11:16 -0700 Subject: [PATCH] Fix ICE #75307 in `format` Remove usages of `unwrap` (even when some are safe today). --- src/librustc_builtin_macros/format.rs | 33 +++++++++++---------------- src/test/ui/issues/issue-75307.rs | 3 +++ src/test/ui/issues/issue-75307.stderr | 10 ++++++++ 3 files changed, 26 insertions(+), 20 deletions(-) create mode 100644 src/test/ui/issues/issue-75307.rs create mode 100644 src/test/ui/issues/issue-75307.stderr diff --git a/src/librustc_builtin_macros/format.rs b/src/librustc_builtin_macros/format.rs index 55eab24b8a510..78cead02b7b7c 100644 --- a/src/librustc_builtin_macros/format.rs +++ b/src/librustc_builtin_macros/format.rs @@ -149,7 +149,7 @@ fn parse_args<'a>( return Err(err); } else { // ...after that delegate to `expect` to also include the other expected tokens. - return Err(p.expect(&token::Comma).err().unwrap()); + let _ = p.expect(&token::Comma)?; } } first = false; @@ -359,24 +359,18 @@ impl<'a, 'b> Context<'a, 'b> { // for `println!("{7:7$}", 1);` refs.sort(); refs.dedup(); - let (arg_list, mut sp) = if refs.len() == 1 { - let spans: Vec<_> = spans.into_iter().filter_map(|sp| sp.copied()).collect(); - ( - format!("argument {}", refs[0]), - if spans.is_empty() { - MultiSpan::from_span(self.fmtsp) - } else { - MultiSpan::from_spans(spans) - }, - ) + let spans: Vec<_> = spans.into_iter().filter_map(|sp| sp.copied()).collect(); + let sp = if self.arg_spans.is_empty() || spans.is_empty() { + MultiSpan::from_span(self.fmtsp) + } else { + MultiSpan::from_spans(spans) + }; + let arg_list = if refs.len() == 1 { + format!("argument {}", refs[0]) } else { - 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) + format!("arguments {head} and {tail}", head = refs.join(", "), tail = reg) }; - if self.arg_spans.is_empty() { - sp = MultiSpan::from_span(self.fmtsp); - } e = self.ecx.struct_span_err( sp, @@ -1067,10 +1061,9 @@ pub fn expand_preparsed_format_args( let args_unused = errs_len; let mut diag = { - if errs_len == 1 { - let (sp, msg) = errs.into_iter().next().unwrap(); - let mut diag = cx.ecx.struct_span_err(sp, msg); - diag.span_label(sp, msg); + if let [(sp, msg)] = &errs[..] { + let mut diag = cx.ecx.struct_span_err(*sp, *msg); + diag.span_label(*sp, *msg); diag } else { let mut diag = cx.ecx.struct_span_err( diff --git a/src/test/ui/issues/issue-75307.rs b/src/test/ui/issues/issue-75307.rs new file mode 100644 index 0000000000000..2fe112a3b95d4 --- /dev/null +++ b/src/test/ui/issues/issue-75307.rs @@ -0,0 +1,3 @@ +fn main() { + format!(r"{}{}{}", named_arg=1); //~ ERROR invalid reference to positional arguments 1 and 2 +} diff --git a/src/test/ui/issues/issue-75307.stderr b/src/test/ui/issues/issue-75307.stderr new file mode 100644 index 0000000000000..4a5d997e00d74 --- /dev/null +++ b/src/test/ui/issues/issue-75307.stderr @@ -0,0 +1,10 @@ +error: invalid reference to positional arguments 1 and 2 (there is 1 argument) + --> $DIR/issue-75307.rs:2:13 + | +LL | format!(r"{}{}{}", named_arg=1); + | ^^^^^^^^^ + | + = note: positional arguments are zero-based + +error: aborting due to previous error +