Skip to content

Commit

Permalink
Fixes #64919. Suggest fix based on operator precendence.
Browse files Browse the repository at this point in the history
  • Loading branch information
sam09 committed Sep 30, 2019
1 parent 22bc9e1 commit c9baaa7
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4344,7 +4344,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let max_len = receiver.rfind(".").unwrap();
format!("{}{}", &receiver[..max_len], method_call)
} else {
format!("{}{}", receiver, method_call)
match &expr.kind {
ExprKind::Binary(_,_,_) => format!("({}){}", receiver, method_call),
ExprKind::Unary(_,_) => format!("({}){}", receiver, method_call),
_ => format!("{}{}", receiver, method_call),
}
};
Some(if is_struct_pat_shorthand_field {
format!("{}: {}", receiver, sugg)
Expand Down
9 changes: 9 additions & 0 deletions src/test/ui/mismatched_types/abridged.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,13 @@ fn e() -> X<X<String, String>, String> {
x //~ ERROR mismatched types
}

fn f() -> String {
1+2 //~ ERROR mismatched types
}


fn g() -> String {
-2 //~ ERROR mismatched types
}

fn main() {}
30 changes: 29 additions & 1 deletion src/test/ui/mismatched_types/abridged.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,34 @@ LL | x
= note: expected type `X<X<_, std::string::String>, _>`
found type `X<X<_, {integer}>, _>`

error: aborting due to 6 previous errors
error[E0308]: mismatched types
--> $DIR/abridged.rs:54:5
|
LL | fn f() -> String {
| ------ expected `std::string::String` because of return type
LL | 1+2
| ^^^
| |
| expected struct `std::string::String`, found integer
| help: try using a conversion method: `(1+2).to_string()`
|
= note: expected type `std::string::String`
found type `{integer}`

error[E0308]: mismatched types
--> $DIR/abridged.rs:59:5
|
LL | fn g() -> String {
| ------ expected `std::string::String` because of return type
LL | -2
| ^^
| |
| expected struct `std::string::String`, found integer
| help: try using a conversion method: `(-2).to_string()`
|
= note: expected type `std::string::String`
found type `{integer}`

error: aborting due to 8 previous errors

For more information about this error, try `rustc --explain E0308`.

0 comments on commit c9baaa7

Please sign in to comment.