From b08bc7836be6a53e733190f3174128fe8d005737 Mon Sep 17 00:00:00 2001
From: Ralf Jung <post@ralfj.de>
Date: Tue, 16 Feb 2021 09:59:38 +0100
Subject: [PATCH 1/2] fix MIR fn-ptr pretty-printing

---
 compiler/rustc_middle/src/ty/print/pretty.rs | 20 ++++++++++++--------
 compiler/rustc_mir/src/interpret/operand.rs  |  2 +-
 2 files changed, 13 insertions(+), 9 deletions(-)

diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs
index a8d9995bd0b2f..f934e31a534d2 100644
--- a/compiler/rustc_middle/src/ty/print/pretty.rs
+++ b/compiler/rustc_middle/src/ty/print/pretty.rs
@@ -1030,15 +1030,19 @@ pub trait PrettyPrinter<'tcx>:
                 )?;
             }
             (Scalar::Ptr(ptr), ty::FnPtr(_)) => {
-                // FIXME: this can ICE when the ptr is dangling or points to a non-function.
-                // We should probably have a helper method to share code with the "Byte strings"
+                // FIXME: 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).
-                let instance = self.tcx().global_alloc(ptr.alloc_id).unwrap_fn();
-                self = self.typed_value(
-                    |this| this.print_value_path(instance.def_id(), instance.substs),
-                    |this| this.print_type(ty),
-                    " as ",
-                )?;
+                match self.tcx().get_global_alloc(ptr.alloc_id) {
+                    Some(GlobalAlloc::Function(instance)) => {
+                        self = self.typed_value(
+                            |this| this.print_value_path(instance.def_id(), instance.substs),
+                            |this| this.print_type(ty),
+                            " as ",
+                        )?;
+                    }
+                    Some(_) => p!("<non-executable memory>"),
+                    None => p!("<dangling pointer>"),
+                }
             }
             // For function type zsts just printing the path is enough
             (Scalar::Int(int), ty::FnDef(d, s)) if int == ScalarInt::ZST => {
diff --git a/compiler/rustc_mir/src/interpret/operand.rs b/compiler/rustc_mir/src/interpret/operand.rs
index 88236458a213a..4b131ecb863ad 100644
--- a/compiler/rustc_mir/src/interpret/operand.rs
+++ b/compiler/rustc_mir/src/interpret/operand.rs
@@ -106,7 +106,7 @@ impl<Tag: Copy> std::fmt::Display for ImmTy<'tcx, Tag> {
                 }
                 ScalarMaybeUninit::Uninit => cx.typed_value(
                     |mut this| {
-                        this.write_str("{uninit ")?;
+                        this.write_str("uninit ")?;
                         Ok(this)
                     },
                     |this| this.print_type(ty),

From e90674574d21f8245716c0644b0c8aa2e1702b54 Mon Sep 17 00:00:00 2001
From: Ralf Jung <post@ralfj.de>
Date: Sat, 20 Feb 2021 11:34:35 +0100
Subject: [PATCH 2/2] fn ptr pretty printing: fall back to raw ptr printing

---
 compiler/rustc_middle/src/ty/print/pretty.rs | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs
index f934e31a534d2..cc41fd855e83b 100644
--- a/compiler/rustc_middle/src/ty/print/pretty.rs
+++ b/compiler/rustc_middle/src/ty/print/pretty.rs
@@ -1018,7 +1018,7 @@ pub trait PrettyPrinter<'tcx>:
                 p!(write("{:?}", char::try_from(int).unwrap()))
             }
             // 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);
                 self = self.typed_value(
                     |mut this| {
@@ -1040,8 +1040,7 @@ pub trait PrettyPrinter<'tcx>:
                             " as ",
                         )?;
                     }
-                    Some(_) => p!("<non-executable memory>"),
-                    None => p!("<dangling pointer>"),
+                    _ => self = self.pretty_print_const_pointer(ptr, ty, print_ty)?,
                 }
             }
             // For function type zsts just printing the path is enough