Use IntoAsyncIterator in for await loop desugaring

This commit is contained in:
Eric Holk 2023-12-18 17:08:19 -08:00
parent 8e34391d6a
commit acb6f17adf
No known key found for this signature in database
GPG Key ID: 8EA6B43ED4CE0911
3 changed files with 21 additions and 1 deletions

View File

@ -1803,7 +1803,25 @@ fn lower_expr_for(
arena_vec![self; head], arena_vec![self; head],
) )
} }
ForLoopKind::ForAwait => self.arena.alloc(head), // ` unsafe { Pin::new_unchecked(&mut into_async_iter(<head>)) }`
ForLoopKind::ForAwait => {
// `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
let iter = self.expr_call_lang_item_fn(
head_span,
hir::LangItem::IntoAsyncIterIntoIter,
arena_vec![self; head],
);
let iter = self.expr_mut_addr_of(head_span, iter);
// `Pin::new_unchecked(...)`
let iter = self.arena.alloc(self.expr_call_lang_item_fn_mut(
head_span,
hir::LangItem::PinNewUnchecked,
arena_vec![self; iter],
));
// `unsafe { ... }`
let iter = self.arena.alloc(self.expr_unsafe(iter));
iter
}
}; };
let match_expr = self.arena.alloc(self.expr_match( let match_expr = self.arena.alloc(self.expr_match(

View File

@ -308,6 +308,7 @@ pub fn extract(attrs: &[ast::Attribute]) -> Option<(Symbol, Span)> {
FuturePoll, sym::poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None; FuturePoll, sym::poll, future_poll_fn, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::None;
AsyncIteratorPollNext, sym::async_iterator_poll_next, async_iterator_poll_next, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0); AsyncIteratorPollNext, sym::async_iterator_poll_next, async_iterator_poll_next, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
IntoAsyncIterIntoIter, sym::into_async_iter_into_iter, into_async_iter_into_iter, Target::Method(MethodKind::Trait { body: false }), GenericRequirement::Exact(0);
Option, sym::Option, option_type, Target::Enum, GenericRequirement::None; Option, sym::Option, option_type, Target::Enum, GenericRequirement::None;
OptionSome, sym::Some, option_some_variant, Target::Variant, GenericRequirement::None; OptionSome, sym::Some, option_some_variant, Target::Variant, GenericRequirement::None;

View File

@ -145,6 +145,7 @@ pub trait IntoAsyncIterator {
type IntoAsyncIter: AsyncIterator<Item = Self::Item>; type IntoAsyncIter: AsyncIterator<Item = Self::Item>;
/// Converts `self` into an async iterator /// Converts `self` into an async iterator
#[cfg_attr(not(bootstrap), lang = "into_async_iter_into_iter")]
fn into_async_iter(self) -> Self::IntoAsyncIter; fn into_async_iter(self) -> Self::IntoAsyncIter;
} }