Skip to content

Commit 613e618

Browse files
committed
Specialize ZeroSized constants
ZeroSized constants can be represented as `mir::Const::Val` even if their layout is not yet known. In those cases, CrateItem::body() was crashing when trying to convert a `ConstValue::ZeroSized` into its stable counterpart `ConstantKind::Allocated`. Instead, we now map `ConstValue::ZeroSized` into a new variant: `ConstantKind::ZeroSized`.
1 parent ab5c841 commit 613e618

File tree

3 files changed

+18
-6
lines changed

3 files changed

+18
-6
lines changed

compiler/rustc_smir/src/rustc_smir/mod.rs

+14-5
Original file line numberDiff line numberDiff line change
@@ -1284,11 +1284,15 @@ impl<'tcx> Stable<'tcx> for ty::Const<'tcx> {
12841284
let kind = match self.kind() {
12851285
ty::Value(val) => {
12861286
let const_val = tables.tcx.valtree_to_const_val((self.ty(), val));
1287-
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
1288-
self.ty(),
1289-
const_val,
1290-
tables,
1291-
))
1287+
if matches!(const_val, mir::ConstValue::ZeroSized) {
1288+
ConstantKind::ZeroSized
1289+
} else {
1290+
stable_mir::ty::ConstantKind::Allocated(alloc::new_allocation(
1291+
self.ty(),
1292+
const_val,
1293+
tables,
1294+
))
1295+
}
12921296
}
12931297
ty::ParamCt(param) => stable_mir::ty::ConstantKind::Param(param.stable(tables)),
12941298
ty::ErrorCt(_) => unreachable!(),
@@ -1402,6 +1406,11 @@ impl<'tcx> Stable<'tcx> for rustc_middle::mir::Const<'tcx> {
14021406
let id = tables.intern_const(*self);
14031407
Const::new(kind, ty, id)
14041408
}
1409+
mir::Const::Val(val, ty) if matches!(val, mir::ConstValue::ZeroSized) => {
1410+
let ty = ty.stable(tables);
1411+
let id = tables.intern_const(*self);
1412+
Const::new(ConstantKind::ZeroSized, ty, id)
1413+
}
14051414
mir::Const::Val(val, ty) => {
14061415
let kind = ConstantKind::Allocated(alloc::new_allocation(ty, val, tables));
14071416
let ty = ty.stable(tables);

compiler/stable_mir/src/ty.rs

+3
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,9 @@ pub enum ConstantKind {
444444
Allocated(Allocation),
445445
Unevaluated(UnevaluatedConst),
446446
Param(ParamConst),
447+
/// Store ZST constants.
448+
/// We have to special handle these constants since its type might be generic.
449+
ZeroSized,
447450
}
448451

449452
#[derive(Clone, Debug)]

compiler/stable_mir/src/visitor.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ impl Visitable for Const {
5050
match &self.kind() {
5151
super::ty::ConstantKind::Allocated(alloc) => alloc.visit(visitor)?,
5252
super::ty::ConstantKind::Unevaluated(uv) => uv.visit(visitor)?,
53-
super::ty::ConstantKind::Param(_) => {}
53+
super::ty::ConstantKind::Param(_) | super::ty::ConstantKind::ZeroSized => {}
5454
}
5555
self.ty().visit(visitor)
5656
}

0 commit comments

Comments
 (0)