Skip to content

Commit c9baaa7

Browse files
committed
Fixes #64919. Suggest fix based on operator precendence.
1 parent 22bc9e1 commit c9baaa7

File tree

3 files changed

+43
-2
lines changed

3 files changed

+43
-2
lines changed

src/librustc_typeck/check/mod.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -4344,7 +4344,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
43444344
let max_len = receiver.rfind(".").unwrap();
43454345
format!("{}{}", &receiver[..max_len], method_call)
43464346
} else {
4347-
format!("{}{}", receiver, method_call)
4347+
match &expr.kind {
4348+
ExprKind::Binary(_,_,_) => format!("({}){}", receiver, method_call),
4349+
ExprKind::Unary(_,_) => format!("({}){}", receiver, method_call),
4350+
_ => format!("{}{}", receiver, method_call),
4351+
}
43484352
};
43494353
Some(if is_struct_pat_shorthand_field {
43504354
format!("{}: {}", receiver, sugg)

src/test/ui/mismatched_types/abridged.rs

+9
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,13 @@ fn e() -> X<X<String, String>, String> {
5050
x //~ ERROR mismatched types
5151
}
5252

53+
fn f() -> String {
54+
1+2 //~ ERROR mismatched types
55+
}
56+
57+
58+
fn g() -> String {
59+
-2 //~ ERROR mismatched types
60+
}
61+
5362
fn main() {}

src/test/ui/mismatched_types/abridged.stderr

+29-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,34 @@ LL | x
6666
= note: expected type `X<X<_, std::string::String>, _>`
6767
found type `X<X<_, {integer}>, _>`
6868

69-
error: aborting due to 6 previous errors
69+
error[E0308]: mismatched types
70+
--> $DIR/abridged.rs:54:5
71+
|
72+
LL | fn f() -> String {
73+
| ------ expected `std::string::String` because of return type
74+
LL | 1+2
75+
| ^^^
76+
| |
77+
| expected struct `std::string::String`, found integer
78+
| help: try using a conversion method: `(1+2).to_string()`
79+
|
80+
= note: expected type `std::string::String`
81+
found type `{integer}`
82+
83+
error[E0308]: mismatched types
84+
--> $DIR/abridged.rs:59:5
85+
|
86+
LL | fn g() -> String {
87+
| ------ expected `std::string::String` because of return type
88+
LL | -2
89+
| ^^
90+
| |
91+
| expected struct `std::string::String`, found integer
92+
| help: try using a conversion method: `(-2).to_string()`
93+
|
94+
= note: expected type `std::string::String`
95+
found type `{integer}`
96+
97+
error: aborting due to 8 previous errors
7098

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

0 commit comments

Comments
 (0)