|
| 1 | +use crate::mir::interpret::GlobalId; |
| 2 | +use crate::query::IntoQueryParam; |
| 3 | +use crate::query::TyCtxt; |
| 4 | +use crate::ty; |
| 5 | +use rustc_hir::def_id::CrateNum; |
| 6 | +use rustc_hir::def_id::DefId; |
| 7 | +use rustc_span::Symbol; |
| 8 | +use std::fmt; |
| 9 | +use std::fmt::Debug; |
| 10 | +use std::fmt::Display; |
| 11 | + |
| 12 | +/// This is used to print query keys without giving them access to `TyCtxt`. |
| 13 | +/// This enables more reliable printing when printing the query stack on panics. |
| 14 | +#[derive(Copy, Clone)] |
| 15 | +pub struct PrintContext<'tcx> { |
| 16 | + tcx: TyCtxt<'tcx>, |
| 17 | + use_queries: bool, |
| 18 | +} |
| 19 | + |
| 20 | +impl<'tcx> PrintContext<'tcx> { |
| 21 | + pub fn arg<T>(&self, arg: T) -> PrintArg<'tcx, T> { |
| 22 | + PrintArg { arg: Some(arg), ctx: *self } |
| 23 | + } |
| 24 | + |
| 25 | + pub fn def_path_str(&self, def_id: PrintArg<'tcx, impl IntoQueryParam<DefId>>) -> String { |
| 26 | + def_id.print(|arg| { |
| 27 | + let def_id = arg.into_query_param(); |
| 28 | + if self.use_queries { |
| 29 | + self.tcx.def_path_str(def_id) |
| 30 | + } else { |
| 31 | + self.tcx.safe_def_path_str_untracked(def_id) |
| 32 | + } |
| 33 | + }) |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// This runs some code in a printing context. If `use_queries` is false this function should |
| 38 | +/// ensure that no queries are run. |
| 39 | +pub fn run<'tcx, R>( |
| 40 | + tcx: TyCtxt<'tcx>, |
| 41 | + use_queries: bool, |
| 42 | + print: impl FnOnce(PrintContext<'tcx>) -> R, |
| 43 | +) -> R { |
| 44 | + let ctx = PrintContext { tcx, use_queries }; |
| 45 | + if use_queries { ty::print::with_no_trimmed_paths!(print(ctx)) } else { print(ctx) } |
| 46 | +} |
| 47 | + |
| 48 | +const INACCESSIBLE: &'static str = "<inaccessible>"; |
| 49 | + |
| 50 | +/// This wraps a value that we want to print without giving access to the regular types |
| 51 | +/// and their Display and Debug impls. `map` and `map_with_tcx` gives access to the inner |
| 52 | +/// type, but erases the value for the no query path. |
| 53 | +#[derive(Copy, Clone)] |
| 54 | +pub struct PrintArg<'tcx, T> { |
| 55 | + arg: Option<T>, |
| 56 | + ctx: PrintContext<'tcx>, |
| 57 | +} |
| 58 | + |
| 59 | +impl<'tcx, T> PrintArg<'tcx, T> { |
| 60 | + fn print(self, f: impl FnOnce(T) -> String) -> String { |
| 61 | + self.arg.map(f).unwrap_or_else(|| INACCESSIBLE.to_owned()) |
| 62 | + } |
| 63 | + |
| 64 | + fn fmt_map( |
| 65 | + &self, |
| 66 | + f: &mut fmt::Formatter<'_>, |
| 67 | + map: impl FnOnce(&mut fmt::Formatter<'_>, &T) -> fmt::Result, |
| 68 | + ) -> fmt::Result { |
| 69 | + match &self.arg { |
| 70 | + Some(arg) => map(f, arg), |
| 71 | + _ => write!(f, "{}", INACCESSIBLE), |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + fn fmt_with_queries( |
| 76 | + &self, |
| 77 | + f: &mut fmt::Formatter<'_>, |
| 78 | + map: impl FnOnce(&mut fmt::Formatter<'_>, &T) -> fmt::Result, |
| 79 | + ) -> fmt::Result { |
| 80 | + match &self.arg { |
| 81 | + Some(arg) if self.ctx.use_queries => map(f, arg), |
| 82 | + _ => write!(f, "{}", INACCESSIBLE), |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + pub fn ctx(&self) -> PrintContext<'tcx> { |
| 87 | + self.ctx |
| 88 | + } |
| 89 | + |
| 90 | + /// Maps the argument where `f` is known to not call queries. |
| 91 | + fn map_unchecked<R>(self, f: impl FnOnce(T) -> R) -> PrintArg<'tcx, R> { |
| 92 | + PrintArg { arg: self.arg.map(f), ctx: self.ctx } |
| 93 | + } |
| 94 | + |
| 95 | + pub fn map<R>(self, f: impl FnOnce(T) -> R) -> PrintArg<'tcx, R> { |
| 96 | + self.map_with_tcx(|_, arg| f(arg)) |
| 97 | + } |
| 98 | + |
| 99 | + pub fn map_with_tcx<R>(self, f: impl FnOnce(TyCtxt<'tcx>, T) -> R) -> PrintArg<'tcx, R> { |
| 100 | + PrintArg { |
| 101 | + arg: if self.ctx.use_queries { self.arg.map(|arg| f(self.ctx.tcx, arg)) } else { None }, |
| 102 | + ctx: self.ctx, |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + pub fn describe_as_module(&self) -> String |
| 107 | + where |
| 108 | + T: IntoQueryParam<DefId> + Copy, |
| 109 | + { |
| 110 | + self.print(|arg| { |
| 111 | + let def_id: DefId = arg.into_query_param(); |
| 112 | + if def_id.is_top_level_module() { |
| 113 | + "top-level module".to_string() |
| 114 | + } else { |
| 115 | + format!("module `{}`", self.ctx.def_path_str(*self)) |
| 116 | + } |
| 117 | + }) |
| 118 | + } |
| 119 | +} |
| 120 | + |
| 121 | +impl<'tcx, T0, T1> PrintArg<'tcx, (T0, T1)> { |
| 122 | + pub fn i_0(self) -> PrintArg<'tcx, T0> { |
| 123 | + self.map_unchecked(|arg| arg.0) |
| 124 | + } |
| 125 | + pub fn i_1(self) -> PrintArg<'tcx, T1> { |
| 126 | + self.map_unchecked(|arg| arg.1) |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +impl<'tcx, T> PrintArg<'tcx, ty::ParamEnvAnd<'tcx, T>> { |
| 131 | + pub fn value(self) -> PrintArg<'tcx, T> { |
| 132 | + self.map_unchecked(|arg| arg.value) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +impl<'tcx> PrintArg<'tcx, GlobalId<'tcx>> { |
| 137 | + pub fn display(self) -> String { |
| 138 | + self.print(|arg| { |
| 139 | + if self.ctx.use_queries { |
| 140 | + arg.display(self.ctx.tcx) |
| 141 | + } else { |
| 142 | + let instance_name = self.ctx.def_path_str(self.ctx.arg(arg.instance.def.def_id())); |
| 143 | + if let Some(promoted) = arg.promoted { |
| 144 | + format!("{instance_name}::{promoted:?}") |
| 145 | + } else { |
| 146 | + instance_name |
| 147 | + } |
| 148 | + } |
| 149 | + }) |
| 150 | + } |
| 151 | +} |
| 152 | + |
| 153 | +impl<T: Debug> Debug for PrintArg<'_, T> { |
| 154 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 155 | + self.fmt_with_queries(f, |f, arg| arg.fmt(f)) |
| 156 | + } |
| 157 | +} |
| 158 | + |
| 159 | +impl<T: Display> Display for PrintArg<'_, T> { |
| 160 | + default fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 161 | + self.fmt_with_queries(f, |f, arg| arg.fmt(f)) |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +impl Display for PrintArg<'_, CrateNum> { |
| 166 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 167 | + self.fmt_map(f, |f, arg| { |
| 168 | + if self.ctx.use_queries { |
| 169 | + write!(f, "`{}`", arg) |
| 170 | + } else { |
| 171 | + write!(f, "`{}`", self.ctx.tcx.safe_crate_str_untracked(*arg)) |
| 172 | + } |
| 173 | + }) |
| 174 | + } |
| 175 | +} |
| 176 | + |
| 177 | +impl Display for PrintArg<'_, Symbol> { |
| 178 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 179 | + self.fmt_map(f, |f, arg| fmt::Display::fmt(arg, f)) |
| 180 | + } |
| 181 | +} |
0 commit comments