about summary refs log tree commit diff
path: root/compiler/rustc_middle/src/ty
diff options
context:
space:
mode:
authorTomasz Miąsko <tomasz.miasko@gmail.com>2022-02-15 13:58:34 +0100
committerTomasz Miąsko <tomasz.miasko@gmail.com>2022-02-16 00:38:59 +0100
commit92d20c4aaddea9507f8ad37fe37c551219153bbf (patch)
tree12922a3d4bbc15f867ab89b344c994e62ab704da /compiler/rustc_middle/src/ty
parentbfb2856f271fcb647b3cad1b88b29ec97bbab2a3 (diff)
downloadrust-92d20c4aaddea9507f8ad37fe37c551219153bbf.tar.gz
rust-92d20c4aaddea9507f8ad37fe37c551219153bbf.zip
Support pretty printing of invalid constants
Make it possible to pretty print invalid constants by introducing a
fallible variant of `destructure_const` and falling back to debug
formatting when it fails.
Diffstat (limited to 'compiler/rustc_middle/src/ty')
-rw-r--r--compiler/rustc_middle/src/ty/print/pretty.rs16
1 files changed, 12 insertions, 4 deletions
diff --git a/compiler/rustc_middle/src/ty/print/pretty.rs b/compiler/rustc_middle/src/ty/print/pretty.rs
index 893df1a009c..ae838a46157 100644
--- a/compiler/rustc_middle/src/ty/print/pretty.rs
+++ b/compiler/rustc_middle/src/ty/print/pretty.rs
@@ -1459,10 +1459,18 @@ pub trait PrettyPrinter<'tcx>:
             // FIXME(eddyb) for `--emit=mir`/`-Z dump-mir`, we should provide the
             // correct `ty::ParamEnv` to allow printing *all* constant values.
             (_, ty::Array(..) | ty::Tuple(..) | ty::Adt(..)) if !ty.has_param_types_or_consts() => {
-                let contents =
-                    self.tcx().destructure_const(ty::ParamEnv::reveal_all().and(
-                        self.tcx().mk_const(ty::ConstS { val: ty::ConstKind::Value(ct), ty }),
-                    ));
+                let Some(contents) = self.tcx().try_destructure_const(
+                    ty::ParamEnv::reveal_all()
+                        .and(self.tcx().mk_const(ty::ConstS { val: ty::ConstKind::Value(ct), ty })),
+                ) else {
+                    // Fall back to debug pretty printing for invalid constants.
+                    p!(write("{:?}", ct));
+                    if print_ty {
+                        p!(": ", print(ty));
+                    }
+                    return Ok(self);
+                };
+
                 let fields = contents.fields.iter().copied();
 
                 match *ty.kind() {