Skip to content

Commit ac3c32c

Browse files
committed
rustc: make pretty_path_generic_args' task as simple as possible.
1 parent f8e4029 commit ac3c32c

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
@@ -208,21 +208,30 @@ pub trait PrettyPrinter:
208208
value.skip_binder().print(self)
209209
}
210210

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

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

494-
// Don't print `'_` if there's no printed region.
495-
let print_regions = params.iter().any(|param| {
496-
match substs[param.index as usize].unpack() {
497-
UnpackedKind::Lifetime(r) => {
498-
self.always_print_region_in_paths(r) ||
499-
self.region_should_not_be_omitted(r)
500-
}
501-
_ => false,
502-
}
503-
});
504-
let mut args = params.iter().map(|param| {
505-
substs[param.index as usize]
506-
}).filter(|arg| {
507-
match arg.unpack() {
508-
UnpackedKind::Lifetime(_) => print_regions,
509-
_ => true,
510-
}
511-
});
512502
let arg0 = args.next();
513-
514-
let mut projections = projections;
515503
let projection0 = projections.next();
516-
517504
if arg0.is_none() && projection0.is_none() {
518505
return self.ok();
519506
}
507+
let args = arg0.into_iter().chain(args);
508+
let projections = projection0.into_iter().chain(projections);
520509

521510
self.generic_delimiters(|mut cx| {
522-
define_scoped_cx!(cx);
523-
524-
let mut empty = true;
525-
let mut maybe_comma = |cx: &mut Self| {
526-
if empty {
527-
empty = false;
528-
Ok(())
529-
} else {
530-
write!(cx, ", ")
531-
}
532-
};
533-
534-
for arg in arg0.into_iter().chain(args) {
535-
maybe_comma(&mut cx)?;
536-
537-
p!(print(arg));
511+
cx = cx.nest(|cx| cx.comma_sep(args, ", "))?;
512+
if arg0.is_some() && projection0.is_some() {
513+
write!(cx, ", ")?;
538514
}
539-
540-
for projection in projection0.into_iter().chain(projections) {
541-
maybe_comma(&mut cx)?;
542-
543-
p!(write("{}=", cx.tcx.associated_item(projection.item_def_id).ident),
544-
print(projection.ty));
545-
}
546-
547-
cx.ok()
515+
cx.comma_sep(projections, ", ")
548516
})
549517
}
550518
}
@@ -624,8 +592,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
624592
})?;
625593
if visible_path_success {
626594
return if let (Some(generics), Some(substs)) = (generics, substs) {
627-
let params = self.generic_params_to_print(generics, substs);
628-
self.path_generic_args(|cx| cx.ok(), params, substs, projections)
595+
let args = self.generic_args_to_print(generics, substs);
596+
self.path_generic_args(|cx| cx.ok(), args, projections)
629597
} else {
630598
self.ok()
631599
};
@@ -742,11 +710,23 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
742710
print_prefix: impl FnOnce(
743711
PrintCx<'_, 'gcx, 'tcx, Self>,
744712
) -> Result<Self::Path, Self::Error>,
745-
params: &[ty::GenericParamDef],
746-
substs: &'tcx Substs<'tcx>,
713+
args: impl Iterator<Item = Kind<'tcx>> + Clone,
747714
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
748715
) -> Result<Self::Path, Self::Error> {
749-
self.pretty_path_generic_args(print_prefix, params, substs, projections)
716+
// Don't print `'_` if there's no unerased regions.
717+
let print_regions = args.clone().any(|arg| {
718+
match arg.unpack() {
719+
UnpackedKind::Lifetime(r) => *r != ty::ReErased,
720+
_ => false,
721+
}
722+
});
723+
let args = args.filter(|arg| {
724+
match arg.unpack() {
725+
UnpackedKind::Lifetime(_) => print_regions,
726+
_ => true,
727+
}
728+
});
729+
self.pretty_path_generic_args(print_prefix, args, projections)
750730
}
751731
}
752732

@@ -801,13 +781,6 @@ impl<F: fmt::Write> PrettyPrinter for FmtPrinter<F> {
801781
Ok(inner)
802782
}
803783

804-
fn always_print_region_in_paths(
805-
self: &PrintCx<'_, '_, '_, Self>,
806-
region: ty::Region<'_>,
807-
) -> bool {
808-
*region != ty::ReErased
809-
}
810-
811784
fn region_should_not_be_omitted(
812785
self: &PrintCx<'_, '_, '_, Self>,
813786
region: ty::Region<'_>,
@@ -1488,6 +1461,11 @@ define_print_and_forward_display! {
14881461
}
14891462
}
14901463

1464+
ty::ExistentialProjection<'tcx> {
1465+
let name = cx.tcx.associated_item(self.item_def_id).ident;
1466+
p!(write("{}=", name), print(self.ty))
1467+
}
1468+
14911469
&'tcx ty::List<Ty<'tcx>> {
14921470
p!(write("{{"));
14931471
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)