Remove Const::from_value

...it's just `mk_const` but without the sparcles
This commit is contained in:
Maybe Waffle 2022-11-28 12:38:15 +00:00
parent 26b87bf8ff
commit f4d00fe785
6 changed files with 9 additions and 19 deletions

View file

@ -1580,7 +1580,7 @@ impl<'tcx> InferCtxt<'tcx> {
span: Option<Span>, span: Option<Span>,
) -> Result<ty::Const<'tcx>, ErrorHandled> { ) -> Result<ty::Const<'tcx>, ErrorHandled> {
match self.const_eval_resolve(param_env, unevaluated, span) { match self.const_eval_resolve(param_env, unevaluated, span) {
Ok(Some(val)) => Ok(ty::Const::from_value(self.tcx, val, ty)), Ok(Some(val)) => Ok(self.tcx.mk_const(val, ty)),
Ok(None) => { Ok(None) => {
let tcx = self.tcx; let tcx = self.tcx;
let def_id = unevaluated.def.did; let def_id = unevaluated.def.did;

View file

@ -140,12 +140,6 @@ impl<'tcx> Const<'tcx> {
} }
} }
/// Interns the given value as a constant.
#[inline]
pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self {
tcx.mk_const(val, ty)
}
/// Panics if self.kind != ty::ConstKind::Value /// Panics if self.kind != ty::ConstKind::Value
pub fn to_valtree(self) -> ty::ValTree<'tcx> { pub fn to_valtree(self) -> ty::ValTree<'tcx> {
match self.kind() { match self.kind() {
@ -156,7 +150,7 @@ impl<'tcx> Const<'tcx> {
pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt, ty: Ty<'tcx>) -> Self { pub fn from_scalar_int(tcx: TyCtxt<'tcx>, i: ScalarInt, ty: Ty<'tcx>) -> Self {
let valtree = ty::ValTree::from_scalar_int(i); let valtree = ty::ValTree::from_scalar_int(i);
Self::from_value(tcx, valtree, ty) tcx.mk_const(valtree, ty)
} }
#[inline] #[inline]
@ -172,8 +166,7 @@ impl<'tcx> Const<'tcx> {
#[inline] #[inline]
/// Creates an interned zst constant. /// Creates an interned zst constant.
pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self { pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
let valtree = ty::ValTree::zst(); tcx.mk_const(ty::ValTree::zst(), ty)
Self::from_value(tcx, valtree, ty)
} }
#[inline] #[inline]
@ -220,7 +213,7 @@ impl<'tcx> Const<'tcx> {
pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Const<'tcx> { pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Const<'tcx> {
if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) { if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) {
match val { match val {
Ok(val) => Const::from_value(tcx, val, self.ty()), Ok(val) => tcx.mk_const(val, self.ty()),
Err(guar) => tcx.const_error_with_guaranteed(self.ty(), guar), Err(guar) => tcx.const_error_with_guaranteed(self.ty(), guar),
} }
} else { } else {

View file

@ -1468,8 +1468,7 @@ pub trait PrettyPrinter<'tcx>:
} }
// Aggregates, printed as array/tuple/struct/variant construction syntax. // Aggregates, printed as array/tuple/struct/variant construction syntax.
(ty::ValTree::Branch(_), ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) => { (ty::ValTree::Branch(_), ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) => {
let contents = let contents = self.tcx().destructure_const(self.tcx().mk_const(valtree, ty));
self.tcx().destructure_const(ty::Const::from_value(self.tcx(), valtree, ty));
let fields = contents.fields.iter().copied(); let fields = contents.fields.iter().copied();
match *ty.kind() { match *ty.kind() {
ty::Array(..) => { ty::Array(..) => {

View file

@ -61,5 +61,5 @@ pub(crate) fn lit_to_const<'tcx>(
_ => return Err(LitToConstError::TypeError), _ => return Err(LitToConstError::TypeError),
}; };
Ok(ty::Const::from_value(tcx, valtree, ty)) Ok(tcx.mk_const(valtree, ty))
} }

View file

@ -799,9 +799,7 @@ impl<'tcx> AutoTraitFinder<'tcx> {
unevaluated, unevaluated,
Some(obligation.cause.span), Some(obligation.cause.span),
) { ) {
Ok(Some(valtree)) => { Ok(Some(valtree)) => Ok(selcx.tcx().mk_const(valtree, c.ty())),
Ok(ty::Const::from_value(selcx.tcx(), valtree, c.ty()))
}
Ok(None) => { Ok(None) => {
let tcx = self.tcx; let tcx = self.tcx;
let def_id = unevaluated.def.did; let def_id = unevaluated.def.did;

View file

@ -125,11 +125,11 @@ fn recurse_build<'tcx>(
} }
&ExprKind::NonHirLiteral { lit, user_ty: _ } => { &ExprKind::NonHirLiteral { lit, user_ty: _ } => {
let val = ty::ValTree::from_scalar_int(lit); let val = ty::ValTree::from_scalar_int(lit);
ty::Const::from_value(tcx, val, node.ty) tcx.mk_const(val, node.ty)
} }
&ExprKind::ZstLiteral { user_ty: _ } => { &ExprKind::ZstLiteral { user_ty: _ } => {
let val = ty::ValTree::zst(); let val = ty::ValTree::zst();
ty::Const::from_value(tcx, val, node.ty) tcx.mk_const(val, node.ty)
} }
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => { &ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
let uneval = ty::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); let uneval = ty::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs);