Review refactoring
This commit is contained in:
parent
fd8e284a20
commit
fe1f651e4c
5 changed files with 48 additions and 52 deletions
|
@ -380,7 +380,6 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
|
||||||
}
|
}
|
||||||
|
|
||||||
for param in generics.params.iter() {
|
for param in generics.params.iter() {
|
||||||
let name = param.name.to_string();
|
|
||||||
let value = match param.kind {
|
let value = match param.kind {
|
||||||
GenericParamDefKind::Type(_) => {
|
GenericParamDefKind::Type(_) => {
|
||||||
let ty = trait_ref.substs.type_for_def(¶m);
|
let ty = trait_ref.substs.type_for_def(¶m);
|
||||||
|
@ -388,6 +387,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
|
||||||
},
|
},
|
||||||
GenericParamDefKind::Lifetime => continue,
|
GenericParamDefKind::Lifetime => continue,
|
||||||
};
|
};
|
||||||
|
let name = param.name.to_string();
|
||||||
flags.push((name.clone(), Some(value.clone())));
|
flags.push((name.clone(), Some(value.clone())));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -288,13 +288,14 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
|
||||||
let trait_str = tcx.item_path_str(trait_ref.def_id);
|
let trait_str = tcx.item_path_str(trait_ref.def_id);
|
||||||
let generics = tcx.generics_of(trait_ref.def_id);
|
let generics = tcx.generics_of(trait_ref.def_id);
|
||||||
let generic_map = generics.params.iter().filter_map(|param| {
|
let generic_map = generics.params.iter().filter_map(|param| {
|
||||||
match param.kind {
|
let value = match param.kind {
|
||||||
GenericParamDefKind::Type(_) => {
|
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::<FxHashMap<String, String>>();
|
}).collect::<FxHashMap<String, String>>();
|
||||||
|
|
||||||
let parser = Parser::new(&self.0);
|
let parser = Parser::new(&self.0);
|
||||||
|
|
|
@ -25,7 +25,6 @@ use util::nodemap::FxHashSet;
|
||||||
use std::cell::Cell;
|
use std::cell::Cell;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::usize;
|
use std::usize;
|
||||||
use std::iter;
|
|
||||||
|
|
||||||
use rustc_data_structures::indexed_vec::Idx;
|
use rustc_data_structures::indexed_vec::Idx;
|
||||||
use rustc_target::spec::abi::Abi;
|
use rustc_target::spec::abi::Abi;
|
||||||
|
@ -342,23 +341,22 @@ impl PrintContext {
|
||||||
GenericParamDefKind::Type(ty) => Some((param.def_id, ty.has_default)),
|
GenericParamDefKind::Type(ty) => Some((param.def_id, ty.has_default)),
|
||||||
GenericParamDefKind::Lifetime => None,
|
GenericParamDefKind::Lifetime => None,
|
||||||
}
|
}
|
||||||
});
|
}).peekable();
|
||||||
if let Some(last_ty) = type_params.next() {
|
let has_default = {
|
||||||
let (_, has_default) = last_ty;
|
let has_default = type_params.peek().map(|(_, has_default)| has_default);
|
||||||
if has_default {
|
*has_default.unwrap_or(&false)
|
||||||
if let Some(substs) = tcx.lift(&substs) {
|
};
|
||||||
let mut types = substs.types().rev().skip(child_types);
|
if has_default {
|
||||||
let zipped = iter::once((last_ty, types.next().unwrap()))
|
if let Some(substs) = tcx.lift(&substs) {
|
||||||
.chain(type_params.zip(types));
|
let mut types = substs.types().rev().skip(child_types);
|
||||||
for ((def_id, has_default), actual) in zipped {
|
for ((def_id, has_default), actual) in type_params.zip(types) {
|
||||||
if !has_default {
|
if !has_default {
|
||||||
break;
|
break;
|
||||||
}
|
|
||||||
if tcx.type_of(def_id).subst(tcx, substs) != actual {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
num_supplied_defaults += 1;
|
|
||||||
}
|
}
|
||||||
|
if tcx.type_of(def_id).subst(tcx, substs) != actual {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
num_supplied_defaults += 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -886,8 +886,10 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||||
generics.parent_count + generics.params.len()
|
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 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 {
|
ty::GenericParamDef {
|
||||||
name: l.lifetime.name.name().as_interned_str(),
|
name: l.lifetime.name.name().as_interned_str(),
|
||||||
index: own_start + i as u32,
|
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,
|
pure_wrt_drop: l.pure_wrt_drop,
|
||||||
kind: ty::GenericParamDefKind::Lifetime,
|
kind: ty::GenericParamDefKind::Lifetime,
|
||||||
}
|
}
|
||||||
}).collect::<Vec<_>>();
|
}));
|
||||||
|
|
||||||
let hir_id = tcx.hir.node_to_hir_id(node_id);
|
let hir_id = tcx.hir.node_to_hir_id(node_id);
|
||||||
let object_lifetime_defaults = tcx.object_lifetime_defaults(hir_id);
|
let object_lifetime_defaults = tcx.object_lifetime_defaults(hir_id);
|
||||||
|
|
||||||
// Now create the real type parameters.
|
// Now create the real type parameters.
|
||||||
let type_start = own_start + lifetimes.len() as u32;
|
let type_start = params.len() as u32;
|
||||||
let mut types: Vec<_> = ast_generics.ty_params().enumerate().map(|(i, p)| {
|
params.extend(ast_generics.ty_params().enumerate().map(|(i, p)| {
|
||||||
if p.name == keywords::SelfType.name() {
|
if p.name == keywords::SelfType.name() {
|
||||||
span_bug!(p.span, "`Self` should not be the name of a regular parameter");
|
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,
|
synthetic: p.synthetic,
|
||||||
}),
|
}),
|
||||||
}
|
}
|
||||||
}).collect();
|
}));
|
||||||
|
|
||||||
// provide junk type parameter defs - the only place that
|
// provide junk type parameter defs - the only place that
|
||||||
// cares about anything but the length is instantiation,
|
// 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() {
|
for (i, &arg) in dummy_args.iter().enumerate() {
|
||||||
types.push(ty::GenericParamDef {
|
params.push(ty::GenericParamDef {
|
||||||
index: type_start + i as u32,
|
index: type_start + i as u32,
|
||||||
name: Symbol::intern(arg).as_interned_str(),
|
name: Symbol::intern(arg).as_interned_str(),
|
||||||
def_id,
|
def_id,
|
||||||
|
@ -957,7 +959,7 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||||
}
|
}
|
||||||
|
|
||||||
tcx.with_freevars(node_id, |fv| {
|
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 {
|
ty::GenericParamDef {
|
||||||
index: type_start + i,
|
index: type_start + i,
|
||||||
name: Symbol::intern("<upvar>").as_interned_str(),
|
name: Symbol::intern("<upvar>").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()
|
let param_def_id_to_index = params.iter()
|
||||||
.map(|param| (param.def_id, param.index))
|
.map(|param| (param.def_id, param.index))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
|
@ -72,10 +72,9 @@ struct ImplWfCheck<'a, 'tcx: 'a> {
|
||||||
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> {
|
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for ImplWfCheck<'a, 'tcx> {
|
||||||
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
fn visit_item(&mut self, item: &'tcx hir::Item) {
|
||||||
match item.node {
|
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);
|
let impl_def_id = self.tcx.hir.local_def_id(item.id);
|
||||||
enforce_impl_params_are_constrained(self.tcx,
|
enforce_impl_params_are_constrained(self.tcx,
|
||||||
generics,
|
|
||||||
impl_def_id,
|
impl_def_id,
|
||||||
impl_item_refs);
|
impl_item_refs);
|
||||||
enforce_impl_items_are_distinct(self.tcx, 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>,
|
fn enforce_impl_params_are_constrained<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
||||||
impl_hir_generics: &hir::Generics,
|
|
||||||
impl_def_id: DefId,
|
impl_def_id: DefId,
|
||||||
impl_item_refs: &[hir::ImplItemRef])
|
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)
|
ctp::parameters_for(&tcx.type_of(def_id), true)
|
||||||
}).collect();
|
}).collect();
|
||||||
|
|
||||||
for (ty_param, hir_param) in impl_generics.params.iter()
|
for param in &impl_generics.params {
|
||||||
.zip(impl_hir_generics.params.iter()) {
|
match param.kind {
|
||||||
match (&ty_param.kind, hir_param) {
|
|
||||||
// Disallow ANY unconstrained type parameters.
|
// Disallow ANY unconstrained type parameters.
|
||||||
(&ty::GenericParamDefKind::Type(_), hir::GenericParam::Type(hir_ty)) => {
|
ty::GenericParamDefKind::Type(_) => {
|
||||||
let param_ty = ty::ParamTy::for_def(ty_param);
|
let param_ty = ty::ParamTy::for_def(param);
|
||||||
if !input_parameters.contains(&ctp::Parameter::from(param_ty)) {
|
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)) => {
|
ty::GenericParamDefKind::Lifetime => {
|
||||||
let param = ctp::Parameter::from(ty_param.to_early_bound_region_data());
|
let param_lt = ctp::Parameter::from(param.to_early_bound_region_data());
|
||||||
if lifetimes_in_associated_types.contains(¶m) && // (*)
|
if lifetimes_in_associated_types.contains(¶m_lt) && // (*)
|
||||||
!input_parameters.contains(¶m) {
|
!input_parameters.contains(¶m_lt) {
|
||||||
report_unused_parameter(tcx, hir_lt.lifetime.span,
|
report_unused_parameter(tcx,
|
||||||
"lifetime", &hir_lt.lifetime.name.name().to_string());
|
tcx.def_span(param.def_id),
|
||||||
|
"lifetime",
|
||||||
|
¶m.name.to_string());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
(&ty::GenericParamDefKind::Type(_), _) => continue,
|
|
||||||
(&ty::GenericParamDefKind::Lifetime, _) => continue,
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue