Rollup merge of #82176 - RalfJung:mir-fn-ptr-pretty, r=oli-obk
fix MIR fn-ptr pretty-printing An uninitialized function pointer would get printed as `{{uninit fn()}` (notice the unbalanced parentheses), and a dangling fn ptr would ICE. This fixes both of that. However, I have no idea how to add tests for this. Also, I don't understand this MIR pretty-printing code. Somehow the print function `pretty_print_const_scalar` actually *returns* a transformed form of the const (but there is no doc comment explaining what is being returned); some match arms do `p!` while others do `self =`, and there's a wild mixture of `p!` and `write!`... all very mysterious and confusing.^^ r? ``@oli-obk``
This commit is contained in:
commit
2d39300e2f
2 changed files with 13 additions and 10 deletions
|
@ -1018,7 +1018,7 @@ pub trait PrettyPrinter<'tcx>:
|
||||||
p!(write("{:?}", char::try_from(int).unwrap()))
|
p!(write("{:?}", char::try_from(int).unwrap()))
|
||||||
}
|
}
|
||||||
// Raw pointers
|
// Raw pointers
|
||||||
(Scalar::Int(int), ty::RawPtr(_)) => {
|
(Scalar::Int(int), ty::RawPtr(_) | ty::FnPtr(_)) => {
|
||||||
let data = int.assert_bits(self.tcx().data_layout.pointer_size);
|
let data = int.assert_bits(self.tcx().data_layout.pointer_size);
|
||||||
self = self.typed_value(
|
self = self.typed_value(
|
||||||
|mut this| {
|
|mut this| {
|
||||||
|
@ -1030,16 +1030,19 @@ pub trait PrettyPrinter<'tcx>:
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
|
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
|
||||||
// FIXME: this can ICE when the ptr is dangling or points to a non-function.
|
// FIXME: We should probably have a helper method to share code with the "Byte strings"
|
||||||
// We should probably have a helper method to share code with the "Byte strings"
|
|
||||||
// printing above (which also has to handle pointers to all sorts of things).
|
// printing above (which also has to handle pointers to all sorts of things).
|
||||||
let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn();
|
match self.tcx().get_global_alloc(ptr.alloc_id) {
|
||||||
|
Some(GlobalAlloc::Function(instance)) => {
|
||||||
self = self.typed_value(
|
self = self.typed_value(
|
||||||
|this| this.print_value_path(instance.def_id(), instance.substs),
|
|this| this.print_value_path(instance.def_id(), instance.substs),
|
||||||
|this| this.print_type(ty),
|
|this| this.print_type(ty),
|
||||||
" as ",
|
" as ",
|
||||||
)?;
|
)?;
|
||||||
}
|
}
|
||||||
|
_ => self = self.pretty_print_const_pointer(ptr, ty, print_ty)?,
|
||||||
|
}
|
||||||
|
}
|
||||||
// For function type zsts just printing the path is enough
|
// For function type zsts just printing the path is enough
|
||||||
(Scalar::Int(int), ty::FnDef(d, s)) if int == ScalarInt::ZST => {
|
(Scalar::Int(int), ty::FnDef(d, s)) if int == ScalarInt::ZST => {
|
||||||
p!(print_value_path(*d, s))
|
p!(print_value_path(*d, s))
|
||||||
|
|
|
@ -112,7 +112,7 @@ impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
|
||||||
}
|
}
|
||||||
ScalarMaybeUninit::Uninit => cx.typed_value(
|
ScalarMaybeUninit::Uninit => cx.typed_value(
|
||||||
|mut this| {
|
|mut this| {
|
||||||
this.write_str("{uninit ")?;
|
this.write_str("uninit ")?;
|
||||||
Ok(this)
|
Ok(this)
|
||||||
},
|
},
|
||||||
|this| this.print_type(ty),
|
|this| this.print_type(ty),
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue