Skip to content

Commit d10c1b5

Browse files
committed
clean up some ty::Const methods
1 parent 9da2625 commit d10c1b5

File tree

7 files changed

+23
-44
lines changed

7 files changed

+23
-44
lines changed

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2594,7 +2594,7 @@ impl<'tcx> ConstantKind<'tcx> {
25942594
}
25952595
}
25962596

2597-
pub fn from_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
2597+
pub fn from_ty_const(c: ty::Const<'tcx>, tcx: TyCtxt<'tcx>) -> Self {
25982598
match c.kind() {
25992599
ty::ConstKind::Value(valtree) => {
26002600
let const_val = tcx.valtree_to_const_val((c.ty(), valtree));
@@ -2907,7 +2907,7 @@ fn pretty_print_const_value<'tcx>(
29072907
}
29082908
}
29092909
(ConstValue::ByRef { alloc, offset }, ty::Array(t, n)) if *t == u8_type => {
2910-
let n = n.try_to_bits(tcx.data_layout.pointer_size).unwrap();
2910+
let n = n.try_to_target_usize(tcx).unwrap();
29112911
// cast is ok because we already checked for pointer size (32 or 64 bit) above
29122912
let range = AllocRange { start: offset, size: Size::from_bytes(n) };
29132913
let byte_str = alloc.inner().get_bytes_strip_provenance(&tcx, range).unwrap();

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 15 additions & 36 deletions
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;
@@ -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,14 +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-
295278
#[inline]
296279
/// Attempts to evaluate the given constant to bits. Can fail to evaluate in the presence of
297280
/// generics (or erroneous code) or if the value can't be represented as bits (e.g. because it
@@ -410,34 +393,30 @@ impl<'tcx> Const<'tcx> {
410393
}
411394
}
412395

413-
#[inline]
414-
pub fn try_to_value(self) -> Option<ty::ValTree<'tcx>> {
415-
if let ConstKind::Value(val) = self.kind() { Some(val) } else { None }
416-
}
417-
418-
#[inline]
419-
pub fn try_to_scalar(self) -> Option<Scalar<AllocId>> {
420-
self.try_to_value()?.try_to_scalar()
421-
}
422-
423-
#[inline]
424-
pub fn try_to_scalar_int(self) -> Option<ScalarInt> {
425-
self.try_to_value()?.try_to_scalar_int()
396+
/// Panics if self.kind != ty::ConstKind::Value
397+
pub fn to_valtree(self) -> ty::ValTree<'tcx> {
398+
match self.kind() {
399+
ty::ConstKind::Value(valtree) => valtree,
400+
_ => bug!("expected ConstKind::Value, got {:?}", self.kind()),
401+
}
426402
}
427403

428-
#[inline]
429-
pub fn try_to_bits(self, size: Size) -> Option<u128> {
430-
self.try_to_scalar_int()?.to_bits(size).ok()
404+
/// Attempts to convert to a `ValTree`
405+
pub fn try_to_valtree(self) -> Option<ty::ValTree<'tcx>> {
406+
match self.kind() {
407+
ty::ConstKind::Value(valtree) => Some(valtree),
408+
_ => None,
409+
}
431410
}
432411

433412
#[inline]
434-
pub fn try_to_bool(self) -> Option<bool> {
435-
self.try_to_scalar_int()?.try_into().ok()
413+
pub fn try_to_scalar(self) -> Option<Scalar<AllocId>> {
414+
self.try_to_valtree()?.try_to_scalar()
436415
}
437416

438417
#[inline]
439418
pub fn try_to_target_usize(self, tcx: TyCtxt<'tcx>) -> Option<u64> {
440-
self.try_to_value()?.try_to_target_usize(tcx)
419+
self.try_to_valtree()?.try_to_target_usize(tcx)
441420
}
442421

443422
pub fn is_ct_infer(self) -> bool {

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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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);

compiler/rustc_symbol_mangling/src/legacy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolPrinter<'tcx> {
230230
self.write_str("[")?;
231231
self = self.print_type(ty)?;
232232
self.write_str("; ")?;
233-
if let Some(size) = size.try_to_bits(self.tcx().data_layout.pointer_size) {
233+
if let Some(size) = size.try_to_target_usize(self.tcx()) {
234234
write!(self, "{size}")?
235235
} else if let ty::ConstKind::Param(param) = size.kind() {
236236
self = param.print(self)?

compiler/rustc_trait_selection/src/traits/error_reporting/on_unimplemented.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -257,7 +257,7 @@ impl<'tcx> TypeErrCtxtExt<'tcx> for TypeErrCtxt<'_, 'tcx> {
257257
// Arrays give us `[]`, `[{ty}; _]` and `[{ty}; N]`
258258
if let ty::Array(aty, len) = self_ty.kind() {
259259
flags.push((sym::_Self, Some("[]".to_string())));
260-
let len = len.try_to_value().and_then(|v| v.try_to_target_usize(self.tcx));
260+
let len = len.try_to_valtree().and_then(|v| v.try_to_target_usize(self.tcx));
261261
flags.push((sym::_Self, Some(format!("[{aty}; _]"))));
262262
if let Some(n) = len {
263263
flags.push((sym::_Self, Some(format!("[{aty}; {n}]"))));

0 commit comments

Comments
 (0)