@@ -121,6 +121,7 @@ use crate::ty;
121
121
use crate :: ty:: codec:: { TyDecoder , TyEncoder } ;
122
122
pub use crate :: ty:: diagnostics:: * ;
123
123
use crate :: ty:: fast_reject:: SimplifiedType ;
124
+ use crate :: ty:: layout:: LayoutError ;
124
125
use crate :: ty:: util:: Discr ;
125
126
use crate :: ty:: walk:: TypeWalker ;
126
127
@@ -1878,6 +1879,11 @@ impl<'tcx> TyCtxt<'tcx> {
1878
1879
self . def_kind ( trait_def_id) == DefKind :: TraitAlias
1879
1880
}
1880
1881
1882
+ /// Arena-alloc of LayoutError for coroutine layout
1883
+ fn layout_error ( self , err : LayoutError < ' tcx > ) -> & ' tcx LayoutError < ' tcx > {
1884
+ self . arena . alloc ( err)
1885
+ }
1886
+
1881
1887
/// Returns layout of a non-async-drop coroutine. Layout might be unavailable if the
1882
1888
/// coroutine is tainted by errors.
1883
1889
///
@@ -1886,12 +1892,14 @@ impl<'tcx> TyCtxt<'tcx> {
1886
1892
fn ordinary_coroutine_layout (
1887
1893
self ,
1888
1894
def_id : DefId ,
1889
- coroutine_kind_ty : Ty < ' tcx > ,
1890
- ) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1895
+ args : GenericArgsRef < ' tcx > ,
1896
+ ) -> Result < & ' tcx CoroutineLayout < ' tcx > , & ' tcx LayoutError < ' tcx > > {
1897
+ let coroutine_kind_ty = args. as_coroutine ( ) . kind_ty ( ) ;
1891
1898
let mir = self . optimized_mir ( def_id) ;
1899
+ let ty = || Ty :: new_coroutine ( self , def_id, args) ;
1892
1900
// Regular coroutine
1893
1901
if coroutine_kind_ty. is_unit ( ) {
1894
- mir. coroutine_layout_raw ( )
1902
+ mir. coroutine_layout_raw ( ) . ok_or_else ( || self . layout_error ( LayoutError :: Unknown ( ty ( ) ) ) )
1895
1903
} else {
1896
1904
// If we have a `Coroutine` that comes from an coroutine-closure,
1897
1905
// then it may be a by-move or by-ref body.
@@ -1905,6 +1913,7 @@ impl<'tcx> TyCtxt<'tcx> {
1905
1913
// a by-ref coroutine.
1906
1914
if identity_kind_ty == coroutine_kind_ty {
1907
1915
mir. coroutine_layout_raw ( )
1916
+ . ok_or_else ( || self . layout_error ( LayoutError :: Unknown ( ty ( ) ) ) )
1908
1917
} else {
1909
1918
assert_matches ! ( coroutine_kind_ty. to_opt_closure_kind( ) , Some ( ClosureKind :: FnOnce ) ) ;
1910
1919
assert_matches ! (
@@ -1913,6 +1922,7 @@ impl<'tcx> TyCtxt<'tcx> {
1913
1922
) ;
1914
1923
self . optimized_mir ( self . coroutine_by_move_body_def_id ( def_id) )
1915
1924
. coroutine_layout_raw ( )
1925
+ . ok_or_else ( || self . layout_error ( LayoutError :: Unknown ( ty ( ) ) ) )
1916
1926
}
1917
1927
}
1918
1928
}
@@ -1924,12 +1934,15 @@ impl<'tcx> TyCtxt<'tcx> {
1924
1934
self ,
1925
1935
def_id : DefId ,
1926
1936
args : GenericArgsRef < ' tcx > ,
1927
- ) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1937
+ ) -> Result < & ' tcx CoroutineLayout < ' tcx > , & ' tcx LayoutError < ' tcx > > {
1938
+ let ty = || Ty :: new_coroutine ( self , def_id, args) ;
1928
1939
if args[ 0 ] . has_placeholders ( ) || args[ 0 ] . has_non_region_param ( ) {
1929
- return None ;
1940
+ return Err ( self . layout_error ( LayoutError :: TooGeneric ( ty ( ) ) ) ) ;
1930
1941
}
1931
1942
let instance = InstanceKind :: AsyncDropGlue ( def_id, Ty :: new_coroutine ( self , def_id, args) ) ;
1932
- self . mir_shims ( instance) . coroutine_layout_raw ( )
1943
+ self . mir_shims ( instance)
1944
+ . coroutine_layout_raw ( )
1945
+ . ok_or_else ( || self . layout_error ( LayoutError :: Unknown ( ty ( ) ) ) )
1933
1946
}
1934
1947
1935
1948
/// Returns layout of a coroutine. Layout might be unavailable if the
@@ -1938,7 +1951,7 @@ impl<'tcx> TyCtxt<'tcx> {
1938
1951
self ,
1939
1952
def_id : DefId ,
1940
1953
args : GenericArgsRef < ' tcx > ,
1941
- ) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1954
+ ) -> Result < & ' tcx CoroutineLayout < ' tcx > , & ' tcx LayoutError < ' tcx > > {
1942
1955
if self . is_async_drop_in_place_coroutine ( def_id) {
1943
1956
// layout of `async_drop_in_place<T>::{closure}` in case,
1944
1957
// when T is a coroutine, contains this internal coroutine's ptr in upvars
@@ -1960,12 +1973,12 @@ impl<'tcx> TyCtxt<'tcx> {
1960
1973
variant_source_info,
1961
1974
storage_conflicts : BitMatrix :: new ( 0 , 0 ) ,
1962
1975
} ;
1963
- return Some ( self . arena . alloc ( proxy_layout) ) ;
1976
+ return Ok ( self . arena . alloc ( proxy_layout) ) ;
1964
1977
} else {
1965
1978
self . async_drop_coroutine_layout ( def_id, args)
1966
1979
}
1967
1980
} else {
1968
- self . ordinary_coroutine_layout ( def_id, args. as_coroutine ( ) . kind_ty ( ) )
1981
+ self . ordinary_coroutine_layout ( def_id, args)
1969
1982
}
1970
1983
}
1971
1984
0 commit comments