Skip to content

Commit 774a029

Browse files
committed
Code review changes.
1 parent 821f4a9 commit 774a029

File tree

11 files changed

+35
-34
lines changed

11 files changed

+35
-34
lines changed

src/librustc/mir/interpret/value.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ impl<'tcx> ConstValue<'tcx> {
8989
ConstValue::Scalar(Scalar::from_u64(i))
9090
}
9191

92-
pub fn from_machine_usize(cx: &impl HasDataLayout, i: u64) -> Self {
93-
ConstValue::Scalar(Scalar::from_machine_usize(cx, i))
92+
pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
93+
ConstValue::Scalar(Scalar::from_machine_usize(i, cx))
9494
}
9595
}
9696

@@ -314,7 +314,7 @@ impl<'tcx, Tag> Scalar<Tag> {
314314
}
315315

316316
#[inline]
317-
pub fn from_machine_usize(cx: &impl HasDataLayout, i: u64) -> Self {
317+
pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
318318
Self::from_uint(i, cx.data_layout().pointer_size)
319319
}
320320

@@ -337,6 +337,11 @@ impl<'tcx, Tag> Scalar<Tag> {
337337
.unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
338338
}
339339

340+
#[inline]
341+
pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self {
342+
Self::from_int(i, cx.data_layout().pointer_size)
343+
}
344+
340345
#[inline]
341346
pub fn from_f32(f: Single) -> Self {
342347
// We trust apfloat to give us properly truncated data.

src/librustc/ty/sty.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2415,9 +2415,14 @@ pub struct Const<'tcx> {
24152415
static_assert_size!(Const<'_>, 48);
24162416

24172417
impl<'tcx> Const<'tcx> {
2418+
#[inline]
2419+
pub fn from_value(tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>, ty: Ty<'tcx>) -> &'tcx Self {
2420+
tcx.mk_const(Self { val: ConstKind::Value(val), ty })
2421+
}
2422+
24182423
#[inline]
24192424
pub fn from_scalar(tcx: TyCtxt<'tcx>, val: Scalar, ty: Ty<'tcx>) -> &'tcx Self {
2420-
tcx.mk_const(Self { val: ConstKind::Value(ConstValue::Scalar(val)), ty })
2425+
Self::from_value(tcx, ConstValue::Scalar(val), ty)
24212426
}
24222427

24232428
#[inline]
@@ -2473,7 +2478,7 @@ impl<'tcx> Const<'tcx> {
24732478
// evaluate the const.
24742479
tcx.const_eval_resolve(param_env, did, substs, promoted, None)
24752480
.ok()
2476-
.map(|val| tcx.mk_const(Const { val: ConstKind::Value(val), ty: self.ty }))
2481+
.map(|val| Const::from_value(tcx, val, self.ty))
24772482
};
24782483

24792484
match self.val {

src/librustc_codegen_ssa/mir/constant.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
6262
if let ty::ConstKind::Value(value) = const_.val {
6363
Ok(value)
6464
} else {
65-
bug!("encountered bad ConstKind in codegen");
65+
span_bug!(constant.span, "encountered bad ConstKind in codegen: {:?}", const_);
6666
}
6767
}
6868
}
@@ -83,7 +83,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
8383
ty::Array(_, n) => n.eval_usize(bx.tcx(), ty::ParamEnv::reveal_all()),
8484
_ => bug!("invalid simd shuffle type: {}", ty),
8585
};
86-
let c = bx.tcx().mk_const(ty::Const { val: ty::ConstKind::Value(val), ty });
86+
let c = ty::Const::from_value(bx.tcx(), val, ty);
8787
let values: Vec<_> = (0..fields)
8888
.map(|field| {
8989
let field = bx.tcx().const_field(

src/librustc_mir/const_eval.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ pub(crate) fn destructure_const<'tcx>(
7979
let fields_iter = (0..field_count).map(|i| {
8080
let field_op = ecx.operand_field(down, i).unwrap();
8181
let val = op_to_const(&ecx, field_op);
82-
tcx.mk_const(ty::Const { val: ty::ConstKind::Value(val), ty: field_op.layout.ty })
82+
ty::Const::from_value(tcx, val, field_op.layout.ty)
8383
});
8484
let fields = tcx.arena.alloc_from_iter(fields_iter);
8585

src/librustc_mir/interpret/intrinsics.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ crate fn eval_nullary_intrinsic<'tcx>(
6565
sym::size_of => layout.size.bytes(),
6666
_ => bug!(),
6767
};
68-
ConstValue::from_machine_usize(&tcx, n)
68+
ConstValue::from_machine_usize(n, &tcx)
6969
}
7070
sym::type_id => ConstValue::from_u64(tcx.type_id_hash(tp_ty).into()),
7171
other => bug!("`{}` is not a zero arg intrinsic", other),

src/librustc_mir_build/hair/constant.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ crate fn lit_to_const<'tcx>(
6565
ast::LitKind::Char(c) => ConstValue::Scalar(Scalar::from_char(c)),
6666
ast::LitKind::Err(_) => return Err(LitToConstError::Reported),
6767
};
68-
Ok(tcx.mk_const(ty::Const { val: ty::ConstKind::Value(lit), ty }))
68+
Ok(ty::Const::from_value(tcx, lit, ty))
6969
}
7070

7171
fn parse_float<'tcx>(num: Symbol, fty: ast::FloatTy, neg: bool) -> Result<ConstValue<'tcx>, ()> {

src/librustc_mir_build/hair/pattern/_match.rs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,12 +343,11 @@ impl<'tcx> PatternFolder<'tcx> for LiteralExpander<'tcx> {
343343
ty: rty,
344344
span: pat.span,
345345
kind: box PatKind::Constant {
346-
value: self.tcx.mk_const(Const {
347-
val: ty::ConstKind::Value(
348-
self.fold_const_value_deref(*val, rty, crty),
349-
),
350-
ty: rty,
351-
}),
346+
value: Const::from_value(
347+
self.tcx,
348+
self.fold_const_value_deref(*val, rty, crty),
349+
rty,
350+
),
352351
},
353352
},
354353
},

src/librustc_mir_build/hair/pattern/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -769,10 +769,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
769769
Some(span),
770770
) {
771771
Ok(value) => {
772-
let const_ = self.tcx.mk_const(ty::Const {
773-
val: ty::ConstKind::Value(value),
774-
ty: self.tables.node_type(id),
775-
});
772+
let const_ =
773+
ty::Const::from_value(self.tcx, value, self.tables.node_type(id));
776774

777775
let pattern = self.const_to_pat(&const_, id, span);
778776
if !is_associated_const {

src/librustc_typeck/check/expr.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ use rustc::ty;
2525
use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
2626
use rustc::ty::Ty;
2727
use rustc::ty::TypeFoldable;
28-
use rustc::ty::{AdtKind, ConstKind, Visibility};
28+
use rustc::ty::{AdtKind, Visibility};
2929
use rustc_data_structures::fx::FxHashMap;
3030
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, DiagnosticId};
3131
use rustc_hir as hir;
@@ -1011,12 +1011,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
10111011
let count = if self.const_param_def_id(count).is_some() {
10121012
Ok(self.to_const(count, tcx.type_of(count_def_id)))
10131013
} else {
1014-
tcx.const_eval_poly(count_def_id).map(|val| {
1015-
tcx.mk_const(ty::Const {
1016-
val: ConstKind::Value(val),
1017-
ty: tcx.type_of(count_def_id),
1018-
})
1019-
})
1014+
tcx.const_eval_poly(count_def_id)
1015+
.map(|val| ty::Const::from_value(tcx, val, tcx.type_of(count_def_id)))
10201016
};
10211017

10221018
let uty = match expected {

src/librustdoc/clean/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,9 +1333,7 @@ impl Clean<Type> for hir::Ty<'_> {
13331333
let def_id = cx.tcx.hir().local_def_id(length.hir_id);
13341334
let length = match cx.tcx.const_eval_poly(def_id) {
13351335
Ok(length) => {
1336-
let const_ =
1337-
ty::Const { val: ty::ConstKind::Value(length), ty: cx.tcx.types.usize };
1338-
print_const(cx, &const_)
1336+
print_const(cx, ty::Const::from_value(cx.tcx, length, cx.tcx.types.usize))
13391337
}
13401338
Err(_) => cx
13411339
.sess()

src/librustdoc/clean/utils.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ pub fn name_from_pat(p: &hir::Pat) -> String {
457457
}
458458
}
459459

460-
pub fn print_const(cx: &DocContext<'_>, n: &ty::Const<'_>) -> String {
460+
pub fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String {
461461
match n.val {
462462
ty::ConstKind::Unevaluated(def_id, _, promoted) => {
463463
let mut s = if let Some(hir_id) = cx.tcx.hir().as_local_hir_id(def_id) {
@@ -493,8 +493,8 @@ pub fn print_evaluated_const(cx: &DocContext<'_>, def_id: DefId) -> Option<Strin
493493
(_, &ty::Ref(..)) => None,
494494
(ConstValue::Scalar(_), &ty::Adt(_, _)) => None,
495495
(ConstValue::Scalar(_), _) => {
496-
let const_ = ty::Const { val: ty::ConstKind::Value(val), ty };
497-
Some(print_const_with_custom_print_scalar(cx, &const_))
496+
let const_ = ty::Const::from_value(cx.tcx, val, ty);
497+
Some(print_const_with_custom_print_scalar(cx, const_))
498498
}
499499
_ => None,
500500
}
@@ -513,7 +513,7 @@ fn format_integer_with_underscore_sep(num: &str) -> String {
513513
.collect()
514514
}
515515

516-
fn print_const_with_custom_print_scalar(cx: &DocContext<'_>, ct: &ty::Const<'tcx>) -> String {
516+
fn print_const_with_custom_print_scalar(cx: &DocContext<'_>, ct: &'tcx ty::Const<'tcx>) -> String {
517517
// Use a slightly different format for integer types which always shows the actual value.
518518
// For all other types, fallback to the original `pretty_print_const`.
519519
match (ct.val, &ct.ty.kind) {

0 commit comments

Comments
 (0)