@@ -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,20 +1878,26 @@ 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
///
1883
1889
/// Takes `coroutine_kind` which can be acquired from the `CoroutineArgs::kind_ty`,
1884
1890
/// e.g. `args.as_coroutine().kind_ty()`.
1885
1891
fn ordinary_coroutine_layout (
1886
1892
self ,
1893
+ ty : Ty < ' tcx > ,
1887
1894
def_id : DefId ,
1888
1895
coroutine_kind_ty : Ty < ' tcx > ,
1889
- ) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1896
+ ) -> Result < & ' tcx CoroutineLayout < ' tcx > , & ' tcx LayoutError < ' tcx > > {
1890
1897
let mir = self . optimized_mir ( def_id) ;
1891
1898
// Regular coroutine
1892
1899
if coroutine_kind_ty. is_unit ( ) {
1893
- mir. coroutine_layout_raw ( )
1900
+ mir. coroutine_layout_raw ( ) . ok_or_else ( || self . layout_error ( LayoutError :: Unknown ( ty ) ) )
1894
1901
} else {
1895
1902
// If we have a `Coroutine` that comes from an coroutine-closure,
1896
1903
// then it may be a by-move or by-ref body.
@@ -1904,6 +1911,7 @@ impl<'tcx> TyCtxt<'tcx> {
1904
1911
// a by-ref coroutine.
1905
1912
if identity_kind_ty == coroutine_kind_ty {
1906
1913
mir. coroutine_layout_raw ( )
1914
+ . ok_or_else ( || self . layout_error ( LayoutError :: Unknown ( ty) ) )
1907
1915
} else {
1908
1916
assert_matches ! ( coroutine_kind_ty. to_opt_closure_kind( ) , Some ( ClosureKind :: FnOnce ) ) ;
1909
1917
assert_matches ! (
@@ -1912,6 +1920,7 @@ impl<'tcx> TyCtxt<'tcx> {
1912
1920
) ;
1913
1921
self . optimized_mir ( self . coroutine_by_move_body_def_id ( def_id) )
1914
1922
. coroutine_layout_raw ( )
1923
+ . ok_or_else ( || self . layout_error ( LayoutError :: Unknown ( ty) ) )
1915
1924
}
1916
1925
}
1917
1926
}
@@ -1921,23 +1930,27 @@ impl<'tcx> TyCtxt<'tcx> {
1921
1930
/// Layout might be unavailable if the coroutine is tainted by errors.
1922
1931
fn async_drop_coroutine_layout (
1923
1932
self ,
1933
+ ty : Ty < ' tcx > ,
1924
1934
def_id : DefId ,
1925
1935
args : GenericArgsRef < ' tcx > ,
1926
- ) -> Option < & ' tcx CoroutineLayout < ' tcx > > {
1936
+ ) -> Result < & ' tcx CoroutineLayout < ' tcx > , & ' tcx LayoutError < ' tcx > > {
1927
1937
if args[ 0 ] . has_placeholders ( ) || args[ 0 ] . has_non_region_param ( ) {
1928
- return None ;
1938
+ return Err ( self . layout_error ( LayoutError :: TooGeneric ( ty ) ) ) ;
1929
1939
}
1930
1940
let instance = InstanceKind :: AsyncDropGlue ( def_id, Ty :: new_coroutine ( self , def_id, args) ) ;
1931
- self . mir_shims ( instance) . coroutine_layout_raw ( )
1941
+ self . mir_shims ( instance)
1942
+ . coroutine_layout_raw ( )
1943
+ . ok_or_else ( || self . layout_error ( LayoutError :: Unknown ( ty) ) )
1932
1944
}
1933
1945
1934
1946
/// Returns layout of a coroutine. Layout might be unavailable if the
1935
1947
/// coroutine is tainted by errors.
1936
1948
pub fn coroutine_layout (
1937
1949
self ,
1950
+ ty : Ty < ' tcx > ,
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
- self . async_drop_coroutine_layout ( def_id, args)
1977
+ self . async_drop_coroutine_layout ( ty , 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 ( ty , def_id, args. as_coroutine ( ) . kind_ty ( ) )
1968
1981
}
1969
1982
}
1970
1983
0 commit comments