@@ -33,6 +33,12 @@ use crate::errors::{
3333
3434mod invariant;
3535
36+ /// The `ValTree` alongside its type.
37+ struct ValTreeAndTy < ' tcx > {
38+ valtree : ty:: ValTree < ' tcx > ,
39+ ty : Ty < ' tcx > ,
40+ }
41+
3642pub ( crate ) fn provide ( providers : & mut Providers ) {
3743 * providers = Providers { layout_of, ..* providers } ;
3844}
@@ -144,13 +150,13 @@ fn univariant_uninterned<'tcx>(
144150 cx. calc . univariant ( fields, repr, kind) . map_err ( |err| map_error ( cx, ty, err) )
145151}
146152
147- fn validate_const_with_value < ' tcx > (
153+ fn extract_valtree_and_ty < ' tcx > (
148154 const_ : ty:: Const < ' tcx > ,
149155 ty : Ty < ' tcx > ,
150156 cx : & LayoutCx < ' tcx > ,
151- ) -> Result < ty :: Const < ' tcx > , & ' tcx LayoutError < ' tcx > > {
157+ ) -> Result < ValTreeAndTy < ' tcx > , & ' tcx LayoutError < ' tcx > > {
152158 match const_. kind ( ) {
153- ty:: ConstKind :: Value ( .. ) => Ok ( const_ ) ,
159+ ty:: ConstKind :: Value ( ty , valtree ) => Ok ( ValTreeAndTy { valtree , ty } ) ,
154160 ty:: ConstKind :: Error ( guar) => {
155161 return Err ( error ( cx, LayoutError :: ReferencesError ( guar) ) ) ;
156162 }
@@ -209,14 +215,16 @@ fn layout_of_uncached<'tcx>(
209215 & mut layout. backend_repr
210216 {
211217 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) ) ) ?;
216223 }
217224 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 )
220228 . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
221229 if !include_end {
222230 end = end. wrapping_sub ( 1 ) ;
@@ -348,9 +356,8 @@ fn layout_of_uncached<'tcx>(
348356
349357 // Arrays and slices.
350358 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
354361 . try_to_target_usize ( tcx)
355362 . ok_or_else ( || error ( cx, LayoutError :: Unknown ( ty) ) ) ?;
356363
0 commit comments