Skip to content

Commit 7c84914

Browse files
committed
Make suggestion include the line number
When there're more than one suggestions in the same diagnostic, they are displayed in their own block, instead of inline. In order to reduce confusion, those blocks now display the line number.
1 parent cd72f2e commit 7c84914

13 files changed

+37
-33
lines changed

src/librustc_errors/emitter.rs

+11-7
Original file line numberDiff line numberDiff line change
@@ -1071,20 +1071,24 @@ impl EmitterWriter {
10711071
buffer.append(0, &level.to_string(), Style::Level(level.clone()));
10721072
buffer.append(0, ": ", Style::HeaderMsg);
10731073
self.msg_to_buffer(&mut buffer,
1074-
&[(suggestion.msg.to_owned(), Style::NoStyle)],
1075-
max_line_num_len,
1076-
"suggestion",
1077-
Some(Style::HeaderMsg));
1074+
&[(suggestion.msg.to_owned(), Style::NoStyle)],
1075+
max_line_num_len,
1076+
"suggestion",
1077+
Some(Style::HeaderMsg));
10781078

10791079
let suggestions = suggestion.splice_lines(cm.borrow());
1080+
let line_start = cm.lookup_char_pos(primary_span.lo).line - 1;
10801081
let mut row_num = 1;
10811082
for complete in suggestions.iter().take(MAX_SUGGESTIONS) {
10821083

1083-
// print the suggestion without any line numbers, but leave
1084-
// space for them. This helps with lining up with previous
1085-
// snippets from the actual error being reported.
10861084
let mut lines = complete.lines();
10871085
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
1086+
// print the span column to avoid confusion
1087+
buffer.puts(row_num,
1088+
0,
1089+
&((line_start + row_num).to_string()),
1090+
Style::LineNumber);
1091+
// print the suggestion
10881092
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
10891093
buffer.append(row_num, line, Style::NoStyle);
10901094
row_num += 1;

src/test/ui/issue-22644.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ error: `<` is interpreted as a start of generic arguments for `usize`, not a com
77
| not interpreted as comparison
88
|
99
help: if you want to compare the casted value then write:
10-
| println!("{}", (a as usize) < b);
10+
16 | println!("{}", (a as usize) < b);
1111

1212
error: `<` is interpreted as a start of generic arguments for `usize`, not a comparison
1313
--> $DIR/issue-22644.rs:17:33
@@ -18,7 +18,7 @@ error: `<` is interpreted as a start of generic arguments for `usize`, not a com
1818
| not interpreted as comparison
1919
|
2020
help: if you want to compare the casted value then write:
21-
| println!("{}", (a as usize) < 4);
21+
17 | println!("{}", (a as usize) < 4);
2222

2323
error: aborting due to 2 previous errors
2424

src/test/ui/resolve/enums-are-namespaced-xc.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0425]: cannot find value `A` in module `namespaced_enums`
55
| ^ not found in `namespaced_enums`
66
|
77
help: possible candidate is found in another module, you can import it into scope
8-
| use namespaced_enums::Foo::A;
8+
12 | use namespaced_enums::Foo::A;
99

1010
error[E0425]: cannot find function `B` in module `namespaced_enums`
1111
--> $DIR/enums-are-namespaced-xc.rs:18:31
@@ -14,7 +14,7 @@ error[E0425]: cannot find function `B` in module `namespaced_enums`
1414
| ^ not found in `namespaced_enums`
1515
|
1616
help: possible candidate is found in another module, you can import it into scope
17-
| use namespaced_enums::Foo::B;
17+
12 | use namespaced_enums::Foo::B;
1818

1919
error[E0422]: cannot find struct, variant or union type `C` in module `namespaced_enums`
2020
--> $DIR/enums-are-namespaced-xc.rs:21:31
@@ -23,7 +23,7 @@ error[E0422]: cannot find struct, variant or union type `C` in module `namespace
2323
| ^ not found in `namespaced_enums`
2424
|
2525
help: possible candidate is found in another module, you can import it into scope
26-
| use namespaced_enums::Foo::C;
26+
12 | use namespaced_enums::Foo::C;
2727

2828
error: aborting due to 3 previous errors
2929

src/test/ui/resolve/issue-16058.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ error[E0574]: expected struct, variant or union type, found enum `Result`
55
| ^^^^^^ not a struct, variant or union type
66
|
77
help: possible better candidates are found in other modules, you can import them into scope
8-
| use std::fmt::Result;
9-
| use std::io::Result;
10-
| use std::thread::Result;
8+
12 | use std::fmt::Result;
9+
13 | use std::io::Result;
10+
14 | use std::thread::Result;
1111

1212
error: aborting due to previous error
1313

src/test/ui/resolve/issue-17518.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0422]: cannot find struct, variant or union type `E` in this scope
55
| ^ not found in this scope
66
|
77
help: possible candidate is found in another module, you can import it into scope
8-
| use SomeEnum::E;
8+
11 | use SomeEnum::E;
99

1010
error: aborting due to previous error
1111

src/test/ui/resolve/issue-21221-1.stderr

+8-8
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ error[E0405]: cannot find trait `Mul` in this scope
55
| ^^^ not found in this scope
66
|
77
help: possible candidates are found in other modules, you can import them into scope
8-
| use mul1::Mul;
9-
| use mul2::Mul;
10-
| use std::ops::Mul;
8+
11 | use mul1::Mul;
9+
12 | use mul2::Mul;
10+
13 | use std::ops::Mul;
1111

1212
error[E0412]: cannot find type `Mul` in this scope
1313
--> $DIR/issue-21221-1.rs:72:16
@@ -16,10 +16,10 @@ error[E0412]: cannot find type `Mul` in this scope
1616
| ^^^ not found in this scope
1717
|
1818
help: possible candidates are found in other modules, you can import them into scope
19-
| use mul1::Mul;
20-
| use mul2::Mul;
21-
| use mul3::Mul;
22-
| use mul4::Mul;
19+
11 | use mul1::Mul;
20+
12 | use mul2::Mul;
21+
13 | use mul3::Mul;
22+
14 | use mul4::Mul;
2323
and 2 other candidates
2424

2525
error[E0405]: cannot find trait `ThisTraitReallyDoesntExistInAnyModuleReally` in this scope
@@ -35,7 +35,7 @@ error[E0405]: cannot find trait `Div` in this scope
3535
| ^^^ not found in this scope
3636
|
3737
help: possible candidate is found in another module, you can import it into scope
38-
| use std::ops::Div;
38+
11 | use std::ops::Div;
3939

4040
error: cannot continue compilation due to previous error
4141

src/test/ui/resolve/issue-21221-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `T` in this scope
55
| ^ not found in this scope
66
|
77
help: possible candidate is found in another module, you can import it into scope
8-
| use foo::bar::T;
8+
11 | use foo::bar::T;
99

1010
error[E0601]: main function not found
1111

src/test/ui/resolve/issue-21221-3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `OuterTrait` in this scope
55
| ^^^^^^^^^^ not found in this scope
66
|
77
help: possible candidate is found in another module, you can import it into scope
8-
| use issue_21221_3::outer::OuterTrait;
8+
16 | use issue_21221_3::outer::OuterTrait;
99

1010
error: cannot continue compilation due to previous error
1111

src/test/ui/resolve/issue-21221-4.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0405]: cannot find trait `T` in this scope
55
| ^ not found in this scope
66
|
77
help: possible candidate is found in another module, you can import it into scope
8-
| use issue_21221_4::T;
8+
16 | use issue_21221_4::T;
99

1010
error: cannot continue compilation due to previous error
1111

src/test/ui/resolve/issue-3907.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0404]: expected trait, found type alias `Foo`
55
| ^^^ type aliases cannot be used for traits
66
|
77
help: possible better candidate is found in another module, you can import it into scope
8-
| use issue_3907::Foo;
8+
12 | use issue_3907::Foo;
99

1010
error: cannot continue compilation due to previous error
1111

src/test/ui/resolve/privacy-struct-ctor.stderr

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ error[E0423]: expected value, found struct `Z`
99
| constructor is not visible here due to private fields
1010
|
1111
help: possible better candidate is found in another module, you can import it into scope
12-
| use m::n::Z;
12+
15 | use m::n::Z;
1313

1414
error[E0423]: expected value, found struct `S`
1515
--> $DIR/privacy-struct-ctor.rs:36:5
@@ -21,7 +21,7 @@ error[E0423]: expected value, found struct `S`
2121
| constructor is not visible here due to private fields
2222
|
2323
help: possible better candidate is found in another module, you can import it into scope
24-
| use m::S;
24+
13 | use m::S;
2525

2626
error[E0423]: expected value, found struct `xcrate::S`
2727
--> $DIR/privacy-struct-ctor.rs:42:5
@@ -33,7 +33,7 @@ error[E0423]: expected value, found struct `xcrate::S`
3333
| constructor is not visible here due to private fields
3434
|
3535
help: possible better candidate is found in another module, you can import it into scope
36-
| use m::S;
36+
13 | use m::S;
3737

3838
error[E0603]: tuple struct `Z` is private
3939
--> $DIR/privacy-struct-ctor.rs:25:9

src/test/ui/span/issue-35987.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0404]: expected trait, found type parameter `Add`
55
| ^^^ not a trait
66
|
77
help: possible better candidate is found in another module, you can import it into scope
8-
| use std::ops::Add;
8+
11 | use std::ops::Add;
99

1010
error[E0601]: main function not found
1111

src/test/ui/span/issue-39018.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ error[E0369]: binary operation `+` cannot be applied to type `&'static str`
55
| ^^^^^^^^^^^^^^^^^^^ `+` can't be used to concatenate two `&str` strings
66
|
77
help: `to_owned()` can be used to create an owned `String` from a string reference. String concatenation appends the string on the right to the string on the left and may require reallocation. This requires ownership of the string on the left.
8-
| let x = "Hello ".to_owned() + "World!";
8+
12 | let x = "Hello ".to_owned() + "World!";
99

1010
error[E0369]: binary operation `+` cannot be applied to type `World`
1111
--> $DIR/issue-39018.rs:17:13

0 commit comments

Comments
 (0)