From fe1f651e4c28bb39da739f1433d94c6ed6caec82 Mon Sep 17 00:00:00 2001 From: varkor Date: Fri, 11 May 2018 01:56:05 +0100 Subject: [PATCH] Review refactoring --- src/librustc/traits/error_reporting.rs | 2 +- src/librustc/traits/on_unimplemented.rs | 11 ++++---- src/librustc/util/ppaux.rs | 32 +++++++++++------------ src/librustc_typeck/collect.rs | 21 +++++++-------- src/librustc_typeck/impl_wf_check.rs | 34 ++++++++++++------------- 5 files changed, 48 insertions(+), 52 deletions(-) diff --git a/src/librustc/traits/error_reporting.rs b/src/librustc/traits/error_reporting.rs index f56720328b2..18c0020a2b4 100644 --- a/src/librustc/traits/error_reporting.rs +++ b/src/librustc/traits/error_reporting.rs @@ -380,7 +380,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { } for param in generics.params.iter() { - let name = param.name.to_string(); let value = match param.kind { GenericParamDefKind::Type(_) => { let ty = trait_ref.substs.type_for_def(¶m); @@ -388,6 +387,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> { }, GenericParamDefKind::Lifetime => continue, }; + let name = param.name.to_string(); flags.push((name.clone(), Some(value.clone()))); } diff --git a/src/librustc/traits/on_unimplemented.rs b/src/librustc/traits/on_unimplemented.rs index ff10c430f7b..bc558c2933c 100644 --- a/src/librustc/traits/on_unimplemented.rs +++ b/src/librustc/traits/on_unimplemented.rs @@ -288,13 +288,14 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString { let trait_str = tcx.item_path_str(trait_ref.def_id); let generics = tcx.generics_of(trait_ref.def_id); let generic_map = generics.params.iter().filter_map(|param| { - match param.kind { + let value = match param.kind { GenericParamDefKind::Type(_) => { - Some((param.name.to_string(), - trait_ref.substs.type_for_def(¶m).to_string())) + trait_ref.substs.type_for_def(¶m).to_string() }, - GenericParamDefKind::Lifetime => None - } + GenericParamDefKind::Lifetime => return None + }; + let name = param.name.to_string(); + Some((name, value)) }).collect::>(); let parser = Parser::new(&self.0); diff --git a/src/librustc/util/ppaux.rs b/src/librustc/util/ppaux.rs index 7044404e655..eaae874635f 100644 --- a/src/librustc/util/ppaux.rs +++ b/src/librustc/util/ppaux.rs @@ -25,7 +25,6 @@ use util::nodemap::FxHashSet; use std::cell::Cell; use std::fmt; use std::usize; -use std::iter; use rustc_data_structures::indexed_vec::Idx; use rustc_target::spec::abi::Abi; @@ -342,23 +341,22 @@ impl PrintContext { GenericParamDefKind::Type(ty) => Some((param.def_id, ty.has_default)), GenericParamDefKind::Lifetime => None, } - }); - if let Some(last_ty) = type_params.next() { - let (_, has_default) = last_ty; - if has_default { - if let Some(substs) = tcx.lift(&substs) { - let mut types = substs.types().rev().skip(child_types); - let zipped = iter::once((last_ty, types.next().unwrap())) - .chain(type_params.zip(types)); - for ((def_id, has_default), actual) in zipped { - if !has_default { - break; - } - if tcx.type_of(def_id).subst(tcx, substs) != actual { - break; - } - num_supplied_defaults += 1; + }).peekable(); + let has_default = { + let has_default = type_params.peek().map(|(_, has_default)| has_default); + *has_default.unwrap_or(&false) + }; + if has_default { + if let Some(substs) = tcx.lift(&substs) { + let mut types = substs.types().rev().skip(child_types); + for ((def_id, has_default), actual) in type_params.zip(types) { + if !has_default { + break; } + if tcx.type_of(def_id).subst(tcx, substs) != actual { + break; + } + num_supplied_defaults += 1; } } } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 1cc2730158f..4ddbe584846 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -886,8 +886,10 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, generics.parent_count + generics.params.len() }); + let mut params: Vec<_> = opt_self.into_iter().collect(); + let early_lifetimes = early_bound_lifetimes_from_generics(tcx, ast_generics); - let lifetimes = early_lifetimes.enumerate().map(|(i, l)| { + params.extend(early_lifetimes.enumerate().map(|(i, l)| { ty::GenericParamDef { name: l.lifetime.name.name().as_interned_str(), index: own_start + i as u32, @@ -895,14 +897,14 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, pure_wrt_drop: l.pure_wrt_drop, kind: ty::GenericParamDefKind::Lifetime, } - }).collect::>(); + })); let hir_id = tcx.hir.node_to_hir_id(node_id); let object_lifetime_defaults = tcx.object_lifetime_defaults(hir_id); // Now create the real type parameters. - let type_start = own_start + lifetimes.len() as u32; - let mut types: Vec<_> = ast_generics.ty_params().enumerate().map(|(i, p)| { + let type_start = params.len() as u32; + params.extend(ast_generics.ty_params().enumerate().map(|(i, p)| { if p.name == keywords::SelfType.name() { span_bug!(p.span, "`Self` should not be the name of a regular parameter"); } @@ -930,7 +932,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, synthetic: p.synthetic, }), } - }).collect(); + })); // provide junk type parameter defs - the only place that // cares about anything but the length is instantiation, @@ -943,7 +945,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, }; for (i, &arg) in dummy_args.iter().enumerate() { - types.push(ty::GenericParamDef { + params.push(ty::GenericParamDef { index: type_start + i as u32, name: Symbol::intern(arg).as_interned_str(), def_id, @@ -957,7 +959,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, } tcx.with_freevars(node_id, |fv| { - types.extend(fv.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| { + params.extend(fv.iter().zip((dummy_args.len() as u32)..).map(|(_, i)| { ty::GenericParamDef { index: type_start + i, name: Symbol::intern("").as_interned_str(), @@ -973,11 +975,6 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, }); } - let params: Vec<_> = opt_self.into_iter() - .chain(lifetimes) - .chain(types) - .collect(); - let param_def_id_to_index = params.iter() .map(|param| (param.def_id, param.index)) .collect(); diff --git a/src/librustc_typeck/impl_wf_check.rs b/src/librustc_typeck/impl_wf_check.rs index ef699b201a5..1ba967ce4b0 100644 --- a/src/librustc_typeck/impl_wf_check.rs +++ b/src/librustc_typeck/impl_wf_check.rs @@ -72,10 +72,9 @@ struct ImplWfCheck<'a, 'tcx: 'a> { impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> { fn visit_item(&mut self, item: &'tcx hir::Item) { match item.node { - hir::ItemImpl(.., ref generics, _, _, ref impl_item_refs) => { + hir::ItemImpl(.., _, _, _, ref impl_item_refs) => { let impl_def_id = self.tcx.hir.local_def_id(item.id); enforce_impl_params_are_constrained(self.tcx, - generics, impl_def_id, impl_item_refs); enforce_impl_items_are_distinct(self.tcx, impl_item_refs); @@ -90,7 +89,6 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> { } fn enforce_impl_params_are_constrained<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, - impl_hir_generics: &hir::Generics, impl_def_id: DefId, impl_item_refs: &[hir::ImplItemRef]) { @@ -115,26 +113,28 @@ fn enforce_impl_params_are_constrained<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, ctp::parameters_for(&tcx.type_of(def_id), true) }).collect(); - for (ty_param, hir_param) in impl_generics.params.iter() - .zip(impl_hir_generics.params.iter()) { - match (&ty_param.kind, hir_param) { + for param in &impl_generics.params { + match param.kind { // Disallow ANY unconstrained type parameters. - (&ty::GenericParamDefKind::Type(_), hir::GenericParam::Type(hir_ty)) => { - let param_ty = ty::ParamTy::for_def(ty_param); + ty::GenericParamDefKind::Type(_) => { + let param_ty = ty::ParamTy::for_def(param); if !input_parameters.contains(&ctp::Parameter::from(param_ty)) { - report_unused_parameter(tcx, hir_ty.span, "type", ¶m_ty.to_string()); + report_unused_parameter(tcx, + tcx.def_span(param.def_id), + "type", + ¶m_ty.to_string()); } } - (&ty::GenericParamDefKind::Lifetime, hir::GenericParam::Lifetime(hir_lt)) => { - let param = ctp::Parameter::from(ty_param.to_early_bound_region_data()); - if lifetimes_in_associated_types.contains(¶m) && // (*) - !input_parameters.contains(¶m) { - report_unused_parameter(tcx, hir_lt.lifetime.span, - "lifetime", &hir_lt.lifetime.name.name().to_string()); + ty::GenericParamDefKind::Lifetime => { + let param_lt = ctp::Parameter::from(param.to_early_bound_region_data()); + if lifetimes_in_associated_types.contains(¶m_lt) && // (*) + !input_parameters.contains(¶m_lt) { + report_unused_parameter(tcx, + tcx.def_span(param.def_id), + "lifetime", + ¶m.name.to_string()); } } - (&ty::GenericParamDefKind::Type(_), _) => continue, - (&ty::GenericParamDefKind::Lifetime, _) => continue, } }