Skip to content

Commit a3cae57

Browse files
authored
Rollup merge of #62995 - estebank:issue-62973, r=varkor
Avoid ICE when suggestion span is at Eof Fix #62973.
2 parents 2ac9b89 + 6263eb4 commit a3cae57

File tree

4 files changed

+85
-3
lines changed

4 files changed

+85
-3
lines changed

src/librustc_errors/lib.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,8 @@ impl CodeSuggestion {
226226
}
227227
}
228228
if let Some(cur_line) = fm.get_line(cur_lo.line - 1) {
229-
buf.push_str(&cur_line[..cur_lo.col.to_usize()]);
229+
let end = std::cmp::min(cur_line.len(), cur_lo.col.to_usize());
230+
buf.push_str(&cur_line[..end]);
230231
}
231232
}
232233
buf.push_str(&part.snippet);

src/test/ui/parser/issue-62973.rs

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// ignore-tidy-trailing-newlines
2+
// error-pattern: aborting due to 6 previous errors
3+
4+
fn main() {}
5+
6+
fn p() { match s { v, E { [) {) }
7+
8+

src/test/ui/parser/issue-62973.stderr

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
error: this file contains an un-closed delimiter
2+
--> $DIR/issue-62973.rs:8:2
3+
|
4+
LL | fn p() { match s { v, E { [) {) }
5+
| - - un-closed delimiter
6+
| |
7+
| un-closed delimiter
8+
LL |
9+
LL |
10+
| ^
11+
12+
error: expected one of `,` or `}`, found `{`
13+
--> $DIR/issue-62973.rs:6:25
14+
|
15+
LL | fn p() { match s { v, E { [) {) }
16+
| - ^ expected one of `,` or `}` here
17+
| |
18+
| while parsing this struct
19+
20+
error: struct literals are not allowed here
21+
--> $DIR/issue-62973.rs:6:16
22+
|
23+
LL | fn p() { match s { v, E { [) {) }
24+
| ________________^
25+
LL | |
26+
LL | |
27+
| |_^
28+
help: surround the struct literal with parentheses
29+
|
30+
LL | fn p() { match (s { v, E { [) {) }
31+
LL |
32+
LL | )
33+
|
34+
35+
error: expected one of `.`, `?`, `{`, or an operator, found `}`
36+
--> $DIR/issue-62973.rs:8:1
37+
|
38+
LL | fn p() { match s { v, E { [) {) }
39+
| ----- while parsing this match expression
40+
LL |
41+
LL |
42+
| ^ expected one of `.`, `?`, `{`, or an operator here
43+
44+
error: incorrect close delimiter: `)`
45+
--> $DIR/issue-62973.rs:6:28
46+
|
47+
LL | fn p() { match s { v, E { [) {) }
48+
| -^ incorrect close delimiter
49+
| |
50+
| un-closed delimiter
51+
52+
error: incorrect close delimiter: `)`
53+
--> $DIR/issue-62973.rs:6:31
54+
|
55+
LL | fn p() { match s { v, E { [) {) }
56+
| -^ incorrect close delimiter
57+
| |
58+
| un-closed delimiter
59+
60+
error: aborting due to 6 previous errors
61+

src/tools/tidy/src/style.rs

+14-2
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,8 @@ pub fn check(path: &Path, bad: &mut bool) {
152152
let mut skip_file_length = contains_ignore_directive(can_contain, &contents, "filelength");
153153
let mut skip_end_whitespace =
154154
contains_ignore_directive(can_contain, &contents, "end-whitespace");
155+
let mut skip_trailing_newlines =
156+
contains_ignore_directive(can_contain, &contents, "trailing-newlines");
155157
let mut skip_copyright = contains_ignore_directive(can_contain, &contents, "copyright");
156158
let mut leading_new_lines = false;
157159
let mut trailing_new_lines = 0;
@@ -214,10 +216,17 @@ pub fn check(path: &Path, bad: &mut bool) {
214216
if leading_new_lines {
215217
tidy_error!(bad, "{}: leading newline", file.display());
216218
}
219+
let mut err = |msg: &str| {
220+
tidy_error!(bad, "{}: {}", file.display(), msg);
221+
};
217222
match trailing_new_lines {
218-
0 => tidy_error!(bad, "{}: missing trailing newline", file.display()),
223+
0 => suppressible_tidy_err!(err, skip_trailing_newlines, "missing trailing newline"),
219224
1 => {}
220-
n => tidy_error!(bad, "{}: too many trailing newlines ({})", file.display(), n),
225+
n => suppressible_tidy_err!(
226+
err,
227+
skip_trailing_newlines,
228+
&format!("too many trailing newlines ({})", n)
229+
),
221230
};
222231
if lines > LINES {
223232
let mut err = |_| {
@@ -247,6 +256,9 @@ pub fn check(path: &Path, bad: &mut bool) {
247256
if let Directive::Ignore(false) = skip_end_whitespace {
248257
tidy_error!(bad, "{}: ignoring trailing whitespace unnecessarily", file.display());
249258
}
259+
if let Directive::Ignore(false) = skip_trailing_newlines {
260+
tidy_error!(bad, "{}: ignoring trailing newlines unnecessarily", file.display());
261+
}
250262
if let Directive::Ignore(false) = skip_copyright {
251263
tidy_error!(bad, "{}: ignoring copyright unnecessarily", file.display());
252264
}

0 commit comments

Comments
 (0)