From c9baaa70beab336c8fcb08b378d999f7309aaef9 Mon Sep 17 00:00:00 2001 From: Sam Radhakrishnan Date: Mon, 30 Sep 2019 17:45:31 -0400 Subject: [PATCH] Fixes #64919. Suggest fix based on operator precendence. --- src/librustc_typeck/check/mod.rs | 6 +++- src/test/ui/mismatched_types/abridged.rs | 9 ++++++ src/test/ui/mismatched_types/abridged.stderr | 30 +++++++++++++++++++- 3 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 5e3db1c7990d..3c825712217e 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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) diff --git a/src/test/ui/mismatched_types/abridged.rs b/src/test/ui/mismatched_types/abridged.rs index 41ab6d4c5784..9a5c183ca342 100644 --- a/src/test/ui/mismatched_types/abridged.rs +++ b/src/test/ui/mismatched_types/abridged.rs @@ -50,4 +50,13 @@ fn e() -> X, String> { x //~ ERROR mismatched types } +fn f() -> String { + 1+2 //~ ERROR mismatched types +} + + +fn g() -> String { + -2 //~ ERROR mismatched types +} + fn main() {} diff --git a/src/test/ui/mismatched_types/abridged.stderr b/src/test/ui/mismatched_types/abridged.stderr index b7f3b3dde8a9..ded12d89c099 100644 --- a/src/test/ui/mismatched_types/abridged.stderr +++ b/src/test/ui/mismatched_types/abridged.stderr @@ -66,6 +66,34 @@ LL | x = note: expected type `X, _>` found type `X, _>` -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`.