|
1 | 1 | 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 | +}; |
3 | 5 | use crate::ty::layout::IntegerExt; |
4 | 6 | use crate::ty::subst::{GenericArg, GenericArgKind, Subst}; |
5 | 7 | use crate::ty::{self, DefIdTree, ParamConst, Ty, TyCtxt, TypeFoldable}; |
@@ -951,15 +953,20 @@ pub trait PrettyPrinter<'tcx>: |
951 | 953 | }, |
952 | 954 | _, |
953 | 955 | ), |
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 | + }, |
963 | 970 | // Bool |
964 | 971 | (Scalar::Raw { data: 0, .. }, ty::Bool) => p!(write("false")), |
965 | 972 | (Scalar::Raw { data: 1, .. }, ty::Bool) => p!(write("true")), |
@@ -1018,6 +1025,9 @@ pub trait PrettyPrinter<'tcx>: |
1018 | 1025 | )?; |
1019 | 1026 | } |
1020 | 1027 | (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). |
1021 | 1031 | let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn(); |
1022 | 1032 | self = self.typed_value( |
1023 | 1033 | |this| this.print_value_path(instance.def_id(), instance.substs), |
|
0 commit comments