Skip to content

Commit 72690d2

Browse files
committed
rustc: always hide defaulted generic args, even in verbose mode.
1 parent 381fa7a commit 72690d2

File tree

4 files changed

+30
-27
lines changed

4 files changed

+30
-27
lines changed

src/librustc/ty/print/mod.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::hir::map::DefPathData;
22
use crate::hir::def_id::{CrateNum, DefId};
33
use crate::ty::{self, DefIdTree, Ty, TyCtxt};
4-
use crate::ty::subst::{Subst, SubstsRef};
4+
use crate::ty::subst::{Kind, Subst, SubstsRef};
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,30 @@ 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: SubstsRef<'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+
ty::GenericParamDefKind::Const => false, // FIXME(const_generics:defaults)
228+
}
229+
}).count();
230+
&params[..params.len() - num_supplied_defaults]
231+
}
232+
210233
fn default_print_impl_path(
211234
self,
212235
impl_def_id: DefId,

src/librustc/ty/print/pretty.rs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -502,25 +502,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> {
502502
_ => false,
503503
}
504504
});
505-
506-
// Don't print args that are the defaults of their respective parameters.
507-
let num_supplied_defaults = if self.tcx.sess.verbose() {
508-
0
509-
} else {
510-
params.iter().rev().take_while(|param| {
511-
match param.kind {
512-
ty::GenericParamDefKind::Lifetime => false,
513-
ty::GenericParamDefKind::Type { has_default, .. } => {
514-
has_default && substs[param.index as usize] == Kind::from(
515-
self.tcx.type_of(param.def_id).subst(self.tcx, substs)
516-
)
517-
}
518-
ty::GenericParamDefKind::Const => false, // FIXME(const_generics:defaults)
519-
}
520-
}).count()
521-
};
522-
523-
let params = &params[..params.len() - num_supplied_defaults];
524505
let mut args = params.iter().map(|param| {
525506
substs[param.index as usize]
526507
}).filter(|arg| {
@@ -657,8 +638,7 @@ impl<F: fmt::Write> Printer for FmtPrinter<F> {
657638
})?;
658639
if visible_path_success {
659640
return if let (Some(generics), Some(substs)) = (generics, substs) {
660-
let has_own_self = generics.has_self && generics.parent_count == 0;
661-
let params = &generics.params[has_own_self as usize..];
641+
let params = self.generic_params_to_print(generics, substs);
662642
self.path_generic_args(|cx| cx.ok(), params, substs, projections)
663643
} else {
664644
self.ok()

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)