@@ -37,6 +37,7 @@ use rustc_hir::LangItem;
3737use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , DocLinkResMap , LifetimeRes , Res } ;
3838use rustc_hir:: def_id:: { CrateNum , DefId , DefIdMap , LocalDefId , LocalDefIdMap } ;
3939use rustc_index:: IndexVec ;
40+ use rustc_index:: bit_set:: BitMatrix ;
4041use rustc_macros:: {
4142 Decodable , Encodable , HashStable , TyDecodable , TyEncodable , TypeFoldable , TypeVisitable ,
4243 extension,
@@ -100,7 +101,7 @@ pub use self::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeV
100101use crate :: error:: { OpaqueHiddenTypeMismatch , TypeMismatchReason } ;
101102use crate :: metadata:: ModChild ;
102103use crate :: middle:: privacy:: EffectiveVisibilities ;
103- use crate :: mir:: { Body , CoroutineLayout } ;
104+ use crate :: mir:: { Body , CoroutineLayout , CoroutineSavedLocal , CoroutineSavedTy , SourceInfo } ;
104105use crate :: query:: { IntoQueryParam , Providers } ;
105106use crate :: ty;
106107pub use crate :: ty:: diagnostics:: * ;
@@ -1740,9 +1741,8 @@ impl<'tcx> TyCtxt<'tcx> {
17401741 | ty:: InstanceKind :: CloneShim ( ..)
17411742 | ty:: InstanceKind :: ThreadLocalShim ( ..)
17421743 | ty:: InstanceKind :: FnPtrAddrShim ( ..)
1743- | ty:: InstanceKind :: AsyncDropGlueCtorShim ( ..) => self . mir_shims ( instance) ,
1744- // async drop glue should be processed specifically, as a templated coroutine
1745- ty:: InstanceKind :: AsyncDropGlue ( _, _ty) => todo ! ( ) ,
1744+ | ty:: InstanceKind :: AsyncDropGlueCtorShim ( ..)
1745+ | ty:: InstanceKind :: AsyncDropGlue ( ..) => self . mir_shims ( instance) ,
17461746 }
17471747 }
17481748
@@ -1853,16 +1853,17 @@ impl<'tcx> TyCtxt<'tcx> {
18531853 self . def_kind ( trait_def_id) == DefKind :: TraitAlias
18541854 }
18551855
1856- /// Returns layout of a coroutine. Layout might be unavailable if the
1856+ /// Returns layout of a non-templated coroutine. Layout might be unavailable if the
18571857 /// coroutine is tainted by errors.
18581858 ///
18591859 /// Takes `coroutine_kind` which can be acquired from the `CoroutineArgs::kind_ty`,
18601860 /// e.g. `args.as_coroutine().kind_ty()`.
1861- pub fn coroutine_layout (
1861+ pub fn ordinary_coroutine_layout (
18621862 self ,
18631863 def_id : DefId ,
18641864 coroutine_kind_ty : Ty < ' tcx > ,
18651865 ) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1866+ debug_assert_ne ! ( Some ( def_id) , self . lang_items( ) . async_drop_in_place_poll_fn( ) ) ;
18661867 let mir = self . optimized_mir ( def_id) ;
18671868 // Regular coroutine
18681869 if coroutine_kind_ty. is_unit ( ) {
@@ -1892,6 +1893,68 @@ impl<'tcx> TyCtxt<'tcx> {
18921893 }
18931894 }
18941895
1896+ /// Returns layout of a templated coroutine. Layout might be unavailable if the
1897+ /// coroutine is tainted by errors. Atm, the only templated coroutine is
1898+ /// `async_drop_in_place<T>::{closure}` returned from `async fn async_drop_in_place<T>(..)`.
1899+ pub fn templated_coroutine_layout ( self , ty : Ty < ' tcx > ) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1900+ self . lang_items ( ) . async_drop_in_place_poll_fn ( ) . and_then ( |def_id| {
1901+ self . mir_shims ( InstanceKind :: AsyncDropGlue ( def_id, ty) ) . coroutine_layout_raw ( )
1902+ } )
1903+ }
1904+
1905+ /// Returns layout of a templated (or not) coroutine. Layout might be unavailable if the
1906+ /// coroutine is tainted by errors.
1907+ pub fn coroutine_layout (
1908+ self ,
1909+ def_id : DefId ,
1910+ args : GenericArgsRef < ' tcx > ,
1911+ ) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1912+ if Some ( def_id) == self . lang_items ( ) . async_drop_in_place_poll_fn ( ) {
1913+ fn find_impl_coroutine < ' tcx > ( tcx : TyCtxt < ' tcx > , mut cor_ty : Ty < ' tcx > ) -> Ty < ' tcx > {
1914+ let mut ty = cor_ty;
1915+ loop {
1916+ if let ty:: Coroutine ( def_id, args) = ty. kind ( ) {
1917+ cor_ty = ty;
1918+ if tcx. is_templated_coroutine ( * def_id) {
1919+ ty = args. first ( ) . unwrap ( ) . expect_ty ( ) ;
1920+ continue ;
1921+ } else {
1922+ return cor_ty;
1923+ }
1924+ } else {
1925+ return cor_ty;
1926+ }
1927+ }
1928+ }
1929+ // layout of `async_drop_in_place<T>::{closure}` in case,
1930+ // when T is a coroutine, contains this internal coroutine's ref
1931+ let arg_cor_ty = args. first ( ) . unwrap ( ) . expect_ty ( ) ;
1932+ if arg_cor_ty. is_coroutine ( ) {
1933+ let impl_cor_ty = find_impl_coroutine ( self , arg_cor_ty) ;
1934+ let impl_ref = Ty :: new_mut_ref ( self , self . lifetimes . re_static , impl_cor_ty) ;
1935+ let span = self . def_span ( def_id) ;
1936+ let source_info = SourceInfo :: outermost ( span) ;
1937+ let proxy_layout = CoroutineLayout {
1938+ field_tys : [ CoroutineSavedTy {
1939+ ty : impl_ref,
1940+ source_info,
1941+ ignore_for_traits : true ,
1942+ } ]
1943+ . into ( ) ,
1944+ field_names : [ None ] . into ( ) ,
1945+ variant_fields : [ IndexVec :: from ( [ CoroutineSavedLocal :: ZERO ] ) ] . into ( ) ,
1946+ variant_source_info : [ source_info] . into ( ) ,
1947+ storage_conflicts : BitMatrix :: new ( 1 , 1 ) ,
1948+ } ;
1949+ return Some ( self . arena . alloc ( proxy_layout) ) ;
1950+ } else {
1951+ self . templated_coroutine_layout ( Ty :: new_coroutine ( self , def_id, args) )
1952+ }
1953+ } else {
1954+ self . ordinary_coroutine_layout ( def_id, args. as_coroutine ( ) . kind_ty ( ) )
1955+ }
1956+ }
1957+
18951958 /// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
18961959 /// If it implements no trait, returns `None`.
18971960 pub fn trait_id_of_impl ( self , def_id : DefId ) -> Option < DefId > {
0 commit comments