Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change const eval to just return the value #69181

Merged
merged 6 commits into from
Feb 18, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions src/librustc/mir/interpret/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

Expand Down Expand Up @@ -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)
}

Expand All @@ -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.
Expand Down
9 changes: 7 additions & 2 deletions src/librustc/ty/sty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/mir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}
}
}
Expand All @@ -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(
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/interpret/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir_build/hair/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>, ()> {
Expand Down
11 changes: 5 additions & 6 deletions src/librustc_mir_build/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
),
},
},
},
Expand Down
6 changes: 2 additions & 4 deletions src/librustc_mir_build/hair/pattern/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 3 additions & 7 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down
4 changes: 1 addition & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions src/librustdoc/clean/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 };
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you can keep the original, as the value isbnot required to be interned

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it actually needs to be interned to be printed, I get the 'could not lift for printing' otherwise.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah indeed

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,
}
Expand All @@ -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) {
Expand Down