Skip to content

Commit 9b8e3eb

Browse files
committed
Move around constants' Stable impls a bit
1 parent 7659abc commit 9b8e3eb

File tree

2 files changed

+28
-27
lines changed

2 files changed

+28
-27
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+20-15
Original file line numberDiff line numberDiff line change
@@ -1083,8 +1083,21 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
10831083
type T = stable_mir::ty::Const;
10841084

10851085
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1086-
let cnst = ConstantKind::from_const(*self, tables.tcx);
1087-
stable_mir::ty::Const { literal: cnst.stable(tables) }
1086+
stable_mir::ty::Const {
1087+
literal: match self.kind() {
1088+
ty::Value(val) => {
1089+
let const_val = tables.tcx.valtree_to_const_val((self.ty(), val));
1090+
stable_mir::ty::ConstantKind::Allocated(new_allocation(
1091+
self.ty(),
1092+
const_val,
1093+
tables,
1094+
))
1095+
}
1096+
ty::ParamCt(param) => stable_mir::ty::ConstantKind::ParamCt(opaque(&param)),
1097+
ty::ErrorCt(_) => unreachable!(),
1098+
_ => unimplemented!(),
1099+
},
1100+
}
10881101
}
10891102
}
10901103

@@ -1155,26 +1168,18 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::ConstantKind<'tcx> {
11551168
type T = stable_mir::ty::ConstantKind;
11561169

11571170
fn stable(&self, tables: &mut Tables<'tcx>) -> Self::T {
1158-
match self {
1159-
ConstantKind::Ty(c) => match c.kind() {
1160-
ty::Value(val) => {
1161-
let const_val = tables.tcx.valtree_to_const_val((c.ty(), val));
1162-
stable_mir::ty::ConstantKind::Allocated(new_allocation(self, const_val, tables))
1163-
}
1164-
ty::ParamCt(param) => stable_mir::ty::ConstantKind::ParamCt(opaque(&param)),
1165-
ty::ErrorCt(_) => unreachable!(),
1166-
_ => unimplemented!(),
1167-
},
1171+
match *self {
1172+
ConstantKind::Ty(c) => c.stable(tables).literal,
11681173
ConstantKind::Unevaluated(unev_const, ty) => {
11691174
stable_mir::ty::ConstantKind::Unevaluated(stable_mir::ty::UnevaluatedConst {
1170-
ty: tables.intern_ty(*ty),
1175+
ty: tables.intern_ty(ty),
11711176
def: tables.const_def(unev_const.def),
11721177
args: unev_const.args.stable(tables),
11731178
promoted: unev_const.promoted.map(|u| u.as_u32()),
11741179
})
11751180
}
1176-
ConstantKind::Val(val, _) => {
1177-
stable_mir::ty::ConstantKind::Allocated(new_allocation(self, *val, tables))
1181+
ConstantKind::Val(val, ty) => {
1182+
stable_mir::ty::ConstantKind::Allocated(new_allocation(ty, val, tables))
11781183
}
11791184
}
11801185
}

compiler/rustc_smir/src/stable_mir/ty.rs

+8-12
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,9 @@ impl Allocation {
301301
// We need this method instead of a Stable implementation
302302
// because we need to get `Ty` of the const we are trying to create, to do that
303303
// we need to have access to `ConstantKind` but we can't access that inside Stable impl.
304+
#[allow(rustc::usage_of_qualified_ty)]
304305
pub fn new_allocation<'tcx>(
305-
const_kind: &rustc_middle::mir::ConstantKind<'tcx>,
306+
ty: rustc_middle::ty::Ty<'tcx>,
306307
const_value: ConstValue<'tcx>,
307308
tables: &mut Tables<'tcx>,
308309
) -> Allocation {
@@ -311,7 +312,7 @@ pub fn new_allocation<'tcx>(
311312
let size = scalar.size();
312313
let align = tables
313314
.tcx
314-
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(const_kind.ty()))
315+
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty))
315316
.unwrap()
316317
.align;
317318
let mut allocation = rustc_middle::mir::interpret::Allocation::uninit(size, align.abi);
@@ -321,11 +322,8 @@ pub fn new_allocation<'tcx>(
321322
allocation.stable(tables)
322323
}
323324
ConstValue::ZeroSized => {
324-
let align = tables
325-
.tcx
326-
.layout_of(rustc_middle::ty::ParamEnv::empty().and(const_kind.ty()))
327-
.unwrap()
328-
.align;
325+
let align =
326+
tables.tcx.layout_of(rustc_middle::ty::ParamEnv::empty().and(ty)).unwrap().align;
329327
Allocation::new_empty_allocation(align.abi)
330328
}
331329
ConstValue::Slice { data, start, end } => {
@@ -336,10 +334,8 @@ pub fn new_allocation<'tcx>(
336334
(end - start) as u64,
337335
&tables.tcx,
338336
);
339-
let layout = tables
340-
.tcx
341-
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(const_kind.ty()))
342-
.unwrap();
337+
let layout =
338+
tables.tcx.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty)).unwrap();
343339
let mut allocation =
344340
rustc_middle::mir::interpret::Allocation::uninit(layout.size, layout.align.abi);
345341
allocation
@@ -361,7 +357,7 @@ pub fn new_allocation<'tcx>(
361357
ConstValue::ByRef { alloc, offset } => {
362358
let ty_size = tables
363359
.tcx
364-
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(const_kind.ty()))
360+
.layout_of(rustc_middle::ty::ParamEnv::reveal_all().and(ty))
365361
.unwrap()
366362
.size;
367363
allocation_filter(&alloc.0, alloc_range(offset, ty_size), tables)

0 commit comments

Comments
 (0)