Skip to content

Commit b3073f4

Browse files
committed
rustc: move the formatter into ty::print::PrintCx.
1 parent a5dcd6d commit b3073f4

File tree

9 files changed

+424
-405
lines changed

9 files changed

+424
-405
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
html_favicon_url = "https://doc.rust-lang.org/favicon.ico",
3131
html_root_url = "https://doc.rust-lang.org/nightly/")]
3232

33+
#![feature(arbitrary_self_types)]
3334
#![feature(box_patterns)]
3435
#![feature(box_syntax)]
3536
#![feature(core_intrinsics)]

src/librustc/mir/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2341,7 +2341,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
23412341
};
23422342

23432343
// When printing regions, add trailing space if necessary.
2344-
ty::print::PrintCx::with(|cx| {
2344+
ty::print::PrintCx::with(ty::print::FmtPrinter { fmt }, |cx| {
23452345
let region = if cx.is_verbose || cx.identify_regions {
23462346
let mut region = region.to_string();
23472347
if region.len() > 0 {
@@ -2352,7 +2352,7 @@ impl<'tcx> Debug for Rvalue<'tcx> {
23522352
// Do not even print 'static
23532353
String::new()
23542354
};
2355-
write!(fmt, "&{}{}{:?}", region, kind_str, place)
2355+
write!(cx.printer.fmt, "&{}{}{:?}", region, kind_str, place)
23562356
})
23572357
}
23582358

src/librustc/ty/item_path.rs

Lines changed: 67 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,10 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
5959
/// root, unless with_forced_absolute_paths was used.
6060
pub fn item_path_str(self, def_id: DefId) -> String {
6161
debug!("item_path_str: def_id={:?}", def_id);
62-
let mut cx = PrintCx::new(self);
6362
if FORCE_ABSOLUTE.with(|force| force.get()) {
64-
AbsolutePathPrinter::print_item_path(&mut cx, def_id)
63+
PrintCx::new(self, AbsolutePathPrinter).print_item_path(def_id)
6564
} else {
66-
LocalPathPrinter::print_item_path(&mut cx, def_id)
65+
PrintCx::new(self, LocalPathPrinter).print_item_path(def_id)
6766
}
6867
}
6968

@@ -76,26 +75,23 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
7675
/// suitable for user output. It always begins with a crate identifier.
7776
pub fn absolute_item_path_str(self, def_id: DefId) -> String {
7877
debug!("absolute_item_path_str: def_id={:?}", def_id);
79-
let mut cx = PrintCx::new(self);
80-
AbsolutePathPrinter::print_item_path(&mut cx, def_id)
78+
PrintCx::new(self, AbsolutePathPrinter).print_item_path(def_id)
8179
}
8280
}
8381

84-
impl PrintCx<'a, 'gcx, 'tcx> {
85-
pub fn default_print_item_path<P>(&mut self, def_id: DefId) -> P::Path
86-
where P: ItemPathPrinter
87-
{
82+
impl<P: ItemPathPrinter> PrintCx<'a, 'gcx, 'tcx, P> {
83+
pub fn default_print_item_path(&mut self, def_id: DefId) -> P::Path {
8884
debug!("default_print_item_path: def_id={:?}", def_id);
8985
let key = self.tcx.def_key(def_id);
9086
debug!("default_print_item_path: key={:?}", key);
9187
match key.disambiguated_data.data {
9288
DefPathData::CrateRoot => {
9389
assert!(key.parent.is_none());
94-
P::path_crate(self, def_id.krate)
90+
self.path_crate(def_id.krate)
9591
}
9692

9793
DefPathData::Impl => {
98-
self.default_print_impl_path::<P>(def_id)
94+
self.default_print_impl_path(def_id)
9995
}
10096

10197
// Unclear if there is any value in distinguishing these.
@@ -120,20 +116,18 @@ impl PrintCx<'a, 'gcx, 'tcx> {
120116
data @ DefPathData::ImplTrait |
121117
data @ DefPathData::GlobalMetaData(..) => {
122118
let parent_did = self.tcx.parent_def_id(def_id).unwrap();
123-
let path = P::print_item_path(self, parent_did);
124-
P::path_append(path, &data.as_interned_str().as_symbol().as_str())
119+
let path = self.print_item_path(parent_did);
120+
self.path_append(path, &data.as_interned_str().as_symbol().as_str())
125121
},
126122

127123
DefPathData::StructCtor => { // present `X` instead of `X::{{constructor}}`
128124
let parent_def_id = self.tcx.parent_def_id(def_id).unwrap();
129-
P::print_item_path(self, parent_def_id)
125+
self.print_item_path(parent_def_id)
130126
}
131127
}
132128
}
133129

134-
fn default_print_impl_path<P>(&mut self, impl_def_id: DefId) -> P::Path
135-
where P: ItemPathPrinter
136-
{
130+
fn default_print_impl_path(&mut self, impl_def_id: DefId) -> P::Path {
137131
debug!("default_print_impl_path: impl_def_id={:?}", impl_def_id);
138132
let parent_def_id = self.tcx.parent_def_id(impl_def_id).unwrap();
139133

@@ -146,7 +140,7 @@ impl PrintCx<'a, 'gcx, 'tcx> {
146140
};
147141

148142
if !use_types {
149-
return self.default_print_impl_path_fallback::<P>(impl_def_id);
143+
return self.default_print_impl_path_fallback(impl_def_id);
150144
}
151145

152146
// Decide whether to print the parent path for the impl.
@@ -170,11 +164,11 @@ impl PrintCx<'a, 'gcx, 'tcx> {
170164
// If the impl is not co-located with either self-type or
171165
// trait-type, then fallback to a format that identifies
172166
// the module more clearly.
173-
let path = P::print_item_path(self, parent_def_id);
167+
let path = self.print_item_path(parent_def_id);
174168
if let Some(trait_ref) = impl_trait_ref {
175-
return P::path_append(path, &format!("<impl {} for {}>", trait_ref, self_ty));
169+
return self.path_append(path, &format!("<impl {} for {}>", trait_ref, self_ty));
176170
} else {
177-
return P::path_append(path, &format!("<impl {}>", self_ty));
171+
return self.path_append(path, &format!("<impl {}>", self_ty));
178172
}
179173
}
180174

@@ -183,7 +177,7 @@ impl PrintCx<'a, 'gcx, 'tcx> {
183177

184178
if let Some(trait_ref) = impl_trait_ref {
185179
// Trait impls.
186-
return P::path_impl(&format!("<{} as {}>", self_ty, trait_ref));
180+
return self.path_impl(&format!("<{} as {}>", self_ty, trait_ref));
187181
}
188182

189183
// Inherent impls. Try to print `Foo::bar` for an inherent
@@ -193,42 +187,40 @@ impl PrintCx<'a, 'gcx, 'tcx> {
193187
ty::Adt(adt_def, substs) => {
194188
// FIXME(eddyb) always print without <> here.
195189
if substs.types().next().is_none() { // ignore regions
196-
P::print_item_path(self, adt_def.did)
190+
self.print_item_path(adt_def.did)
197191
} else {
198-
P::path_impl(&format!("<{}>", self_ty))
192+
self.path_impl(&format!("<{}>", self_ty))
199193
}
200194
}
201195

202-
ty::Foreign(did) => P::print_item_path(self, did),
196+
ty::Foreign(did) => self.print_item_path(did),
203197

204198
ty::Bool |
205199
ty::Char |
206200
ty::Int(_) |
207201
ty::Uint(_) |
208202
ty::Float(_) |
209203
ty::Str => {
210-
P::path_impl(&self_ty.to_string())
204+
self.path_impl(&self_ty.to_string())
211205
}
212206

213207
_ => {
214-
P::path_impl(&format!("<{}>", self_ty))
208+
self.path_impl(&format!("<{}>", self_ty))
215209
}
216210
}
217211
}
218212

219-
fn default_print_impl_path_fallback<P>(&mut self, impl_def_id: DefId) -> P::Path
220-
where P: ItemPathPrinter
221-
{
213+
fn default_print_impl_path_fallback(&mut self, impl_def_id: DefId) -> P::Path {
222214
// If no type info is available, fall back to
223215
// pretty printing some span information. This should
224216
// only occur very early in the compiler pipeline.
225217
// FIXME(eddyb) this should just be using `tcx.def_span(impl_def_id)`
226218
let parent_def_id = self.tcx.parent_def_id(impl_def_id).unwrap();
227-
let path = P::print_item_path(self, parent_def_id);
219+
let path = self.print_item_path(parent_def_id);
228220
let node_id = self.tcx.hir().as_local_node_id(impl_def_id).unwrap();
229221
let item = self.tcx.hir().expect_item(node_id);
230222
let span_str = self.tcx.sess.source_map().span_to_string(item.span);
231-
P::path_append(path, &format!("<impl at {}>", span_str))
223+
self.path_append(path, &format!("<impl at {}>", span_str))
232224
}
233225
}
234226

@@ -295,27 +287,35 @@ pub fn characteristic_def_id_of_type(ty: Ty<'_>) -> Option<DefId> {
295287
pub trait ItemPathPrinter: Sized {
296288
type Path;
297289

298-
fn print_item_path(cx: &mut PrintCx<'_, '_, '_>, def_id: DefId) -> Self::Path {
299-
cx.default_print_item_path::<Self>(def_id)
290+
fn print_item_path(self: &mut PrintCx<'_, '_, '_, Self>, def_id: DefId) -> Self::Path {
291+
self.default_print_item_path(def_id)
300292
}
301293

302-
fn path_crate(cx: &mut PrintCx<'_, '_, '_>, cnum: CrateNum) -> Self::Path;
303-
fn path_impl(text: &str) -> Self::Path;
304-
fn path_append(path: Self::Path, text: &str) -> Self::Path;
294+
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path;
295+
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path;
296+
fn path_append(
297+
self: &mut PrintCx<'_, '_, '_, Self>,
298+
path: Self::Path,
299+
text: &str,
300+
) -> Self::Path;
305301
}
306302

307303
struct AbsolutePathPrinter;
308304

309305
impl ItemPathPrinter for AbsolutePathPrinter {
310306
type Path = String;
311307

312-
fn path_crate(cx: &mut PrintCx<'_, '_, '_>, cnum: CrateNum) -> Self::Path {
313-
cx.tcx.original_crate_name(cnum).to_string()
308+
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
309+
self.tcx.original_crate_name(cnum).to_string()
314310
}
315-
fn path_impl(text: &str) -> Self::Path {
311+
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
316312
text.to_string()
317313
}
318-
fn path_append(mut path: Self::Path, text: &str) -> Self::Path {
314+
fn path_append(
315+
self: &mut PrintCx<'_, '_, '_, Self>,
316+
mut path: Self::Path,
317+
text: &str,
318+
) -> Self::Path {
319319
if !path.is_empty() {
320320
path.push_str("::");
321321
}
@@ -331,7 +331,7 @@ impl LocalPathPrinter {
331331
/// from at least one local module and returns true. If the crate defining `def_id` is
332332
/// declared with an `extern crate`, the path is guaranteed to use the `extern crate`.
333333
fn try_print_visible_item_path(
334-
cx: &mut PrintCx<'_, '_, '_>,
334+
self: &mut PrintCx<'_, '_, '_, Self>,
335335
def_id: DefId,
336336
) -> Option<<Self as ItemPathPrinter>::Path> {
337337
debug!("try_print_visible_item_path: def_id={:?}", def_id);
@@ -342,7 +342,7 @@ impl LocalPathPrinter {
342342
let cnum = def_id.krate;
343343

344344
if cnum == LOCAL_CRATE {
345-
return Some(Self::path_crate(cx, cnum));
345+
return Some(self.path_crate(cnum));
346346
}
347347

348348
// In local mode, when we encounter a crate other than
@@ -355,7 +355,7 @@ impl LocalPathPrinter {
355355
// 2. for an extern inferred from a path or an indirect crate,
356356
// where there is no explicit `extern crate`, we just prepend
357357
// the crate name.
358-
match *cx.tcx.extern_crate(def_id) {
358+
match *self.tcx.extern_crate(def_id) {
359359
Some(ExternCrate {
360360
src: ExternCrateSource::Extern(def_id),
361361
direct: true,
@@ -364,14 +364,14 @@ impl LocalPathPrinter {
364364
}) => {
365365
debug!("try_print_visible_item_path: def_id={:?}", def_id);
366366
let path = if !span.is_dummy() {
367-
Self::print_item_path(cx, def_id)
367+
self.print_item_path(def_id)
368368
} else {
369-
Self::path_crate(cx, cnum)
369+
self.path_crate(cnum)
370370
};
371371
return Some(path);
372372
}
373373
None => {
374-
return Some(Self::path_crate(cx, cnum));
374+
return Some(self.path_crate(cnum));
375375
}
376376
_ => {},
377377
}
@@ -381,9 +381,9 @@ impl LocalPathPrinter {
381381
return None;
382382
}
383383

384-
let visible_parent_map = cx.tcx.visible_parent_map(LOCAL_CRATE);
384+
let visible_parent_map = self.tcx.visible_parent_map(LOCAL_CRATE);
385385

386-
let mut cur_def_key = cx.tcx.def_key(def_id);
386+
let mut cur_def_key = self.tcx.def_key(def_id);
387387
debug!("try_print_visible_item_path: cur_def_key={:?}", cur_def_key);
388388

389389
// For a UnitStruct or TupleStruct we want the name of its parent rather than <unnamed>.
@@ -393,12 +393,12 @@ impl LocalPathPrinter {
393393
index: cur_def_key.parent.expect("DefPathData::StructCtor missing a parent"),
394394
};
395395

396-
cur_def_key = cx.tcx.def_key(parent);
396+
cur_def_key = self.tcx.def_key(parent);
397397
}
398398

399399
let visible_parent = visible_parent_map.get(&def_id).cloned()?;
400-
let path = Self::try_print_visible_item_path(cx, visible_parent)?;
401-
let actual_parent = cx.tcx.parent(def_id);
400+
let path = self.try_print_visible_item_path(visible_parent)?;
401+
let actual_parent = self.tcx.parent(def_id);
402402

403403
let data = cur_def_key.disambiguated_data.data;
404404
debug!(
@@ -442,7 +442,7 @@ impl LocalPathPrinter {
442442
DefPathData::TypeNs(actual_name) if visible_parent != actual_parent => {
443443
visible_parent
444444
.and_then(|parent| {
445-
self.item_children(parent)
445+
self.tcx.item_children(parent)
446446
.iter()
447447
.find(|child| child.def.def_id() == cur_def)
448448
.map(|child| child.ident.as_str())
@@ -453,43 +453,47 @@ impl LocalPathPrinter {
453453
data.get_opt_name().map(|n| n.as_str()).unwrap_or_else(|| {
454454
// Re-exported `extern crate` (#43189).
455455
if let DefPathData::CrateRoot = data {
456-
cx.tcx.original_crate_name(def_id.krate).as_str()
456+
self.tcx.original_crate_name(def_id.krate).as_str()
457457
} else {
458458
Symbol::intern("<unnamed>").as_str()
459459
}
460460
})
461461
},
462462
};
463463
debug!("try_print_visible_item_path: symbol={:?}", symbol);
464-
Some(Self::path_append(path, &symbol))
464+
Some(self.path_append(path, &symbol))
465465
}
466466
}
467467

468468
impl ItemPathPrinter for LocalPathPrinter {
469469
type Path = String;
470470

471-
fn print_item_path(cx: &mut PrintCx<'_, '_, '_>, def_id: DefId) -> Self::Path {
472-
Self::try_print_visible_item_path(cx, def_id)
473-
.unwrap_or_else(|| cx.default_print_item_path::<Self>(def_id))
471+
fn print_item_path(self: &mut PrintCx<'_, '_, '_, Self>, def_id: DefId) -> Self::Path {
472+
self.try_print_visible_item_path(def_id)
473+
.unwrap_or_else(|| self.default_print_item_path(def_id))
474474
}
475475

476-
fn path_crate(cx: &mut PrintCx<'_, '_, '_>, cnum: CrateNum) -> Self::Path {
476+
fn path_crate(self: &mut PrintCx<'_, '_, '_, Self>, cnum: CrateNum) -> Self::Path {
477477
if cnum == LOCAL_CRATE {
478-
if cx.tcx.sess.rust_2018() {
478+
if self.tcx.sess.rust_2018() {
479479
// We add the `crate::` keyword on Rust 2018, only when desired.
480480
if SHOULD_PREFIX_WITH_CRATE.with(|flag| flag.get()) {
481481
return keywords::Crate.name().to_string();
482482
}
483483
}
484484
String::new()
485485
} else {
486-
cx.tcx.crate_name(cnum).to_string()
486+
self.tcx.crate_name(cnum).to_string()
487487
}
488488
}
489-
fn path_impl(text: &str) -> Self::Path {
489+
fn path_impl(self: &mut PrintCx<'_, '_, '_, Self>, text: &str) -> Self::Path {
490490
text.to_string()
491491
}
492-
fn path_append(mut path: Self::Path, text: &str) -> Self::Path {
492+
fn path_append(
493+
self: &mut PrintCx<'_, '_, '_, Self>,
494+
mut path: Self::Path,
495+
text: &str,
496+
) -> Self::Path {
493497
if !path.is_empty() {
494498
path.push_str("::");
495499
}

0 commit comments

Comments
 (0)