Skip to content

Commit ffa00d4

Browse files
committed
rustc: make pretty_path_generic_args' task as simple as possible.
1 parent 4deaa69 commit ffa00d4

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
@@ -446,7 +446,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
446446
) {
447447
use hir::def_id::CrateNum;
448448
use ty::print::{PrintCx, Printer};
449-
use ty::subst::SubstsRef;
449+
use ty::subst::Kind;
450450

451451
struct AbsolutePathPrinter;
452452

@@ -513,8 +513,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
513513
print_prefix: impl FnOnce(
514514
PrintCx<'_, 'gcx, 'tcx, Self>,
515515
) -> Result<Self::Path, Self::Error>,
516-
_params: &[ty::GenericParamDef],
517-
_substs: SubstsRef<'tcx>,
516+
_args: impl Iterator<Item = Kind<'tcx>> + Clone,
518517
_projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
519518
) -> Result<Self::Path, Self::Error> {
520519
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: SubstsRef<'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: SubstsRef<'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..];
@@ -227,7 +226,9 @@ impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
227226
ty::GenericParamDefKind::Const => false, // FIXME(const_generics:defaults)
228227
}
229228
}).count();
230-
&params[..params.len() - num_supplied_defaults]
229+
params[..params.len() - num_supplied_defaults].iter().map(move |param| {
230+
substs[param.index as usize]
231+
})
231232
}
232233

233234
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.
@@ -482,66 +491,25 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
482491
print_prefix: impl FnOnce(
483492
PrintCx<'_, 'gcx, 'tcx, P>,
484493
) -> Result<P::Path, P::Error>,
485-
params: &[ty::GenericParamDef],
486-
substs: SubstsRef<'tcx>,
487-
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
494+
mut args: impl Iterator<Item = Kind<'tcx>>,
495+
mut projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
488496
) -> Result<P::Path, P::Error> {
489497
self = self.nest(print_prefix)?;
490498

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

518507
self.generic_delimiters(|mut cx| {
519-
define_scoped_cx!(cx);
520-
521-
let mut empty = true;
522-
let mut maybe_comma = |cx: &mut Self| {
523-
if empty {
524-
empty = false;
525-
Ok(())
526-
} else {
527-
write!(cx, ", ")
528-
}
529-
};
530-
531-
for arg in arg0.into_iter().chain(args) {
532-
maybe_comma(&mut cx)?;
533-
534-
p!(print(arg));
508+
cx = cx.nest(|cx| cx.comma_sep(args, ", "))?;
509+
if arg0.is_some() && projection0.is_some() {
510+
write!(cx, ", ")?;
535511
}
536-
537-
for projection in projection0.into_iter().chain(projections) {
538-
maybe_comma(&mut cx)?;
539-
540-
p!(write("{}=", cx.tcx.associated_item(projection.item_def_id).ident),
541-
print(projection.ty));
542-
}
543-
544-
cx.ok()
512+
cx.comma_sep(projections, ", ")
545513
})
546514
}
547515
}
@@ -621,8 +589,8 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
621589
})?;
622590
if visible_path_success {
623591
return if let (Some(generics), Some(substs)) = (generics, substs) {
624-
let params = self.generic_params_to_print(generics, substs);
625-
self.path_generic_args(|cx| cx.ok(), params, substs, projections)
592+
let args = self.generic_args_to_print(generics, substs);
593+
self.path_generic_args(|cx| cx.ok(), args, projections)
626594
} else {
627595
self.ok()
628596
};
@@ -739,11 +707,23 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
739707
print_prefix: impl FnOnce(
740708
PrintCx<'_, 'gcx, 'tcx, Self>,
741709
) -> Result<Self::Path, Self::Error>,
742-
params: &[ty::GenericParamDef],
743-
substs: SubstsRef<'tcx>,
710+
args: impl Iterator<Item = Kind<'tcx>> + Clone,
744711
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
745712
) -> Result<Self::Path, Self::Error> {
746-
self.pretty_path_generic_args(print_prefix, params, substs, projections)
713+
// Don't print `'_` if there's no unerased regions.
714+
let print_regions = args.clone().any(|arg| {
715+
match arg.unpack() {
716+
UnpackedKind::Lifetime(r) => *r != ty::ReErased,
717+
_ => false,
718+
}
719+
});
720+
let args = args.filter(|arg| {
721+
match arg.unpack() {
722+
UnpackedKind::Lifetime(_) => print_regions,
723+
_ => true,
724+
}
725+
});
726+
self.pretty_path_generic_args(print_prefix, args, projections)
747727
}
748728
}
749729

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

801-
fn always_print_region_in_paths(
802-
self: &PrintCx<'_, '_, '_, Self>,
803-
region: ty::Region<'_>,
804-
) -> bool {
805-
*region != ty::ReErased
806-
}
807-
808781
fn region_should_not_be_omitted(
809782
self: &PrintCx<'_, '_, '_, Self>,
810783
region: ty::Region<'_>,
@@ -1498,6 +1471,11 @@ define_print_and_forward_display! {
14981471
}
14991472
}
15001473

1474+
ty::ExistentialProjection<'tcx> {
1475+
let name = cx.tcx.associated_item(self.item_def_id).ident;
1476+
p!(write("{}=", name), print(self.ty))
1477+
}
1478+
15011479
&'tcx ty::List<Ty<'tcx>> {
15021480
p!(write("{{"));
15031481
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::SubstsRef;
97+
use rustc::ty::subst::{Kind, SubstsRef, 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};
@@ -503,11 +503,16 @@ impl Printer for SymbolPath {
503503
print_prefix: impl FnOnce(
504504
PrintCx<'_, 'gcx, 'tcx, Self>,
505505
) -> Result<Self::Path, Self::Error>,
506-
params: &[ty::GenericParamDef],
507-
substs: SubstsRef<'tcx>,
506+
args: impl Iterator<Item = Kind<'tcx>> + Clone,
508507
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
509508
) -> Result<Self::Path, Self::Error> {
510-
self.pretty_path_generic_args(print_prefix, params, substs, projections)
509+
let args = args.filter(|arg| {
510+
match arg.unpack() {
511+
UnpackedKind::Lifetime(_) => false,
512+
_ => true,
513+
}
514+
});
515+
self.pretty_path_generic_args(print_prefix, args, projections)
511516
}
512517
}
513518

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::{InternalSubsts, SubstsRef};
23+
use rustc::ty::subst::{Kind, InternalSubsts, SubstsRef};
2424
use rustc::ty::{self, DefIdTree, TyCtxt, Region, RegionVid, Ty, AdtKind};
2525
use rustc::ty::fold::TypeFolder;
2626
use rustc::ty::layout::VariantIdx;
@@ -4304,8 +4304,7 @@ where F: Fn(DefId) -> Def {
43044304
print_prefix: impl FnOnce(
43054305
PrintCx<'_, 'gcx, 'tcx, Self>,
43064306
) -> Result<Self::Path, Self::Error>,
4307-
_params: &[ty::GenericParamDef],
4308-
_substs: SubstsRef<'tcx>,
4307+
_args: impl Iterator<Item = Kind<'tcx>> + Clone,
43094308
_projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
43104309
) -> Result<Self::Path, Self::Error> {
43114310
print_prefix(self)

0 commit comments

Comments
 (0)