Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ICE #75307 in format #75319

Merged
merged 1 commit into from
Aug 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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