@@ -33,6 +33,12 @@ use crate::errors::{
33
33
34
34
mod invariant;
35
35
36
+ /// The `ValTree` alongside its type.
37
+ struct ValTreeAndTy < ' tcx > {
38
+ valtree : ty:: ValTree < ' tcx > ,
39
+ ty : Ty < ' tcx > ,
40
+ }
41
+
36
42
pub ( crate ) fn provide ( providers : & mut Providers ) {
37
43
* providers = Providers { layout_of, ..* providers } ;
38
44
}
@@ -144,13 +150,13 @@ fn univariant_uninterned<'tcx>(
144
150
cx. calc . univariant ( fields, repr, kind) . map_err ( |err| map_error ( cx, ty, err) )
145
151
}
146
152
147
- fn validate_const_with_value < ' tcx > (
153
+ fn extract_valtree_and_ty < ' tcx > (
148
154
const_ : ty:: Const < ' tcx > ,
149
155
ty : Ty < ' tcx > ,
150
156
cx : & LayoutCx < ' tcx > ,
151
- ) -> Result < ty :: Const < ' tcx > , & ' tcx LayoutError < ' tcx > > {
157
+ ) -> Result < ValTreeAndTy < ' tcx > , & ' tcx LayoutError < ' tcx > > {
152
158
match const_. kind ( ) {
153
- ty:: ConstKind :: Value ( .. ) => Ok ( const_ ) ,
159
+ ty:: ConstKind :: Value ( ty , valtree ) => Ok ( ValTreeAndTy { valtree , ty } ) ,
154
160
ty:: ConstKind :: Error ( guar) => {
155
161
return Err ( error ( cx, LayoutError :: ReferencesError ( guar) ) ) ;
156
162
}
@@ -209,14 +215,16 @@ fn layout_of_uncached<'tcx>(
209
215
& mut layout. backend_repr
210
216
{
211
217
if let Some ( start) = start {
212
- scalar. valid_range_mut ( ) . start =
213
- validate_const_with_value ( start, ty, cx) ?
214
- . try_to_bits ( tcx, cx. typing_env )
215
- . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
218
+ let ValTreeAndTy { valtree, ty } =
219
+ extract_valtree_and_ty ( start, ty, cx) ?;
220
+ scalar. valid_range_mut ( ) . start = valtree
221
+ . try_to_bits ( tcx, ty, cx. typing_env )
222
+ . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
216
223
}
217
224
if let Some ( end) = end {
218
- let mut end = validate_const_with_value ( end, ty, cx) ?
219
- . try_to_bits ( tcx, cx. typing_env )
225
+ let ValTreeAndTy { valtree, ty } = extract_valtree_and_ty ( end, ty, cx) ?;
226
+ let mut end = valtree
227
+ . try_to_bits ( tcx, ty, cx. typing_env )
220
228
. ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
221
229
if !include_end {
222
230
end = end. wrapping_sub ( 1 ) ;
@@ -348,9 +356,8 @@ fn layout_of_uncached<'tcx>(
348
356
349
357
// Arrays and slices.
350
358
ty:: Array ( element, count) => {
351
- let count = validate_const_with_value ( count, ty, cx) ?
352
- . to_valtree ( )
353
- . 0
359
+ let count = extract_valtree_and_ty ( count, ty, cx) ?
360
+ . valtree
354
361
. try_to_target_usize ( tcx)
355
362
. ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
356
363
0 commit comments