@@ -16,7 +16,6 @@ pub use int::*;
16
16
pub use kind:: * ;
17
17
use rustc_span:: Span ;
18
18
use rustc_span:: DUMMY_SP ;
19
- use rustc_target:: abi:: Size ;
20
19
pub use valtree:: * ;
21
20
22
21
use super :: sty:: ConstKind ;
@@ -155,7 +154,7 @@ impl<'tcx> Const<'tcx> {
155
154
156
155
let ty = tcx. type_of ( def) . no_bound_vars ( ) . expect ( "const parameter types cannot be generic" ) ;
157
156
158
- match Self :: try_eval_lit_or_param ( tcx, ty, expr) {
157
+ match Self :: try_from_lit_or_param ( tcx, ty, expr) {
159
158
Some ( v) => v,
160
159
None => ty:: Const :: new_unevaluated (
161
160
tcx,
@@ -169,7 +168,7 @@ impl<'tcx> Const<'tcx> {
169
168
}
170
169
171
170
#[ instrument( skip( tcx) , level = "debug" ) ]
172
- fn try_eval_lit_or_param (
171
+ fn try_from_lit_or_param (
173
172
tcx : TyCtxt < ' tcx > ,
174
173
ty : Ty < ' tcx > ,
175
174
expr : & ' tcx hir:: Expr < ' tcx > ,
@@ -244,14 +243,6 @@ impl<'tcx> Const<'tcx> {
244
243
}
245
244
}
246
245
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
-
255
246
#[ inline]
256
247
/// Creates a constant with the given integer value and interns it.
257
248
pub fn from_bits ( tcx : TyCtxt < ' tcx > , bits : u128 , ty : ParamEnvAnd < ' tcx , Ty < ' tcx > > ) -> Self {
@@ -284,81 +275,6 @@ impl<'tcx> Const<'tcx> {
284
275
Self :: from_bits ( tcx, n as u128 , ParamEnv :: empty ( ) . and ( tcx. types . usize ) )
285
276
}
286
277
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
-
362
278
/// Returns the evaluated constant
363
279
pub fn eval (
364
280
self ,
@@ -410,34 +326,106 @@ impl<'tcx> Const<'tcx> {
410
326
}
411
327
}
412
328
329
+ /// Normalizes the constant to a value or an error if possible.
413
330
#[ 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
+ }
416
337
}
417
338
418
339
#[ 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 ( )
421
346
}
422
347
423
348
#[ 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 ( )
426
358
}
427
359
428
360
#[ 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 ( )
431
375
}
432
376
433
377
#[ 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 ( )
436
424
}
437
425
438
426
#[ inline]
439
427
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)
441
429
}
442
430
443
431
pub fn is_ct_infer ( self ) -> bool {
0 commit comments