Skip to content

Idea: suggest coercing to &T from &mut T if it satisfies type check #109352

Closed

Description

@ErichDonGubler

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 Foo

Desired 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

Activity

  1. added
    A-diagnostics
    T-compiler
    on Mar 19, 2023
  2. WalterSmuts commented on Mar 19, 2023

    @WalterSmuts

    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:

    help: consider re-borrowing here: `&*ref_mut_foo

    Since we're not just adding a & but rather a &*?

  3. ErichDonGubler commented on Mar 20, 2023

    @ErichDonGubler
    ContributorAuthor

    @WalterSmuts:

    Should the suggested help-message not read:

    help: consider re-borrowing here: `&*ref_mut_foo

    Since we're not just adding a & but rather a &*?

    I think your point is valid. Edited!

  4. added
    A-suggestion-diagnostics
    on Mar 20, 2023
  5. added a commit that references this issue on Aug 1, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsA-suggestion-diagnosticsT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions