You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Unhelpful error message when using bad receiver type #57994
use std::pin::Pin;traitTest{fntest(self:Pin<&mutSelf>);}fnfoo(t:implTest){
t.test()}
The error here is that test can only be called on Pin<&mut Self>, so t.test does not work. But the error message is less than helpful:
error[E0599]: no method named `test` found for type `impl Test` in the current scope
--> src/lib.rs:8:7
|
8 | t.test()
| ^^^^
|
= help: items from traits can only be used if the trait is implemented and in scope
= note: the following trait defines an item `test`, perhaps you need to implement it:
candidate #1: `Test`
It suggests I import a trait that is already in scope. This leads down the totally wrong path -- not showing the "help" and "note" at all would be better than this. Ideally it could point out the receiver type mismatch.
Reacted by Yosh, Gregory, Lofty, arthur, Guillaume Depardon, Taiki Endo, Jonathas Conceição, jaen, Squirrel, Jeb Rosen and 5 more
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.
error[E0599]: no method named `m` found for type `T` in the current scope
--> src/lib.rs:4:11
|
4 | a.m();
| ^ method not found in `T`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
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) {
| ^^^^^^^^^^^^^^^^
error[E0599]: no method named `m` found for type `impl Bar` in the current scope
--> src/lib.rs:10:11
|
10 | b.m();
| ^ method not found in `impl Bar`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
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) {
| ^^^^^^^^^^^^^^^^^^^^^^
error[E0599]: no method named `test` found for type `impl Test` in the current scope
--> src/lib.rs:8:7
|
8 | t.test()
| ^^^^ method not found in `impl Test`
|
= help: items from traits can only be used if the type parameter is bounded by the trait
help: the following trait defines an item `test`, perhaps you need to restrict type parameter `impl Test` with it:
|
7 | fn foo(t: impl Test + Test) {
| ^^^^^^^^^^^^^^^^
This error message comes up a lot when users attempt to manually implement Future or the associated traits like futures::io::{AsyncRead, AsyncWrite} by delegating to a field without pinning it.
error[E0599]: no method named `test` found for type parameter `impl Test` in the current scope
--> file5.rs:8:7
|
4 | fn test(self: Pin<&mut Self>);
| -------------- the method might not be found because of this arbitrary self type
...
8 | t.test()
| ^^^^ method not found in `impl Test`
That wording does indicates the actual problem (hooray!), but it also doesn't feel very newcomer-friendly to me because of the phrase "arbitrary self type". IMO something like "test() is called on an X but must be called on a Pin<&mut X>" would be more obvious, but I can see that getting difficult to implement in the error messages.
Only outstanding issue is for the poll case, and I'm not quite sure how we can help the user there (hell, I don't know how to make that code compile myself). @SNCPlay42, what would have been the right code the user should have written for #57994 (comment)?
@RalfJung I noticed that we already provide an appropriate note on the E0277 to use the pin! macro, but it isn't as prominent as I'd like:
error[E0277]: `[async fn body@src/lib.rs:3:14: 5:2]` cannot be unpinned
--> src/lib.rs:8:24
|
3 | async fn f() {
| - within this `impl Future<Output = ()>`
...
8 | std::pin::Pin::new(&mut f()).poll(cx);
| ------------------ ^^^^^^^^ within `impl Future<Output = ()>`, the trait `Unpin` is not implemented for `[async fn body@src/lib.rs:3:14: 5:2]`
| |
| required by a bound introduced by this call
|
= note: consider using the `pin!` macro
consider using `Box::pin` if you need to access the pinned value outside of the current scope
note: required because it appears within the type `impl Future<Output = ()>`
--> src/lib.rs:3:14
|
3 | async fn f() {
| ^
note: required by a bound in `Pin::<P>::new`
--> /rustc/f88a8b71cebb730cbd5058c45ebcae1d4d9be377/library/core/src/pin.rs:503:5
error[E0599]: no method named `poll` found for opaque type `impl Future<Output = ()>` in the current scope
--> f63.rs:4:9
|
4 | f().poll(cx);
| ^^^^ method not found in `impl Future<Output = ()>`
|
= help: method `poll` found on `Pin<&mut impl Future<Output = ()>>`, see documentation for `std::pin::Pin`
= help: self type must be pinned to call `Future::poll`, see https://rust-lang.github.io/async-book/04_pinning/01_chapter.html#pinning-in-practice
help: consider pinning the expression
|
4 ~ let mut pinned = std::pin::pin!(f());
5 ~ pinned.as_mut().poll(cx);
|
error[E0599]: no method named `x` found for struct `Pin<&S>` in the current scope
--> $DIR/arbitrary_self_type_mut_difference.rs:11:18
|
LL | Pin::new(&S).x();
| ^ help: there is a method with a similar name: `y`
|
note: method is available for `Pin<&mut S>`
--> $DIR/arbitrary_self_type_mut_difference.rs:6:5
|
LL | fn x(self: Pin<&mut Self>) {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^
Activity
memoryruins commentedon Aug 29, 2019
Re-posting from #63966 since it was closed in favor of this thread.
playground
1.39.0-nightly 2019-08-23 9eae1fc0ea9b00341b8fThe above suggests a trait from another module with a different name
and the next suggestion isn't syntactically correct.
estebank commentedon Sep 26, 2019
Current output:
Nemo157 commentedon Oct 15, 2019
@rustbot modify labels to +AsyncAwait-OnDeck
This error message comes up a lot when users attempt to manually implement
Futureor the associated traits likefutures::io::{AsyncRead, AsyncWrite}by delegating to a field without pinning it.estebank commentedon Feb 19, 2020
Triage: we no longer emit the incorrect suggestions.
estebank commentedon Feb 19, 2020
Is the following output acceptable?
jebrosen commentedon Feb 20, 2020
That wording does indicates the actual problem (hooray!), but it also doesn't feel very newcomer-friendly to me because of the phrase "arbitrary self type". IMO something like "
test()is called on anXbut must be called on aPin<&mut X>" would be more obvious, but I can see that getting difficult to implement in the error messages.13 remaining items
estebank commentedon Aug 4, 2023
Only outstanding issue is for the
pollcase, and I'm not quite sure how we can help the user there (hell, I don't know how to make that code compile myself). @SNCPlay42, what would have been the right code the user should have written for #57994 (comment)?RalfJung commentedon Aug 4, 2023
The code it currently suggests does not even build, so that should definitely be fixed.
Here's how
pollcan be called:estebank commentedon Aug 9, 2023
@RalfJung I noticed that we already provide an appropriate note on the E0277 to use the
pin!macro, but it isn't as prominent as I'd like:Having said that, between #114654:
and #114469:
I think we can consider this ticket addressed.
Rollup merge of rust-lang#114469 - estebank:arbitrary-self-types-mut-…
Suggest `pin!()` instead of `Pin::new()` when appropriate
Rollup merge of rust-lang#114654 - estebank:suggest-pin-macro, r=davi…
Unrolled build for rust-lang#114654