@@ -24,7 +24,7 @@ pub type ConstKind<'tcx> = ir::ConstKind<TyCtxt<'tcx>>;
2424pub type UnevaluatedConst < ' tcx > = ir:: UnevaluatedConst < TyCtxt < ' tcx > > ;
2525
2626#[ cfg( target_pointer_width = "64" ) ]
27- rustc_data_structures:: static_assert_size!( ConstKind <' _>, 24 ) ;
27+ rustc_data_structures:: static_assert_size!( ConstKind <' _>, 32 ) ;
2828
2929#[ derive( Copy , Clone , PartialEq , Eq , Hash , HashStable ) ]
3030#[ rustc_pass_by_value]
@@ -168,10 +168,6 @@ impl<'tcx> rustc_type_ir::inherent::Const<TyCtxt<'tcx>> for Const<'tcx> {
168168 fn new_unevaluated ( interner : TyCtxt < ' tcx > , uv : ty:: UnevaluatedConst < ' tcx > ) -> Self {
169169 Const :: new_unevaluated ( interner, uv)
170170 }
171-
172- fn ty ( self ) -> Ty < ' tcx > {
173- self . ty ( )
174- }
175171}
176172
177173impl < ' tcx > Const < ' tcx > {
@@ -311,7 +307,7 @@ impl<'tcx> Const<'tcx> {
311307 tcx : TyCtxt < ' tcx > ,
312308 param_env : ParamEnv < ' tcx > ,
313309 span : Span ,
314- ) -> Result < ValTree < ' tcx > , ErrorHandled > {
310+ ) -> Result < ( Ty < ' tcx > , ValTree < ' tcx > ) , ErrorHandled > {
315311 assert ! ( !self . has_escaping_bound_vars( ) , "escaping vars in {self:?}" ) ;
316312 match self . kind ( ) {
317313 ConstKind :: Unevaluated ( unevaluated) => {
@@ -329,9 +325,9 @@ impl<'tcx> Const<'tcx> {
329325 ) ;
330326 return Err ( e. into ( ) ) ;
331327 } ;
332- Ok ( c )
328+ Ok ( ( tcx . type_of ( unevaluated . def ) . instantiate ( tcx , unevaluated . args ) , c ) )
333329 }
334- ConstKind :: Value ( val) => Ok ( val) ,
330+ ConstKind :: Value ( ty , val) => Ok ( ( ty , val) ) ,
335331 ConstKind :: Error ( g) => Err ( g. into ( ) ) ,
336332 ConstKind :: Param ( _)
337333 | ConstKind :: Infer ( _)
@@ -345,7 +341,7 @@ impl<'tcx> Const<'tcx> {
345341 #[ inline]
346342 pub fn normalize ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Self {
347343 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 ) ,
349345 Err ( ErrorHandled :: Reported ( r, _span) ) => Self :: new_error ( tcx, r. into ( ) ) ,
350346 Err ( ErrorHandled :: TooGeneric ( _span) ) => self ,
351347 }
@@ -356,8 +352,10 @@ impl<'tcx> Const<'tcx> {
356352 self ,
357353 tcx : TyCtxt < ' tcx > ,
358354 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) )
361359 }
362360
363361 #[ inline]
@@ -368,24 +366,21 @@ impl<'tcx> Const<'tcx> {
368366 self ,
369367 tcx : TyCtxt < ' tcx > ,
370368 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) )
373373 }
374374
375375 #[ inline]
376376 /// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
377377 /// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
378378 /// contains const generic parameters or pointers).
379379 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 ;
387382 // 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 ( )
389384 }
390385
391386 #[ inline]
@@ -401,12 +396,14 @@ impl<'tcx> Const<'tcx> {
401396 tcx : TyCtxt < ' tcx > ,
402397 param_env : ParamEnv < ' tcx > ,
403398 ) -> 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 ( )
405401 }
406402
407403 #[ inline]
408404 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 ( )
410407 }
411408
412409 #[ inline]
@@ -419,15 +416,15 @@ impl<'tcx> Const<'tcx> {
419416 /// Panics if self.kind != ty::ConstKind::Value
420417 pub fn to_valtree ( self ) -> ty:: ValTree < ' tcx > {
421418 match self . kind ( ) {
422- ty:: ConstKind :: Value ( valtree) => valtree,
419+ ty:: ConstKind :: Value ( _ , valtree) => valtree,
423420 _ => bug ! ( "expected ConstKind::Value, got {:?}" , self . kind( ) ) ,
424421 }
425422 }
426423
427424 /// Attempts to convert to a `ValTree`
428425 pub fn try_to_valtree ( self ) -> Option < ty:: ValTree < ' tcx > > {
429426 match self . kind ( ) {
430- ty:: ConstKind :: Value ( valtree) => Some ( valtree) ,
427+ ty:: ConstKind :: Value ( _ , valtree) => Some ( valtree) ,
431428 _ => None ,
432429 }
433430 }
0 commit comments