diff options
| author | Mazdak Farrokhzad <twingoow@gmail.com> | 2019-12-15 05:57:25 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2019-12-15 05:57:25 +0100 |
| commit | 8e2689c42ef3dad7d91427340b2e698139aaa197 (patch) | |
| tree | a1b5b929fa5b668f0eba976ea8bbccb1632ae1ea | |
| parent | 541dc62d64345bb64e16ade2d0e485144b664dfd (diff) | |
| parent | e08944fdafac547aecc1a94e44cc978a202eec86 (diff) | |
| download | rust-8e2689c42ef3dad7d91427340b2e698139aaa197.tar.gz rust-8e2689c42ef3dad7d91427340b2e698139aaa197.zip | |
Rollup merge of #67289 - estebank:unnamed-closure, r=Centril
Do not ICE on unnamed future Fix #67252.
| -rw-r--r-- | src/librustc/hir/map/mod.rs | 13 | ||||
| -rw-r--r-- | src/librustc/traits/error_reporting.rs | 2 | ||||
| -rw-r--r-- | src/test/ui/async-await/issue-67252-unnamed-future.rs | 24 | ||||
| -rw-r--r-- | src/test/ui/async-await/issue-67252-unnamed-future.stderr | 22 |
4 files changed, 57 insertions, 4 deletions
diff --git a/src/librustc/hir/map/mod.rs b/src/librustc/hir/map/mod.rs index 5bf5a93ad01..69e772697f8 100644 --- a/src/librustc/hir/map/mod.rs +++ b/src/librustc/hir/map/mod.rs @@ -1016,8 +1016,8 @@ impl<'hir> Map<'hir> { } } - pub fn name(&self, id: HirId) -> Name { - match self.get(id) { + pub fn opt_name(&self, id: HirId) -> Option<Name> { + Some(match self.get(id) { Node::Item(i) => i.ident.name, Node::ForeignItem(fi) => fi.ident.name, Node::ImplItem(ii) => ii.ident.name, @@ -1028,7 +1028,14 @@ impl<'hir> Map<'hir> { Node::GenericParam(param) => param.name.ident().name, Node::Binding(&Pat { kind: PatKind::Binding(_, _, l, _), .. }) => l.name, Node::Ctor(..) => self.name(self.get_parent_item(id)), - _ => bug!("no name for {}", self.node_to_string(id)) + _ => return None, + }) + } + + pub fn name(&self, id: HirId) -> Name { + match self.opt_name(id) { + Some(name) => name, + None => bug!("no name for {}", self.node_to_string(id)), } } diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index da36b31038d..701c19085be 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -2404,7 +2404,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> { let message = if let Some(name) = last_generator .and_then(|generator_did| self.tcx.parent(generator_did)) .and_then(|parent_did| self.tcx.hir().as_local_hir_id(parent_did)) - .map(|parent_hir_id| self.tcx.hir().name(parent_hir_id)) + .and_then(|parent_hir_id| self.tcx.hir().opt_name(parent_hir_id)) { format!("future returned by `{}` is not {}", name, trait_name) } else { diff --git a/src/test/ui/async-await/issue-67252-unnamed-future.rs b/src/test/ui/async-await/issue-67252-unnamed-future.rs new file mode 100644 index 00000000000..1a7ff613341 --- /dev/null +++ b/src/test/ui/async-await/issue-67252-unnamed-future.rs @@ -0,0 +1,24 @@ +// edition:2018 +use std::future::Future; +use std::pin::Pin; +use std::task::{Context, Poll}; + +fn spawn<T: Send>(_: T) {} + +pub struct AFuture; +impl Future for AFuture{ + type Output = (); + + fn poll(mut self: Pin<&mut Self>, _: &mut Context<'_>) -> Poll<()> { + unimplemented!() + } +} + +async fn foo() { + spawn(async { //~ ERROR future cannot be sent between threads safely + let _a = std::ptr::null_mut::<()>(); // `*mut ()` is not `Send` + AFuture.await; + }); +} + +fn main() {} diff --git a/src/test/ui/async-await/issue-67252-unnamed-future.stderr b/src/test/ui/async-await/issue-67252-unnamed-future.stderr new file mode 100644 index 00000000000..24aedeb9659 --- /dev/null +++ b/src/test/ui/async-await/issue-67252-unnamed-future.stderr @@ -0,0 +1,22 @@ +error: future cannot be sent between threads safely + --> $DIR/issue-67252-unnamed-future.rs:18:5 + | +LL | fn spawn<T: Send>(_: T) {} + | ----- ---- required by this bound in `spawn` +... +LL | spawn(async { + | ^^^^^ future is not `Send` + | + = help: within `impl std::future::Future`, the trait `std::marker::Send` is not implemented for `*mut ()` +note: future is not `Send` as this value is used across an await + --> $DIR/issue-67252-unnamed-future.rs:20:9 + | +LL | let _a = std::ptr::null_mut::<()>(); // `*mut ()` is not `Send` + | -- has type `*mut ()` +LL | AFuture.await; + | ^^^^^^^^^^^^^ await occurs here, with `_a` maybe used later +LL | }); + | - `_a` is later dropped here + +error: aborting due to previous error + |
