Skip to content

Commit ddbc774

Browse files
committed
Replace mk_const with Const::new_x methods
1 parent cd68ead commit ddbc774

File tree

32 files changed

+219
-134
lines changed

32 files changed

+219
-134
lines changed

compiler/rustc_hir_analysis/src/astconv/bounds.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -386,11 +386,10 @@ impl<'tcx> dyn AstConv<'tcx> + '_ {
386386
.type_of(param.def_id)
387387
.no_bound_vars()
388388
.expect("ct params cannot have early bound vars");
389-
tcx.mk_const(
390-
ty::ConstKind::Bound(
391-
ty::INNERMOST,
392-
ty::BoundVar::from_usize(num_bound_vars),
393-
),
389+
ty::Const::new_bound(
390+
tcx,
391+
ty::INNERMOST,
392+
ty::BoundVar::from_usize(num_bound_vars),
394393
ty,
395394
)
396395
.into()

compiler/rustc_hir_analysis/src/astconv/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1970,7 +1970,8 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
19701970
assert!(!ct.ty().has_escaping_bound_vars());
19711971

19721972
match ct.kind() {
1973-
ty::ConstKind::Bound(_, bv) => self.tcx.mk_const(
1973+
ty::ConstKind::Bound(_, bv) => ty::Const::new_placeholder(
1974+
self.tcx,
19741975
ty::PlaceholderConst { universe: self.universe, bound: bv },
19751976
ct.ty(),
19761977
),

compiler/rustc_hir_analysis/src/check/compare_impl_item.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,11 +2047,10 @@ pub(super) fn check_type_bounds<'tcx>(
20472047
GenericParamDefKind::Const { .. } => {
20482048
let bound_var = ty::BoundVariableKind::Const;
20492049
bound_vars.push(bound_var);
2050-
tcx.mk_const(
2051-
ty::ConstKind::Bound(
2052-
ty::INNERMOST,
2053-
ty::BoundVar::from_usize(bound_vars.len() - 1),
2054-
),
2050+
ty::Const::new_bound(
2051+
tcx,
2052+
ty::INNERMOST,
2053+
ty::BoundVar::from_usize(bound_vars.len() - 1),
20552054
tcx.type_of(param.def_id)
20562055
.no_bound_vars()
20572056
.expect("const parameter types cannot be generic"),

compiler/rustc_hir_analysis/src/collect/predicates_of.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,12 @@ fn gather_explicit_predicates_of(tcx: TyCtxt<'_>, def_id: LocalDefId) -> ty::Gen
243243
let name = param.name.ident().name;
244244
let param_const = ty::ParamConst::new(index, name);
245245

246-
let ct_ty = tcx.type_of(param.def_id.to_def_id()).subst_identity();
246+
let ct_ty = tcx
247+
.type_of(param.def_id.to_def_id())
248+
.no_bound_vars()
249+
.expect("const parameters cannot be generic");
247250

248-
let ct = tcx.mk_const(param_const, ct_ty);
251+
let ct = ty::Const::new_param(tcx, param_const, ct_ty);
249252

250253
predicates.insert((
251254
ty::ClauseKind::ConstArgHasType(ct, ct_ty).to_predicate(tcx),

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ impl<'cx, 'tcx> TypeFolder<TyCtxt<'tcx>> for Canonicalizer<'cx, 'tcx> {
497497
// any equated inference vars correctly!
498498
let root_vid = self.infcx.root_const_var(vid);
499499
if root_vid != vid {
500-
ct = self.infcx.tcx.mk_const(ty::InferConst::Var(root_vid), ct.ty());
500+
ct = ty::Const::new_var(self.infcx.tcx, root_vid, ct.ty());
501501
vid = root_vid;
502502
}
503503

@@ -804,10 +804,7 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
804804
self.fold_const(bound_to)
805805
} else {
806806
let var = self.canonical_var(info, const_var.into());
807-
self.interner().mk_const(
808-
ty::ConstKind::Bound(self.binder_index, var),
809-
self.fold_ty(const_var.ty()),
810-
)
807+
ty::Const::new_bound(self.tcx, self.binder_index, var, self.fold_ty(const_var.ty()))
811808
}
812809
}
813810
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ impl<'tcx> InferCtxt<'tcx> {
155155
CanonicalVarKind::PlaceholderConst(ty::PlaceholderConst { universe, bound }, ty) => {
156156
let universe_mapped = universe_map(universe);
157157
let placeholder_mapped = ty::PlaceholderConst { universe: universe_mapped, bound };
158-
self.tcx.mk_const(placeholder_mapped, ty).into()
158+
ty::Const::new_placeholder(self.tcx, placeholder_mapped, ty).into()
159159
}
160160
}
161161
}

compiler/rustc_infer/src/infer/freshen.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl<'a, 'tcx> TypeFreshener<'a, 'tcx> {
9595
Entry::Vacant(entry) => {
9696
let index = self.const_freshen_count;
9797
self.const_freshen_count += 1;
98-
let ct = self.infcx.tcx.mk_const(freshener(index), ty);
98+
let ct = ty::Const::new_infer(self.infcx.tcx, freshener(index), ty);
9999
entry.insert(ct);
100100
ct
101101
}

compiler/rustc_infer/src/infer/generalize.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,7 +398,7 @@ where
398398
origin: var_value.origin,
399399
val: ConstVariableValue::Unknown { universe: self.for_universe },
400400
});
401-
Ok(self.tcx().mk_const(new_var_id, c.ty()))
401+
Ok(ty::Const::new_var(self.tcx(), new_var_id, c.ty()))
402402
}
403403
}
404404
}
@@ -412,7 +412,11 @@ where
412412
substs,
413413
substs,
414414
)?;
415-
Ok(self.tcx().mk_const(ty::UnevaluatedConst { def, substs }, c.ty()))
415+
Ok(ty::Const::new_unevaluated(
416+
self.tcx(),
417+
ty::UnevaluatedConst { def, substs },
418+
c.ty(),
419+
))
416420
}
417421
ty::ConstKind::Placeholder(placeholder) => {
418422
if self.for_universe.can_name(placeholder.universe) {

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,8 @@ impl<'tcx> InferCtxt<'tcx> {
9494
})
9595
},
9696
consts: &mut |bound_var: ty::BoundVar, ty| {
97-
self.tcx.mk_const(
97+
ty::Const::new_placeholder(
98+
self.tcx,
9899
ty::PlaceholderConst { universe: next_universe, bound: bound_var },
99100
ty,
100101
)

compiler/rustc_infer/src/infer/mod.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -999,7 +999,7 @@ impl<'tcx> InferCtxt<'tcx> {
999999
}
10001000

10011001
pub fn next_const_var(&self, ty: Ty<'tcx>, origin: ConstVariableOrigin) -> ty::Const<'tcx> {
1002-
self.tcx.mk_const(self.next_const_var_id(origin), ty)
1002+
ty::Const::new_var(self.tcx, self.next_const_var_id(origin), ty)
10031003
}
10041004

10051005
pub fn next_const_var_in_universe(
@@ -1013,7 +1013,7 @@ impl<'tcx> InferCtxt<'tcx> {
10131013
.borrow_mut()
10141014
.const_unification_table()
10151015
.new_key(ConstVarValue { origin, val: ConstVariableValue::Unknown { universe } });
1016-
self.tcx.mk_const(vid, ty)
1016+
ty::Const::new_var(self.tcx, vid, ty)
10171017
}
10181018

10191019
pub fn next_const_var_id(&self, origin: ConstVariableOrigin) -> ConstVid<'tcx> {
@@ -1131,15 +1131,15 @@ impl<'tcx> InferCtxt<'tcx> {
11311131
origin,
11321132
val: ConstVariableValue::Unknown { universe: self.universe() },
11331133
});
1134-
self.tcx
1135-
.mk_const(
1136-
const_var_id,
1137-
self.tcx
1138-
.type_of(param.def_id)
1139-
.no_bound_vars()
1140-
.expect("const parameter types cannot be generic"),
1141-
)
1142-
.into()
1134+
ty::Const::new_var(
1135+
self.tcx,
1136+
const_var_id,
1137+
self.tcx
1138+
.type_of(param.def_id)
1139+
.no_bound_vars()
1140+
.expect("const parameter types cannot be generic"),
1141+
)
1142+
.into()
11431143
}
11441144
}
11451145
}
@@ -1472,7 +1472,7 @@ impl<'tcx> InferCtxt<'tcx> {
14721472
span: Option<Span>,
14731473
) -> Result<ty::Const<'tcx>, ErrorHandled> {
14741474
match self.const_eval_resolve(param_env, unevaluated, span) {
1475-
Ok(Some(val)) => Ok(self.tcx.mk_const(val, ty)),
1475+
Ok(Some(val)) => Ok(ty::Const::new_value(self.tcx, val, ty)),
14761476
Ok(None) => {
14771477
let tcx = self.tcx;
14781478
let def_id = unevaluated.def;
@@ -1964,7 +1964,8 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
19641964
if ty.has_non_region_param() || ty.has_non_region_infer() {
19651965
bug!("const `{c}`'s type should not reference params or types");
19661966
}
1967-
self.tcx.mk_const(
1967+
ty::Const::new_placeholder(
1968+
self.tcx,
19681969
ty::PlaceholderConst {
19691970
universe: ty::UniverseIndex::ROOT,
19701971
bound: ty::BoundVar::from_u32({

compiler/rustc_middle/src/infer/canonical.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -443,12 +443,13 @@ impl<'tcx> CanonicalVarValues<'tcx> {
443443
ty::Region::new_late_bound(tcx, ty::INNERMOST, br).into()
444444
}
445445
CanonicalVarKind::Const(_, ty)
446-
| CanonicalVarKind::PlaceholderConst(_, ty) => tcx
447-
.mk_const(
448-
ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_usize(i)),
449-
ty,
450-
)
451-
.into(),
446+
| CanonicalVarKind::PlaceholderConst(_, ty) => ty::Const::new_bound(
447+
tcx,
448+
ty::INNERMOST,
449+
ty::BoundVar::from_usize(i),
450+
ty,
451+
)
452+
.into(),
452453
}
453454
},
454455
)),

compiler/rustc_middle/src/mir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2510,7 +2510,7 @@ impl<'tcx> ConstantKind<'tcx> {
25102510
let generics = tcx.generics_of(item_def_id);
25112511
let index = generics.param_def_id_to_index[&def_id];
25122512
let name = tcx.item_name(def_id);
2513-
let ty_const = tcx.mk_const(ty::ParamConst::new(index, name), ty);
2513+
let ty_const = ty::Const::new_param(tcx, ty::ParamConst::new(index, name), ty);
25142514
debug!(?ty_const);
25152515

25162516
return Self::Ty(ty_const);

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
344344
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Const<'tcx> {
345345
fn decode(decoder: &mut D) -> Self {
346346
let consts: ty::ConstData<'tcx> = Decodable::decode(decoder);
347-
decoder.interner().mk_const(consts.kind, consts.ty)
347+
decoder.interner().mk_ct_from_kind(consts.kind, consts.ty)
348348
}
349349
}
350350

compiler/rustc_middle/src/ty/consts.rs

Lines changed: 78 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,69 @@ impl<'tcx> Const<'tcx> {
4141
self.0.kind
4242
}
4343

44+
#[inline]
45+
pub fn new(tcx: TyCtxt<'tcx>, kind: ty::ConstKind<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
46+
tcx.mk_ct_from_kind(kind, ty)
47+
}
48+
49+
#[inline]
50+
pub fn new_param(tcx: TyCtxt<'tcx>, param: ty::ParamConst, ty: Ty<'tcx>) -> Const<'tcx> {
51+
Const::new(tcx, ty::ConstKind::Param(param), ty)
52+
}
53+
54+
#[inline]
55+
pub fn new_var(tcx: TyCtxt<'tcx>, infer: ty::ConstVid<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
56+
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Var(infer)), ty)
57+
}
58+
59+
#[inline]
60+
pub fn new_fresh(tcx: TyCtxt<'tcx>, fresh: u32, ty: Ty<'tcx>) -> Const<'tcx> {
61+
Const::new(tcx, ty::ConstKind::Infer(ty::InferConst::Fresh(fresh)), ty)
62+
}
63+
64+
#[inline]
65+
pub fn new_infer(tcx: TyCtxt<'tcx>, infer: ty::InferConst<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
66+
Const::new(tcx, ty::ConstKind::Infer(infer), ty)
67+
}
68+
69+
#[inline]
70+
pub fn new_bound(
71+
tcx: TyCtxt<'tcx>,
72+
debruijn: ty::DebruijnIndex,
73+
var: ty::BoundVar,
74+
ty: Ty<'tcx>,
75+
) -> Const<'tcx> {
76+
Const::new(tcx, ty::ConstKind::Bound(debruijn, var), ty)
77+
}
78+
79+
#[inline]
80+
pub fn new_placeholder(
81+
tcx: TyCtxt<'tcx>,
82+
placeholder: ty::PlaceholderConst<'tcx>,
83+
ty: Ty<'tcx>,
84+
) -> Const<'tcx> {
85+
Const::new(tcx, ty::ConstKind::Placeholder(placeholder), ty)
86+
}
87+
88+
#[inline]
89+
pub fn new_unevaluated(
90+
tcx: TyCtxt<'tcx>,
91+
uv: ty::UnevaluatedConst<'tcx>,
92+
ty: Ty<'tcx>,
93+
) -> Const<'tcx> {
94+
Const::new(tcx, ty::ConstKind::Unevaluated(uv), ty)
95+
}
96+
97+
#[inline]
98+
pub fn new_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
99+
Const::new(tcx, ty::ConstKind::Value(val), ty)
100+
}
101+
102+
#[inline]
103+
pub fn new_expr(tcx: TyCtxt<'tcx>, expr: ty::Expr<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
104+
Const::new(tcx, ty::ConstKind::Expr(expr), ty)
105+
}
106+
44107
/// Literals and const generic parameters are eagerly converted to a constant, everything else
45108
/// becomes `Unevaluated`.
46109
#[instrument(skip(tcx), level = "debug")]
@@ -60,7 +123,8 @@ impl<'tcx> Const<'tcx> {
60123

61124
match Self::try_eval_lit_or_param(tcx, ty, expr) {
62125
Some(v) => v,
63-
None => tcx.mk_const(
126+
None => ty::Const::new_unevaluated(
127+
tcx,
64128
ty::UnevaluatedConst {
65129
def: def.to_def_id(),
66130
substs: InternalSubsts::identity_for_item(tcx, def.to_def_id()),
@@ -126,12 +190,16 @@ impl<'tcx> Const<'tcx> {
126190
let generics = tcx.generics_of(item_def_id);
127191
let index = generics.param_def_id_to_index[&def_id];
128192
let name = tcx.item_name(def_id);
129-
Some(tcx.mk_const(ty::ParamConst::new(index, name), param_ty))
193+
Some(ty::Const::new_param(tcx, ty::ParamConst::new(index, name), param_ty))
194+
}
195+
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => {
196+
Some(ty::Const::new_bound(
197+
tcx,
198+
debruijn,
199+
ty::BoundVar::from_u32(index),
200+
param_ty,
201+
))
130202
}
131-
Some(rbv::ResolvedArg::LateBound(debruijn, index, _)) => Some(tcx.mk_const(
132-
ty::ConstKind::Bound(debruijn, ty::BoundVar::from_u32(index)),
133-
param_ty,
134-
)),
135203
Some(rbv::ResolvedArg::Error(guar)) => Some(tcx.const_error(param_ty, guar)),
136204
arg => bug!("unexpected bound var resolution for {:?}: {arg:?}", expr.hir_id),
137205
}
@@ -155,7 +223,8 @@ impl<'tcx> Const<'tcx> {
155223
.layout_of(ty)
156224
.unwrap_or_else(|e| panic!("could not compute layout for {:?}: {:?}", ty, e))
157225
.size;
158-
tcx.mk_const(
226+
ty::Const::new_value(
227+
tcx,
159228
ty::ValTree::from_scalar_int(ScalarInt::try_from_uint(bits, size).unwrap()),
160229
ty.value,
161230
)
@@ -164,7 +233,7 @@ impl<'tcx> Const<'tcx> {
164233
#[inline]
165234
/// Creates an interned zst constant.
166235
pub fn zero_sized(tcx: TyCtxt<'tcx>, ty: Ty<'tcx>) -> Self {
167-
tcx.mk_const(ty::ValTree::zst(), ty)
236+
ty::Const::new_value(tcx, ty::ValTree::zst(), ty)
168237
}
169238

170239
#[inline]
@@ -215,7 +284,7 @@ impl<'tcx> Const<'tcx> {
215284
pub fn eval(self, tcx: TyCtxt<'tcx>, param_env: ParamEnv<'tcx>) -> Const<'tcx> {
216285
if let Some(val) = self.kind().try_eval_for_typeck(tcx, param_env) {
217286
match val {
218-
Ok(val) => tcx.mk_const(val, self.ty()),
287+
Ok(val) => ty::Const::new_value(tcx, val, self.ty()),
219288
Err(guar) => tcx.const_error(self.ty(), guar),
220289
}
221290
} else {

0 commit comments

Comments
 (0)