Lokasi ngalangkungan proxy:   [ UP ]  
[Ngawartoskeun bug]   [Panyetelan cookie]                
Skip to content

Suggest as_mut to reborrow Pin when calling method #65409

Closed

Description

@Nemo157

I have seen a lot of newcomers to futures stumble with Pin<&mut Self> methods consuming the pin, if possible this seems like a good candidate for a custom diagnostic.

As a self-contained example (playground):

use std::pin::Pin;

struct Foo;

impl Foo {
    fn foo(self: Pin<&mut Self>) {}
}

fn main() {
    let mut foo = Foo;
    let foo = Pin::new(&mut foo);
    foo.foo();
    foo.foo();
}

currently errors with

   Compiling playground v0.0.1 (/playground)
error[E0382]: use of moved value: `foo`
  --> src/main.rs:13:5
   |
11 |     let foo = Pin::new(&mut foo);
   |         --- move occurs because `foo` has type `std::pin::Pin<&mut Foo>`, which does not implement the `Copy` trait
12 |     foo.foo();
   |     --- value moved here
13 |     foo.foo();
   |     ^^^ value used here after move

error: aborting due to previous error

For more information about this error, try `rustc --explain E0382`.
error: Could not compile `playground`.

To learn more, run the command again with --verbose.

it would be useful if this error included something like

hint: use `foo.as_mut().foo()` to reborrow the `Pin` instead of moving it

Activity

  1. added
    A-diagnostics
    A-suggestion-diagnostics
    D-newcomer-roadblock
    A-borrow-checker
    T-compiler
    C-enhancement
    on Oct 14, 2019
  2. Centril commented on Oct 14, 2019

    @Centril
    Contributor
  3. nikomatsakis commented on Oct 18, 2019

    @nikomatsakis
    Contributor

    Seems reasonable to me. Should we tag this A-async-await? Seems to fall under the "async foundations" purview, anyway. (Slight mismatch between those labels, I guess)

  4. added
    A-async-await
    on Oct 18, 2019
  5. Centril commented on Oct 18, 2019

    @Centril
    Contributor

    @nikomatsakis Yes, let's. Any thoughts re. implementation? ;)

  6. added
    AsyncAwait-Triaged
    on Oct 22, 2019
  7. gilescope commented on Oct 30, 2019

    @gilescope
    Contributor

    Happy to give this one a go - the hint would certainly help me if I stumbled into this.
    @rustbot claim

  8. gilescope commented on Nov 5, 2019

    @gilescope
    Contributor

    Rather than being specific to Pin, can we expand the scope and add a hint to the general error message:

          hint: consider [mutably] borrowing '&[mut ]foo' [or foo.clone()] here?
                                                            ^ only suggest if T:Clone
    

    and if possible specialise the error message if the type supports as_ref()/as_mut()?

    As a user, that's the kind of error message I'd be greatful to receive - a hint as to whether the type is clonable or not and if there's a as_mut() call I could be using rather than trying to &mut it.

  9. Nemo157 commented on Nov 5, 2019

    @Nemo157
    ContributorAuthor

    For normal &self/&mut self methods the compiler will implicitly reborrow. If there’s a generalisation here that could help suggest cloning that sounds good to me (small playground to check the current error message).

  10. dtolnay commented on Jan 27, 2022

    @dtolnay
    Member

    @rustbot release-assignment

  11. estebank commented on Dec 23, 2022

    @estebank
    Contributor

    Addressed the Pin<&mut T> case in #106095.

    For the general case of suggesting .clone() can't be accomplished at this time because by the time borrowck comes around I can no longer use the inference context to evaluate T: Clone for an accurate suggestion.

    Edit: Figured out a hack to suggest clone. That PR will close this ticket.

  12. added a commit that references this issue on Jan 6, 2023
    9f6039a
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-async-awaitA-borrow-checkerA-diagnosticsA-suggestion-diagnosticsAsyncAwait-TriagedC-enhancementD-newcomer-roadblockT-compiler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions