From aaaffa9a3ee4851422ff435bf6b24dd2e55da171 Mon Sep 17 00:00:00 2001 From: Ezra Shaw Date: Sat, 4 Mar 2023 21:08:15 +1300 Subject: [PATCH] feat: impl better help for `.poll()` not found on `impl Future` --- compiler/rustc_hir_typeck/src/method/suggest.rs | 10 ++++++++++ tests/ui/async-await/issue-108572.rs | 12 ++++++++++++ tests/ui/async-await/issue-108572.stderr | 12 ++++++++++++ 3 files changed, 34 insertions(+) create mode 100644 tests/ui/async-await/issue-108572.rs create mode 100644 tests/ui/async-await/issue-108572.stderr diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index 60d56263d2e..5d09915c749 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -348,6 +348,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { err.downgrade_to_delayed_bug(); } + if tcx.ty_is_opaque_future(rcvr_ty) && item_name.name == sym::poll { + err.help(&format!( + "method `poll` found on `Pin<&mut {ty_str}>`, \ + see documentation for `std::pin::Pin`" + )); + err.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" + ); + } + if let Mode::MethodCall = mode && let SelfSource::MethodCall(cal) = source { self.suggest_await_before_method( &mut err, item_name, rcvr_ty, cal, span, expected.only_has_type(self), diff --git a/tests/ui/async-await/issue-108572.rs b/tests/ui/async-await/issue-108572.rs new file mode 100644 index 00000000000..efcb8b8ebab --- /dev/null +++ b/tests/ui/async-await/issue-108572.rs @@ -0,0 +1,12 @@ +// edition: 2021 + +use std::future::Future; +fn foo() -> impl Future { + async { } +} + +fn main() { + let fut = foo(); + fut.poll(); + //~^ ERROR no method named `poll` found for opaque type `impl Future` in the current scope [E0599] +} diff --git a/tests/ui/async-await/issue-108572.stderr b/tests/ui/async-await/issue-108572.stderr new file mode 100644 index 00000000000..0dbcf4d660a --- /dev/null +++ b/tests/ui/async-await/issue-108572.stderr @@ -0,0 +1,12 @@ +error[E0599]: no method named `poll` found for opaque type `impl Future` in the current scope + --> $DIR/issue-108572.rs:10:9 + | +LL | fut.poll(); + | ^^^^ method not found in `impl Future` + | + = help: method `poll` found on `Pin<&mut impl Future>`, 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 + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0599`.