@@ -24,7 +24,7 @@ pub type ConstKind<'tcx> = ir::ConstKind<TyCtxt<'tcx>>;
24
24
pub type UnevaluatedConst < ' tcx > = ir:: UnevaluatedConst < TyCtxt < ' tcx > > ;
25
25
26
26
#[ cfg( target_pointer_width = "64" ) ]
27
- rustc_data_structures:: static_assert_size!( ConstKind <' _>, 24 ) ;
27
+ rustc_data_structures:: static_assert_size!( ConstKind <' _>, 32 ) ;
28
28
29
29
#[ derive( Copy , Clone , PartialEq , Eq , Hash , HashStable ) ]
30
30
#[ rustc_pass_by_value]
@@ -168,10 +168,6 @@ impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
168
168
fn new_unevaluated ( interner : TyCtxt < ' tcx > , uv : ty:: UnevaluatedConst < ' tcx > ) -> Self {
169
169
Const :: new_unevaluated ( interner, uv)
170
170
}
171
-
172
- fn ty ( self ) -> Ty < ' tcx > {
173
- self . ty ( )
174
- }
175
171
}
176
172
177
173
impl < ' tcx > Const < ' tcx > {
@@ -311,7 +307,7 @@ impl<'tcx> Const<'tcx> {
311
307
tcx : TyCtxt < ' tcx > ,
312
308
param_env : ParamEnv < ' tcx > ,
313
309
span : Span ,
314
- ) -> Result < ValTree < ' tcx > , ErrorHandled > {
310
+ ) -> Result < ( Ty < ' tcx > , ValTree < ' tcx > ) , ErrorHandled > {
315
311
assert ! ( !self . has_escaping_bound_vars( ) , "escaping vars in {self:?}" ) ;
316
312
match self . kind ( ) {
317
313
ConstKind :: Unevaluated ( unevaluated) => {
@@ -329,9 +325,9 @@ impl<'tcx> Const<'tcx> {
329
325
) ;
330
326
return Err ( e. into ( ) ) ;
331
327
} ;
332
- Ok ( c )
328
+ Ok ( ( tcx . type_of ( unevaluated . def ) . instantiate ( tcx , unevaluated . args ) , c ) )
333
329
}
334
- ConstKind :: Value ( val) => Ok ( val) ,
330
+ ConstKind :: Value ( ty , val) => Ok ( ( ty , val) ) ,
335
331
ConstKind :: Error ( g) => Err ( g. into ( ) ) ,
336
332
ConstKind :: Param ( _)
337
333
| ConstKind :: Infer ( _)
@@ -345,7 +341,7 @@ impl<'tcx> Const<'tcx> {
345
341
#[ inline]
346
342
pub fn normalize ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Self {
347
343
match self . eval ( tcx, param_env, DUMMY_SP ) {
348
- Ok ( val) => Self :: new_value ( tcx, val, self . ty_for_ctfe ( tcx ) . unwrap ( ) ) ,
344
+ Ok ( ( ty , val) ) => Self :: new_value ( tcx, val, ty ) ,
349
345
Err ( ErrorHandled :: Reported ( r, _span) ) => Self :: new_error ( tcx, r. into ( ) ) ,
350
346
Err ( ErrorHandled :: TooGeneric ( _span) ) => self ,
351
347
}
@@ -356,8 +352,10 @@ impl<'tcx> Const<'tcx> {
356
352
self ,
357
353
tcx : TyCtxt < ' tcx > ,
358
354
param_env : ty:: ParamEnv < ' tcx > ,
359
- ) -> Option < Scalar > {
360
- self . eval ( tcx, param_env, DUMMY_SP ) . ok ( ) ?. try_to_scalar ( )
355
+ ) -> Option < ( Ty < ' tcx > , Scalar ) > {
356
+ let ( ty, val) = self . eval ( tcx, param_env, DUMMY_SP ) . ok ( ) ?;
357
+ let val = val. try_to_scalar ( ) ?;
358
+ Some ( ( ty, val) )
361
359
}
362
360
363
361
#[ inline]
@@ -368,24 +366,21 @@ impl<'tcx> Const<'tcx> {
368
366
self ,
369
367
tcx : TyCtxt < ' tcx > ,
370
368
param_env : ParamEnv < ' tcx > ,
371
- ) -> Option < ScalarInt > {
372
- self . try_eval_scalar ( tcx, param_env) ?. try_to_int ( ) . ok ( )
369
+ ) -> Option < ( Ty < ' tcx > , ScalarInt ) > {
370
+ let ( ty, scalar) = self . try_eval_scalar ( tcx, param_env) ?;
371
+ let val = scalar. try_to_int ( ) . ok ( ) ?;
372
+ Some ( ( ty, val) )
373
373
}
374
374
375
375
#[ inline]
376
376
/// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
377
377
/// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
378
378
/// contains const generic parameters or pointers).
379
379
pub fn try_eval_bits ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Option < u128 > {
380
- let int = self . try_eval_scalar_int ( tcx, param_env) ?;
381
- let size = tcx
382
- . layout_of (
383
- param_env. with_reveal_all_normalized ( tcx) . and ( self . ty_for_ctfe ( tcx) . unwrap ( ) ) ,
384
- )
385
- . ok ( ) ?
386
- . size ;
380
+ let ( ty, scalar) = self . try_eval_scalar_int ( tcx, param_env) ?;
381
+ let size = tcx. layout_of ( param_env. with_reveal_all_normalized ( tcx) . and ( ty) ) . ok ( ) ?. size ;
387
382
// if `ty` does not depend on generic parameters, use an empty param_env
388
- int . try_to_bits ( size) . ok ( )
383
+ scalar . try_to_bits ( size) . ok ( )
389
384
}
390
385
391
386
#[ inline]
@@ -401,12 +396,14 @@ impl<'tcx> Const<'tcx> {
401
396
tcx : TyCtxt < ' tcx > ,
402
397
param_env : ParamEnv < ' tcx > ,
403
398
) -> Option < u64 > {
404
- self . try_eval_scalar_int ( tcx, param_env) ?. try_to_target_usize ( tcx) . ok ( )
399
+ let ( _, scalar) = self . try_eval_scalar_int ( tcx, param_env) ?;
400
+ scalar. try_to_target_usize ( tcx) . ok ( )
405
401
}
406
402
407
403
#[ inline]
408
404
pub fn try_eval_bool ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Option < bool > {
409
- self . try_eval_scalar_int ( tcx, param_env) ?. try_into ( ) . ok ( )
405
+ let ( _, scalar) = self . try_eval_scalar_int ( tcx, param_env) ?;
406
+ scalar. try_into ( ) . ok ( )
410
407
}
411
408
412
409
#[ inline]
@@ -419,15 +416,15 @@ impl<'tcx> Const<'tcx> {
419
416
/// Panics if self.kind != ty::ConstKind::Value
420
417
pub fn to_valtree ( self ) -> ty:: ValTree < ' tcx > {
421
418
match self . kind ( ) {
422
- ty:: ConstKind :: Value ( valtree) => valtree,
419
+ ty:: ConstKind :: Value ( _ , valtree) => valtree,
423
420
_ => bug ! ( "expected ConstKind::Value, got {:?}" , self . kind( ) ) ,
424
421
}
425
422
}
426
423
427
424
/// Attempts to convert to a `ValTree`
428
425
pub fn try_to_valtree ( self ) -> Option < ty:: ValTree < ' tcx > > {
429
426
match self . kind ( ) {
430
- ty:: ConstKind :: Value ( valtree) => Some ( valtree) ,
427
+ ty:: ConstKind :: Value ( _ , valtree) => Some ( valtree) ,
431
428
_ => None ,
432
429
}
433
430
}
0 commit comments