Skip to content

Commit eb478e2

Browse files
committed
Add extra whitespace for suggestions
1 parent 7c84914 commit eb478e2

14 files changed

+79
-17
lines changed

src/librustc_errors/emitter.rs

+31-6
Original file line numberDiff line numberDiff line change
@@ -1060,11 +1060,11 @@ impl EmitterWriter {
10601060
-> io::Result<()> {
10611061
use std::borrow::Borrow;
10621062

1063-
let primary_span = suggestion.substitution_spans().next().unwrap();
1063+
let primary_sub = &suggestion.substitution_parts[0];
10641064
if let Some(ref cm) = self.cm {
10651065
let mut buffer = StyledBuffer::new();
10661066

1067-
let lines = cm.span_to_lines(primary_span).unwrap();
1067+
let lines = cm.span_to_lines(primary_sub.span).unwrap();
10681068

10691069
assert!(!lines.lines.is_empty());
10701070

@@ -1077,26 +1077,51 @@ impl EmitterWriter {
10771077
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;
1081-
let mut row_num = 1;
1080+
let span_start_pos = cm.lookup_char_pos(primary_sub.span.lo);
1081+
let span_end_pos = cm.lookup_char_pos(primary_sub.span.hi);
1082+
let line_start = span_start_pos.line;
1083+
draw_col_separator_no_space(&mut buffer, 1, max_line_num_len + 1);
1084+
let mut row_num = 2;
10821085
for complete in suggestions.iter().take(MAX_SUGGESTIONS) {
1086+
let mut line_pos = 0;
1087+
// Only show underline if there's a single suggestion and it is a single line
1088+
let show_underline = complete.lines().count() == 1
1089+
&& span_start_pos.line == span_end_pos.line
1090+
&& primary_sub.substitutions.len() == 1;
10831091

10841092
let mut lines = complete.lines();
10851093
for line in lines.by_ref().take(MAX_HIGHLIGHT_LINES) {
1086-
// print the span column to avoid confusion
1094+
// Print the span column to avoid confusion
10871095
buffer.puts(row_num,
10881096
0,
1089-
&((line_start + row_num).to_string()),
1097+
&((line_start + line_pos).to_string()),
10901098
Style::LineNumber);
10911099
// print the suggestion
10921100
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
10931101
buffer.append(row_num, line, Style::NoStyle);
10941102
row_num += 1;
1103+
if show_underline {
1104+
draw_col_separator(&mut buffer, row_num, max_line_num_len + 1);
1105+
1106+
let sub_len = primary_sub.substitutions[0].trim_right().len();
1107+
let underline_start = span_start_pos.col.0;
1108+
let underline_end = span_start_pos.col.0 + sub_len;
1109+
for p in underline_start..underline_end {
1110+
buffer.putc(row_num,
1111+
max_line_num_len + 3 + p,
1112+
'^',
1113+
Style::UnderlinePrimary);
1114+
}
1115+
row_num += 1;
1116+
}
1117+
line_pos += 1;
10951118
}
10961119

10971120
// if we elided some lines, add an ellipsis
10981121
if let Some(_) = lines.next() {
10991122
buffer.append(row_num, "...", Style::NoStyle);
1123+
} else if !show_underline && suggestions.len() <= MAX_SUGGESTIONS {
1124+
draw_col_separator_no_space(&mut buffer, row_num, max_line_num_len + 1);
11001125
}
11011126
}
11021127
if suggestions.len() > MAX_SUGGESTIONS {

src/libsyntax/parse/parser.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2920,10 +2920,6 @@ impl<'a> Parser<'a> {
29202920
err.cancel();
29212921
let codemap = self.sess.codemap();
29222922
let suggestion_span = lhs_span.to(self.prev_span);
2923-
let suggestion = match codemap.span_to_snippet(suggestion_span) {
2924-
Ok(lstring) => format!("({})", lstring),
2925-
_ => format!("(<expression> as <type>)")
2926-
};
29272923
let warn_message = match codemap.span_to_snippet(self.prev_span) {
29282924
Ok(lstring) => format!("`{}`", lstring),
29292925
_ => "a type".to_string(),
@@ -2934,6 +2930,10 @@ impl<'a> Parser<'a> {
29342930
let mut err = self.sess.span_diagnostic.struct_span_err(sp, &msg);
29352931
err.span_label(sp, "interpreted as generic argument");
29362932
err.span_label(self.span, "not interpreted as comparison");
2933+
let suggestion = match codemap.span_to_snippet(suggestion_span) {
2934+
Ok(lstring) => format!("({})", lstring),
2935+
_ => format!("(<expression> as <type>)")
2936+
};
29372937
err.span_suggestion(suggestion_span,
29382938
"if you want to compare the casted value then write:",
29392939
suggestion);

src/test/ui/issue-22644.stderr

+4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ 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+
|
1011
16 | println!("{}", (a as usize) < b);
12+
| ^^^^^^^^^^^^
1113

1214
error: `<` is interpreted as a start of generic arguments for `usize`, not a comparison
1315
--> $DIR/issue-22644.rs:17:33
@@ -18,7 +20,9 @@ error: `<` is interpreted as a start of generic arguments for `usize`, not a com
1820
| not interpreted as comparison
1921
|
2022
help: if you want to compare the casted value then write:
23+
|
2124
17 | println!("{}", (a as usize) < 4);
25+
| ^^^^^^^^^^^^
2226

2327
error: aborting due to 2 previous errors
2428

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

+6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
|
89
12 | use namespaced_enums::Foo::A;
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
911

1012
error[E0425]: cannot find function `B` in module `namespaced_enums`
1113
--> $DIR/enums-are-namespaced-xc.rs:18:31
@@ -14,7 +16,9 @@ error[E0425]: cannot find function `B` in module `namespaced_enums`
1416
| ^ not found in `namespaced_enums`
1517
|
1618
help: possible candidate is found in another module, you can import it into scope
19+
|
1720
12 | use namespaced_enums::Foo::B;
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1822

1923
error[E0422]: cannot find struct, variant or union type `C` in module `namespaced_enums`
2024
--> $DIR/enums-are-namespaced-xc.rs:21:31
@@ -23,7 +27,9 @@ error[E0422]: cannot find struct, variant or union type `C` in module `namespace
2327
| ^ not found in `namespaced_enums`
2428
|
2529
help: possible candidate is found in another module, you can import it into scope
30+
|
2631
12 | use namespaced_enums::Foo::C;
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2733

2834
error: aborting due to 3 previous errors
2935

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

+4-2
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ 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+
|
89
12 | use std::fmt::Result;
9-
13 | use std::io::Result;
10-
14 | use std::thread::Result;
10+
12 | use std::io::Result;
11+
12 | use std::thread::Result;
12+
|
1113

1214
error: aborting due to previous error
1315

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
|
89
11 | use SomeEnum::E;
10+
| ^^^^^^^^^^^^^^^^
911

1012
error: aborting due to previous error
1113

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

+10-5
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,11 @@ 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+
|
89
11 | use mul1::Mul;
9-
12 | use mul2::Mul;
10-
13 | use std::ops::Mul;
10+
11 | use mul2::Mul;
11+
11 | use std::ops::Mul;
12+
|
1113

1214
error[E0412]: cannot find type `Mul` in this scope
1315
--> $DIR/issue-21221-1.rs:72:16
@@ -16,10 +18,11 @@ error[E0412]: cannot find type `Mul` in this scope
1618
| ^^^ not found in this scope
1719
|
1820
help: possible candidates are found in other modules, you can import them into scope
21+
|
1922
11 | use mul1::Mul;
20-
12 | use mul2::Mul;
21-
13 | use mul3::Mul;
22-
14 | use mul4::Mul;
23+
11 | use mul2::Mul;
24+
11 | use mul3::Mul;
25+
11 | use mul4::Mul;
2326
and 2 other candidates
2427

2528
error[E0405]: cannot find trait `ThisTraitReallyDoesntExistInAnyModuleReally` in this scope
@@ -35,7 +38,9 @@ error[E0405]: cannot find trait `Div` in this scope
3538
| ^^^ not found in this scope
3639
|
3740
help: possible candidate is found in another module, you can import it into scope
41+
|
3842
11 | use std::ops::Div;
43+
| ^^^^^^^^^^^^^^^^^^
3944

4045
error: cannot continue compilation due to previous error
4146

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
|
89
11 | use foo::bar::T;
10+
| ^^^^^^^^^^^^^^^^
911

1012
error[E0601]: main function not found
1113

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
|
89
16 | use issue_21221_3::outer::OuterTrait;
10+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
911

1012
error: cannot continue compilation due to previous error
1113

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
|
89
16 | use issue_21221_4::T;
10+
| ^^^^^^^^^^^^^^^^^^^^^
911

1012
error: cannot continue compilation due to previous error
1113

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
|
89
12 | use issue_3907::Foo;
10+
| ^^^^^^^^^^^^^^^^^^^^
911

1012
error: cannot continue compilation due to previous error
1113

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

+6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@ 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+
|
1213
15 | use m::n::Z;
14+
| ^^^^^^^^^^^^
1315

1416
error[E0423]: expected value, found struct `S`
1517
--> $DIR/privacy-struct-ctor.rs:36:5
@@ -21,7 +23,9 @@ error[E0423]: expected value, found struct `S`
2123
| constructor is not visible here due to private fields
2224
|
2325
help: possible better candidate is found in another module, you can import it into scope
26+
|
2427
13 | use m::S;
28+
| ^^^^^^^^^
2529

2630
error[E0423]: expected value, found struct `xcrate::S`
2731
--> $DIR/privacy-struct-ctor.rs:42:5
@@ -33,7 +37,9 @@ error[E0423]: expected value, found struct `xcrate::S`
3337
| constructor is not visible here due to private fields
3438
|
3539
help: possible better candidate is found in another module, you can import it into scope
40+
|
3641
13 | use m::S;
42+
| ^^^^^^^^^
3743

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

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
|
89
11 | use std::ops::Add;
10+
| ^^^^^^^^^^^^^^^^^^
911

1012
error[E0601]: main function not found
1113

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

+2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ 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+
|
89
12 | let x = "Hello ".to_owned() + "World!";
10+
| ^^^^^^^^^^^^^^^^^^^
911

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

0 commit comments

Comments
 (0)