Skip to content

Commit 0498845

Browse files
authored
Rollup merge of #72424 - RalfJung:mir-print-ice, r=oli-obk
fix ICE when debug-printing MIR Fixes #72105 This bug also makes debugging Miri harder as `MIRI_LOG=info` ICEs.
2 parents b3f1b95 + d59fa08 commit 0498845

File tree

1 file changed

+20
-10
lines changed

1 file changed

+20
-10
lines changed

src/librustc_middle/ty/print/pretty.rs

+20-10
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
use crate::middle::cstore::{ExternCrate, ExternCrateSource};
2-
use crate::mir::interpret::{sign_extend, truncate, AllocId, ConstValue, Pointer, Scalar};
2+
use crate::mir::interpret::{
3+
sign_extend, truncate, AllocId, ConstValue, GlobalAlloc, Pointer, Scalar,
4+
};
35
use crate::ty::layout::IntegerExt;
46
use crate::ty::subst::{GenericArg, GenericArgKind, Subst};
57
use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable};
@@ -951,15 +953,20 @@ pub trait PrettyPrinter<'tcx>:
951953
},
952954
_,
953955
),
954-
) => {
955-
let byte_str = self
956-
.tcx()
957-
.global_alloc(ptr.alloc_id)
958-
.unwrap_memory()
959-
.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
960-
.unwrap();
961-
p!(pretty_print_byte_str(byte_str));
962-
}
956+
) => match self.tcx().get_global_alloc(ptr.alloc_id) {
957+
Some(GlobalAlloc::Memory(alloc)) => {
958+
if let Ok(byte_str) = alloc.get_bytes(&self.tcx(), ptr, Size::from_bytes(*data))
959+
{
960+
p!(pretty_print_byte_str(byte_str))
961+
} else {
962+
p!(write("<too short allocation>"))
963+
}
964+
}
965+
// FIXME: for statics and functions, we could in principle print more detail.
966+
Some(GlobalAlloc::Static(def_id)) => p!(write("<static({:?})>", def_id)),
967+
Some(GlobalAlloc::Function(_)) => p!(write("<function>")),
968+
None => p!(write("<dangling pointer>")),
969+
},
963970
// Bool
964971
(Scalar::Raw { data: 0, .. }, ty::Bool) => p!(write("false")),
965972
(Scalar::Raw { data: 1, .. }, ty::Bool) => p!(write("true")),
@@ -1018,6 +1025,9 @@ pub trait PrettyPrinter<'tcx>:
10181025
)?;
10191026
}
10201027
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
1028+
// FIXME: this can ICE when the ptr is dangling or points to a non-function.
1029+
// We should probably have a helper method to share code with the "Byte strings"
1030+
// printing above (which also has to handle pointers to all sorts of things).
10211031
let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn();
10221032
self = self.typed_value(
10231033
|this| this.print_value_path(instance.def_id(), instance.substs),

0 commit comments

Comments
 (0)