Skip to content

Compiler should hint to user to deref when doing reference assignment #33570

Closed

Description

@frewsxcv

https://is.gd/wm0S9G

fn change_opt(opt: &mut Option<String>){
    opt = None
}
<anon>:2:11: 2:15 error: mismatched types:
 expected `&mut core::option::Option<collections::string::String>`,
    found `core::option::Option<_>`
(expected &-ptr,
    found enum `core::option::Option`) [E0308]
<anon>:2     opt = None
                   ^~~~
<anon>:2:11: 2:15 help: see the detailed explanation for E0308
error: aborting due to previous error

It'd be great if this had a hint:

 Did you mean `*opt = None`

Someone on IRC just ran into this.

Activity

  1. pnkfelix commented on May 11, 2016

    @pnkfelix
    Contributor

    In the general case (i.e. for arbitrary expr: T and let mut opt: &mut T), can we know whether the intention was

    opt = &mut expr;

    or if it was

    *opt = expr;

    ?

    I would be a little cautious about unilaterally suggesting the latter when the former is potentially what was desired...

    • Update: after thinking on the matter more (see bottom of comment), I am less nervous about a unilateral suggestion.

    (However, when opt is declared as non-mut, I'm a little more amenable towards suggesting *opt = expr...)


    Update: of course in many cases suggesting opt = &mut expr; will be a bad idea. E.g. opt = &mut None; will almost certainly yield a "borrowed value does not live long enough" message. Maybe there's some smarter analysis of the expr that can figure out when to not bother suggesting the nonsensical one.

  2. durka commented on May 11, 2016

    @durka
    Contributor

    @pnkfelix if opt is mut you could suggest both, otherwise just the dereference.

  3. estebank commented on Sep 21, 2017

    @estebank
    Contributor

    The compiler now suggests:

    error[E0308]: mismatched types
     --> src/main.rs:2:11
      |
    2 |     opt = None
      |           ^^^^ expected mutable reference, found enum `std::option::Option`
      |
      = note: expected type `&mut std::option::Option<std::string::String>`
                 found type `std::option::Option<_>`
      = help: try with `&mut None`
    

    The suggestion is not smart enough to also suggest *opt = None.

  4. estebank commented on May 22, 2019

    @estebank
    Contributor

    Update:

    error[E0308]: mismatched types
     --> src/main.rs:2:11
      |
    2 |     opt = None
      |           ^^^^
      |           |
      |           expected mutable reference, found enum `std::option::Option`
      |           help: consider mutably borrowing here: `&mut None`
      |
      = note: expected type `&mut std::option::Option<std::string::String>`
                 found type `std::option::Option<_>`
    

    Compiler still not smart enough to suggest *opt = None.

  5. added a commit that references this issue on May 23, 2019
    b9459e7
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-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions