From 26b87bf8ff8406e80e70559b71cf0095475cc64a Mon Sep 17 00:00:00 2001 From: Maybe Waffle Date: Mon, 28 Nov 2022 12:28:32 +0000 Subject: [PATCH] Simplify calls to `tcx.mk_const` `mk_const(ty::ConstKind::X(...), ty)` can now be simplified to `mk_cosnt(..., ty)`. I searched with the following regex: \mk_const\([\n\s]*(ty::)?ConstKind\ I've left `ty::ConstKind::{Bound, Error}` as-is, they seem clearer this way. --- .../rustc_infer/src/infer/canonical/mod.rs | 2 +- compiler/rustc_infer/src/infer/combine.rs | 10 ++----- .../src/infer/higher_ranked/mod.rs | 9 ++---- compiler/rustc_infer/src/infer/mod.rs | 4 +-- compiler/rustc_middle/src/mir/mod.rs | 3 +- compiler/rustc_middle/src/ty/consts.rs | 8 +++--- compiler/rustc_middle/src/ty/relate.rs | 5 +--- .../src/build/expr/as_constant.rs | 2 +- .../src/traits/project.rs | 2 +- compiler/rustc_ty_utils/src/consts.rs | 28 ++++++++----------- 10 files changed, 27 insertions(+), 46 deletions(-) diff --git a/compiler/rustc_infer/src/infer/canonical/mod.rs b/compiler/rustc_infer/src/infer/canonical/mod.rs index 0794792d8cb3..ba0ce16bb81a 100644 --- a/compiler/rustc_infer/src/infer/canonical/mod.rs +++ b/compiler/rustc_infer/src/infer/canonical/mod.rs @@ -147,7 +147,7 @@ impl<'tcx> InferCtxt<'tcx> { CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, name }, ty) => { let universe_mapped = universe_map(universe); let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, name }; - self.tcx.mk_const(ty::ConstKind::Placeholder(placeholder_mapped), ty).into() + self.tcx.mk_const(placeholder_mapped, ty).into() } } } diff --git a/compiler/rustc_infer/src/infer/combine.rs b/compiler/rustc_infer/src/infer/combine.rs index ec2017b3d895..cf895ed0d3e5 100644 --- a/compiler/rustc_infer/src/infer/combine.rs +++ b/compiler/rustc_infer/src/infer/combine.rs @@ -765,10 +765,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> { substs, substs, )?; - Ok(self.tcx().mk_const( - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }), - c.ty(), - )) + Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty())) } _ => relate::super_relate_consts(self, c, c), } @@ -988,10 +985,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> { substs, )?; - Ok(self.tcx().mk_const( - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }), - c.ty(), - )) + Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty())) } _ => relate::super_relate_consts(self, c, c), } diff --git a/compiler/rustc_infer/src/infer/higher_ranked/mod.rs b/compiler/rustc_infer/src/infer/higher_ranked/mod.rs index d739323de77c..817ae10c7608 100644 --- a/compiler/rustc_infer/src/infer/higher_ranked/mod.rs +++ b/compiler/rustc_infer/src/infer/higher_ranked/mod.rs @@ -94,13 +94,8 @@ impl<'tcx> InferCtxt<'tcx> { })) }, consts: &mut |bound_var: ty::BoundVar, ty| { - self.tcx.mk_const( - ty::ConstKind::Placeholder(ty::PlaceholderConst { - universe: next_universe, - name: bound_var, - }), - ty, - ) + self.tcx + .mk_const(ty::PlaceholderConst { universe: next_universe, name: bound_var }, ty) }, }; diff --git a/compiler/rustc_infer/src/infer/mod.rs b/compiler/rustc_infer/src/infer/mod.rs index df00ed0cb9cc..c5de1d6d78af 100644 --- a/compiler/rustc_infer/src/infer/mod.rs +++ b/compiler/rustc_infer/src/infer/mod.rs @@ -2049,10 +2049,10 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>( bug!("const `{ct}`'s type should not reference params or types"); } tcx.mk_const( - ty::ConstKind::Placeholder(ty::PlaceholderConst { + ty::PlaceholderConst { universe: ty::UniverseIndex::ROOT, name: ty::BoundVar::from_usize(idx), - }), + }, ty, ) .into() diff --git a/compiler/rustc_middle/src/mir/mod.rs b/compiler/rustc_middle/src/mir/mod.rs index 364c1b375ae5..20dde64e51b0 100644 --- a/compiler/rustc_middle/src/mir/mod.rs +++ b/compiler/rustc_middle/src/mir/mod.rs @@ -2527,8 +2527,7 @@ impl<'tcx> ConstantKind<'tcx> { let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id]; let name = tcx.item_name(def_id); - let ty_const = - tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty); + let ty_const = tcx.mk_const(ty::ParamConst::new(index, name), ty); debug!(?ty_const); return Self::Ty(ty_const); diff --git a/compiler/rustc_middle/src/ty/consts.rs b/compiler/rustc_middle/src/ty/consts.rs index 9a58a196ed72..cd0b280ce4e4 100644 --- a/compiler/rustc_middle/src/ty/consts.rs +++ b/compiler/rustc_middle/src/ty/consts.rs @@ -76,10 +76,10 @@ impl<'tcx> Const<'tcx> { match Self::try_eval_lit_or_param(tcx, ty, expr) { Some(v) => v, None => tcx.mk_const( - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { + ty::UnevaluatedConst { def: def.to_global(), substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()), - }), + }, ty, ), } @@ -134,7 +134,7 @@ impl<'tcx> Const<'tcx> { let generics = tcx.generics_of(item_def_id); let index = generics.param_def_id_to_index[&def_id]; let name = tcx.item_name(def_id); - Some(tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty)) + Some(tcx.mk_const(ty::ParamConst::new(index, name), ty)) } _ => None, } @@ -143,7 +143,7 @@ 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(ConstKind::Value(val), ty) + tcx.mk_const(val, ty) } /// Panics if self.kind != ty::ConstKind::Value diff --git a/compiler/rustc_middle/src/ty/relate.rs b/compiler/rustc_middle/src/ty/relate.rs index e6340040e9c1..c759fb6d5e4f 100644 --- a/compiler/rustc_middle/src/ty/relate.rs +++ b/compiler/rustc_middle/src/ty/relate.rs @@ -663,10 +663,7 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>( au.substs, bu.substs, )?; - return Ok(tcx.mk_const( - ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def: au.def, substs }), - a.ty(), - )); + return Ok(tcx.mk_const(ty::UnevaluatedConst { def: au.def, substs }, a.ty())); } // Before calling relate on exprs, it is necessary to ensure that the nested consts // have identical types. diff --git a/compiler/rustc_mir_build/src/build/expr/as_constant.rs b/compiler/rustc_mir_build/src/build/expr/as_constant.rs index 7d8a940bde5c..32c0207cb680 100644 --- a/compiler/rustc_mir_build/src/build/expr/as_constant.rs +++ b/compiler/rustc_mir_build/src/build/expr/as_constant.rs @@ -77,7 +77,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> { Constant { user_ty, span, literal } } ExprKind::ConstParam { param, def_id: _ } => { - let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty); + let const_param = tcx.mk_const(param, expr.ty); let literal = ConstantKind::Ty(const_param); Constant { user_ty: None, span, literal } diff --git a/compiler/rustc_trait_selection/src/traits/project.rs b/compiler/rustc_trait_selection/src/traits/project.rs index 18f4379d8f89..e33e89e9c5c3 100644 --- a/compiler/rustc_trait_selection/src/traits/project.rs +++ b/compiler/rustc_trait_selection/src/traits/project.rs @@ -818,7 +818,7 @@ impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> { let universe = self.universe_for(debruijn); let p = ty::PlaceholderConst { universe, name: bound_const }; self.mapped_consts.insert(p, bound_const); - self.infcx.tcx.mk_const(ty::ConstKind::Placeholder(p), ct.ty()) + self.infcx.tcx.mk_const(p, ct.ty()) } _ => ct.super_fold_with(self), } diff --git a/compiler/rustc_ty_utils/src/consts.rs b/compiler/rustc_ty_utils/src/consts.rs index 2b7018bc9c30..77cb22434826 100644 --- a/compiler/rustc_ty_utils/src/consts.rs +++ b/compiler/rustc_ty_utils/src/consts.rs @@ -5,7 +5,7 @@ use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput}; use rustc_middle::thir::visit; use rustc_middle::thir::visit::Visitor; use rustc_middle::ty::abstract_const::CastKind; -use rustc_middle::ty::{self, ConstKind, Expr, TyCtxt, TypeVisitable}; +use rustc_middle::ty::{self, Expr, TyCtxt, TypeVisitable}; use rustc_middle::{mir, thir}; use rustc_span::Span; use rustc_target::abi::VariantIdx; @@ -32,10 +32,8 @@ pub(crate) fn destructure_const<'tcx>( let (fields, variant) = match const_.ty().kind() { ty::Array(inner_ty, _) | ty::Slice(inner_ty) => { // construct the consts for the elements of the array/slice - let field_consts = branches - .iter() - .map(|b| tcx.mk_const(ty::ConstKind::Value(*b), *inner_ty)) - .collect::>(); + let field_consts = + branches.iter().map(|b| tcx.mk_const(*b, *inner_ty)).collect::>(); debug!(?field_consts); (field_consts, None) @@ -53,7 +51,7 @@ pub(crate) fn destructure_const<'tcx>( for (field, field_valtree) in iter::zip(fields, branches) { let field_ty = field.ty(tcx, substs); - let field_const = tcx.mk_const(ty::ConstKind::Value(*field_valtree), field_ty); + let field_const = tcx.mk_const(*field_valtree, field_ty); field_consts.push(field_const); } debug!(?field_consts); @@ -62,9 +60,7 @@ pub(crate) fn destructure_const<'tcx>( } ty::Tuple(elem_tys) => { let fields = iter::zip(*elem_tys, branches) - .map(|(elem_ty, elem_valtree)| { - tcx.mk_const(ty::ConstKind::Value(*elem_valtree), elem_ty) - }) + .map(|(elem_ty, elem_valtree)| tcx.mk_const(*elem_valtree, elem_ty)) .collect::>(); (fields, None) @@ -137,9 +133,9 @@ fn recurse_build<'tcx>( } &ExprKind::NamedConst { def_id, substs, user_ty: _ } => { let uneval = ty::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs); - tcx.mk_const(ty::ConstKind::Unevaluated(uneval), node.ty) + tcx.mk_const(uneval, node.ty) } - ExprKind::ConstParam { param, .. } => tcx.mk_const(ty::ConstKind::Param(*param), node.ty), + ExprKind::ConstParam { param, .. } => tcx.mk_const(*param, node.ty), ExprKind::Call { fun, args, .. } => { let fun = recurse_build(tcx, body, *fun, root_span)?; @@ -149,16 +145,16 @@ fn recurse_build<'tcx>( new_args.push(recurse_build(tcx, body, id, root_span)?); } let new_args = tcx.mk_const_list(new_args.iter()); - tcx.mk_const(ConstKind::Expr(Expr::FunctionCall(fun, new_args)), node.ty) + tcx.mk_const(Expr::FunctionCall(fun, new_args), node.ty) } &ExprKind::Binary { op, lhs, rhs } if check_binop(op) => { let lhs = recurse_build(tcx, body, lhs, root_span)?; let rhs = recurse_build(tcx, body, rhs, root_span)?; - tcx.mk_const(ConstKind::Expr(Expr::Binop(op, lhs, rhs)), node.ty) + tcx.mk_const(Expr::Binop(op, lhs, rhs), node.ty) } &ExprKind::Unary { op, arg } if check_unop(op) => { let arg = recurse_build(tcx, body, arg, root_span)?; - tcx.mk_const(ConstKind::Expr(Expr::UnOp(op, arg)), node.ty) + tcx.mk_const(Expr::UnOp(op, arg), node.ty) } // This is necessary so that the following compiles: // @@ -179,11 +175,11 @@ fn recurse_build<'tcx>( // This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested) &ExprKind::Use { source } => { let arg = recurse_build(tcx, body, source, root_span)?; - tcx.mk_const(ConstKind::Expr(Expr::Cast(CastKind::Use, arg, node.ty)), node.ty) + tcx.mk_const(Expr::Cast(CastKind::Use, arg, node.ty), node.ty) } &ExprKind::Cast { source } => { let arg = recurse_build(tcx, body, source, root_span)?; - tcx.mk_const(ConstKind::Expr(Expr::Cast(CastKind::As, arg, node.ty)), node.ty) + tcx.mk_const(Expr::Cast(CastKind::As, arg, node.ty), node.ty) } ExprKind::Borrow { arg, .. } => { let arg_node = &body.exprs[*arg];