@@ -2422,6 +2422,8 @@ fn const_param_def_id(expr: &hir::Expr<'_>) -> Option<DefId> {
2422
2422
}
2423
2423
2424
2424
impl < ' tcx > Const < ' tcx > {
2425
+ /// Literals and const generic parameters are eagerly converted to a constant, everything else
2426
+ /// becomes `Unevaluated`.
2425
2427
pub fn from_hir_anon_const (
2426
2428
tcx : TyCtxt < ' tcx > ,
2427
2429
ast_const : & hir:: AnonConst ,
@@ -2471,16 +2473,19 @@ impl<'tcx> Const<'tcx> {
2471
2473
}
2472
2474
2473
2475
#[ inline]
2476
+ /// Interns the given value as a constant.
2474
2477
pub fn from_value ( tcx : TyCtxt < ' tcx > , val : ConstValue < ' tcx > , ty : Ty < ' tcx > ) -> & ' tcx Self {
2475
2478
tcx. mk_const ( Self { val : ConstKind :: Value ( val) , ty } )
2476
2479
}
2477
2480
2478
2481
#[ inline]
2482
+ /// Interns the given scalar as a constant.
2479
2483
pub fn from_scalar ( tcx : TyCtxt < ' tcx > , val : Scalar , ty : Ty < ' tcx > ) -> & ' tcx Self {
2480
2484
Self :: from_value ( tcx, ConstValue :: Scalar ( val) , ty)
2481
2485
}
2482
2486
2483
2487
#[ inline]
2488
+ /// Creates a constant with the given integer value and interns it.
2484
2489
pub fn from_bits ( tcx : TyCtxt < ' tcx > , bits : u128 , ty : ParamEnvAnd < ' tcx , Ty < ' tcx > > ) -> & ' tcx Self {
2485
2490
let size = tcx
2486
2491
. layout_of ( ty)
@@ -2490,21 +2495,27 @@ impl<'tcx> Const<'tcx> {
2490
2495
}
2491
2496
2492
2497
#[ inline]
2498
+ /// Creates an interned zst constant.
2493
2499
pub fn zero_sized ( tcx : TyCtxt < ' tcx > , ty : Ty < ' tcx > ) -> & ' tcx Self {
2494
2500
Self :: from_scalar ( tcx, Scalar :: zst ( ) , ty)
2495
2501
}
2496
2502
2497
2503
#[ inline]
2504
+ /// Creates an interned bool constant.
2498
2505
pub fn from_bool ( tcx : TyCtxt < ' tcx > , v : bool ) -> & ' tcx Self {
2499
2506
Self :: from_bits ( tcx, v as u128 , ParamEnv :: empty ( ) . and ( tcx. types . bool ) )
2500
2507
}
2501
2508
2502
2509
#[ inline]
2510
+ /// Creates an interned usize constant.
2503
2511
pub fn from_usize ( tcx : TyCtxt < ' tcx > , n : u64 ) -> & ' tcx Self {
2504
2512
Self :: from_bits ( tcx, n as u128 , ParamEnv :: empty ( ) . and ( tcx. types . usize ) )
2505
2513
}
2506
2514
2507
2515
#[ inline]
2516
+ /// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
2517
+ /// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
2518
+ /// contains const generic parameters or pointers).
2508
2519
pub fn try_eval_bits (
2509
2520
& self ,
2510
2521
tcx : TyCtxt < ' tcx > ,
@@ -2518,6 +2529,8 @@ impl<'tcx> Const<'tcx> {
2518
2529
}
2519
2530
2520
2531
#[ inline]
2532
+ /// Tries to evaluate the constant if it is `Unevaluated`. If that doesn't succeed, return the
2533
+ /// unevaluated constant.
2521
2534
pub fn eval ( & self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> & Const < ' tcx > {
2522
2535
let try_const_eval = |did, param_env : ParamEnv < ' tcx > , substs, promoted| {
2523
2536
let param_env_and_substs = param_env. with_reveal_all ( ) . and ( substs) ;
@@ -2574,12 +2587,14 @@ impl<'tcx> Const<'tcx> {
2574
2587
}
2575
2588
2576
2589
#[ inline]
2590
+ /// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
2577
2591
pub fn eval_bits ( & self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > , ty : Ty < ' tcx > ) -> u128 {
2578
2592
self . try_eval_bits ( tcx, param_env, ty)
2579
2593
. unwrap_or_else ( || bug ! ( "expected bits of {:#?}, got {:#?}" , ty, self ) )
2580
2594
}
2581
2595
2582
2596
#[ inline]
2597
+ /// Panics if the value cannot be evaluated or doesn't contain a valid `usize`.
2583
2598
pub fn eval_usize ( & self , tcx : TyCtxt < ' tcx > , param_env : ParamEnv < ' tcx > ) -> u64 {
2584
2599
self . eval_bits ( tcx, param_env, tcx. types . usize ) as u64
2585
2600
}
0 commit comments