Skip to content

Commit

Permalink
Rollup merge of rust-lang#75319 - estebank:format-ice, r=eddyb
Browse files Browse the repository at this point in the history
Fix ICE rust-lang#75307 in `format`

Remove usages of `unwrap` (even when some are safe today).

Fix rust-lang#75307.
  • Loading branch information
tmandry authored Aug 14, 2020
2 parents 5b5eec7 + 0a4f4e8 commit 8c361aa
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 20 deletions.
33 changes: 13 additions & 20 deletions src/librustc_builtin_macros/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 3 additions & 0 deletions src/test/ui/issues/issue-75307.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
fn main() {
format!(r"{}{}{}", named_arg=1); //~ ERROR invalid reference to positional arguments 1 and 2
}
10 changes: 10 additions & 0 deletions src/test/ui/issues/issue-75307.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8c361aa

Please sign in to comment.