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