Code review changes.

This commit is contained in:
Ben Lewis 2020-02-16 09:59:01 +13:00
parent 821f4a9dfb
commit 774a029e96
11 changed files with 35 additions and 34 deletions

View File

@ -89,8 +89,8 @@ impl<'tcx> ConstValue<'tcx> {
ConstValue::Scalar(Scalar::from_u64(i))
}
pub fn from_machine_usize(cx: &impl HasDataLayout, i: u64) -> Self {
ConstValue::Scalar(Scalar::from_machine_usize(cx, i))
pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
ConstValue::Scalar(Scalar::from_machine_usize(i, cx))
}
}
@ -314,7 +314,7 @@ impl<'tcx, Tag> Scalar<Tag> {
}
#[inline]
pub fn from_machine_usize(cx: &impl HasDataLayout, i: u64) -> Self {
pub fn from_machine_usize(i: u64, cx: &impl HasDataLayout) -> Self {
Self::from_uint(i, cx.data_layout().pointer_size)
}
@ -337,6 +337,11 @@ impl<'tcx, Tag> Scalar<Tag> {
.unwrap_or_else(|| bug!("Signed value {:#x} does not fit in {} bits", i, size.bits()))
}
#[inline]
pub fn from_machine_isize(i: i64, cx: &impl HasDataLayout) -> Self {
Self::from_int(i, cx.data_layout().pointer_size)
}
#[inline]
pub fn from_f32(f: Single) -> Self {
// We trust apfloat to give us properly truncated data.

View File

@ -2415,9 +2415,14 @@ pub struct Const<'tcx> {
static_assert_size!(Const<'_>, 48);
impl<'tcx> Const<'tcx> {
#[inline]
pub fn from_value(tcx: TyCtxt<'tcx>, val: ConstValue<'tcx>, ty: Ty<'tcx>) -> &'tcx Self {
tcx.mk_const(Self { val: ConstKind::Value(val), ty })
}
#[inline]
pub fn from_scalar(tcx: TyCtxt<'tcx>, val: Scalar, ty: Ty<'tcx>) -> &'tcx Self {
tcx.mk_const(Self { val: ConstKind::Value(ConstValue::Scalar(val)), ty })
Self::from_value(tcx, ConstValue::Scalar(val), ty)
}
#[inline]
@ -2473,7 +2478,7 @@ impl<'tcx> Const<'tcx> {
// evaluate the const.
tcx.const_eval_resolve(param_env, did, substs, promoted, None)
.ok()
.map(|val| tcx.mk_const(Const { val: ConstKind::Value(val), ty: self.ty }))
.map(|val| Const::from_value(tcx, val, self.ty))
};
match self.val {

View File

@ -62,7 +62,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
if let ty::ConstKind::Value(value) = const_.val {
Ok(value)
} else {
bug!("encountered bad ConstKind in codegen");
span_bug!(constant.span, "encountered bad ConstKind in codegen: {:?}", const_);
}
}
}
@ -83,7 +83,7 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
ty::Array(_, n) => n.eval_usize(bx.tcx(), ty::ParamEnv::reveal_all()),
_ => bug!("invalid simd shuffle type: {}", ty),
};
let c = bx.tcx().mk_const(ty::Const { val: ty::ConstKind::Value(val), ty });
let c = ty::Const::from_value(bx.tcx(), val, ty);
let values: Vec<_> = (0..fields)
.map(|field| {
let field = bx.tcx().const_field(

View File

@ -79,7 +79,7 @@ pub(crate) fn destructure_const<'tcx>(
let fields_iter = (0..field_count).map(|i| {
let field_op = ecx.operand_field(down, i).unwrap();
let val = op_to_const(&ecx, field_op);
tcx.mk_const(ty::Const { val: ty::ConstKind::Value(val), ty: field_op.layout.ty })
ty::Const::from_value(tcx, val, field_op.layout.ty)
});
let fields = tcx.arena.alloc_from_iter(fields_iter);

View File

@ -65,7 +65,7 @@ crate fn eval_nullary_intrinsic<'tcx>(
sym::size_of => layout.size.bytes(),
_ => bug!(),
};
ConstValue::from_machine_usize(&tcx, n)
ConstValue::from_machine_usize(n, &tcx)
}
sym::type_id => ConstValue::from_u64(tcx.type_id_hash(tp_ty).into()),
other => bug!("`{}` is not a zero arg intrinsic", other),

View File

@ -65,7 +65,7 @@ crate fn lit_to_const<'tcx>(
ast::LitKind::Char(c) => ConstValue::Scalar(Scalar::from_char(c)),
ast::LitKind::Err(_) => return Err(LitToConstError::Reported),
};
Ok(tcx.mk_const(ty::Const { val: ty::ConstKind::Value(lit), ty }))
Ok(ty::Const::from_value(tcx, lit, ty))
}
fn parse_float<'tcx>(num: Symbol, fty: ast::FloatTy, neg: bool) -> Result<ConstValue<'tcx>, ()> {

View File

@ -343,12 +343,11 @@ impl<'tcx> PatternFolder<'tcx> for LiteralExpander<'tcx> {
ty: rty,
span: pat.span,
kind: box PatKind::Constant {
value: self.tcx.mk_const(Const {
val: ty::ConstKind::Value(
self.fold_const_value_deref(*val, rty, crty),
),
ty: rty,
}),
value: Const::from_value(
self.tcx,
self.fold_const_value_deref(*val, rty, crty),
rty,
),
},
},
},

View File

@ -769,10 +769,8 @@ impl<'a, 'tcx> PatCtxt<'a, 'tcx> {
Some(span),
) {
Ok(value) => {
let const_ = self.tcx.mk_const(ty::Const {
val: ty::ConstKind::Value(value),
ty: self.tables.node_type(id),
});
let const_ =
ty::Const::from_value(self.tcx, value, self.tables.node_type(id));
let pattern = self.const_to_pat(&const_, id, span);
if !is_associated_const {

View File

@ -25,7 +25,7 @@ use rustc::ty;
use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
use rustc::ty::Ty;
use rustc::ty::TypeFoldable;
use rustc::ty::{AdtKind, ConstKind, Visibility};
use rustc::ty::{AdtKind, Visibility};
use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder, DiagnosticId};
use rustc_hir as hir;
@ -1011,12 +1011,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
let count = if self.const_param_def_id(count).is_some() {
Ok(self.to_const(count, tcx.type_of(count_def_id)))
} else {
tcx.const_eval_poly(count_def_id).map(|val| {
tcx.mk_const(ty::Const {
val: ConstKind::Value(val),
ty: tcx.type_of(count_def_id),
})
})
tcx.const_eval_poly(count_def_id)
.map(|val| ty::Const::from_value(tcx, val, tcx.type_of(count_def_id)))
};
let uty = match expected {

View File

@ -1333,9 +1333,7 @@ impl Clean<Type> for hir::Ty<'_> {
let def_id = cx.tcx.hir().local_def_id(length.hir_id);
let length = match cx.tcx.const_eval_poly(def_id) {
Ok(length) => {
let const_ =
ty::Const { val: ty::ConstKind::Value(length), ty: cx.tcx.types.usize };
print_const(cx, &const_)
print_const(cx, ty::Const::from_value(cx.tcx, length, cx.tcx.types.usize))
}
Err(_) => cx
.sess()

View File

@ -457,7 +457,7 @@ pub fn name_from_pat(p: &hir::Pat) -> String {
}
}
pub fn print_const(cx: &DocContext<'_>, n: &ty::Const<'_>) -> String {
pub fn print_const(cx: &DocContext<'_>, n: &'tcx ty::Const<'_>) -> String {
match n.val {
ty::ConstKind::Unevaluated(def_id, _, promoted) => {
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
(_, &ty::Ref(..)) => None,
(ConstValue::Scalar(_), &ty::Adt(_, _)) => None,
(ConstValue::Scalar(_), _) => {
let const_ = ty::Const { val: ty::ConstKind::Value(val), ty };
Some(print_const_with_custom_print_scalar(cx, &const_))
let const_ = ty::Const::from_value(cx.tcx, val, ty);
Some(print_const_with_custom_print_scalar(cx, const_))
}
_ => None,
}
@ -513,7 +513,7 @@ fn format_integer_with_underscore_sep(num: &str) -> String {
.collect()
}
fn print_const_with_custom_print_scalar(cx: &DocContext<'_>, ct: &ty::Const<'tcx>) -> String {
fn print_const_with_custom_print_scalar(cx: &DocContext<'_>, ct: &'tcx ty::Const<'tcx>) -> String {
// Use a slightly different format for integer types which always shows the actual value.
// For all other types, fallback to the original `pretty_print_const`.
match (ct.val, &ct.ty.kind) {