Remove impl trait names and move bits of await into a function

This commit is contained in:
Taylor Cramer 2018-06-22 16:08:07 -07:00
parent 0b2d2d1dc5
commit 9a310abf79
3 changed files with 28 additions and 30 deletions

View File

@ -877,7 +877,7 @@ impl<'a> LoweringContext<'a> {
let unstable_span = self.allow_internal_unstable(CompilerDesugaringKind::Async, span);
let gen_future = self.expr_std_path(
unstable_span, &["future", "future_from_generator"], None, ThinVec::new());
unstable_span, &["future", "from_generator"], None, ThinVec::new());
hir::ExprCall(P(gen_future), hir_vec![generator])
}
@ -1173,11 +1173,8 @@ impl<'a> LoweringContext<'a> {
let span = t.span;
match itctx {
ImplTraitContext::Existential(fn_def_id) => {
// Set the name to `impl Bound1 + Bound2`
let exist_ty_name = Symbol::intern(&pprust::ty_to_string(t));
self.lower_existential_impl_trait(
span, fn_def_id, exist_ty_name,
|this| this.lower_param_bounds(bounds, itctx))
span, fn_def_id, |this| this.lower_param_bounds(bounds, itctx))
}
ImplTraitContext::Universal(def_id) => {
let def_node_id = self.next_id().node_id;
@ -1245,7 +1242,6 @@ impl<'a> LoweringContext<'a> {
&mut self,
span: Span,
fn_def_id: DefId,
exist_ty_name: Name,
lower_bounds: impl FnOnce(&mut LoweringContext) -> hir::GenericBounds,
) -> hir::Ty_ {
// We need to manually repeat the code of `next_id` because the lowering
@ -1307,7 +1303,7 @@ impl<'a> LoweringContext<'a> {
let exist_ty_item = hir::Item {
id: exist_ty_id.node_id,
hir_id: exist_ty_id.hir_id,
name: exist_ty_name,
name: keywords::Invalid.name(),
attrs: Default::default(),
node: exist_ty_item_kind,
vis: hir::Visibility::Inherited,
@ -2090,19 +2086,13 @@ impl<'a> LoweringContext<'a> {
lifetime_collector.output_lifetime
};
let output_ty_name_owned;
let (output_ty_name, span) = match output {
FunctionRetTy::Ty(ty) => {
output_ty_name_owned = pprust::ty_to_string(ty);
(&*output_ty_name_owned, ty.span)
},
FunctionRetTy::Default(span) => ("()", *span),
let span = match output {
FunctionRetTy::Ty(ty) => ty.span,
FunctionRetTy::Default(span) => *span,
};
// FIXME(cramertj) add lifetimes (see FIXME below) to the name
let exist_ty_name = Symbol::intern(&format!("impl Future<Output = {}>", output_ty_name));
let impl_trait_ty = self.lower_existential_impl_trait(
span, fn_def_id, exist_ty_name, |this| {
span, fn_def_id, |this| {
let output_ty = match output {
FunctionRetTy::Ty(ty) =>
this.lower_ty(ty, ImplTraitContext::Existential(fn_def_id)),

View File

@ -26,7 +26,7 @@ pub use core::future::*;
/// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
/// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
#[unstable(feature = "gen_future", issue = "50547")]
pub fn future_from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
pub fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
GenFuture(x)
}
@ -71,19 +71,22 @@ where
F: FnOnce() -> R
{
let old_cx = TLS_CX.with(|tls_cx| {
let old_cx = tls_cx.get();
tls_cx.set(NonNull::new(
cx as *mut task::Context as *mut () as *mut task::Context<'static>));
old_cx
tls_cx.replace(NonNull::new(
cx
as *mut task::Context
as *mut ()
as *mut task::Context<'static>
))
});
let _reset_cx = SetOnDrop(old_cx);
let res = f();
res
f()
}
#[unstable(feature = "gen_future", issue = "50547")]
/// Retrieves the thread-local task context used by async/await futures.
///
/// This function acquires exclusive access to the task context.
///
/// Panics if no task has been set or if the task context has already been
/// retrived by a surrounding call to get_task_cx.
pub fn get_task_cx<F, R>(f: F) -> R
@ -91,11 +94,9 @@ where
F: FnOnce(&mut task::Context) -> R
{
let cx_ptr = TLS_CX.with(|tls_cx| {
let cx_ptr = tls_cx.get();
// Clear the entry so that nested `with_get_cx` calls
// will fail or set their own value.
tls_cx.set(None);
cx_ptr
tls_cx.replace(None)
});
let _reset_cx = SetOnDrop(cx_ptr);
@ -104,3 +105,12 @@ where
Please file an issue on https://github.com/rust-lang/rust.");
unsafe { f(cx_ptr.as_mut()) }
}
#[unstable(feature = "gen_future", issue = "50547")]
/// Polls a future in the current thread-local task context.
pub fn poll_in_task_cx<F>(f: &mut PinMut<F>) -> Poll<F::Output>
where
F: Future
{
get_task_cx(|cx| f.reborrow().poll(cx))
}

View File

@ -222,9 +222,7 @@ macro_rules! await {
let mut pinned = $e;
let mut pinned = unsafe { $crate::mem::PinMut::new_unchecked(&mut pinned) };
loop {
match $crate::future::get_task_cx(|cx|
$crate::future::Future::poll(pinned.reborrow(), cx))
{
match $crate::future::poll_in_task_cx(&mut pinned) {
// FIXME(cramertj) prior to stabilizing await, we have to ensure that this
// can't be used to create a generator on stable via `|| await!()`.
$crate::task::Poll::Pending => yield,