Skip to content

Commit

Permalink
Rollup merge of rust-lang#83343 - osa1:issue83340, r=jackh726
Browse files Browse the repository at this point in the history
Simplify and fix byte skipping in format! string parser

Fixes '\\' handling in format strings.

Fixes rust-lang#83340
  • Loading branch information
JohnTitor authored Mar 27, 2021
2 parents 36e0a42 + ae8ef70 commit cfd81d9
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
23 changes: 9 additions & 14 deletions compiler/rustc_parse_format/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -735,25 +735,24 @@ fn find_skips_from_snippet(
};

fn find_skips(snippet: &str, is_raw: bool) -> Vec<usize> {
let mut eat_ws = false;
let mut s = snippet.char_indices().peekable();
let mut skips = vec![];
while let Some((pos, c)) = s.next() {
match (c, s.peek()) {
// skip whitespace and empty lines ending in '\\'
('\\', Some((next_pos, '\n'))) if !is_raw => {
eat_ws = true;
skips.push(pos);
skips.push(*next_pos);
let _ = s.next();
}
('\\', Some((next_pos, '\n' | 'n' | 't'))) if eat_ws => {
skips.push(pos);
skips.push(*next_pos);
let _ = s.next();
}
(' ' | '\n' | '\t', _) if eat_ws => {
skips.push(pos);

while let Some((pos, c)) = s.peek() {
if matches!(c, ' ' | '\n' | '\t') {
skips.push(*pos);
let _ = s.next();
} else {
break;
}
}
}
('\\', Some((next_pos, 'n' | 't' | 'r' | '0' | '\\' | '\'' | '\"'))) => {
skips.push(*next_pos);
Expand Down Expand Up @@ -804,10 +803,6 @@ fn find_skips_from_snippet(
}
}
}
_ if eat_ws => {
// `take_while(|c| c.is_whitespace())`
eat_ws = false;
}
_ => {}
}
}
Expand Down
8 changes: 8 additions & 0 deletions src/test/ui/macros/issue-83340.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// check-fail

fn main() {
println!(
"\
\n {} │", //~ ERROR: 1 positional argument in format string, but no arguments were given
);
}
8 changes: 8 additions & 0 deletions src/test/ui/macros/issue-83340.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: 1 positional argument in format string, but no arguments were given
--> $DIR/issue-83340.rs:6:4
|
LL | \n {} │",
| ^^

error: aborting due to previous error

0 comments on commit cfd81d9

Please sign in to comment.