Skip to content

Commit 6c7bf3b

Browse files
committed
Avoid copying and reconstruction
1 parent b99be88 commit 6c7bf3b

File tree

3 files changed

+12
-8
lines changed

3 files changed

+12
-8
lines changed

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
123123
thir_abstract_const => { cdata.get_thir_abstract_const(tcx, def_id.index) }
124124
unused_generic_params => { cdata.get_unused_generic_params(def_id.index) }
125125
const_param_default => {
126-
let c = cdata.get_const_param_default(tcx, def_id.index);
127-
tcx.mk_const(c.ty(), c.val())
126+
tcx.mk_const_(cdata.get_const_param_default(tcx, def_id.index))
128127
}
129128
mir_const_qualif => { cdata.mir_const_qualif(def_id.index) }
130129
fn_sig => { cdata.fn_sig(def_id.index, tcx) }

compiler/rustc_middle/src/ty/codec.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -333,8 +333,7 @@ impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D>
333333

334334
impl<'tcx, D: TyDecoder<'tcx>> RefDecodable<'tcx, D> for ty::Const<'tcx> {
335335
fn decode(decoder: &mut D) -> Result<&'tcx Self, D::Error> {
336-
let c: ty::Const<'_> = Decodable::decode(decoder)?;
337-
Ok(decoder.tcx().mk_const(c.ty, c.val))
336+
Ok(decoder.tcx().mk_const_(Decodable::decode(decoder)?))
338337
}
339338
}
340339

compiler/rustc_middle/src/ty/context.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@ impl<'tcx> CtxtInterners<'tcx> {
176176
}
177177

178178
#[inline(never)]
179-
fn intern_const(&self, ty: Ty<'tcx>, val: ty::ConstKind<'tcx>) -> &'tcx Const<'tcx> {
180-
self.const_.intern(CstHash(Const { ty, val }), |c| Interned(self.arena.alloc(c.0))).0
179+
fn intern_const(&self, const_: Const<'tcx>) -> &'tcx Const<'tcx> {
180+
self.const_.intern(CstHash(const_), |c| Interned(self.arena.alloc(c.0))).0
181181
}
182182
}
183183

@@ -934,7 +934,7 @@ impl<'tcx> CommonLifetimes<'tcx> {
934934

935935
impl<'tcx> CommonConsts<'tcx> {
936936
fn new(interners: &CtxtInterners<'tcx>, types: &CommonTypes<'tcx>) -> CommonConsts<'tcx> {
937-
let mk_const = |ty, val| interners.intern_const(ty, val);
937+
let mk_const = |ty, val| interners.intern_const(ty::Const { ty, val });
938938

939939
CommonConsts {
940940
unit: mk_const(types.unit, ty::ConstKind::Value(ConstValue::Scalar(Scalar::ZST))),
@@ -2248,7 +2248,13 @@ impl<'tcx> TyCtxt<'tcx> {
22482248

22492249
#[inline]
22502250
pub fn mk_const(self, ty: Ty<'tcx>, val: ty::ConstKind<'tcx>) -> &'tcx Const<'tcx> {
2251-
self.interners.intern_const(ty, val)
2251+
self.interners.intern_const(ty::Const { ty, val })
2252+
}
2253+
2254+
/// Same as `mk_const` but interns a constructed `ty::Const` which one might get from decoding the on-disk cache
2255+
#[inline]
2256+
pub fn mk_const_(self, const_: ty::Const<'tcx>) -> &'tcx Const<'tcx> {
2257+
self.interners.intern_const(const_)
22522258
}
22532259

22542260
#[inline]

0 commit comments

Comments
 (0)