Repository navigation
Closed
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
Description
Finally getting around to filing this issue at @estebank's encouragement in this (rather embarrassing) Reddit thread. CC @WalterSmuts and @pali6.
Code
I've tried to capture things in this Playground link; to summarize:
struct Foo;
impl std::ops::Mul for &Foo {
type Output = Foo;
fn mul(self, _rhs: Self) -> Self::Output {
unimplemented!()
}
}
#[test]
fn it_works() {
let ref_mut_foo: &mut Foo = &mut Foo;
let ref_foo: &Foo = &Foo;
let owned_foo: Foo = Foo;
// The following assignments cause compiler errors that can be resolved by
// getting immutable borrows of `ref_mut_foo`'s pointee (i.e.,
// `&*ref_mut_foo`):
let _ = ref_mut_foo * ref_foo;
let _ = ref_mut_foo * ref_mut_foo;
let _ = ref_mut_foo * &owned_foo;
// …but we currently miss the opportunity to inform a user of the
// possibility (😢).
}Current output
error[E0369]: cannot multiply `&mut Foo` by `&Foo`
--> src/lib.rs:20:25
|
20 | let _ = ref_mut_foo * ref_foo;
| ----------- ^ ------- &Foo
| |
| &mut Foo
error[E0369]: cannot multiply `&mut Foo` by `&mut Foo`
--> src/lib.rs:21:25
|
21 | let _ = ref_mut_foo * ref_mut_foo;
| ----------- ^ ----------- &mut Foo
| |
| &mut Foo
error[E0369]: cannot multiply `&mut Foo` by `&Foo`
--> src/lib.rs:22:25
|
22 | let _ = ref_mut_foo * &owned_foo;
| ----------- ^ ---------- &Foo
| |
| &mut FooDesired output
error[E0369]: cannot multiply `&mut Foo` by `&Foo`
--> src/lib.rs:20:25
|
20 | let _ = ref_mut_foo * ref_foo;
| ----------- ^ ------- &Foo
| |
| &mut Foo
|
= help: consider re-borrowing here: `&*ref_mut_foo * ref_foo`
error[E0369]: cannot multiply `&mut Foo` by `&mut Foo`
--> src/lib.rs:21:25
|
21 | let _ = ref_mut_foo * ref_mut_foo;
| ----------- ^ ----------- &mut Foo
| |
| &mut Foo
|
= help: consider re-borrowing here: `&*ref_mut_foo * &*ref_mut_foo`
error[E0369]: cannot multiply `&mut Foo` by `&Foo`
--> src/lib.rs:22:25
|
22 | let _ = ref_mut_foo * &owned_foo;
| ----------- ^ ---------- &Foo
| |
| &mut Foo
|
= help: consider re-borrowing here: `&*ref_mut_foo * &owned_foo`Rationale and extra context
It seems like this should generally be a valid suggestion, and we have to create a diagnostic for this; current error output doesn't help the user to discover the "right" solution at all.
Other cases
No response
Anything else?
No response
Reacted by Jubilee and Walter Smuts
Metadata
Metadata
Assignees
Labels
A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
Activity
WalterSmuts commentedon Mar 19, 2023
Hi Erich, thanks for tagging me :) Took me a couple of minutes to refresh my memory 😅 but glad to see there is something positive coming from that thread :)
Should the suggested help-message not read:
Since we're not just adding a
&but rather a&*?ErichDonGubler commentedon Mar 20, 2023
@WalterSmuts:
I think your point is valid. Edited!
Rollup merge of rust-lang#114288 - Urgau:fix-issue-109352, r=b-naber
Rollup merge of #114288 - Urgau:fix-issue-109352, r=b-naber
WalterSmuts commentedon Aug 5, 2023
Whoot whoot!
https://play.rust-lang.org/?version=nightly&mode=debug&edition=2021&gist=bad8419f3b5e2979e3e3f6a07622c0a6