Skip to content

Commit 9a310ab

Browse files
committed
Remove impl trait names and move bits of await into a function
1 parent 0b2d2d1 commit 9a310ab

File tree

3 files changed

+28
-30
lines changed

3 files changed

+28
-30
lines changed

src/librustc/hir/lowering.rs

+7-17
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ impl<'a> LoweringContext<'a> {
877877

878878
let unstable_span = self.allow_internal_unstable(CompilerDesugaringKind::Async, span);
879879
let gen_future = self.expr_std_path(
880-
unstable_span, &["future", "future_from_generator"], None, ThinVec::new());
880+
unstable_span, &["future", "from_generator"], None, ThinVec::new());
881881
hir::ExprCall(P(gen_future), hir_vec![generator])
882882
}
883883

@@ -1173,11 +1173,8 @@ impl<'a> LoweringContext<'a> {
11731173
let span = t.span;
11741174
match itctx {
11751175
ImplTraitContext::Existential(fn_def_id) => {
1176-
// Set the name to `impl Bound1 + Bound2`
1177-
let exist_ty_name = Symbol::intern(&pprust::ty_to_string(t));
11781176
self.lower_existential_impl_trait(
1179-
span, fn_def_id, exist_ty_name,
1180-
|this| this.lower_param_bounds(bounds, itctx))
1177+
span, fn_def_id, |this| this.lower_param_bounds(bounds, itctx))
11811178
}
11821179
ImplTraitContext::Universal(def_id) => {
11831180
let def_node_id = self.next_id().node_id;
@@ -1245,7 +1242,6 @@ impl<'a> LoweringContext<'a> {
12451242
&mut self,
12461243
span: Span,
12471244
fn_def_id: DefId,
1248-
exist_ty_name: Name,
12491245
lower_bounds: impl FnOnce(&mut LoweringContext) -> hir::GenericBounds,
12501246
) -> hir::Ty_ {
12511247
// We need to manually repeat the code of `next_id` because the lowering
@@ -1307,7 +1303,7 @@ impl<'a> LoweringContext<'a> {
13071303
let exist_ty_item = hir::Item {
13081304
id: exist_ty_id.node_id,
13091305
hir_id: exist_ty_id.hir_id,
1310-
name: exist_ty_name,
1306+
name: keywords::Invalid.name(),
13111307
attrs: Default::default(),
13121308
node: exist_ty_item_kind,
13131309
vis: hir::Visibility::Inherited,
@@ -2090,19 +2086,13 @@ impl<'a> LoweringContext<'a> {
20902086
lifetime_collector.output_lifetime
20912087
};
20922088

2093-
let output_ty_name_owned;
2094-
let (output_ty_name, span) = match output {
2095-
FunctionRetTy::Ty(ty) => {
2096-
output_ty_name_owned = pprust::ty_to_string(ty);
2097-
(&*output_ty_name_owned, ty.span)
2098-
},
2099-
FunctionRetTy::Default(span) => ("()", *span),
2089+
let span = match output {
2090+
FunctionRetTy::Ty(ty) => ty.span,
2091+
FunctionRetTy::Default(span) => *span,
21002092
};
21012093

2102-
// FIXME(cramertj) add lifetimes (see FIXME below) to the name
2103-
let exist_ty_name = Symbol::intern(&format!("impl Future<Output = {}>", output_ty_name));
21042094
let impl_trait_ty = self.lower_existential_impl_trait(
2105-
span, fn_def_id, exist_ty_name, |this| {
2095+
span, fn_def_id, |this| {
21062096
let output_ty = match output {
21072097
FunctionRetTy::Ty(ty) =>
21082098
this.lower_ty(ty, ImplTraitContext::Existential(fn_def_id)),

src/libstd/future.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub use core::future::*;
2626
/// This function returns a `GenFuture` underneath, but hides it in `impl Trait` to give
2727
/// better error messages (`impl Future` rather than `GenFuture<[closure.....]>`).
2828
#[unstable(feature = "gen_future", issue = "50547")]
29-
pub fn future_from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
29+
pub fn from_generator<T: Generator<Yield = ()>>(x: T) -> impl Future<Output = T::Return> {
3030
GenFuture(x)
3131
}
3232

@@ -71,31 +71,32 @@ where
7171
F: FnOnce() -> R
7272
{
7373
let old_cx = TLS_CX.with(|tls_cx| {
74-
let old_cx = tls_cx.get();
75-
tls_cx.set(NonNull::new(
76-
cx as *mut task::Context as *mut () as *mut task::Context<'static>));
77-
old_cx
74+
tls_cx.replace(NonNull::new(
75+
cx
76+
as *mut task::Context
77+
as *mut ()
78+
as *mut task::Context<'static>
79+
))
7880
});
7981
let _reset_cx = SetOnDrop(old_cx);
80-
let res = f();
81-
res
82+
f()
8283
}
8384

8485
#[unstable(feature = "gen_future", issue = "50547")]
8586
/// Retrieves the thread-local task context used by async/await futures.
8687
///
88+
/// This function acquires exclusive access to the task context.
89+
///
8790
/// Panics if no task has been set or if the task context has already been
8891
/// retrived by a surrounding call to get_task_cx.
8992
pub fn get_task_cx<F, R>(f: F) -> R
9093
where
9194
F: FnOnce(&mut task::Context) -> R
9295
{
9396
let cx_ptr = TLS_CX.with(|tls_cx| {
94-
let cx_ptr = tls_cx.get();
9597
// Clear the entry so that nested `with_get_cx` calls
9698
// will fail or set their own value.
97-
tls_cx.set(None);
98-
cx_ptr
99+
tls_cx.replace(None)
99100
});
100101
let _reset_cx = SetOnDrop(cx_ptr);
101102

@@ -104,3 +105,12 @@ where
104105
Please file an issue on https://github.com/rust-lang/rust.");
105106
unsafe { f(cx_ptr.as_mut()) }
106107
}
108+
109+
#[unstable(feature = "gen_future", issue = "50547")]
110+
/// Polls a future in the current thread-local task context.
111+
pub fn poll_in_task_cx<F>(f: &mut PinMut<F>) -> Poll<F::Output>
112+
where
113+
F: Future
114+
{
115+
get_task_cx(|cx| f.reborrow().poll(cx))
116+
}

src/libstd/macros.rs

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

0 commit comments

Comments
 (0)