Skip to content

Commit 2d39300

Browse files
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``
2 parents 39af025 + e906745 commit 2d39300

File tree

2 files changed

+13
-10
lines changed

2 files changed

+13
-10
lines changed

compiler/rustc_middle/src/ty/print/pretty.rs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ pub trait PrettyPrinter<'tcx>:
10181018
p!(write("{:?}", char::try_from(int).unwrap()))
10191019
}
10201020
// Raw pointers
1021-
(Scalar::Int(int), ty::RawPtr(_)) => {
1021+
(Scalar::Int(int), ty::RawPtr(_) | ty::FnPtr(_)) => {
10221022
let data = int.assert_bits(self.tcx().data_layout.pointer_size);
10231023
self = self.typed_value(
10241024
|mut this| {
@@ -1030,15 +1030,18 @@ pub trait PrettyPrinter<'tcx>:
10301030
)?;
10311031
}
10321032
(Scalar::Ptr(ptr), ty::FnPtr(_)) => {
1033-
// FIXME: this can ICE when the ptr is dangling or points to a non-function.
1034-
// We should probably have a helper method to share code with the "Byte strings"
1033+
// FIXME: We should probably have a helper method to share code with the "Byte strings"
10351034
// printing above (which also has to handle pointers to all sorts of things).
1036-
let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn();
1037-
self = self.typed_value(
1038-
|this| this.print_value_path(instance.def_id(), instance.substs),
1039-
|this| this.print_type(ty),
1040-
" as ",
1041-
)?;
1035+
match self.tcx().get_global_alloc(ptr.alloc_id) {
1036+
Some(GlobalAlloc::Function(instance)) => {
1037+
self = self.typed_value(
1038+
|this| this.print_value_path(instance.def_id(), instance.substs),
1039+
|this| this.print_type(ty),
1040+
" as ",
1041+
)?;
1042+
}
1043+
_ => self = self.pretty_print_const_pointer(ptr, ty, print_ty)?,
1044+
}
10421045
}
10431046
// For function type zsts just printing the path is enough
10441047
(Scalar::Int(int), ty::FnDef(d, s)) if int == ScalarInt::ZST => {

compiler/rustc_mir/src/interpret/operand.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
112112
}
113113
ScalarMaybeUninit::Uninit => cx.typed_value(
114114
|mut this| {
115-
this.write_str("{uninit ")?;
115+
this.write_str("uninit ")?;
116116
Ok(this)
117117
},
118118
|this| this.print_type(ty),

0 commit comments

Comments
 (0)