@@ -16,7 +16,6 @@ pub use int::*;
1616pub use kind:: * ;
1717use rustc_span:: Span ;
1818use rustc_span:: DUMMY_SP ;
19- use rustc_target:: abi:: Size ;
2019pub use valtree:: * ;
2120
2221use super :: sty:: ConstKind ;
@@ -155,7 +154,7 @@ impl<'tcx> Const<'tcx> {
155154
156155 let ty = tcx. type_of ( def) . no_bound_vars ( ) . expect ( "const parameter types cannot be generic" ) ;
157156
158- match Self :: try_eval_lit_or_param ( tcx, ty, expr) {
157+ match Self :: try_from_lit_or_param ( tcx, ty, expr) {
159158 Some ( v) => v,
160159 None => ty:: Const :: new_unevaluated (
161160 tcx,
@@ -169,7 +168,7 @@ impl<'tcx> Const<'tcx> {
169168 }
170169
171170 #[ instrument( skip( tcx) , level = "debug" ) ]
172- fn try_eval_lit_or_param (
171+ fn try_from_lit_or_param (
173172 tcx : TyCtxt < ' tcx > ,
174173 ty : Ty < ' tcx > ,
175174 expr : & ' tcx hir:: Expr < ' tcx > ,
@@ -244,14 +243,6 @@ impl<'tcx> Const<'tcx> {
244243 }
245244 }
246245
247- /// Panics if self.kind != ty::ConstKind::Value
248- pub fn to_valtree ( self ) -> ty:: ValTree < ' tcx > {
249- match self . kind ( ) {
250- ty:: ConstKind :: Value ( valtree) => valtree,
251- _ => bug ! ( "expected ConstKind::Value, got {:?}" , self . kind( ) ) ,
252- }
253- }
254-
255246 #[ inline]
256247 /// Creates a constant with the given integer value and interns it.
257248 pub fn from_bits ( tcx : TyCtxt < ' tcx > , bits : u128 , ty : ParamEnvAnd < ' tcx , Ty < ' tcx > > ) -> Self {
@@ -284,81 +275,6 @@ impl<'tcx> Const<'tcx> {
284275 Self :: from_bits ( tcx, n as u128 , ParamEnv :: empty ( ) . and ( tcx. types . usize ) )
285276 }
286277
287- /// Attempts to convert to a `ValTree`
288- pub fn try_to_valtree ( self ) -> Option < ty:: ValTree < ' tcx > > {
289- match self . kind ( ) {
290- ty:: ConstKind :: Value ( valtree) => Some ( valtree) ,
291- _ => None ,
292- }
293- }
294-
295- #[ inline]
296- /// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
297- /// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
298- /// contains const generic parameters or pointers).
299- pub fn try_eval_scalar_int (
300- self ,
301- tcx : TyCtxt < ' tcx > ,
302- param_env : ParamEnv < ' tcx > ,
303- ) -> Option < ScalarInt > {
304- self . eval ( tcx, param_env, None ) . ok ( ) ?. try_to_scalar_int ( )
305- }
306-
307- #[ inline]
308- /// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
309- /// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
310- /// contains const generic parameters or pointers).
311- pub fn try_eval_bits (
312- self ,
313- tcx : TyCtxt < ' tcx > ,
314- param_env : ParamEnv < ' tcx > ,
315- ty : Ty < ' tcx > ,
316- ) -> Option < u128 > {
317- let int = self . try_eval_scalar_int ( tcx, param_env) ?;
318- assert_eq ! ( self . ty( ) , ty) ;
319- let size = tcx. layout_of ( param_env. with_reveal_all_normalized ( tcx) . and ( ty) ) . ok ( ) ?. size ;
320- // if `ty` does not depend on generic parameters, use an empty param_env
321- int. to_bits ( size) . ok ( )
322- }
323-
324- #[ inline]
325- pub fn try_eval_bool ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Option < bool > {
326- self . try_eval_scalar_int ( tcx, param_env) ?. try_into ( ) . ok ( )
327- }
328-
329- #[ inline]
330- pub fn try_eval_target_usize (
331- self ,
332- tcx : TyCtxt < ' tcx > ,
333- param_env : ParamEnv < ' tcx > ,
334- ) -> Option < u64 > {
335- self . try_eval_scalar_int ( tcx, param_env) ?. try_to_target_usize ( tcx) . ok ( )
336- }
337-
338- /// Normalizes the constant to a value or an error if possible.
339- #[ inline]
340- pub fn normalize ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Self {
341- match self . eval ( tcx, param_env, None ) {
342- Ok ( val) => Self :: new_value ( tcx, val, self . ty ( ) ) ,
343- Err ( ErrorHandled :: Reported ( r) ) => Self :: new_error ( tcx, r. into ( ) , self . ty ( ) ) ,
344- Err ( ErrorHandled :: TooGeneric ) => self ,
345- }
346- }
347-
348- #[ inline]
349- /// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
350- pub fn eval_bits ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > , ty : Ty < ' tcx > ) -> u128 {
351- self . try_eval_bits ( tcx, param_env, ty)
352- . unwrap_or_else ( || bug ! ( "expected bits of {:#?}, got {:#?}" , ty, self ) )
353- }
354-
355- #[ inline]
356- /// Panics if the value cannot be evaluated or doesn't contain a valid `usize`.
357- pub fn eval_target_usize ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> u64 {
358- self . try_eval_target_usize ( tcx, param_env)
359- . unwrap_or_else ( || bug ! ( "expected usize, got {:#?}" , self ) )
360- }
361-
362278 /// Returns the evaluated constant
363279 pub fn eval (
364280 self ,
@@ -410,34 +326,106 @@ impl<'tcx> Const<'tcx> {
410326 }
411327 }
412328
329+ /// Normalizes the constant to a value or an error if possible.
413330 #[ inline]
414- pub fn try_to_value ( self ) -> Option < ty:: ValTree < ' tcx > > {
415- if let ConstKind :: Value ( val) = self . kind ( ) { Some ( val) } else { None }
331+ pub fn normalize ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Self {
332+ match self . eval ( tcx, param_env, None ) {
333+ Ok ( val) => Self :: new_value ( tcx, val, self . ty ( ) ) ,
334+ Err ( ErrorHandled :: Reported ( r) ) => Self :: new_error ( tcx, r. into ( ) , self . ty ( ) ) ,
335+ Err ( ErrorHandled :: TooGeneric ) => self ,
336+ }
416337 }
417338
418339 #[ inline]
419- pub fn try_to_scalar ( self ) -> Option < Scalar < AllocId > > {
420- self . try_to_value ( ) ?. try_to_scalar ( )
340+ pub fn try_eval_scalar (
341+ self ,
342+ tcx : TyCtxt < ' tcx > ,
343+ param_env : ty:: ParamEnv < ' tcx > ,
344+ ) -> Option < Scalar > {
345+ self . eval ( tcx, param_env, None ) . ok ( ) ?. try_to_scalar ( )
421346 }
422347
423348 #[ inline]
424- pub fn try_to_scalar_int ( self ) -> Option < ScalarInt > {
425- self . try_to_value ( ) ?. try_to_scalar_int ( )
349+ /// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
350+ /// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
351+ /// contains const generic parameters or pointers).
352+ pub fn try_eval_scalar_int (
353+ self ,
354+ tcx : TyCtxt < ' tcx > ,
355+ param_env : ParamEnv < ' tcx > ,
356+ ) -> Option < ScalarInt > {
357+ self . try_eval_scalar ( tcx, param_env) ?. try_to_int ( ) . ok ( )
426358 }
427359
428360 #[ inline]
429- pub fn try_to_bits ( self , size : Size ) -> Option < u128 > {
430- self . try_to_scalar_int ( ) ?. to_bits ( size) . ok ( )
361+ /// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
362+ /// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
363+ /// contains const generic parameters or pointers).
364+ pub fn try_eval_bits (
365+ self ,
366+ tcx : TyCtxt < ' tcx > ,
367+ param_env : ParamEnv < ' tcx > ,
368+ ty : Ty < ' tcx > ,
369+ ) -> Option < u128 > {
370+ let int = self . try_eval_scalar_int ( tcx, param_env) ?;
371+ assert_eq ! ( self . ty( ) , ty) ;
372+ let size = tcx. layout_of ( param_env. with_reveal_all_normalized ( tcx) . and ( ty) ) . ok ( ) ?. size ;
373+ // if `ty` does not depend on generic parameters, use an empty param_env
374+ int. to_bits ( size) . ok ( )
431375 }
432376
433377 #[ inline]
434- pub fn try_to_bool ( self ) -> Option < bool > {
435- self . try_to_scalar_int ( ) ?. try_into ( ) . ok ( )
378+ /// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
379+ pub fn eval_bits ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > , ty : Ty < ' tcx > ) -> u128 {
380+ self . try_eval_bits ( tcx, param_env, ty)
381+ . unwrap_or_else ( || bug ! ( "expected bits of {:#?}, got {:#?}" , ty, self ) )
382+ }
383+
384+ #[ inline]
385+ pub fn try_eval_target_usize (
386+ self ,
387+ tcx : TyCtxt < ' tcx > ,
388+ param_env : ParamEnv < ' tcx > ,
389+ ) -> Option < u64 > {
390+ self . try_eval_scalar_int ( tcx, param_env) ?. try_to_target_usize ( tcx) . ok ( )
391+ }
392+
393+ #[ inline]
394+ pub fn try_eval_bool ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> Option < bool > {
395+ self . try_eval_scalar_int ( tcx, param_env) ?. try_into ( ) . ok ( )
396+ }
397+
398+ #[ inline]
399+ /// Panics if the value cannot be evaluated or doesn't contain a valid `usize`.
400+ pub fn eval_target_usize ( self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> u64 {
401+ self . try_eval_target_usize ( tcx, param_env)
402+ . unwrap_or_else ( || bug ! ( "expected usize, got {:#?}" , self ) )
403+ }
404+
405+ /// Panics if self.kind != ty::ConstKind::Value
406+ pub fn to_valtree ( self ) -> ty:: ValTree < ' tcx > {
407+ match self . kind ( ) {
408+ ty:: ConstKind :: Value ( valtree) => valtree,
409+ _ => bug ! ( "expected ConstKind::Value, got {:?}" , self . kind( ) ) ,
410+ }
411+ }
412+
413+ /// Attempts to convert to a `ValTree`
414+ pub fn try_to_valtree ( self ) -> Option < ty:: ValTree < ' tcx > > {
415+ match self . kind ( ) {
416+ ty:: ConstKind :: Value ( valtree) => Some ( valtree) ,
417+ _ => None ,
418+ }
419+ }
420+
421+ #[ inline]
422+ pub fn try_to_scalar ( self ) -> Option < Scalar < AllocId > > {
423+ self . try_to_valtree ( ) ?. try_to_scalar ( )
436424 }
437425
438426 #[ inline]
439427 pub fn try_to_target_usize ( self , tcx : TyCtxt < ' tcx > ) -> Option < u64 > {
440- self . try_to_value ( ) ?. try_to_target_usize ( tcx)
428+ self . try_to_valtree ( ) ?. try_to_target_usize ( tcx)
441429 }
442430
443431 pub fn is_ct_infer ( self ) -> bool {
0 commit comments