Skip to content

Commit 11a4a24

Browse files
committed
make the set of methods between our two Const types more consistent
1 parent 6e4779a commit 11a4a24

File tree

8 files changed

+124
-166
lines changed

8 files changed

+124
-166
lines changed

compiler/rustc_middle/src/mir/mod.rs

+31-53
Original file line numberDiff line numberDiff line change
@@ -2299,18 +2299,6 @@ impl<'tcx> ConstantKind<'tcx> {
22992299
}
23002300
}
23012301

2302-
#[inline]
2303-
pub fn try_to_value(self, tcx: TyCtxt<'tcx>) -> Option<interpret::ConstValue<'tcx>> {
2304-
match self {
2305-
ConstantKind::Ty(c) => match c.kind() {
2306-
ty::ConstKind::Value(valtree) => Some(tcx.valtree_to_const_val((c.ty(), valtree))),
2307-
_ => None,
2308-
},
2309-
ConstantKind::Val(val, _) => Some(val),
2310-
ConstantKind::Unevaluated(..) => None,
2311-
}
2312-
}
2313-
23142302
#[inline]
23152303
pub fn try_to_scalar(self) -> Option<Scalar> {
23162304
match self {
@@ -2378,43 +2366,6 @@ impl<'tcx> ConstantKind<'tcx> {
23782366
}
23792367
}
23802368

2381-
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
2382-
#[inline]
2383-
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
2384-
self.try_eval_bits(tcx, param_env, ty)
2385-
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
2386-
}
2387-
2388-
#[inline]
2389-
pub fn try_eval_bits(
2390-
&self,
2391-
tcx: TyCtxt<'tcx>,
2392-
param_env: ty::ParamEnv<'tcx>,
2393-
ty: Ty<'tcx>,
2394-
) -> Option<u128> {
2395-
match self {
2396-
Self::Ty(ct) => ct.try_eval_bits(tcx, param_env, ty),
2397-
Self::Val(val, t) => {
2398-
assert_eq!(*t, ty);
2399-
let size =
2400-
tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
2401-
val.try_to_bits(size)
2402-
}
2403-
Self::Unevaluated(uneval, ty) => {
2404-
match tcx.const_eval_resolve(param_env, *uneval, None) {
2405-
Ok(val) => {
2406-
let size = tcx
2407-
.layout_of(param_env.with_reveal_all_normalized(tcx).and(*ty))
2408-
.ok()?
2409-
.size;
2410-
val.try_to_bits(size)
2411-
}
2412-
Err(_) => None,
2413-
}
2414-
}
2415-
}
2416-
}
2417-
24182369
#[inline]
24192370
pub fn try_eval_scalar(
24202371
self,
@@ -2434,8 +2385,23 @@ impl<'tcx> ConstantKind<'tcx> {
24342385
}
24352386

24362387
#[inline]
2437-
pub fn try_eval_bool(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<bool> {
2438-
self.try_eval_scalar_int(tcx, param_env)?.try_into().ok()
2388+
pub fn try_eval_bits(
2389+
&self,
2390+
tcx: TyCtxt<'tcx>,
2391+
param_env: ty::ParamEnv<'tcx>,
2392+
ty: Ty<'tcx>,
2393+
) -> Option<u128> {
2394+
let int = self.try_eval_scalar_int(tcx, param_env)?;
2395+
assert_eq!(self.ty(), ty);
2396+
let size = tcx.layout_of(param_env.with_reveal_all_normalized(tcx).and(ty)).ok()?.size;
2397+
int.to_bits(size).ok()
2398+
}
2399+
2400+
/// Panics if the value cannot be evaluated or doesn't contain a valid integer of the given type.
2401+
#[inline]
2402+
pub fn eval_bits(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>, ty: Ty<'tcx>) -> u128 {
2403+
self.try_eval_bits(tcx, param_env, ty)
2404+
.unwrap_or_else(|| bug!("expected bits of {:#?}, got {:#?}", ty, self))
24392405
}
24402406

24412407
#[inline]
@@ -2447,6 +2413,18 @@ impl<'tcx> ConstantKind<'tcx> {
24472413
self.try_eval_scalar_int(tcx, param_env)?.try_to_target_usize(tcx).ok()
24482414
}
24492415

2416+
#[inline]
2417+
/// Panics if the value cannot be evaluated or doesn't contain a valid `usize`.
2418+
pub fn eval_target_usize(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> u64 {
2419+
self.try_eval_target_usize(tcx, param_env)
2420+
.unwrap_or_else(|| bug!("expected usize, got {:#?}", self))
2421+
}
2422+
2423+
#[inline]
2424+
pub fn try_eval_bool(self, tcx: TyCtxt<'tcx>, param_env: ty::ParamEnv<'tcx>) -> Option<bool> {
2425+
self.try_eval_scalar_int(tcx, param_env)?.try_into().ok()
2426+
}
2427+
24502428
#[inline]
24512429
pub fn from_value(val: ConstValue<'tcx>, ty: Ty<'tcx>) -> Self {
24522430
Self::Val(val, ty)
@@ -2586,7 +2564,7 @@ impl<'tcx> ConstantKind<'tcx> {
25862564
}
25872565
}
25882566

2589-
pub fn from_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
2567+
pub fn from_ty_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
25902568
match c.kind() {
25912569
ty::ConstKind::Value(valtree) => {
25922570
let const_val = tcx.valtree_to_const_val((c.ty(), valtree));
@@ -2899,7 +2877,7 @@ fn pretty_print_const_value<'tcx>(
28992877
}
29002878
}
29012879
(ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => {
2902-
let n = n.try_to_bits(tcx.data_layout.pointer_size).unwrap();
2880+
let n = n.try_to_target_usize(tcx).unwrap();
29032881
// cast is ok because we already checked for pointer size (32 or 64 bit) above
29042882
let range = AllocRange { start: offset, size: Size::from_bytes(n) };
29052883
let byte_str = alloc.inner().get_bytes_strip_provenance(&tcx, range).unwrap();

compiler/rustc_middle/src/ty/consts.rs

+85-97
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ pub use int::*;
1616
pub use kind::*;
1717
use rustc_span::Span;
1818
use rustc_span::DUMMY_SP;
19-
use rustc_target::abi::Size;
2019
pub use valtree::*;
2120

2221
use 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 {

compiler/rustc_mir_build/src/thir/pattern/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -198,11 +198,11 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
198198
}
199199
(Some(PatKind::Constant { value: lo }), None) => {
200200
let hi = ty.numeric_max_val(self.tcx)?;
201-
Some((*lo, mir::ConstantKind::from_const(hi, self.tcx)))
201+
Some((*lo, mir::ConstantKind::from_ty_const(hi, self.tcx)))
202202
}
203203
(None, Some(PatKind::Constant { value: hi })) => {
204204
let lo = ty.numeric_min_val(self.tcx)?;
205-
Some((mir::ConstantKind::from_const(lo, self.tcx), *hi))
205+
Some((mir::ConstantKind::from_ty_const(lo, self.tcx), *hi))
206206
}
207207
_ => None,
208208
}

compiler/rustc_mir_transform/src/instsimplify.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ impl<'tcx> InstSimplifyContext<'tcx, '_> {
136136
return;
137137
}
138138

139-
let literal = ConstantKind::from_const(len, self.tcx);
139+
let literal = ConstantKind::from_ty_const(len, self.tcx);
140140
let constant = Constant { span: source_info.span, literal, user_ty: None };
141141
*rvalue = Rvalue::Use(Operand::Constant(Box::new(constant)));
142142
}

compiler/rustc_mir_transform/src/normalize_array_len.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ impl<'tcx> MutVisitor<'tcx> for Replacer<'tcx> {
9393
*rvalue = Rvalue::Use(Operand::Constant(Box::new(Constant {
9494
span: rustc_span::DUMMY_SP,
9595
user_ty: None,
96-
literal: ConstantKind::from_const(len, self.tcx),
96+
literal: ConstantKind::from_ty_const(len, self.tcx),
9797
})));
9898
}
9999
self.super_rvalue(rvalue, loc);

0 commit comments

Comments
 (0)