Skip to content

Commit ae8ef70

Browse files
committed
Simplify and fix byte skipping in format! string parser
Fixes '\\' handling in format strings. Fixes #83340
1 parent bbf07c0 commit ae8ef70

File tree

3 files changed

+25
-14
lines changed

3 files changed

+25
-14
lines changed

compiler/rustc_parse_format/src/lib.rs

+9-14
Original file line numberDiff line numberDiff line change
@@ -735,25 +735,24 @@ fn find_skips_from_snippet(
735735
};
736736

737737
fn find_skips(snippet: &str, is_raw: bool) -> Vec<usize> {
738-
let mut eat_ws = false;
739738
let mut s = snippet.char_indices().peekable();
740739
let mut skips = vec![];
741740
while let Some((pos, c)) = s.next() {
742741
match (c, s.peek()) {
743742
// skip whitespace and empty lines ending in '\\'
744743
('\\', Some((next_pos, '\n'))) if !is_raw => {
745-
eat_ws = true;
746744
skips.push(pos);
747745
skips.push(*next_pos);
748746
let _ = s.next();
749-
}
750-
('\\', Some((next_pos, '\n' | 'n' | 't'))) if eat_ws => {
751-
skips.push(pos);
752-
skips.push(*next_pos);
753-
let _ = s.next();
754-
}
755-
(' ' | '\n' | '\t', _) if eat_ws => {
756-
skips.push(pos);
747+
748+
while let Some((pos, c)) = s.peek() {
749+
if matches!(c, ' ' | '\n' | '\t') {
750+
skips.push(*pos);
751+
let _ = s.next();
752+
} else {
753+
break;
754+
}
755+
}
757756
}
758757
('\\', Some((next_pos, 'n' | 't' | 'r' | '0' | '\\' | '\'' | '\"'))) => {
759758
skips.push(*next_pos);
@@ -804,10 +803,6 @@ fn find_skips_from_snippet(
804803
}
805804
}
806805
}
807-
_ if eat_ws => {
808-
// `take_while(|c| c.is_whitespace())`
809-
eat_ws = false;
810-
}
811806
_ => {}
812807
}
813808
}

src/test/ui/macros/issue-83340.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// check-fail
2+
3+
fn main() {
4+
println!(
5+
"\
6+
\n {} │", //~ ERROR: 1 positional argument in format string, but no arguments were given
7+
);
8+
}

src/test/ui/macros/issue-83340.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: 1 positional argument in format string, but no arguments were given
2+
--> $DIR/issue-83340.rs:6:4
3+
|
4+
LL | \n {} │",
5+
| ^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)