-
Notifications
You must be signed in to change notification settings - Fork 13k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #114288 - Urgau:fix-issue-109352, r=b-naber
Improve diagnostic for wrong borrow on binary operations This PR improves the diagnostic for wrong borrow on binary operations by suggesting to reborrow on appropriate expressions. ```diff + = note: an implementation for `&Foo * &Foo` exist + help: consider reborrowing both sides + | + LL | let _ = &*ref_mut_foo * &*ref_mut_foo; + | ++ ++ ``` Fixes #109352
- Loading branch information
Showing
6 changed files
with
326 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
struct Bar; | ||
|
||
impl std::ops::Mul for &mut Bar { | ||
type Output = Bar; | ||
|
||
fn mul(self, _rhs: Self) -> Self::Output { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn main() { | ||
let ref_mut_bar: &mut Bar = &mut Bar; | ||
let ref_bar: &Bar = &Bar; | ||
let owned_bar: Bar = Bar; | ||
|
||
let _ = ref_mut_bar * ref_mut_bar; | ||
|
||
// FIXME: we should be able to suggest borrowing both side | ||
let _ = owned_bar * owned_bar; | ||
//~^ ERROR cannot multiply | ||
let _ = ref_bar * ref_bar; | ||
//~^ ERROR cannot multiply | ||
let _ = ref_bar * ref_mut_bar; | ||
//~^ ERROR cannot multiply | ||
let _ = ref_mut_bar * ref_bar; | ||
//~^ ERROR mismatched types | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
error[E0369]: cannot multiply `Bar` by `Bar` | ||
--> $DIR/borrow-suggestion-109352-2.rs:19:23 | ||
| | ||
LL | let _ = owned_bar * owned_bar; | ||
| --------- ^ --------- Bar | ||
| | | ||
| Bar | ||
| | ||
note: an implementation of `Mul` might be missing for `Bar` | ||
--> $DIR/borrow-suggestion-109352-2.rs:1:1 | ||
| | ||
LL | struct Bar; | ||
| ^^^^^^^^^^ must implement `Mul` | ||
note: the trait `Mul` must be implemented | ||
--> $SRC_DIR/core/src/ops/arith.rs:LL:COL | ||
|
||
error[E0369]: cannot multiply `&Bar` by `&Bar` | ||
--> $DIR/borrow-suggestion-109352-2.rs:21:21 | ||
| | ||
LL | let _ = ref_bar * ref_bar; | ||
| ------- ^ ------- &Bar | ||
| | | ||
| &Bar | ||
| | ||
= note: an implementation for `&mut Bar * &mut Bar` exists | ||
help: consider making this expression a mutable borrow | ||
--> $DIR/borrow-suggestion-109352-2.rs:21:13 | ||
| | ||
LL | let _ = ref_bar * ref_bar; | ||
| ^^^^^^^ | ||
help: consider making this expression a mutable borrow | ||
--> $DIR/borrow-suggestion-109352-2.rs:21:23 | ||
| | ||
LL | let _ = ref_bar * ref_bar; | ||
| ^^^^^^^ | ||
|
||
error[E0369]: cannot multiply `&Bar` by `&mut Bar` | ||
--> $DIR/borrow-suggestion-109352-2.rs:23:21 | ||
| | ||
LL | let _ = ref_bar * ref_mut_bar; | ||
| ------- ^ ----------- &mut Bar | ||
| | | ||
| &Bar | ||
| | ||
= note: an implementation for `&mut Bar * &mut Bar` exists | ||
help: consider making this expression a mutable borrow | ||
--> $DIR/borrow-suggestion-109352-2.rs:23:13 | ||
| | ||
LL | let _ = ref_bar * ref_mut_bar; | ||
| ^^^^^^^ | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/borrow-suggestion-109352-2.rs:25:27 | ||
| | ||
LL | let _ = ref_mut_bar * ref_bar; | ||
| ^^^^^^^ types differ in mutability | ||
| | ||
= note: expected mutable reference `&mut Bar` | ||
found reference `&Bar` | ||
|
||
error: aborting due to 4 previous errors | ||
|
||
Some errors have detailed explanations: E0308, E0369. | ||
For more information about an error, try `rustc --explain E0308`. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// run-rustfix | ||
|
||
struct Foo; | ||
|
||
impl std::ops::Mul for &Foo { | ||
type Output = Foo; | ||
|
||
fn mul(self, _rhs: Self) -> Self::Output { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn main() { | ||
let ref_mut_foo: &mut Foo = &mut Foo; | ||
let ref_foo: &Foo = &Foo; | ||
let owned_foo: Foo = Foo; | ||
|
||
let _ = ref_foo * ref_foo; | ||
let _ = ref_foo * ref_mut_foo; | ||
|
||
let _ = &*ref_mut_foo * ref_foo; | ||
//~^ ERROR cannot multiply | ||
let _ = &*ref_mut_foo * &*ref_mut_foo; | ||
//~^ ERROR cannot multiply | ||
let _ = &*ref_mut_foo * &owned_foo; | ||
//~^ ERROR cannot multiply | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// run-rustfix | ||
|
||
struct Foo; | ||
|
||
impl std::ops::Mul for &Foo { | ||
type Output = Foo; | ||
|
||
fn mul(self, _rhs: Self) -> Self::Output { | ||
unimplemented!() | ||
} | ||
} | ||
|
||
fn main() { | ||
let ref_mut_foo: &mut Foo = &mut Foo; | ||
let ref_foo: &Foo = &Foo; | ||
let owned_foo: Foo = Foo; | ||
|
||
let _ = ref_foo * ref_foo; | ||
let _ = ref_foo * ref_mut_foo; | ||
|
||
let _ = ref_mut_foo * ref_foo; | ||
//~^ ERROR cannot multiply | ||
let _ = ref_mut_foo * ref_mut_foo; | ||
//~^ ERROR cannot multiply | ||
let _ = ref_mut_foo * &owned_foo; | ||
//~^ ERROR cannot multiply | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
error[E0369]: cannot multiply `&mut Foo` by `&Foo` | ||
--> $DIR/borrow-suggestion-109352.rs:21:25 | ||
| | ||
LL | let _ = ref_mut_foo * ref_foo; | ||
| ----------- ^ ------- &Foo | ||
| | | ||
| &mut Foo | ||
| | ||
= note: an implementation for `&Foo * &Foo` exists | ||
help: consider reborrowing this side | ||
| | ||
LL | let _ = &*ref_mut_foo * ref_foo; | ||
| ++ | ||
|
||
error[E0369]: cannot multiply `&mut Foo` by `&mut Foo` | ||
--> $DIR/borrow-suggestion-109352.rs:23:25 | ||
| | ||
LL | let _ = ref_mut_foo * ref_mut_foo; | ||
| ----------- ^ ----------- &mut Foo | ||
| | | ||
| &mut Foo | ||
| | ||
= note: an implementation for `&Foo * &Foo` exists | ||
help: consider reborrowing both sides | ||
| | ||
LL | let _ = &*ref_mut_foo * &*ref_mut_foo; | ||
| ++ ++ | ||
|
||
error[E0369]: cannot multiply `&mut Foo` by `&Foo` | ||
--> $DIR/borrow-suggestion-109352.rs:25:25 | ||
| | ||
LL | let _ = ref_mut_foo * &owned_foo; | ||
| ----------- ^ ---------- &Foo | ||
| | | ||
| &mut Foo | ||
| | ||
= note: an implementation for `&Foo * &Foo` exists | ||
help: consider reborrowing this side | ||
| | ||
LL | let _ = &*ref_mut_foo * &owned_foo; | ||
| ++ | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0369`. |