@@ -1716,24 +1716,28 @@ impl<'hir> LoweringContext<'_, 'hir> {
17161716 // `mut iter => { ... }`
17171717 let iter_arm = self . arm ( iter_pat, loop_expr) ;
17181718
1719- let into_iter_expr = match loop_kind {
1719+ let match_expr = match loop_kind {
17201720 ForLoopKind :: For => {
17211721 // `::std::iter::IntoIterator::into_iter(<head>)`
1722- self . expr_call_lang_item_fn (
1722+ let into_iter_expr = self . expr_call_lang_item_fn (
17231723 head_span,
17241724 hir:: LangItem :: IntoIterIntoIter ,
17251725 arena_vec ! [ self ; head] ,
1726- )
1726+ ) ;
1727+
1728+ self . arena . alloc ( self . expr_match (
1729+ for_span,
1730+ into_iter_expr,
1731+ arena_vec ! [ self ; iter_arm] ,
1732+ hir:: MatchSource :: ForLoopDesugar ,
1733+ ) )
17271734 }
1728- // ` unsafe { Pin::new_unchecked(&mut into_async_iter(<head>)) }`
1735+ // `match into_async_iter(<head>) { ref mut iter => match unsafe { Pin::new_unchecked(iter) } { ... } }`
17291736 ForLoopKind :: ForAwait => {
1730- // `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
1731- let iter = self . expr_call_lang_item_fn (
1732- head_span,
1733- hir:: LangItem :: IntoAsyncIterIntoIter ,
1734- arena_vec ! [ self ; head] ,
1735- ) ;
1736- let iter = self . expr_mut_addr_of ( head_span, iter) ;
1737+ let iter_ident = iter;
1738+ let ( async_iter_pat, async_iter_pat_id) =
1739+ self . pat_ident_binding_mode ( head_span, iter_ident, hir:: BindingMode :: REF_MUT ) ;
1740+ let iter = self . expr_ident_mut ( head_span, iter_ident, async_iter_pat_id) ;
17371741 // `Pin::new_unchecked(...)`
17381742 let iter = self . arena . alloc ( self . expr_call_lang_item_fn_mut (
17391743 head_span,
@@ -1742,17 +1746,29 @@ impl<'hir> LoweringContext<'_, 'hir> {
17421746 ) ) ;
17431747 // `unsafe { ... }`
17441748 let iter = self . arena . alloc ( self . expr_unsafe ( iter) ) ;
1745- iter
1749+ let inner_match_expr = self . arena . alloc ( self . expr_match (
1750+ for_span,
1751+ iter,
1752+ arena_vec ! [ self ; iter_arm] ,
1753+ hir:: MatchSource :: ForLoopDesugar ,
1754+ ) ) ;
1755+
1756+ // `::core::async_iter::IntoAsyncIterator::into_async_iter(<head>)`
1757+ let iter = self . expr_call_lang_item_fn (
1758+ head_span,
1759+ hir:: LangItem :: IntoAsyncIterIntoIter ,
1760+ arena_vec ! [ self ; head] ,
1761+ ) ;
1762+ let iter_arm = self . arm ( async_iter_pat, inner_match_expr) ;
1763+ self . arena . alloc ( self . expr_match (
1764+ for_span,
1765+ iter,
1766+ arena_vec ! [ self ; iter_arm] ,
1767+ hir:: MatchSource :: ForLoopDesugar ,
1768+ ) )
17461769 }
17471770 } ;
17481771
1749- let match_expr = self . arena . alloc ( self . expr_match (
1750- for_span,
1751- into_iter_expr,
1752- arena_vec ! [ self ; iter_arm] ,
1753- hir:: MatchSource :: ForLoopDesugar ,
1754- ) ) ;
1755-
17561772 // This is effectively `{ let _result = ...; _result }`.
17571773 // The construct was introduced in #21984 and is necessary to make sure that
17581774 // temporaries in the `head` expression are dropped and do not leak to the
0 commit comments