Skip to content

Commit 26b87bf

Browse files
committed
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.
1 parent 7087d9b commit 26b87bf

File tree

10 files changed

+27
-46
lines changed

10 files changed

+27
-46
lines changed

compiler/rustc_infer/src/infer/canonical/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl<'tcx> InferCtxt<'tcx> {
147147
CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, name }, ty) => {
148148
let universe_mapped = universe_map(universe);
149149
let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, name };
150-
self.tcx.mk_const(ty::ConstKind::Placeholder(placeholder_mapped), ty).into()
150+
self.tcx.mk_const(placeholder_mapped, ty).into()
151151
}
152152
}
153153
}

compiler/rustc_infer/src/infer/combine.rs

+2-8
Original file line numberDiff line numberDiff line change
@@ -765,10 +765,7 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
765765
substs,
766766
substs,
767767
)?;
768-
Ok(self.tcx().mk_const(
769-
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
770-
c.ty(),
771-
))
768+
Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty()))
772769
}
773770
_ => relate::super_relate_consts(self, c, c),
774771
}
@@ -988,10 +985,7 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
988985
substs,
989986
)?;
990987

991-
Ok(self.tcx().mk_const(
992-
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
993-
c.ty(),
994-
))
988+
Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty()))
995989
}
996990
_ => relate::super_relate_consts(self, c, c),
997991
}

compiler/rustc_infer/src/infer/higher_ranked/mod.rs

+2-7
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,8 @@ impl<'tcx> InferCtxt<'tcx> {
9494
}))
9595
},
9696
consts: &mut |bound_var: ty::BoundVar, ty| {
97-
self.tcx.mk_const(
98-
ty::ConstKind::Placeholder(ty::PlaceholderConst {
99-
universe: next_universe,
100-
name: bound_var,
101-
}),
102-
ty,
103-
)
97+
self.tcx
98+
.mk_const(ty::PlaceholderConst { universe: next_universe, name: bound_var }, ty)
10499
},
105100
};
106101

compiler/rustc_infer/src/infer/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -2049,10 +2049,10 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
20492049
bug!("const `{ct}`'s type should not reference params or types");
20502050
}
20512051
tcx.mk_const(
2052-
ty::ConstKind::Placeholder(ty::PlaceholderConst {
2052+
ty::PlaceholderConst {
20532053
universe: ty::UniverseIndex::ROOT,
20542054
name: ty::BoundVar::from_usize(idx),
2055-
}),
2055+
},
20562056
ty,
20572057
)
20582058
.into()

compiler/rustc_middle/src/mir/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2527,8 +2527,7 @@ impl<'tcx> ConstantKind<'tcx> {
25272527
let generics = tcx.generics_of(item_def_id);
25282528
let index = generics.param_def_id_to_index[&def_id];
25292529
let name = tcx.item_name(def_id);
2530-
let ty_const =
2531-
tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty);
2530+
let ty_const = tcx.mk_const(ty::ParamConst::new(index, name), ty);
25322531
debug!(?ty_const);
25332532

25342533
return Self::Ty(ty_const);

compiler/rustc_middle/src/ty/consts.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,10 @@ impl<'tcx> Const<'tcx> {
7676
match Self::try_eval_lit_or_param(tcx, ty, expr) {
7777
Some(v) => v,
7878
None => tcx.mk_const(
79-
ty::ConstKind::Unevaluated(ty::UnevaluatedConst {
79+
ty::UnevaluatedConst {
8080
def: def.to_global(),
8181
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
82-
}),
82+
},
8383
ty,
8484
),
8585
}
@@ -134,7 +134,7 @@ impl<'tcx> Const<'tcx> {
134134
let generics = tcx.generics_of(item_def_id);
135135
let index = generics.param_def_id_to_index[&def_id];
136136
let name = tcx.item_name(def_id);
137-
Some(tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty))
137+
Some(tcx.mk_const(ty::ParamConst::new(index, name), ty))
138138
}
139139
_ => None,
140140
}
@@ -143,7 +143,7 @@ impl<'tcx> Const<'tcx> {
143143
/// Interns the given value as a constant.
144144
#[inline]
145145
pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self {
146-
tcx.mk_const(ConstKind::Value(val), ty)
146+
tcx.mk_const(val, ty)
147147
}
148148

149149
/// Panics if self.kind != ty::ConstKind::Value

compiler/rustc_middle/src/ty/relate.rs

+1-4
Original file line numberDiff line numberDiff line change
@@ -663,10 +663,7 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>(
663663
au.substs,
664664
bu.substs,
665665
)?;
666-
return Ok(tcx.mk_const(
667-
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def: au.def, substs }),
668-
a.ty(),
669-
));
666+
return Ok(tcx.mk_const(ty::UnevaluatedConst { def: au.def, substs }, a.ty()));
670667
}
671668
// Before calling relate on exprs, it is necessary to ensure that the nested consts
672669
// have identical types.

compiler/rustc_mir_build/src/build/expr/as_constant.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7777
Constant { user_ty, span, literal }
7878
}
7979
ExprKind::ConstParam { param, def_id: _ } => {
80-
let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty);
80+
let const_param = tcx.mk_const(param, expr.ty);
8181
let literal = ConstantKind::Ty(const_param);
8282

8383
Constant { user_ty: None, span, literal }

compiler/rustc_trait_selection/src/traits/project.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
818818
let universe = self.universe_for(debruijn);
819819
let p = ty::PlaceholderConst { universe, name: bound_const };
820820
self.mapped_consts.insert(p, bound_const);
821-
self.infcx.tcx.mk_const(ty::ConstKind::Placeholder(p), ct.ty())
821+
self.infcx.tcx.mk_const(p, ct.ty())
822822
}
823823
_ => ct.super_fold_with(self),
824824
}

compiler/rustc_ty_utils/src/consts.rs

+12-16
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_middle::mir::interpret::{LitToConstError, LitToConstInput};
55
use rustc_middle::thir::visit;
66
use rustc_middle::thir::visit::Visitor;
77
use rustc_middle::ty::abstract_const::CastKind;
8-
use rustc_middle::ty::{self, ConstKind, Expr, TyCtxt, TypeVisitable};
8+
use rustc_middle::ty::{self, Expr, TyCtxt, TypeVisitable};
99
use rustc_middle::{mir, thir};
1010
use rustc_span::Span;
1111
use rustc_target::abi::VariantIdx;
@@ -32,10 +32,8 @@ pub(crate) fn destructure_const<'tcx>(
3232
let (fields, variant) = match const_.ty().kind() {
3333
ty::Array(inner_ty, _) | ty::Slice(inner_ty) => {
3434
// construct the consts for the elements of the array/slice
35-
let field_consts = branches
36-
.iter()
37-
.map(|b| tcx.mk_const(ty::ConstKind::Value(*b), *inner_ty))
38-
.collect::<Vec<_>>();
35+
let field_consts =
36+
branches.iter().map(|b| tcx.mk_const(*b, *inner_ty)).collect::<Vec<_>>();
3937
debug!(?field_consts);
4038

4139
(field_consts, None)
@@ -53,7 +51,7 @@ pub(crate) fn destructure_const<'tcx>(
5351

5452
for (field, field_valtree) in iter::zip(fields, branches) {
5553
let field_ty = field.ty(tcx, substs);
56-
let field_const = tcx.mk_const(ty::ConstKind::Value(*field_valtree), field_ty);
54+
let field_const = tcx.mk_const(*field_valtree, field_ty);
5755
field_consts.push(field_const);
5856
}
5957
debug!(?field_consts);
@@ -62,9 +60,7 @@ pub(crate) fn destructure_const<'tcx>(
6260
}
6361
ty::Tuple(elem_tys) => {
6462
let fields = iter::zip(*elem_tys, branches)
65-
.map(|(elem_ty, elem_valtree)| {
66-
tcx.mk_const(ty::ConstKind::Value(*elem_valtree), elem_ty)
67-
})
63+
.map(|(elem_ty, elem_valtree)| tcx.mk_const(*elem_valtree, elem_ty))
6864
.collect::<Vec<_>>();
6965

7066
(fields, None)
@@ -137,9 +133,9 @@ fn recurse_build<'tcx>(
137133
}
138134
&ExprKind::NamedConst { def_id, substs, user_ty: _ } => {
139135
let uneval = ty::UnevaluatedConst::new(ty::WithOptConstParam::unknown(def_id), substs);
140-
tcx.mk_const(ty::ConstKind::Unevaluated(uneval), node.ty)
136+
tcx.mk_const(uneval, node.ty)
141137
}
142-
ExprKind::ConstParam { param, .. } => tcx.mk_const(ty::ConstKind::Param(*param), node.ty),
138+
ExprKind::ConstParam { param, .. } => tcx.mk_const(*param, node.ty),
143139

144140
ExprKind::Call { fun, args, .. } => {
145141
let fun = recurse_build(tcx, body, *fun, root_span)?;
@@ -149,16 +145,16 @@ fn recurse_build<'tcx>(
149145
new_args.push(recurse_build(tcx, body, id, root_span)?);
150146
}
151147
let new_args = tcx.mk_const_list(new_args.iter());
152-
tcx.mk_const(ConstKind::Expr(Expr::FunctionCall(fun, new_args)), node.ty)
148+
tcx.mk_const(Expr::FunctionCall(fun, new_args), node.ty)
153149
}
154150
&ExprKind::Binary { op, lhs, rhs } if check_binop(op) => {
155151
let lhs = recurse_build(tcx, body, lhs, root_span)?;
156152
let rhs = recurse_build(tcx, body, rhs, root_span)?;
157-
tcx.mk_const(ConstKind::Expr(Expr::Binop(op, lhs, rhs)), node.ty)
153+
tcx.mk_const(Expr::Binop(op, lhs, rhs), node.ty)
158154
}
159155
&ExprKind::Unary { op, arg } if check_unop(op) => {
160156
let arg = recurse_build(tcx, body, arg, root_span)?;
161-
tcx.mk_const(ConstKind::Expr(Expr::UnOp(op, arg)), node.ty)
157+
tcx.mk_const(Expr::UnOp(op, arg), node.ty)
162158
}
163159
// This is necessary so that the following compiles:
164160
//
@@ -179,11 +175,11 @@ fn recurse_build<'tcx>(
179175
// This is important so that `N as usize as usize` doesnt unify with `N as usize`. (untested)
180176
&ExprKind::Use { source } => {
181177
let arg = recurse_build(tcx, body, source, root_span)?;
182-
tcx.mk_const(ConstKind::Expr(Expr::Cast(CastKind::Use, arg, node.ty)), node.ty)
178+
tcx.mk_const(Expr::Cast(CastKind::Use, arg, node.ty), node.ty)
183179
}
184180
&ExprKind::Cast { source } => {
185181
let arg = recurse_build(tcx, body, source, root_span)?;
186-
tcx.mk_const(ConstKind::Expr(Expr::Cast(CastKind::As, arg, node.ty)), node.ty)
182+
tcx.mk_const(Expr::Cast(CastKind::As, arg, node.ty), node.ty)
187183
}
188184
ExprKind::Borrow { arg, .. } => {
189185
let arg_node = &body.exprs[*arg];

0 commit comments

Comments
 (0)