@@ -1788,22 +1788,18 @@ impl<'tcx> LayoutCx<'tcx, TyCtxt<'tcx>> {
1788
1788
let field_info: Vec < _ > = flds
1789
1789
. iter ( )
1790
1790
. enumerate ( )
1791
- . map ( |( i, & name) | match layout. field ( self , i) {
1792
- Err ( err) => {
1793
- bug ! ( "no layout found for field {}: `{:?}`" , name, err) ;
1791
+ . map ( |( i, & name) | {
1792
+ let field_layout = layout. field ( self , i) ;
1793
+ let offset = layout. fields . offset ( i) ;
1794
+ let field_end = offset + field_layout. size ;
1795
+ if min_size < field_end {
1796
+ min_size = field_end;
1794
1797
}
1795
- Ok ( field_layout) => {
1796
- let offset = layout. fields . offset ( i) ;
1797
- let field_end = offset + field_layout. size ;
1798
- if min_size < field_end {
1799
- min_size = field_end;
1800
- }
1801
- FieldInfo {
1802
- name : name. to_string ( ) ,
1803
- offset : offset. bytes ( ) ,
1804
- size : field_layout. size . bytes ( ) ,
1805
- align : field_layout. align . abi . bytes ( ) ,
1806
- }
1798
+ FieldInfo {
1799
+ name : name. to_string ( ) ,
1800
+ offset : offset. bytes ( ) ,
1801
+ size : field_layout. size . bytes ( ) ,
1802
+ align : field_layout. align . abi . bytes ( ) ,
1807
1803
}
1808
1804
} )
1809
1805
. collect ( ) ;
@@ -2146,30 +2142,24 @@ where
2146
2142
TyAndLayout { ty : this. ty , layout }
2147
2143
}
2148
2144
2149
- fn ty_and_layout_field ( this : TyAndLayout < ' tcx > , cx : & C , i : usize ) -> C :: TyAndLayout {
2150
- enum TyMaybeWithLayout < ' tcx , C : LayoutOf < ' tcx > > {
2151
- Ty ( C :: Ty ) ,
2152
- TyAndLayout ( C :: TyAndLayout ) ,
2145
+ fn ty_and_layout_field ( this : TyAndLayout < ' tcx > , cx : & C , i : usize ) -> TyAndLayout < ' tcx > {
2146
+ enum TyMaybeWithLayout < ' tcx > {
2147
+ Ty ( Ty < ' tcx > ) ,
2148
+ TyAndLayout ( TyAndLayout < ' tcx > ) ,
2153
2149
}
2154
2150
2155
- fn ty_and_layout_kind <
2156
- C : LayoutOf < ' tcx , Ty = Ty < ' tcx > > + HasTyCtxt < ' tcx > + HasParamEnv < ' tcx > ,
2157
- > (
2151
+ fn field_ty_or_layout (
2158
2152
this : TyAndLayout < ' tcx > ,
2159
- cx : & C ,
2153
+ cx : & ( impl HasTyCtxt < ' tcx > + HasParamEnv < ' tcx > ) ,
2160
2154
i : usize ,
2161
- ty : C :: Ty ,
2162
- ) -> TyMaybeWithLayout < ' tcx , C > {
2155
+ ) -> TyMaybeWithLayout < ' tcx > {
2163
2156
let tcx = cx. tcx ( ) ;
2164
- let tag_layout = |tag : & Scalar | -> C :: TyAndLayout {
2157
+ let tag_layout = |tag : & Scalar | -> TyAndLayout < ' tcx > {
2165
2158
let layout = Layout :: scalar ( cx, tag. clone ( ) ) ;
2166
- MaybeResult :: from ( Ok ( TyAndLayout {
2167
- layout : tcx. intern_layout ( layout) ,
2168
- ty : tag. value . to_ty ( tcx) ,
2169
- } ) )
2159
+ TyAndLayout { layout : tcx. intern_layout ( layout) , ty : tag. value . to_ty ( tcx) }
2170
2160
} ;
2171
2161
2172
- match * ty. kind ( ) {
2162
+ match * this . ty . kind ( ) {
2173
2163
ty:: Bool
2174
2164
| ty:: Char
2175
2165
| ty:: Int ( _)
@@ -2180,7 +2170,7 @@ where
2180
2170
| ty:: FnDef ( ..)
2181
2171
| ty:: GeneratorWitness ( ..)
2182
2172
| ty:: Foreign ( ..)
2183
- | ty:: Dynamic ( ..) => bug ! ( "TyAndLayout::field_type ({:?}): not applicable" , this) ,
2173
+ | ty:: Dynamic ( ..) => bug ! ( "TyAndLayout::field ({:?}): not applicable" , this) ,
2184
2174
2185
2175
// Potentially-fat pointers.
2186
2176
ty:: Ref ( _, pointee, _) | ty:: RawPtr ( ty:: TypeAndMut { ty : pointee, .. } ) => {
@@ -2192,17 +2182,19 @@ where
2192
2182
// as the `Abi` or `FieldsShape` is checked by users.
2193
2183
if i == 0 {
2194
2184
let nil = tcx. mk_unit ( ) ;
2195
- let ptr_ty = if ty. is_unsafe_ptr ( ) {
2185
+ let unit_ptr_ty = if this . ty . is_unsafe_ptr ( ) {
2196
2186
tcx. mk_mut_ptr ( nil)
2197
2187
} else {
2198
2188
tcx. mk_mut_ref ( tcx. lifetimes . re_static , nil)
2199
2189
} ;
2200
- return TyMaybeWithLayout :: TyAndLayout ( MaybeResult :: from (
2201
- cx. layout_of ( ptr_ty) . to_result ( ) . map ( |mut ptr_layout| {
2202
- ptr_layout. ty = ty;
2203
- ptr_layout
2204
- } ) ,
2205
- ) ) ;
2190
+
2191
+ // NOTE(eddyb) using an empty `ParamEnv`, and `unwrap`-ing
2192
+ // the `Result` should always work because the type is
2193
+ // always either `*mut ()` or `&'static mut ()`.
2194
+ return TyMaybeWithLayout :: TyAndLayout ( TyAndLayout {
2195
+ ty : this. ty ,
2196
+ ..tcx. layout_of ( ty:: ParamEnv :: reveal_all ( ) . and ( unit_ptr_ty) ) . unwrap ( )
2197
+ } ) ;
2206
2198
}
2207
2199
2208
2200
match tcx. struct_tail_erasing_lifetimes ( pointee, cx. param_env ( ) ) . kind ( ) {
@@ -2226,7 +2218,7 @@ where
2226
2218
])
2227
2219
*/
2228
2220
}
2229
- _ => bug ! ( "TyAndLayout::field_type ({:?}): not applicable" , this) ,
2221
+ _ => bug ! ( "TyAndLayout::field ({:?}): not applicable" , this) ,
2230
2222
}
2231
2223
}
2232
2224
@@ -2235,9 +2227,11 @@ where
2235
2227
ty:: Str => TyMaybeWithLayout :: Ty ( tcx. types . u8 ) ,
2236
2228
2237
2229
// Tuples, generators and closures.
2238
- ty:: Closure ( _, ref substs) => {
2239
- ty_and_layout_kind ( this, cx, i, substs. as_closure ( ) . tupled_upvars_ty ( ) )
2240
- }
2230
+ ty:: Closure ( _, ref substs) => field_ty_or_layout (
2231
+ TyAndLayout { ty : substs. as_closure ( ) . tupled_upvars_ty ( ) , ..this } ,
2232
+ cx,
2233
+ i,
2234
+ ) ,
2241
2235
2242
2236
ty:: Generator ( def_id, ref substs, _) => match this. variants {
2243
2237
Variants :: Single { index } => TyMaybeWithLayout :: Ty (
@@ -2280,14 +2274,25 @@ where
2280
2274
| ty:: Opaque ( ..)
2281
2275
| ty:: Param ( _)
2282
2276
| ty:: Infer ( _)
2283
- | ty:: Error ( _) => bug ! ( "TyAndLayout::field_type : unexpected type `{}`" , this. ty) ,
2277
+ | ty:: Error ( _) => bug ! ( "TyAndLayout::field : unexpected type `{}`" , this. ty) ,
2284
2278
}
2285
2279
}
2286
2280
2287
- cx. layout_of ( match ty_and_layout_kind ( this, cx, i, this. ty ) {
2288
- TyMaybeWithLayout :: Ty ( result) => result,
2289
- TyMaybeWithLayout :: TyAndLayout ( result) => return result,
2290
- } )
2281
+ match field_ty_or_layout ( this, cx, i) {
2282
+ TyMaybeWithLayout :: Ty ( field_ty) => {
2283
+ cx. tcx ( ) . layout_of ( cx. param_env ( ) . and ( field_ty) ) . unwrap_or_else ( |e| {
2284
+ bug ! (
2285
+ "failed to get layout for `{}`: {},\n \
2286
+ despite it being a field (#{}) of an existing layout: {:#?}",
2287
+ field_ty,
2288
+ e,
2289
+ i,
2290
+ this
2291
+ )
2292
+ } )
2293
+ }
2294
+ TyMaybeWithLayout :: TyAndLayout ( field_layout) => field_layout,
2295
+ }
2291
2296
}
2292
2297
2293
2298
fn ty_and_layout_pointee_info_at (
0 commit comments