Skip to content

Commit 9e44ba3

Browse files
committed
rustc: make pretty_path_generic_args' task as simple as possible.
1 parent 2311ae9 commit 9e44ba3

File tree

5 files changed

+70
-88
lines changed

5 files changed

+70
-88
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -447,7 +447,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
447447
) {
448448
use hir::def_id::CrateNum;
449449
use ty::print::{PrintCx, Printer};
450-
use ty::subst::Substs;
450+
use ty::subst::Kind;
451451

452452
struct AbsolutePathPrinter;
453453

@@ -514,8 +514,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
514514
print_prefix: impl FnOnce(
515515
PrintCx<'_, 'gcx, 'tcx, Self>,
516516
) -> Result<Self::Path, Self::Error>,
517-
_params: &[ty::GenericParamDef],
518-
_substs: &'tcx Substs<'tcx>,
517+
_args: impl Iterator<Item = Kind<'tcx>> + Clone,
519518
_projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
520519
) -> Result<Self::Path, Self::Error> {
521520
print_prefix(self)

src/librustc/ty/print/mod.rs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,7 @@ pub trait Printer: Sized {
123123
print_prefix: impl FnOnce(
124124
PrintCx<'_, 'gcx, 'tcx, Self>,
125125
) -> Result<Self::Path, Self::Error>,
126-
params: &[ty::GenericParamDef],
127-
substs: &'tcx Substs<'tcx>,
126+
args: impl Iterator<Item = Kind<'tcx>> + Clone,
128127
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
129128
) -> Result<Self::Path, Self::Error>;
130129
}
@@ -197,20 +196,20 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
197196
};
198197

199198
if let (Some(generics), Some(substs)) = (generics, substs) {
200-
let params = self.generic_params_to_print(generics, substs);
201-
self.path_generic_args(print_path, params, substs, projections)
199+
let args = self.generic_args_to_print(generics, substs);
200+
self.path_generic_args(print_path, args, projections)
202201
} else {
203202
print_path(self)
204203
}
205204
}
206205
}
207206
}
208207

209-
pub fn generic_params_to_print(
208+
pub fn generic_args_to_print(
210209
&self,
211-
generics: &'a ty::Generics,
210+
generics: &'tcx ty::Generics,
212211
substs: &'tcx Substs<'tcx>,
213-
) -> &'a [ty::GenericParamDef] {
212+
) -> impl Iterator<Item = Kind<'tcx>> + Clone {
214213
// Don't print args for `Self` parameters (of traits).
215214
let has_own_self = generics.has_self && generics.parent_count == 0;
216215
let params = &generics.params[has_own_self as usize..];
@@ -226,7 +225,9 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
226225
}
227226
}
228227
}).count();
229-
&params[..params.len() - num_supplied_defaults]
228+
params[..params.len() - num_supplied_defaults].iter().map(move |param| {
229+
substs[param.index as usize]
230+
})
230231
}
231232

232233
fn default_print_impl_path(

src/librustc/ty/print/pretty.rs

Lines changed: 48 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -207,21 +207,30 @@ pub trait PrettyPrinter:
207207
value.skip_binder().print(self)
208208
}
209209

210+
/// Print comma-separated elements.
211+
fn comma_sep<T>(
212+
mut self: PrintCx<'_, '_, 'tcx, Self>,
213+
mut elems: impl Iterator<Item = T>,
214+
comma: &str,
215+
) -> Result<Self, Self::Error>
216+
where T: Print<'tcx, Self, Output = Self, Error = Self::Error>
217+
{
218+
if let Some(first) = elems.next() {
219+
self = self.nest(|cx| first.print(cx))?;
220+
for elem in elems {
221+
self.write_str(comma)?;
222+
self = self.nest(|cx| elem.print(cx))?;
223+
}
224+
}
225+
self.ok()
226+
}
227+
210228
/// Print `<...>` around what `f` prints.
211229
fn generic_delimiters<'gcx, 'tcx>(
212230
self: PrintCx<'_, 'gcx, 'tcx, Self>,
213231
f: impl FnOnce(PrintCx<'_, 'gcx, 'tcx, Self>) -> Result<Self, Self::Error>,
214232
) -> Result<Self, Self::Error>;
215233

216-
/// Return `true` if the region should be printed in path generic args
217-
/// even when it's `'_`, such as in e.g. `Foo<'_, '_, '_>`.
218-
fn always_print_region_in_paths(
219-
self: &PrintCx<'_, '_, '_, Self>,
220-
_region: ty::Region<'_>,
221-
) -> bool {
222-
false
223-
}
224-
225234
/// Return `true` if the region should be printed in
226235
/// optional positions, e.g. `&'a T` or `dyn Tr + 'b`.
227236
/// This is typically the case for all non-`'_` regions.
@@ -481,66 +490,25 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
481490
print_prefix: impl FnOnce(
482491
PrintCx<'_, 'gcx, 'tcx, P>,
483492
) -> Result<P::Path, P::Error>,
484-
params: &[ty::GenericParamDef],
485-
substs: &'tcx Substs<'tcx>,
486-
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
493+
mut args: impl Iterator<Item = Kind<'tcx>>,
494+
mut projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
487495
) -> Result<P::Path, P::Error> {
488496
self = self.nest(print_prefix)?;
489497

490-
// Don't print `'_` if there's no printed region.
491-
let print_regions = params.iter().any(|param| {
492-
match substs[param.index as usize].unpack() {
493-
UnpackedKind::Lifetime(r) => {
494-
self.always_print_region_in_paths(r) ||
495-
self.region_should_not_be_omitted(r)
496-
}
497-
_ => false,
498-
}
499-
});
500-
let mut args = params.iter().map(|param| {
501-
substs[param.index as usize]
502-
}).filter(|arg| {
503-
match arg.unpack() {
504-
UnpackedKind::Lifetime(_) => print_regions,
505-
_ => true,
506-
}
507-
});
508498
let arg0 = args.next();
509-
510-
let mut projections = projections;
511499
let projection0 = projections.next();
512-
513500
if arg0.is_none() && projection0.is_none() {
514501
return self.ok();
515502
}
503+
let args = arg0.into_iter().chain(args);
504+
let projections = projection0.into_iter().chain(projections);
516505

517506
self.generic_delimiters(|mut cx| {
518-
define_scoped_cx!(cx);
519-
520-
let mut empty = true;
521-
let mut maybe_comma = |cx: &mut Self| {
522-
if empty {
523-
empty = false;
524-
Ok(())
525-
} else {
526-
write!(cx, ", ")
527-
}
528-
};
529-
530-
for arg in arg0.into_iter().chain(args) {
531-
maybe_comma(&mut cx)?;
532-
533-
p!(print(arg));
507+
cx = cx.nest(|cx| cx.comma_sep(args, ", "))?;
508+
if arg0.is_some() && projection0.is_some() {
509+
write!(cx, ", ")?;
534510
}
535-
536-
for projection in projection0.into_iter().chain(projections) {
537-
maybe_comma(&mut cx)?;
538-
539-
p!(write("{}=", cx.tcx.associated_item(projection.item_def_id).ident),
540-
print(projection.ty));
541-
}
542-
543-
cx.ok()
511+
cx.comma_sep(projections, ", ")
544512
})
545513
}
546514
}
@@ -620,8 +588,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
620588
})?;
621589
if visible_path_success {
622590
return if let (Some(generics), Some(substs)) = (generics, substs) {
623-
let params = self.generic_params_to_print(generics, substs);
624-
self.path_generic_args(|cx| cx.ok(), params, substs, projections)
591+
let args = self.generic_args_to_print(generics, substs);
592+
self.path_generic_args(|cx| cx.ok(), args, projections)
625593
} else {
626594
self.ok()
627595
};
@@ -738,11 +706,23 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
738706
print_prefix: impl FnOnce(
739707
PrintCx<'_, 'gcx, 'tcx, Self>,
740708
) -> Result<Self::Path, Self::Error>,
741-
params: &[ty::GenericParamDef],
742-
substs: &'tcx Substs<'tcx>,
709+
args: impl Iterator<Item = Kind<'tcx>> + Clone,
743710
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
744711
) -> Result<Self::Path, Self::Error> {
745-
self.pretty_path_generic_args(print_prefix, params, substs, projections)
712+
// Don't print `'_` if there's no unerased regions.
713+
let print_regions = args.clone().any(|arg| {
714+
match arg.unpack() {
715+
UnpackedKind::Lifetime(r) => *r != ty::ReErased,
716+
_ => false,
717+
}
718+
});
719+
let args = args.filter(|arg| {
720+
match arg.unpack() {
721+
UnpackedKind::Lifetime(_) => print_regions,
722+
_ => true,
723+
}
724+
});
725+
self.pretty_path_generic_args(print_prefix, args, projections)
746726
}
747727
}
748728

@@ -797,13 +777,6 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
797777
Ok(inner)
798778
}
799779

800-
fn always_print_region_in_paths(
801-
self: &PrintCx<'_, '_, '_, Self>,
802-
region: ty::Region<'_>,
803-
) -> bool {
804-
*region != ty::ReErased
805-
}
806-
807780
fn region_should_not_be_omitted(
808781
self: &PrintCx<'_, '_, '_, Self>,
809782
region: ty::Region<'_>,
@@ -1484,6 +1457,11 @@ define_print_and_forward_display! {
14841457
}
14851458
}
14861459

1460+
ty::ExistentialProjection<'tcx> {
1461+
let name = cx.tcx.associated_item(self.item_def_id).ident;
1462+
p!(write("{}=", name), print(self.ty))
1463+
}
1464+
14871465
&'tcx ty::List<Ty<'tcx>> {
14881466
p!(write("{{"));
14891467
let mut tys = self.iter();

src/librustc_codegen_utils/symbol_names.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ use rustc::hir::map::definitions::DefPathData;
9494
use rustc::ich::NodeIdHashingMode;
9595
use rustc::ty::print::{PrettyPrinter, PrintCx, Printer};
9696
use rustc::ty::query::Providers;
97-
use rustc::ty::subst::Substs;
97+
use rustc::ty::subst::{Kind, Substs, UnpackedKind};
9898
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
9999
use rustc::util::common::record_time;
100100
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
@@ -504,11 +504,16 @@ impl Printer for SymbolPath {
504504
print_prefix: impl FnOnce(
505505
PrintCx<'_, 'gcx, 'tcx, Self>,
506506
) -> Result<Self::Path, Self::Error>,
507-
params: &[ty::GenericParamDef],
508-
substs: &'tcx Substs<'tcx>,
507+
args: impl Iterator<Item = Kind<'tcx>> + Clone,
509508
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
510509
) -> Result<Self::Path, Self::Error> {
511-
self.pretty_path_generic_args(print_prefix, params, substs, projections)
510+
let args = args.filter(|arg| {
511+
match arg.unpack() {
512+
UnpackedKind::Lifetime(_) => false,
513+
_ => true,
514+
}
515+
});
516+
self.pretty_path_generic_args(print_prefix, args, projections)
512517
}
513518
}
514519

src/librustdoc/clean/mod.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc::mir::interpret::GlobalId;
2020
use rustc::hir::{self, GenericArg, HirVec};
2121
use rustc::hir::def::{self, Def, CtorKind};
2222
use rustc::hir::def_id::{CrateNum, DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
23-
use rustc::ty::subst::Substs;
23+
use rustc::ty::subst::{Kind, Substs};
2424
use rustc::ty::{self, DefIdTree, TyCtxt, Region, RegionVid, Ty, AdtKind};
2525
use rustc::ty::fold::TypeFolder;
2626
use rustc::ty::layout::VariantIdx;
@@ -4124,8 +4124,7 @@ where F: Fn(DefId) -> Def {
41244124
print_prefix: impl FnOnce(
41254125
PrintCx<'_, 'gcx, 'tcx, Self>,
41264126
) -> Result<Self::Path, Self::Error>,
4127-
_params: &[ty::GenericParamDef],
4128-
_substs: &'tcx Substs<'tcx>,
4127+
_args: impl Iterator<Item = Kind<'tcx>> + Clone,
41294128
_projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
41304129
) -> Result<Self::Path, Self::Error> {
41314130
print_prefix(self)

0 commit comments

Comments
 (0)