Skip to content

Commit acb6f17

Browse files
committed
Use IntoAsyncIterator in for await loop desugaring
1 parent 8e34391 commit acb6f17

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

compiler/rustc_ast_lowering/src/expr.rs

+19-1
Original file line numberDiff line numberDiff line change
@@ -1803,7 +1803,25 @@ impl<'hir> LoweringContext<'_, 'hir> {
18031803
arena_vec![self; head],
18041804
)
18051805
}
1806-
ForLoopKind::ForAwait => self.arena.alloc(head),
1806+
// ` unsafe { Pin::new_unchecked(&mut into_async_iter(<head>)) }`
1807+
ForLoopKind::ForAwait => {
1808+
// `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
1809+
let iter = self.expr_call_lang_item_fn(
1810+
head_span,
1811+
hir::LangItem::IntoAsyncIterIntoIter,
1812+
arena_vec![self; head],
1813+
);
1814+
let iter = self.expr_mut_addr_of(head_span, iter);
1815+
// `Pin::new_unchecked(...)`
1816+
let iter = self.arena.alloc(self.expr_call_lang_item_fn_mut(
1817+
head_span,
1818+
hir::LangItem::PinNewUnchecked,
1819+
arena_vec![self; iter],
1820+
));
1821+
// `unsafe { ... }`
1822+
let iter = self.arena.alloc(self.expr_unsafe(iter));
1823+
iter
1824+
}
18071825
};
18081826

18091827
let match_expr = self.arena.alloc(self.expr_match(

compiler/rustc_hir/src/lang_items.rs

+1
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ language_item_table! {
308308
FuturePoll, sym::poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
309309

310310
AsyncIteratorPollNext, sym::async_iterator_poll_next, async_iterator_poll_next, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
311+
IntoAsyncIterIntoIter, sym::into_async_iter_into_iter, into_async_iter_into_iter, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
311312

312313
Option, sym::Option, option_type, Target::Enum, GenericRequirement::None;
313314
OptionSome, sym::Some, option_some_variant, Target::Variant, GenericRequirement::None;

library/core/src/async_iter/async_iter.rs

+1
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ pub trait IntoAsyncIterator {
145145
type IntoAsyncIter: AsyncIterator<Item = Self::Item>;
146146

147147
/// Converts `self` into an async iterator
148+
#[cfg_attr(not(bootstrap), lang = "into_async_iter_into_iter")]
148149
fn into_async_iter(self) -> Self::IntoAsyncIter;
149150
}
150151

0 commit comments

Comments
 (0)