Skip to content

Commit 6bd5910

Browse files
authored
Rollup merge of rust-lang#87671 - jesyspa:issue-87319-multiple-newlines, r=estebank
Warn when an escaped newline skips multiple lines Resolves rust-lang#87319
2 parents 8ac3d7b + 07aacf5 commit 6bd5910

File tree

7 files changed

+59
-8
lines changed

7 files changed

+59
-8
lines changed

compiler/rustc_lexer/src/unescape.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,17 @@ pub enum EscapeError {
6060
/// After a line ending with '\', the next line contains whitespace
6161
/// characters that are not skipped.
6262
UnskippedWhitespaceWarning,
63+
64+
/// After a line ending with '\', multiple lines are skipped.
65+
MultipleSkippedLinesWarning,
6366
}
6467

6568
impl EscapeError {
6669
/// Returns true for actual errors, as opposed to warnings.
6770
pub fn is_fatal(&self) -> bool {
6871
match self {
6972
EscapeError::UnskippedWhitespaceWarning => false,
73+
EscapeError::MultipleSkippedLinesWarning => false,
7074
_ => true,
7175
}
7276
}
@@ -315,12 +319,17 @@ where
315319
where
316320
F: FnMut(Range<usize>, Result<char, EscapeError>),
317321
{
318-
let str = chars.as_str();
319-
let first_non_space = str
322+
let tail = chars.as_str();
323+
let first_non_space = tail
320324
.bytes()
321325
.position(|b| b != b' ' && b != b'\t' && b != b'\n' && b != b'\r')
322-
.unwrap_or(str.len());
323-
let tail = &str[first_non_space..];
326+
.unwrap_or(tail.len());
327+
if tail[1..first_non_space].contains('\n') {
328+
// The +1 accounts for the escaping slash.
329+
let end = start + first_non_space + 1;
330+
callback(start..end, Err(EscapeError::MultipleSkippedLinesWarning));
331+
}
332+
let tail = &tail[first_non_space..];
324333
if let Some(c) = tail.chars().nth(0) {
325334
// For error reporting, we would like the span to contain the character that was not
326335
// skipped. The +1 is necessary to account for the leading \ that started the escape.

compiler/rustc_lexer/src/unescape/tests.rs

+5
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,10 @@ fn test_unescape_str_warn() {
106106
assert_eq!(unescaped, expected);
107107
}
108108

109+
// Check we can handle escaped newlines at the end of a file.
110+
check("\\\n", &[]);
111+
check("\\\n ", &[]);
112+
109113
check(
110114
"\\\n \u{a0} x",
111115
&[
@@ -115,6 +119,7 @@ fn test_unescape_str_warn() {
115119
(6..7, Ok('x')),
116120
],
117121
);
122+
check("\\\n \n x", &[(0..7, Err(EscapeError::MultipleSkippedLinesWarning)), (7..8, Ok('x'))]);
118123
}
119124

120125
#[test]

compiler/rustc_parse/src/lexer/unescape_error_reporting.rs

+5
Original file line numberDiff line numberDiff line change
@@ -280,6 +280,11 @@ pub(crate) fn emit_unescape_error(
280280
format!("non-ASCII whitespace symbol '{}' is not skipped", c.escape_unicode());
281281
handler.struct_span_warn(span, &msg).span_label(char_span, &msg).emit();
282282
}
283+
EscapeError::MultipleSkippedLinesWarning => {
284+
let msg = "multiple lines skipped by escaped newline";
285+
let bottom_msg = "skipping everything up to and including this point";
286+
handler.struct_span_warn(span, msg).span_label(span, bottom_msg).emit();
287+
}
283288
}
284289
}
285290

src/test/ui/fmt/format-string-error-2.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ fn main() {
55
a");
66
//~^ ERROR invalid format string
77
format!("{ \
8-
8+
\
99
b");
1010
//~^ ERROR invalid format string
1111
format!(r#"{ \
@@ -38,12 +38,12 @@ fn main() {
3838
{ \
3939
\
4040
b \
41-
41+
\
4242
");
4343
//~^^^ ERROR invalid format string
4444
format!(r#"
4545
raw { \
46-
46+
\
4747
c"#);
4848
//~^^^ ERROR invalid format string
4949
format!(r#"

src/test/ui/fmt/format-string-error-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ error: invalid format string: expected `'}'`, found `'b'`
1919
|
2020
LL | format!("{ \
2121
| - because of this opening brace
22-
LL |
22+
LL | \
2323
LL | b");
2424
| ^ expected `}` in format string
2525
|

src/test/ui/str/str-escape.rs

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// check-pass
2+
fn main() {
3+
let s = "\
4+
5+
";
6+
//~^^^ WARNING multiple lines skipped by escaped newline
7+
let s = "foo\
8+
  bar
9+
";
10+
//~^^^ WARNING non-ASCII whitespace symbol '\u{a0}' is not skipped
11+
}

src/test/ui/str/str-escape.stderr

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
warning: multiple lines skipped by escaped newline
2+
--> $DIR/str-escape.rs:3:14
3+
|
4+
LL | let s = "\
5+
| ______________^
6+
LL | |
7+
LL | | ";
8+
| |_____________^ skipping everything up to and including this point
9+
10+
warning: non-ASCII whitespace symbol '\u{a0}' is not skipped
11+
--> $DIR/str-escape.rs:7:17
12+
|
13+
LL | let s = "foo\
14+
| _________________^
15+
LL | |   bar
16+
| | ^ non-ASCII whitespace symbol '\u{a0}' is not skipped
17+
| |___|
18+
|
19+
20+
warning: 2 warnings emitted
21+

0 commit comments

Comments
 (0)