diff --git a/src/librustc/ty/print/mod.rs b/src/librustc/ty/print/mod.rs index aac76025951f..af6f8ba72f07 100644 --- a/src/librustc/ty/print/mod.rs +++ b/src/librustc/ty/print/mod.rs @@ -1,7 +1,7 @@ use crate::hir::map::DefPathData; use crate::hir::def_id::{CrateNum, DefId}; use crate::ty::{self, DefIdTree, Ty, TyCtxt}; -use crate::ty::subst::{Subst, SubstsRef}; +use crate::ty::subst::{Kind, Subst, SubstsRef}; use rustc_data_structures::fx::FxHashSet; @@ -129,7 +129,7 @@ pub trait Printer: Sized { ) -> Result; } -impl PrintCx<'a, 'gcx, 'tcx, P> { +impl PrintCx<'_, 'gcx, 'tcx, P> { pub fn default_print_def_path( self, def_id: DefId, @@ -197,8 +197,7 @@ impl PrintCx<'a, 'gcx, 'tcx, P> { }; if let (Some(generics), Some(substs)) = (generics, substs) { - let has_own_self = generics.has_self && generics.parent_count == 0; - let params = &generics.params[has_own_self as usize..]; + let params = self.generic_params_to_print(generics, substs); self.path_generic_args(print_path, params, substs, projections) } else { print_path(self) @@ -207,6 +206,30 @@ impl PrintCx<'a, 'gcx, 'tcx, P> { } } + pub fn generic_params_to_print( + &self, + generics: &'a ty::Generics, + substs: SubstsRef<'tcx>, + ) -> &'a [ty::GenericParamDef] { + // Don't print args for `Self` parameters (of traits). + let has_own_self = generics.has_self && generics.parent_count == 0; + let params = &generics.params[has_own_self as usize..]; + + // Don't print args that are the defaults of their respective parameters. + let num_supplied_defaults = params.iter().rev().take_while(|param| { + match param.kind { + ty::GenericParamDefKind::Lifetime => false, + ty::GenericParamDefKind::Type { has_default, .. } => { + has_default && substs[param.index as usize] == Kind::from( + self.tcx.type_of(param.def_id).subst(self.tcx, substs) + ) + } + ty::GenericParamDefKind::Const => false, // FIXME(const_generics:defaults) + } + }).count(); + ¶ms[..params.len() - num_supplied_defaults] + } + fn default_print_impl_path( self, impl_def_id: DefId, diff --git a/src/librustc/ty/print/pretty.rs b/src/librustc/ty/print/pretty.rs index 1f609dbc6e56..7358dd1932f4 100644 --- a/src/librustc/ty/print/pretty.rs +++ b/src/librustc/ty/print/pretty.rs @@ -502,25 +502,6 @@ impl<'gcx, 'tcx, P: PrettyPrinter> PrintCx<'_, 'gcx, 'tcx, P> { _ => false, } }); - - // Don't print args that are the defaults of their respective parameters. - let num_supplied_defaults = if self.tcx.sess.verbose() { - 0 - } else { - params.iter().rev().take_while(|param| { - match param.kind { - ty::GenericParamDefKind::Lifetime => false, - ty::GenericParamDefKind::Type { has_default, .. } => { - has_default && substs[param.index as usize] == Kind::from( - self.tcx.type_of(param.def_id).subst(self.tcx, substs) - ) - } - ty::GenericParamDefKind::Const => false, // FIXME(const_generics:defaults) - } - }).count() - }; - - let params = ¶ms[..params.len() - num_supplied_defaults]; let mut args = params.iter().map(|param| { substs[param.index as usize] }).filter(|arg| { @@ -657,8 +638,7 @@ impl Printer for FmtPrinter { })?; if visible_path_success { return if let (Some(generics), Some(substs)) = (generics, substs) { - let has_own_self = generics.has_self && generics.parent_count == 0; - let params = &generics.params[has_own_self as usize..]; + let params = self.generic_params_to_print(generics, substs); self.path_generic_args(|cx| cx.ok(), params, substs, projections) } else { self.ok() diff --git a/src/test/ui/substs-ppaux.rs b/src/test/ui/substs-ppaux.rs index 7ad7ccb54448..129ebd43594c 100644 --- a/src/test/ui/substs-ppaux.rs +++ b/src/test/ui/substs-ppaux.rs @@ -25,7 +25,7 @@ fn foo<'z>() where &'z (): Sized { let x: () = >::bar::<'static, char>; //[verbose]~^ ERROR mismatched types //[verbose]~| expected type `()` - //[verbose]~| found type `fn() {>::bar::}` + //[verbose]~| found type `fn() {>::bar::}` //[normal]~^^^^ ERROR mismatched types //[normal]~| expected type `()` //[normal]~| found type `fn() {>::bar::<'static, char>}` diff --git a/src/test/ui/substs-ppaux.verbose.stderr b/src/test/ui/substs-ppaux.verbose.stderr index 9d8a555dffe1..86936475f8c1 100644 --- a/src/test/ui/substs-ppaux.verbose.stderr +++ b/src/test/ui/substs-ppaux.verbose.stderr @@ -14,7 +14,7 @@ LL | let x: () = >::bar::<'static, char>; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected (), found fn item | = note: expected type `()` - found type `fn() {>::bar::}` + found type `fn() {>::bar::}` error[E0308]: mismatched types --> $DIR/substs-ppaux.rs:33:17