Skip to content

Commit ca9666e

Browse files
committed
rustc: always hide defaulted generic args, even in verbose mode.
1 parent dda6ae8 commit ca9666e

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

src/librustc/ty/print/mod.rs

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use hir::map::DefPathData;
22
use hir::def_id::{CrateNum, DefId};
33
use ty::{self, DefIdTree, Ty, TyCtxt};
4-
use ty::subst::{Subst, Substs};
4+
use ty::subst::{Kind, Subst, Substs};
55

66
use rustc_data_structures::fx::FxHashSet;
77

@@ -129,7 +129,7 @@ pub trait Printer: Sized {
129129
) -> Result<Self::Path, Self::Error>;
130130
}
131131

132-
impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
132+
impl<P: Printer> PrintCx<'_, 'gcx, 'tcx, P> {
133133
pub fn default_print_def_path(
134134
self,
135135
def_id: DefId,
@@ -197,8 +197,7 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
197197
};
198198

199199
if let (Some(generics), Some(substs)) = (generics, substs) {
200-
let has_own_self = generics.has_self && generics.parent_count == 0;
201-
let params = &generics.params[has_own_self as usize..];
200+
let params = self.generic_params_to_print(generics, substs);
202201
self.path_generic_args(print_path, params, substs, projections)
203202
} else {
204203
print_path(self)
@@ -207,6 +206,29 @@ impl<P: Printer> PrintCx<'a, 'gcx, 'tcx, P> {
207206
}
208207
}
209208

209+
pub fn generic_params_to_print(
210+
&self,
211+
generics: &'a ty::Generics,
212+
substs: &'tcx Substs<'tcx>,
213+
) -> &'a [ty::GenericParamDef] {
214+
// Don't print args for `Self` parameters (of traits).
215+
let has_own_self = generics.has_self && generics.parent_count == 0;
216+
let params = &generics.params[has_own_self as usize..];
217+
218+
// Don't print args that are the defaults of their respective parameters.
219+
let num_supplied_defaults = params.iter().rev().take_while(|param| {
220+
match param.kind {
221+
ty::GenericParamDefKind::Lifetime => false,
222+
ty::GenericParamDefKind::Type { has_default, .. } => {
223+
has_default && substs[param.index as usize] == Kind::from(
224+
self.tcx.type_of(param.def_id).subst(self.tcx, substs)
225+
)
226+
}
227+
}
228+
}).count();
229+
&params[..params.len() - num_supplied_defaults]
230+
}
231+
210232
fn default_print_impl_path(
211233
self,
212234
impl_def_id: DefId,

src/librustc/ty/print/pretty.rs

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -505,24 +505,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
505505
_ => false,
506506
}
507507
});
508-
509-
// Don't print args that are the defaults of their respective parameters.
510-
let num_supplied_defaults = if self.tcx.sess.verbose() {
511-
0
512-
} else {
513-
params.iter().rev().take_while(|param| {
514-
match param.kind {
515-
ty::GenericParamDefKind::Lifetime => false,
516-
ty::GenericParamDefKind::Type { has_default, .. } => {
517-
has_default && substs[param.index as usize] == Kind::from(
518-
self.tcx.type_of(param.def_id).subst(self.tcx, substs)
519-
)
520-
}
521-
}
522-
}).count()
523-
};
524-
525-
let params = &params[..params.len() - num_supplied_defaults];
526508
let mut args = params.iter().map(|param| {
527509
substs[param.index as usize]
528510
}).filter(|arg| {
@@ -659,8 +641,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
659641
})?;
660642
if visible_path_success {
661643
return if let (Some(generics), Some(substs)) = (generics, substs) {
662-
let has_own_self = generics.has_self && generics.parent_count == 0;
663-
let params = &generics.params[has_own_self as usize..];
644+
let params = self.generic_params_to_print(generics, substs);
664645
self.path_generic_args(|cx| cx.ok(), params, substs, projections)
665646
} else {
666647
self.ok()
@@ -778,7 +759,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
778759
print_prefix: impl FnOnce(
779760
PrintCx<'_, 'gcx, 'tcx, Self>,
780761
) -> Result<Self::Path, Self::Error>,
781-
params: &[ty::GenericParamDef],
762+
mut params: &[ty::GenericParamDef],
782763
substs: &'tcx Substs<'tcx>,
783764
projections: impl Iterator<Item = ty::ExistentialProjection<'tcx>>,
784765
) -> Result<Self::Path, Self::Error> {

src/test/ui/substs-ppaux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ fn foo<'z>() where &'z (): Sized {
2525
let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
2626
//[verbose]~^ ERROR mismatched types
2727
//[verbose]~| expected type `()`
28-
//[verbose]~| found type `fn() {<i8 as Foo<ReStatic, ReStatic, u32>>::bar::<ReStatic, char>}`
28+
//[verbose]~| found type `fn() {<i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>}`
2929
//[normal]~^^^^ ERROR mismatched types
3030
//[normal]~| expected type `()`
3131
//[normal]~| found type `fn() {<i8 as Foo<'static, 'static>>::bar::<'static, char>}`

src/test/ui/substs-ppaux.verbose.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ LL | let x: () = <i8 as Foo<'static, 'static, u32>>::bar::<'static, char>;
1414
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found fn item
1515
|
1616
= note: expected type `()`
17-
found type `fn() {<i8 as Foo<ReStatic, ReStatic, u32>>::bar::<ReStatic, char>}`
17+
found type `fn() {<i8 as Foo<ReStatic, ReStatic>>::bar::<ReStatic, char>}`
1818

1919
error[E0308]: mismatched types
2020
--> $DIR/substs-ppaux.rs:33:17

0 commit comments

Comments
 (0)