Skip to content

Commit f4b1e2a

Browse files
committed
Improve E0178 suggestion placement
1 parent 3a5567b commit f4b1e2a

File tree

7 files changed

+60
-23
lines changed

7 files changed

+60
-23
lines changed

src/librustc_errors/emitter.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ impl Emitter for EmitterWriter {
3737

3838
if let Some(sugg) = db.suggestion.clone() {
3939
assert_eq!(sugg.msp.primary_spans().len(), sugg.substitutes.len());
40-
if sugg.substitutes.len() == 1 && // don't display multispans as labels
41-
sugg.msg.split_whitespace().count() < 10 && // don't display long messages as labels
42-
sugg.substitutes[0].find('\n').is_none() { // don't display multiline suggestions as labels
40+
// don't display multispans as labels
41+
if sugg.substitutes.len() == 1 &&
42+
// don't display long messages as labels
43+
sugg.msg.split_whitespace().count() < 10 &&
44+
// don't display multiline suggestions as labels
45+
sugg.substitutes[0].find('\n').is_none() {
4346
let msg = format!("{} `{}`", sugg.msg, sugg.substitutes[0]);
4447
primary_span.push_span_label(sugg.msp.primary_spans()[0], msg);
4548
} else {

src/librustc_typeck/diagnostics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3185,7 +3185,7 @@ implementing traits from `std::ops`.
31853185
String concatenation appends the string on the right to the string on the
31863186
left and may require reallocation. This requires ownership of the string
31873187
on the left. If something should be added to a string literal, move the
3188-
literal to the heap by allocating it with `to_owned()` like in
3188+
literal to the heap by allocating it with `to_owned()` like in
31893189
`"Your text".to_owned()`.
31903190
31913191
"##,

src/libsyntax/parse/parser.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1490,9 +1490,8 @@ impl<'a> Parser<'a> {
14901490
let bounds = self.parse_ty_param_bounds()?;
14911491
let sum_span = ty.span.to(self.prev_span);
14921492

1493-
let mut err = struct_span_err!(self.sess.span_diagnostic, ty.span, E0178,
1493+
let mut err = struct_span_err!(self.sess.span_diagnostic, sum_span, E0178,
14941494
"expected a path on the left-hand side of `+`, not `{}`", pprust::ty_to_string(&ty));
1495-
err.span_label(ty.span, &format!("expected a path"));
14961495

14971496
match ty.node {
14981497
TyKind::Rptr(ref lifetime, ref mut_ty) => {
@@ -1511,9 +1510,11 @@ impl<'a> Parser<'a> {
15111510
err.span_suggestion(sum_span, "try adding parentheses:", sum_with_parens);
15121511
}
15131512
TyKind::Ptr(..) | TyKind::BareFn(..) => {
1514-
help!(&mut err, "perhaps you forgot parentheses?");
1513+
err.span_label(sum_span, &"perhaps you forgot parentheses?");
15151514
}
1516-
_ => {}
1515+
_ => {
1516+
err.span_label(sum_span, &"expected a path");
1517+
},
15171518
}
15181519
err.emit();
15191520
Ok(())

src/test/compile-fail/E0178.rs renamed to src/test/ui/did_you_mean/E0178.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,9 @@ trait Foo {}
1212

1313
struct Bar<'a> {
1414
w: &'a Foo + Copy,
15-
//~^ ERROR E0178
16-
//~| NOTE expected a path
1715
x: &'a Foo + 'a,
18-
//~^ ERROR E0178
19-
//~| NOTE expected a path
2016
y: &'a mut Foo + 'a,
21-
//~^ ERROR E0178
22-
//~| NOTE expected a path
2317
z: fn() -> Foo + 'a,
24-
//~^ ERROR E0178
25-
//~| NOTE expected a path
2618
}
2719

2820
fn main() {

src/test/ui/did_you_mean/E0178.stderr

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
error[E0178]: expected a path on the left-hand side of `+`, not `&'a Foo`
2+
--> $DIR/E0178.rs:14:8
3+
|
4+
14 | w: &'a Foo + Copy,
5+
| ^^^^^^^^^^^^^^ try adding parentheses: `&'a (Foo + Copy)`
6+
7+
error[E0178]: expected a path on the left-hand side of `+`, not `&'a Foo`
8+
--> $DIR/E0178.rs:15:8
9+
|
10+
15 | x: &'a Foo + 'a,
11+
| ^^^^^^^^^^^^ try adding parentheses: `&'a (Foo + 'a)`
12+
13+
error[E0178]: expected a path on the left-hand side of `+`, not `&'a mut Foo`
14+
--> $DIR/E0178.rs:16:8
15+
|
16+
16 | y: &'a mut Foo + 'a,
17+
| ^^^^^^^^^^^^^^^^ try adding parentheses: `&'a mut (Foo + 'a)`
18+
19+
error[E0178]: expected a path on the left-hand side of `+`, not `fn() -> Foo`
20+
--> $DIR/E0178.rs:17:8
21+
|
22+
17 | z: fn() -> Foo + 'a,
23+
| ^^^^^^^^^^^^^^^^ perhaps you forgot parentheses?
24+
25+
error: aborting due to 4 previous errors
26+

src/test/compile-fail/trait-object-reference-without-parens-suggestion.rs renamed to src/test/ui/did_you_mean/trait-object-reference-without-parens-suggestion.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,5 @@
1010

1111
fn main() {
1212
let _: &Copy + 'static;
13-
//~^ ERROR expected a path
14-
//~| HELP try adding parentheses
15-
//~| SUGGESTION let _: &(Copy + 'static);
16-
//~| ERROR the trait `std::marker::Copy` cannot be made into an object
1713
let _: &'static Copy + 'static;
18-
//~^ ERROR expected a path
19-
//~| HELP try adding parentheses
20-
//~| SUGGESTION let _: &'static (Copy + 'static);
2114
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0178]: expected a path on the left-hand side of `+`, not `&Copy`
2+
--> $DIR/trait-object-reference-without-parens-suggestion.rs:12:12
3+
|
4+
12 | let _: &Copy + 'static;
5+
| ^^^^^^^^^^^^^^^ try adding parentheses: `&(Copy + 'static)`
6+
7+
error[E0178]: expected a path on the left-hand side of `+`, not `&'static Copy`
8+
--> $DIR/trait-object-reference-without-parens-suggestion.rs:13:12
9+
|
10+
13 | let _: &'static Copy + 'static;
11+
| ^^^^^^^^^^^^^^^^^^^^^^^ try adding parentheses: `&'static (Copy + 'static)`
12+
13+
error[E0038]: the trait `std::marker::Copy` cannot be made into an object
14+
--> $DIR/trait-object-reference-without-parens-suggestion.rs:12:12
15+
|
16+
12 | let _: &Copy + 'static;
17+
| ^^^^^ the trait `std::marker::Copy` cannot be made into an object
18+
|
19+
= note: the trait cannot require that `Self : Sized`
20+
21+
error: aborting due to previous error
22+

0 commit comments

Comments
 (0)