Skip to content

Call pinned method from Trait on generic parameter #63966

Closed

Description

@Jonathas-Conceicao

I've found a problem regarding Pin that I just can't solve and the current Nightly compiler gives a misleading suggestion.

This playground example serves well as a minimalist example. I can't manege to call quack on the Animal inside the Transporter even with the restriction to Animal: Quack.

And the Nightly compiler suggests:

   = help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `quack`, perhaps you need to restrict type parameter `Animal` with it:
   |
17 | impl<Animal: Quack + Quack> Quack for Transporter<Animal> {
   |      ^^^^^^^^^^^^^^^

Which clearly doesn't really solve anything.

If a take off the Pin from the Traits and it's impl and just use a &self I can get the expected Quack! from inside the transporter.

The compiler suggestion surely doesn't seams right to me, but what about the Pin? Am I missing something or is there a bug too?

Activity

  1. memoryruins commented on Aug 28, 2019

    @memoryruins
    Contributor

    The suggestions are strange with other self types as well, e.g. using Box and Arc instead of Pin.

    playground 1.39.0-nightly 2019-08-23 9eae1fc0ea9b00341b8f

    mod First {
        trait Foo { fn m(self: Box<Self>); }
        fn foo<T: Foo>(a: T) {
            a.m();
        }
    }
    mod Second {
        trait Bar { fn m(self: std::sync::Arc<Self>); }
        fn bar(b: impl Bar) {
            b.m();
        }
    }
    help: the following traits define an item `m`, perhaps you need to restrict type parameter `T` with one of them:
      |
    3 |     fn foo<T: First::Foo + Foo>(a: T) {
      |            ^^^^^^^^^^^^^^^
    3 |     fn foo<T: Second::Bar + Foo>(a: T) {
      |            ^^^^^^^^^^^^^^^^
    

    The above suggests a trait from another module with a different name

    help: the following traits define an item `m`, perhaps you need to restrict type parameter `impl Bar` with one of them:
       |
    9  |     fn bar(b: impl Bar: First::Foo + {
       |               ^^^^^^^^^^^^^^^^^^^^^^
    9  |     fn bar(b: impl Bar: Second::Bar + {
       |               ^^^^^^^^^^^^^^^^^^^^^^^
    

    and the next suggestion isn't syntactically correct.

  2. added
    A-diagnosticsArea: Messages for errors, warnings, and lints
    A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`
    C-bugCategory: This is a bug.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Aug 28, 2019
  3. Aaron1011 commented on Aug 29, 2019

    @Aaron1011
    Contributor

    @Jonathas-Conceicao: as @memoryruins mentioned, the diagnostic suggestions are wrong in this case.

    You're trying to go from a Pin<&MyType> to a Pin<&MyField>, where &MyField is a reference to a field of MyType. This is known as a pinned projection, and has additional requirements that you must meet in order for it to be sound.

    <shameless-plug>You might want to take a look at the pin-project crate (disclaiminer: I'm a contributor), which makes it easy to write safe pin projections.</shameless-plug>

  4. taiki-e commented on Aug 29, 2019

    @taiki-e
    Member

    The diagnostic issue seems to dup of #57994.

  5. Jonathas-Conceicao commented on Aug 29, 2019

    @Jonathas-Conceicao
    Author

    @Aaron1011, thank you for the explanation and the suggestion, that does indeed solve the error.

    Regarding the suggestion, it really does seams to be a duplicate, will be closing this in favor of #57994.

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 lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`C-bugCategory: This is a bug.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions