@@ -37,6 +37,7 @@ use rustc_hir::LangItem;
37
37
use rustc_hir:: def:: { CtorKind , CtorOf , DefKind , DocLinkResMap , LifetimeRes , Res } ;
38
38
use rustc_hir:: def_id:: { CrateNum , DefId , DefIdMap , LocalDefId , LocalDefIdMap } ;
39
39
use rustc_index:: IndexVec ;
40
+ use rustc_index:: bit_set:: BitMatrix ;
40
41
use rustc_macros:: {
41
42
Decodable , Encodable , HashStable , TyDecodable , TyEncodable , TypeFoldable , TypeVisitable ,
42
43
extension,
@@ -100,7 +101,7 @@ pub use self::visit::{TypeSuperVisitable, TypeVisitable, TypeVisitableExt, TypeV
100
101
use crate :: error:: { OpaqueHiddenTypeMismatch , TypeMismatchReason } ;
101
102
use crate :: metadata:: ModChild ;
102
103
use crate :: middle:: privacy:: EffectiveVisibilities ;
103
- use crate :: mir:: { Body , CoroutineLayout } ;
104
+ use crate :: mir:: { Body , CoroutineLayout , CoroutineSavedLocal , CoroutineSavedTy , SourceInfo } ;
104
105
use crate :: query:: { IntoQueryParam , Providers } ;
105
106
use crate :: ty;
106
107
pub use crate :: ty:: diagnostics:: * ;
@@ -1740,9 +1741,8 @@ impl<'tcx> TyCtxt<'tcx> {
1740
1741
| ty:: InstanceKind :: CloneShim ( ..)
1741
1742
| ty:: InstanceKind :: ThreadLocalShim ( ..)
1742
1743
| 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) ,
1746
1746
}
1747
1747
}
1748
1748
@@ -1853,16 +1853,17 @@ impl<'tcx> TyCtxt<'tcx> {
1853
1853
self . def_kind ( trait_def_id) == DefKind :: TraitAlias
1854
1854
}
1855
1855
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
1857
1857
/// coroutine is tainted by errors.
1858
1858
///
1859
1859
/// Takes `coroutine_kind` which can be acquired from the `CoroutineArgs::kind_ty`,
1860
1860
/// e.g. `args.as_coroutine().kind_ty()`.
1861
- pub fn coroutine_layout (
1861
+ pub fn ordinary_coroutine_layout (
1862
1862
self ,
1863
1863
def_id : DefId ,
1864
1864
coroutine_kind_ty : Ty < ' tcx > ,
1865
1865
) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1866
+ debug_assert_ne ! ( Some ( def_id) , self . lang_items( ) . async_drop_in_place_poll_fn( ) ) ;
1866
1867
let mir = self . optimized_mir ( def_id) ;
1867
1868
// Regular coroutine
1868
1869
if coroutine_kind_ty. is_unit ( ) {
@@ -1892,6 +1893,68 @@ impl<'tcx> TyCtxt<'tcx> {
1892
1893
}
1893
1894
}
1894
1895
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
+
1895
1958
/// Given the `DefId` of an impl, returns the `DefId` of the trait it implements.
1896
1959
/// If it implements no trait, returns `None`.
1897
1960
pub fn trait_id_of_impl ( self , def_id : DefId ) -> Option < DefId > {
0 commit comments