Skip to content

Commit fa5a15c

Browse files
committed
Document most methods on ty::Const
1 parent 799b15e commit fa5a15c

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/librustc/ty/sty.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2422,6 +2422,8 @@ fn const_param_def_id(expr: &hir::Expr<'_>) -> Option<DefId> {
24222422
}
24232423

24242424
impl<'tcx> Const<'tcx> {
2425+
/// Literals and const generic parameters are eagerly converted to a constant, everything else
2426+
/// becomes `Unevaluated`.
24252427
pub fn from_hir_anon_const(
24262428
tcx: TyCtxt<'tcx>,
24272429
ast_const: &hir::AnonConst,
@@ -2471,16 +2473,19 @@ impl<'tcx> Const<'tcx> {
24712473
}
24722474

24732475
#[inline]
2476+
/// Interns the given value as a constant.
24742477
pub fn from_value(tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>, ty: Ty<'tcx>) -> &'tcx Self {
24752478
tcx.mk_const(Self { val: ConstKind::Value(val), ty })
24762479
}
24772480

24782481
#[inline]
2482+
/// Interns the given scalar as a constant.
24792483
pub fn from_scalar(tcx: TyCtxt<'tcx>, val: Scalar, ty: Ty<'tcx>) -> &'tcx Self {
24802484
Self::from_value(tcx, ConstValue::Scalar(val), ty)
24812485
}
24822486

24832487
#[inline]
2488+
/// Creates a constant with the given integer value and interns it.
24842489
pub fn from_bits(tcx: TyCtxt<'tcx>, bits: u128, ty: ParamEnvAnd<'tcx, Ty<'tcx>>) -> &'tcx Self {
24852490
let size = tcx
24862491
.layout_of(ty)
@@ -2490,21 +2495,27 @@ impl<'tcx> Const<'tcx> {
24902495
}
24912496

24922497
#[inline]
2498+
/// Creates an interned zst constant.
24932499
pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> &'tcx Self {
24942500
Self::from_scalar(tcx, Scalar::zst(), ty)
24952501
}
24962502

24972503
#[inline]
2504+
/// Creates an interned bool constant.
24982505
pub fn from_bool(tcx: TyCtxt<'tcx>, v: bool) -> &'tcx Self {
24992506
Self::from_bits(tcx, v as u128, ParamEnv::empty().and(tcx.types.bool))
25002507
}
25012508

25022509
#[inline]
2510+
/// Creates an interned usize constant.
25032511
pub fn from_usize(tcx: TyCtxt<'tcx>, n: u64) -> &'tcx Self {
25042512
Self::from_bits(tcx, n as u128, ParamEnv::empty().and(tcx.types.usize))
25052513
}
25062514

25072515
#[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).
25082519
pub fn try_eval_bits(
25092520
&self,
25102521
tcx: TyCtxt<'tcx>,
@@ -2518,6 +2529,8 @@ impl<'tcx> Const<'tcx> {
25182529
}
25192530

25202531
#[inline]
2532+
/// Tries to evaluate the constant if it is `Unevaluated`. If that doesn't succeed, return the
2533+
/// unevaluated constant.
25212534
pub fn eval(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> &Const<'tcx> {
25222535
let try_const_eval = |did, param_env: ParamEnv<'tcx>, substs, promoted| {
25232536
let param_env_and_substs = param_env.with_reveal_all().and(substs);
@@ -2574,12 +2587,14 @@ impl<'tcx> Const<'tcx> {
25742587
}
25752588

25762589
#[inline]
2590+
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
25772591
pub fn eval_bits(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
25782592
self.try_eval_bits(tcx, param_env, ty)
25792593
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
25802594
}
25812595

25822596
#[inline]
2597+
/// Panics if the value cannot be evaluated or doesn't contain a valid `usize`.
25832598
pub fn eval_usize(&self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> u64 {
25842599
self.eval_bits(tcx, param_env, tcx.types.usize) as u64
25852600
}

0 commit comments

Comments
 (0)