1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
|
//! Allows for producing a unique string key for a mono item.
//! These keys are used by the handwritten auto-tests, so they need to be
//! predictable and human-readable.
//!
//! Note: A lot of this could looks very similar to what's already in `ty::print`.
//! FIXME(eddyb) implement a custom `PrettyPrinter` for this.
use crate::bug;
use crate::ty::subst::SubstsRef;
use crate::ty::{self, Const, Instance, Ty, TyCtxt};
use rustc_hir as hir;
use rustc_hir::def_id::DefId;
use std::fmt::Write;
use std::iter;
/// Same as `unique_type_name()` but with the result pushed onto the given
/// `output` parameter.
pub struct DefPathBasedNames<'tcx> {
tcx: TyCtxt<'tcx>,
omit_disambiguators: bool,
omit_local_crate_name: bool,
}
impl DefPathBasedNames<'tcx> {
pub fn new(tcx: TyCtxt<'tcx>, omit_disambiguators: bool, omit_local_crate_name: bool) -> Self {
DefPathBasedNames { tcx, omit_disambiguators, omit_local_crate_name }
}
// Pushes the type name of the specified type to the provided string.
// If `debug` is true, printing normally unprintable types is allowed
// (e.g. `ty::GeneratorWitness`). This parameter should only be set when
// this method is being used for logging purposes (e.g. with `debug!` or `info!`)
// When being used for codegen purposes, `debug` should be set to `false`
// in order to catch unexpected types that should never end up in a type name.
pub fn push_type_name(&self, t: Ty<'tcx>, output: &mut String, debug: bool) {
match t.kind {
ty::Bool => output.push_str("bool"),
ty::Char => output.push_str("char"),
ty::Str => output.push_str("str"),
ty::Never => output.push_str("!"),
ty::Int(ty) => output.push_str(ty.name_str()),
ty::Uint(ty) => output.push_str(ty.name_str()),
ty::Float(ty) => output.push_str(ty.name_str()),
ty::Adt(adt_def, substs) => {
self.push_def_path(adt_def.did, output);
self.push_generic_params(substs, iter::empty(), output, debug);
}
ty::Tuple(component_types) => {
output.push('(');
for component_type in component_types {
self.push_type_name(component_type.expect_ty(), output, debug);
output.push_str(", ");
}
if !component_types.is_empty() {
output.pop();
output.pop();
}
output.push(')');
}
ty::RawPtr(ty::TypeAndMut { ty: inner_type, mutbl }) => {
output.push('*');
match mutbl {
hir::Mutability::Not => output.push_str("const "),
hir::Mutability::Mut => output.push_str("mut "),
}
self.push_type_name(inner_type, output, debug);
}
ty::Ref(_, inner_type, mutbl) => {
output.push('&');
output.push_str(mutbl.prefix_str());
self.push_type_name(inner_type, output, debug);
}
ty::Array(inner_type, len) => {
output.push('[');
self.push_type_name(inner_type, output, debug);
let len = len.eval_usize(self.tcx, ty::ParamEnv::reveal_all());
write!(output, "; {}", len).unwrap();
output.push(']');
}
ty::Slice(inner_type) => {
output.push('[');
self.push_type_name(inner_type, output, debug);
output.push(']');
}
ty::Dynamic(ref trait_data, ..) => {
if let Some(principal) = trait_data.principal() {
self.push_def_path(principal.def_id(), output);
self.push_generic_params(
principal.skip_binder().substs,
trait_data.projection_bounds(),
output,
debug,
);
} else {
output.push_str("dyn '_");
}
}
ty::Foreign(did) => self.push_def_path(did, output),
ty::FnDef(..) | ty::FnPtr(_) => {
let sig = t.fn_sig(self.tcx);
output.push_str(sig.unsafety().prefix_str());
let abi = sig.abi();
if abi != ::rustc_target::spec::abi::Abi::Rust {
output.push_str("extern \"");
output.push_str(abi.name());
output.push_str("\" ");
}
output.push_str("fn(");
let sig =
self.tcx.normalize_erasing_late_bound_regions(ty::ParamEnv::reveal_all(), &sig);
if !sig.inputs().is_empty() {
for ¶meter_type in sig.inputs() {
self.push_type_name(parameter_type, output, debug);
output.push_str(", ");
}
output.pop();
output.pop();
}
if sig.c_variadic {
if !sig.inputs().is_empty() {
output.push_str(", ...");
} else {
output.push_str("...");
}
}
output.push(')');
if !sig.output().is_unit() {
output.push_str(" -> ");
self.push_type_name(sig.output(), output, debug);
}
}
ty::Generator(def_id, substs, _) | ty::Closure(def_id, substs) => {
self.push_def_path(def_id, output);
let generics = self.tcx.generics_of(self.tcx.closure_base_def_id(def_id));
let substs = substs.truncate_to(self.tcx, generics);
self.push_generic_params(substs, iter::empty(), output, debug);
}
ty::Param(_) => {
output.push_str(&t.to_string());
}
ty::Error(_)
| ty::Bound(..)
| ty::Infer(_)
| ty::Placeholder(..)
| ty::Projection(..)
| ty::GeneratorWitness(_)
| ty::Opaque(..) => {
if debug {
output.push_str(&format!("`{:?}`", t));
} else {
bug!(
"DefPathBasedNames: trying to create type name for unexpected type: {:?}",
t,
);
}
}
}
}
// Pushes the the name of the specified const to the provided string.
// If `debug` is true, the unprintable types of constants will be printed with `fmt::Debug`
// (see `push_type_name` for more details).
pub fn push_const_name(&self, ct: &Const<'tcx>, output: &mut String, debug: bool) {
write!(output, "{}", ct).unwrap();
output.push_str(": ");
self.push_type_name(ct.ty, output, debug);
}
pub fn push_def_path(&self, def_id: DefId, output: &mut String) {
let def_path = self.tcx.def_path(def_id);
// some_crate::
if !(self.omit_local_crate_name && def_id.is_local()) {
output.push_str(&self.tcx.crate_name(def_path.krate).as_str());
output.push_str("::");
}
// foo::bar::ItemName::
for part in self.tcx.def_path(def_id).data {
if self.omit_disambiguators {
write!(output, "{}::", part.data.as_symbol()).unwrap();
} else {
write!(output, "{}[{}]::", part.data.as_symbol(), part.disambiguator).unwrap();
}
}
// remove final "::"
output.pop();
output.pop();
}
fn push_generic_params<I>(
&self,
substs: SubstsRef<'tcx>,
projections: I,
output: &mut String,
debug: bool,
) where
I: Iterator<Item = ty::PolyExistentialProjection<'tcx>>,
{
let mut projections = projections.peekable();
if substs.non_erasable_generics().next().is_none() && projections.peek().is_none() {
return;
}
output.push('<');
for type_parameter in substs.types() {
self.push_type_name(type_parameter, output, debug);
output.push_str(", ");
}
for projection in projections {
let projection = projection.skip_binder();
let name = &self.tcx.associated_item(projection.item_def_id).ident.as_str();
output.push_str(name);
output.push_str("=");
self.push_type_name(projection.ty, output, debug);
output.push_str(", ");
}
for const_parameter in substs.consts() {
self.push_const_name(const_parameter, output, debug);
output.push_str(", ");
}
output.pop();
output.pop();
output.push('>');
}
pub fn push_instance_as_string(
&self,
instance: Instance<'tcx>,
output: &mut String,
debug: bool,
) {
self.push_def_path(instance.def_id(), output);
self.push_generic_params(instance.substs, iter::empty(), output, debug);
}
}
|