Skip to content

Commit b101f3a

Browse files
authored
Rollup merge of #103984 - V0ldek:103974-refactor-mk_const, r=BoxyUwU
Refactor tcx mk_const parameters. Unroll the `ty::ConstS` parameter to `TyCtxt::mk_const` into separate `ty::ConstKind` and `Ty` parameters. Signature change is in: https://github.com/V0ldek/rust/blob/c97fd8183a98d6a89b8fc2e02eb068298e6fb7dc/compiler/rustc_middle/src/ty/context.rs#L2234 and https://github.com/V0ldek/rust/blob/c97fd8183a98d6a89b8fc2e02eb068298e6fb7dc/compiler/rustc_middle/src/ty/context.rs#L2572-L2575 the rest is callsites. Closes #103974 r? `@oli-obk`
2 parents ded8d03 + c97fd81 commit b101f3a

File tree

21 files changed

+74
-104
lines changed

21 files changed

+74
-104
lines changed

compiler/rustc_hir_analysis/src/check/compare_method.rs

+4-7
Original file line numberDiff line numberDiff line change
@@ -1655,13 +1655,10 @@ pub fn check_type_bounds<'tcx>(
16551655
GenericParamDefKind::Const { .. } => {
16561656
let bound_var = ty::BoundVariableKind::Const;
16571657
bound_vars.push(bound_var);
1658-
tcx.mk_const(ty::ConstS {
1659-
ty: tcx.type_of(param.def_id),
1660-
kind: ty::ConstKind::Bound(
1661-
ty::INNERMOST,
1662-
ty::BoundVar::from_usize(bound_vars.len() - 1),
1663-
),
1664-
})
1658+
tcx.mk_const(
1659+
ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_usize(bound_vars.len() - 1)),
1660+
tcx.type_of(param.def_id),
1661+
)
16651662
.into()
16661663
}
16671664
});

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -773,10 +773,10 @@ impl<'cx, 'tcx> Canonicalizer<'cx, 'tcx> {
773773
self.fold_const(bound_to)
774774
} else {
775775
let var = self.canonical_var(info, const_var.into());
776-
self.tcx().mk_const(ty::ConstS {
777-
kind: ty::ConstKind::Bound(self.binder_index, var),
778-
ty: self.fold_ty(const_var.ty()),
779-
})
776+
self.tcx().mk_const(
777+
ty::ConstKind::Bound(self.binder_index, var),
778+
self.fold_ty(const_var.ty()),
779+
)
780780
}
781781
}
782782
}

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -147,12 +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
151-
.mk_const(ty::ConstS {
152-
kind: ty::ConstKind::Placeholder(placeholder_mapped),
153-
ty,
154-
})
155-
.into()
150+
self.tcx.mk_const(ty::ConstKind::Placeholder(placeholder_mapped), ty).into()
156151
}
157152
}
158153
}

compiler/rustc_infer/src/infer/combine.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -741,10 +741,10 @@ impl<'tcx> TypeRelation<'tcx> for Generalizer<'_, 'tcx> {
741741
substs,
742742
substs,
743743
)?;
744-
Ok(self.tcx().mk_const(ty::ConstS {
745-
ty: c.ty(),
746-
kind: ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
747-
}))
744+
Ok(self.tcx().mk_const(
745+
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
746+
c.ty(),
747+
))
748748
}
749749
_ => relate::super_relate_consts(self, c, c),
750750
}
@@ -955,10 +955,10 @@ impl<'tcx> TypeRelation<'tcx> for ConstInferUnifier<'_, 'tcx> {
955955
substs,
956956
)?;
957957

958-
Ok(self.tcx().mk_const(ty::ConstS {
959-
ty: c.ty(),
960-
kind: ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
961-
}))
958+
Ok(self.tcx().mk_const(
959+
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def, substs }),
960+
c.ty(),
961+
))
962962
}
963963
_ => relate::super_relate_consts(self, c, c),
964964
}

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

+3-3
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@ impl<'tcx> InferCtxt<'tcx> {
9494
}))
9595
},
9696
consts: &mut |bound_var: ty::BoundVar, ty| {
97-
self.tcx.mk_const(ty::ConstS {
98-
kind: ty::ConstKind::Placeholder(ty::PlaceholderConst {
97+
self.tcx.mk_const(
98+
ty::ConstKind::Placeholder(ty::PlaceholderConst {
9999
universe: next_universe,
100100
name: bound_var,
101101
}),
102102
ty,
103-
})
103+
)
104104
},
105105
};
106106

compiler/rustc_infer/src/infer/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -2065,13 +2065,13 @@ fn replace_param_and_infer_substs_with_placeholder<'tcx>(
20652065
if ty.has_non_region_param() || ty.has_non_region_infer() {
20662066
bug!("const `{ct}`'s type should not reference params or types");
20672067
}
2068-
tcx.mk_const(ty::ConstS {
2069-
ty,
2070-
kind: ty::ConstKind::Placeholder(ty::PlaceholderConst {
2068+
tcx.mk_const(
2069+
ty::ConstKind::Placeholder(ty::PlaceholderConst {
20712070
universe: ty::UniverseIndex::ROOT,
20722071
name: ty::BoundVar::from_usize(idx),
20732072
}),
2074-
})
2073+
ty,
2074+
)
20752075
.into()
20762076
}
20772077
_ => arg,

compiler/rustc_middle/src/infer/canonical.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,10 @@ impl<'tcx> CanonicalVarValues<'tcx> {
341341
tcx.mk_region(ty::ReLateBound(ty::INNERMOST, br)).into()
342342
}
343343
GenericArgKind::Const(ct) => tcx
344-
.mk_const(ty::ConstS {
345-
ty: ct.ty(),
346-
kind: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)),
347-
})
344+
.mk_const(
345+
ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from_u32(i)),
346+
ct.ty(),
347+
)
348348
.into(),
349349
})
350350
.collect(),

compiler/rustc_middle/src/mir/mod.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -2414,10 +2414,8 @@ impl<'tcx> ConstantKind<'tcx> {
24142414
let generics = tcx.generics_of(item_def_id.to_def_id());
24152415
let index = generics.param_def_id_to_index[&def_id];
24162416
let name = tcx.hir().name(hir_id);
2417-
let ty_const = tcx.mk_const(ty::ConstS {
2418-
kind: ty::ConstKind::Param(ty::ParamConst::new(index, name)),
2419-
ty,
2420-
});
2417+
let ty_const =
2418+
tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty);
24212419
debug!(?ty_const);
24222420

24232421
return Self::Ty(ty_const);

compiler/rustc_middle/src/ty/codec.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> RefDecodable<'tcx, D>
310310

311311
impl<'tcx, D: TyDecoder<I = TyCtxt<'tcx>>> Decodable<D> for ty::Const<'tcx> {
312312
fn decode(decoder: &mut D) -> Self {
313-
decoder.interner().mk_const(Decodable::decode(decoder))
313+
let consts: ty::ConstS<'tcx> = Decodable::decode(decoder);
314+
decoder.interner().mk_const(consts.kind, consts.ty)
314315
}
315316
}
316317

compiler/rustc_middle/src/ty/consts.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -77,13 +77,13 @@ impl<'tcx> Const<'tcx> {
7777

7878
match Self::try_eval_lit_or_param(tcx, ty, expr) {
7979
Some(v) => v,
80-
None => tcx.mk_const(ty::ConstS {
81-
kind: ty::ConstKind::Unevaluated(ty::UnevaluatedConst {
80+
None => tcx.mk_const(
81+
ty::ConstKind::Unevaluated(ty::UnevaluatedConst {
8282
def: def.to_global(),
8383
substs: InternalSubsts::identity_for_item(tcx, def.did.to_def_id()),
8484
}),
8585
ty,
86-
}),
86+
),
8787
}
8888
}
8989

@@ -138,10 +138,7 @@ impl<'tcx> Const<'tcx> {
138138
let generics = tcx.generics_of(item_def_id.to_def_id());
139139
let index = generics.param_def_id_to_index[&def_id];
140140
let name = tcx.hir().name(hir_id);
141-
Some(tcx.mk_const(ty::ConstS {
142-
kind: ty::ConstKind::Param(ty::ParamConst::new(index, name)),
143-
ty,
144-
}))
141+
Some(tcx.mk_const(ty::ConstKind::Param(ty::ParamConst::new(index, name)), ty))
145142
}
146143
_ => None,
147144
}
@@ -150,7 +147,7 @@ impl<'tcx> Const<'tcx> {
150147
/// Interns the given value as a constant.
151148
#[inline]
152149
pub fn from_value(tcx: TyCtxt<'tcx>, val: ty::ValTree<'tcx>, ty: Ty<'tcx>) -> Self {
153-
tcx.mk_const(ConstS { kind: ConstKind::Value(val), ty })
150+
tcx.mk_const(ConstKind::Value(val), ty)
154151
}
155152

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

compiler/rustc_middle/src/ty/context.rs

+10-5
Original file line numberDiff line numberDiff line change
@@ -1316,7 +1316,7 @@ impl<'tcx> TyCtxt<'tcx> {
13161316
msg: &str,
13171317
) -> Const<'tcx> {
13181318
let reported = self.sess.delay_span_bug(span, msg);
1319-
self.mk_const(ty::ConstS { kind: ty::ConstKind::Error(reported), ty })
1319+
self.mk_const(ty::ConstKind::Error(reported), ty)
13201320
}
13211321

13221322
pub fn consider_optimizing<T: Fn() -> String>(self, msg: T) -> bool {
@@ -2231,7 +2231,7 @@ macro_rules! direct_interners {
22312231

22322232
direct_interners! {
22332233
region: mk_region(RegionKind<'tcx>): Region -> Region<'tcx>,
2234-
const_: mk_const(ConstS<'tcx>): Const -> Const<'tcx>,
2234+
const_: mk_const_internal(ConstS<'tcx>): Const -> Const<'tcx>,
22352235
const_allocation: intern_const_alloc(Allocation): ConstAllocation -> ConstAllocation<'tcx>,
22362236
layout: intern_layout(LayoutS<'tcx>): Layout -> Layout<'tcx>,
22372237
adt_def: intern_adt_def(AdtDefData): AdtDef -> AdtDef<'tcx>,
@@ -2569,9 +2569,14 @@ impl<'tcx> TyCtxt<'tcx> {
25692569
self.mk_ty_infer(TyVar(v))
25702570
}
25712571

2572+
#[inline]
2573+
pub fn mk_const(self, kind: ty::ConstKind<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
2574+
self.mk_const_internal(ty::ConstS { kind, ty })
2575+
}
2576+
25722577
#[inline]
25732578
pub fn mk_const_var(self, v: ConstVid<'tcx>, ty: Ty<'tcx>) -> Const<'tcx> {
2574-
self.mk_const(ty::ConstS { kind: ty::ConstKind::Infer(InferConst::Var(v)), ty })
2579+
self.mk_const(ty::ConstKind::Infer(InferConst::Var(v)), ty)
25752580
}
25762581

25772582
#[inline]
@@ -2591,7 +2596,7 @@ impl<'tcx> TyCtxt<'tcx> {
25912596

25922597
#[inline]
25932598
pub fn mk_const_infer(self, ic: InferConst<'tcx>, ty: Ty<'tcx>) -> ty::Const<'tcx> {
2594-
self.mk_const(ty::ConstS { kind: ty::ConstKind::Infer(ic), ty })
2599+
self.mk_const(ty::ConstKind::Infer(ic), ty)
25952600
}
25962601

25972602
#[inline]
@@ -2601,7 +2606,7 @@ impl<'tcx> TyCtxt<'tcx> {
26012606

26022607
#[inline]
26032608
pub fn mk_const_param(self, index: u32, name: Symbol, ty: Ty<'tcx>) -> Const<'tcx> {
2604-
self.mk_const(ty::ConstS { kind: ty::ConstKind::Param(ParamConst { index, name }), ty })
2609+
self.mk_const(ty::ConstKind::Param(ParamConst { index, name }), ty)
26052610
}
26062611

26072612
pub fn mk_param_from_def(self, param: &ty::GenericParamDef) -> GenericArg<'tcx> {

compiler/rustc_middle/src/ty/fold.rs

+3-9
Original file line numberDiff line numberDiff line change
@@ -566,10 +566,7 @@ impl<'tcx> TyCtxt<'tcx> {
566566
))
567567
},
568568
consts: &mut |c, ty: Ty<'tcx>| {
569-
self.mk_const(ty::ConstS {
570-
kind: ty::ConstKind::Bound(ty::INNERMOST, shift_bv(c)),
571-
ty,
572-
})
569+
self.mk_const(ty::ConstKind::Bound(ty::INNERMOST, shift_bv(c)), ty)
573570
},
574571
},
575572
)
@@ -648,7 +645,7 @@ impl<'tcx> TyCtxt<'tcx> {
648645
let index = entry.index();
649646
let var = ty::BoundVar::from_usize(index);
650647
let () = entry.or_insert_with(|| ty::BoundVariableKind::Const).expect_const();
651-
self.tcx.mk_const(ty::ConstS { ty, kind: ty::ConstKind::Bound(ty::INNERMOST, var) })
648+
self.tcx.mk_const(ty::ConstKind::Bound(ty::INNERMOST, var), ty)
652649
}
653650
}
654651

@@ -732,10 +729,7 @@ impl<'tcx> TypeFolder<'tcx> for Shifter<'tcx> {
732729
ct
733730
} else {
734731
let debruijn = debruijn.shifted_in(self.amount);
735-
self.tcx.mk_const(ty::ConstS {
736-
kind: ty::ConstKind::Bound(debruijn, bound_ct),
737-
ty: ct.ty(),
738-
})
732+
self.tcx.mk_const(ty::ConstKind::Bound(debruijn, bound_ct), ct.ty())
739733
}
740734
} else {
741735
ct.super_fold_with(self)

compiler/rustc_middle/src/ty/relate.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -639,10 +639,10 @@ pub fn super_relate_consts<'tcx, R: TypeRelation<'tcx>>(
639639
au.substs,
640640
bu.substs,
641641
)?;
642-
return Ok(tcx.mk_const(ty::ConstS {
643-
kind: ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def: au.def, substs }),
644-
ty: a.ty(),
645-
}));
642+
return Ok(tcx.mk_const(
643+
ty::ConstKind::Unevaluated(ty::UnevaluatedConst { def: au.def, substs }),
644+
a.ty(),
645+
));
646646
}
647647
_ => false,
648648
};

compiler/rustc_middle/src/ty/structural_impls.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -805,7 +805,7 @@ impl<'tcx> TypeSuperFoldable<'tcx> for ty::Const<'tcx> {
805805
let ty = self.ty().try_fold_with(folder)?;
806806
let kind = self.kind().try_fold_with(folder)?;
807807
if ty != self.ty() || kind != self.kind() {
808-
Ok(folder.tcx().mk_const(ty::ConstS { ty, kind }))
808+
Ok(folder.tcx().mk_const(kind, ty))
809809
} else {
810810
Ok(self)
811811
}

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

+1-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
7474
Constant { user_ty, span, literal }
7575
}
7676
ExprKind::ConstParam { param, def_id: _ } => {
77-
let const_param =
78-
tcx.mk_const(ty::ConstS { kind: ty::ConstKind::Param(param), ty: expr.ty });
77+
let const_param = tcx.mk_const(ty::ConstKind::Param(param), expr.ty);
7978
let literal = ConstantKind::Ty(const_param);
8079

8180
Constant { user_ty: None, span, literal }

compiler/rustc_symbol_mangling/src/v0.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -654,8 +654,7 @@ impl<'tcx> Printer<'tcx> for &mut SymbolMangler<'tcx> {
654654
.builtin_deref(true)
655655
.expect("tried to dereference on non-ptr type")
656656
.ty;
657-
let dereferenced_const =
658-
self.tcx.mk_const(ty::ConstS { kind: ct.kind(), ty: pointee_ty });
657+
let dereferenced_const = self.tcx.mk_const(ct.kind(), pointee_ty);
659658
self = dereferenced_const.print(self)?;
660659
}
661660
}

compiler/rustc_trait_selection/src/traits/project.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -831,9 +831,7 @@ impl<'tcx> TypeFolder<'tcx> for BoundVarReplacer<'_, 'tcx> {
831831
let universe = self.universe_for(debruijn);
832832
let p = ty::PlaceholderConst { universe, name: bound_const };
833833
self.mapped_consts.insert(p, bound_const);
834-
self.infcx
835-
.tcx
836-
.mk_const(ty::ConstS { kind: ty::ConstKind::Placeholder(p), ty: ct.ty() })
834+
self.infcx.tcx.mk_const(ty::ConstKind::Placeholder(p), ct.ty())
837835
}
838836
_ => ct.super_fold_with(self),
839837
}
@@ -968,10 +966,7 @@ impl<'tcx> TypeFolder<'tcx> for PlaceholderReplacer<'_, 'tcx> {
968966
let db = ty::DebruijnIndex::from_usize(
969967
self.universe_indices.len() - index + self.current_index.as_usize() - 1,
970968
);
971-
self.tcx().mk_const(ty::ConstS {
972-
kind: ty::ConstKind::Bound(db, *replace_var),
973-
ty: ct.ty(),
974-
})
969+
self.tcx().mk_const(ty::ConstKind::Bound(db, *replace_var), ct.ty())
975970
}
976971
None => ct,
977972
}
@@ -2173,7 +2168,7 @@ fn confirm_impl_candidate<'cx, 'tcx>(
21732168
crate::traits::InternalSubsts::identity_for_item(tcx, assoc_ty.item.def_id);
21742169
let did = ty::WithOptConstParam::unknown(assoc_ty.item.def_id);
21752170
let kind = ty::ConstKind::Unevaluated(ty::UnevaluatedConst::new(did, identity_substs));
2176-
ty.map_bound(|ty| tcx.mk_const(ty::ConstS { ty, kind }).into())
2171+
ty.map_bound(|ty| tcx.mk_const(kind, ty).into())
21772172
} else {
21782173
ty.map_bound(|ty| ty.into())
21792174
};

compiler/rustc_trait_selection/src/traits/select/confirmation.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -555,13 +555,13 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
555555
GenericParamDefKind::Const { .. } => {
556556
let bound_var = ty::BoundVariableKind::Const;
557557
bound_vars.push(bound_var);
558-
tcx.mk_const(ty::ConstS {
559-
ty: tcx.type_of(param.def_id),
560-
kind: ty::ConstKind::Bound(
558+
tcx.mk_const(
559+
ty::ConstKind::Bound(
561560
ty::INNERMOST,
562561
ty::BoundVar::from_usize(bound_vars.len() - 1),
563562
),
564-
})
563+
tcx.type_of(param.def_id),
564+
)
565565
.into()
566566
}
567567
});

compiler/rustc_traits/src/chalk/db.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -734,10 +734,10 @@ fn bound_vars_for_item<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> SubstsRef<'tcx
734734
}
735735

736736
ty::GenericParamDefKind::Const { .. } => tcx
737-
.mk_const(ty::ConstS {
738-
kind: ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)),
739-
ty: tcx.type_of(param.def_id),
740-
})
737+
.mk_const(
738+
ty::ConstKind::Bound(ty::INNERMOST, ty::BoundVar::from(param.index)),
739+
tcx.type_of(param.def_id),
740+
)
741741
.into(),
742742
})
743743
}

compiler/rustc_traits/src/chalk/lowering.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -546,7 +546,7 @@ impl<'tcx> LowerInto<'tcx, ty::Const<'tcx>> for &chalk_ir::Const<RustInterner<'t
546546
chalk_ir::ConstValue::Placeholder(_p) => unimplemented!(),
547547
chalk_ir::ConstValue::Concrete(c) => ty::ConstKind::Value(c.interned),
548548
};
549-
interner.tcx.mk_const(ty::ConstS { ty, kind })
549+
interner.tcx.mk_const(kind, ty)
550550
}
551551
}
552552

0 commit comments

Comments
 (0)