Skip to content

Call pinned method from Trait on generic parameter #63966

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Jonathas-Conceicao opened this issue Aug 28, 2019 · 4 comments
Closed

Call pinned method from Trait on generic parameter #63966

Jonathas-Conceicao opened this issue Aug 28, 2019 · 4 comments
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@Jonathas-Conceicao
Copy link

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?

@memoryruins
Copy link
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.

@jonas-schievink jonas-schievink added A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Aug 28, 2019
@Aaron1011
Copy link
Member

Aaron1011 commented Aug 29, 2019

@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>

@taiki-e
Copy link
Member

taiki-e commented Aug 29, 2019

The diagnostic issue seems to dup of #57994.

@Jonathas-Conceicao
Copy link
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
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

5 participants