1
Fork 0

Inline get_type

This commit is contained in:
varkor 2018-04-18 17:54:05 +01:00
parent fc27c2eb38
commit 5e89312a22
11 changed files with 63 additions and 40 deletions

View file

@ -20,7 +20,7 @@ use hir::def_id::{CrateNum, DefId, LocalDefId, LOCAL_CRATE};
use hir::map::Map; use hir::map::Map;
use hir::ItemLocalId; use hir::ItemLocalId;
use hir::LifetimeName; use hir::LifetimeName;
use ty::{self, TyCtxt}; use ty::{self, TyCtxt, GenericParamDef};
use errors::DiagnosticBuilder; use errors::DiagnosticBuilder;
use rustc::lint; use rustc::lint;
@ -1662,7 +1662,10 @@ impl<'a, 'tcx> LifetimeContext<'a, 'tcx> {
.params .params
.iter() .iter()
.filter_map(|param| { .filter_map(|param| {
param.get_type().and_then(|ty| Some(ty.object_lifetime_default)) match *param {
GenericParamDef::Type(ty) => Some(ty.object_lifetime_default),
GenericParamDef::Lifetime(_) => None,
}
}) })
.collect() .collect()
}) })

View file

@ -35,7 +35,7 @@ use infer::type_variable::TypeVariableOrigin;
use std::fmt; use std::fmt;
use syntax::ast; use syntax::ast;
use session::DiagnosticMessageId; use session::DiagnosticMessageId;
use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable}; use ty::{self, AdtKind, ToPredicate, ToPolyTraitRef, Ty, TyCtxt, TypeFoldable, GenericParamDef};
use ty::error::ExpectedFound; use ty::error::ExpectedFound;
use ty::fast_reject; use ty::fast_reject;
use ty::fold::TypeFolder; use ty::fold::TypeFolder;
@ -378,7 +378,12 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
flags.push(("_Self".to_string(), Some(self.tcx.type_of(def.did).to_string()))); flags.push(("_Self".to_string(), Some(self.tcx.type_of(def.did).to_string())));
} }
for param in generics.params.iter().filter_map(|param| param.get_type()) { for param in generics.params.iter().filter_map(|param| {
match *param {
GenericParamDef::Type(ty) => Some(ty),
GenericParamDef::Lifetime(_) => None,
}
}) {
let name = param.name.to_string(); let name = param.name.to_string();
let ty = trait_ref.substs.type_for_def(&param); let ty = trait_ref.substs.type_for_def(&param);
let ty_str = ty.to_string(); let ty_str = ty.to_string();

View file

@ -254,10 +254,9 @@ impl<'a, 'gcx, 'tcx> OnUnimplementedFormatString {
Position::ArgumentNamed(s) if s == name => (), Position::ArgumentNamed(s) if s == name => (),
// So is `{A}` if A is a type parameter // So is `{A}` if A is a type parameter
Position::ArgumentNamed(s) => match generics.params.iter().find(|param| { Position::ArgumentNamed(s) => match generics.params.iter().find(|param| {
if let GenericParamDef::Type(ty) = param { match *param {
ty.name == s GenericParamDef::Type(ty) => ty.name == s,
} else { GenericParamDef::Lifetime(_) => false,
false
} }
}) { }) {
Some(_) => (), Some(_) => (),
@ -292,10 +291,11 @@ 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| {
if let Some(ty) = param.get_type() { match *param {
GenericParamDef::Type(ty) => {
Some((ty.name.to_string(), trait_ref.substs.type_for_def(&ty).to_string())) Some((ty.name.to_string(), trait_ref.substs.type_for_def(&ty).to_string()))
} else { },
None GenericParamDef::Lifetime(_) => None
} }
}).collect::<FxHashMap<String, String>>(); }).collect::<FxHashMap<String, String>>();

View file

@ -777,13 +777,6 @@ impl GenericParamDef {
GenericParamDef::Type(ty) => ty.def_id, GenericParamDef::Type(ty) => ty.def_id,
} }
} }
pub fn get_type(&self) -> Option<TypeParamDef> {
match *self {
GenericParamDef::Type(ty) => Some(ty),
_ => None,
}
}
} }
pub struct GenericParamCount { pub struct GenericParamCount {
@ -834,7 +827,12 @@ impl<'a, 'gcx, 'tcx> Generics {
} }
pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool { pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
if self.params.iter().any(|p| p.get_type().is_some()) { if self.params.iter().any(|param| {
match *param {
GenericParamDef::Type(_) => true,
GenericParamDef::Lifetime(_) => false
}
}) {
return true; return true;
} }
if let Some(parent_def_id) = self.parent { if let Some(parent_def_id) = self.parent {

View file

@ -243,12 +243,8 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
FT: FnMut(&ty::TypeParamDef, &[Kind<'tcx>]) -> Ty<'tcx> { FT: FnMut(&ty::TypeParamDef, &[Kind<'tcx>]) -> Ty<'tcx> {
for def in &defs.params { for def in &defs.params {
let param = match def { let param = match def {
ty::GenericParamDef::Lifetime(ref lt) => { ty::GenericParamDef::Lifetime(ref lt) => mk_region(lt, substs).into(),
mk_region(lt, substs).into() ty::GenericParamDef::Type(ref ty) => mk_type(ty, substs).into(),
}
ty::GenericParamDef::Type(ref ty) => {
mk_type(ty, substs).into()
}
}; };
assert_eq!(def.index() as usize, substs.len()); assert_eq!(def.index() as usize, substs.len());
substs.push(param); substs.push(param);

View file

@ -19,7 +19,7 @@ use ty::{TyError, TyStr, TyArray, TySlice, TyFloat, TyFnDef, TyFnPtr};
use ty::{TyParam, TyRawPtr, TyRef, TyNever, TyTuple}; use ty::{TyParam, TyRawPtr, TyRef, TyNever, TyTuple};
use ty::{TyClosure, TyGenerator, TyGeneratorWitness, TyForeign, TyProjection, TyAnon}; use ty::{TyClosure, TyGenerator, TyGeneratorWitness, TyForeign, TyProjection, TyAnon};
use ty::{TyDynamic, TyInt, TyUint, TyInfer}; use ty::{TyDynamic, TyInt, TyUint, TyInfer};
use ty::{self, Ty, TyCtxt, TypeFoldable, GenericParamCount}; use ty::{self, Ty, TyCtxt, TypeFoldable, GenericParamCount, GenericParamDef};
use util::nodemap::FxHashSet; use util::nodemap::FxHashSet;
use std::cell::Cell; use std::cell::Cell;
@ -337,7 +337,12 @@ impl PrintContext {
if !verbose { if !verbose {
let mut type_params = let mut type_params =
generics.params.iter().rev().filter_map(|param| param.get_type()); generics.params.iter().rev().filter_map(|param| {
match *param {
GenericParamDef::Type(ty) => Some(ty),
GenericParamDef::Lifetime(_) => None,
}
});
if let Some(last_ty) = type_params.next() { if let Some(last_ty) = type_params.next() {
if last_ty.has_default { if last_ty.has_default {
if let Some(substs) = tcx.lift(&substs) { if let Some(substs) = tcx.lift(&substs) {

View file

@ -13,7 +13,7 @@ use rustc::hir::def_id::DefId;
use rustc::traits::{FulfillmentContext, Normalized, ObligationCause}; use rustc::traits::{FulfillmentContext, Normalized, ObligationCause};
use rustc::traits::query::{CanonicalTyGoal, NoSolution}; use rustc::traits::query::{CanonicalTyGoal, NoSolution};
use rustc::traits::query::dropck_outlives::{DtorckConstraint, DropckOutlivesResult}; use rustc::traits::query::dropck_outlives::{DtorckConstraint, DropckOutlivesResult};
use rustc::ty::{self, ParamEnvAnd, Ty, TyCtxt}; use rustc::ty::{self, ParamEnvAnd, Ty, TyCtxt, GenericParamDef};
use rustc::ty::subst::Subst; use rustc::ty::subst::Subst;
use rustc::util::nodemap::FxHashSet; use rustc::util::nodemap::FxHashSet;
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
@ -278,9 +278,13 @@ crate fn adt_dtorck_constraint<'a, 'tcx>(
debug!("dtorck_constraint: {:?}", def); debug!("dtorck_constraint: {:?}", def);
if def.is_phantom_data() { if def.is_phantom_data() {
let type_param = match tcx.generics_of(def_id).params[0] {
GenericParamDef::Type(ty) => ty,
GenericParamDef::Lifetime(_) => unreachable!(),
};
let result = DtorckConstraint { let result = DtorckConstraint {
outlives: vec![], outlives: vec![],
dtorck_types: vec![tcx.mk_param_from_def(&tcx.generics_of(def_id).params[0].get_type().unwrap())], dtorck_types: vec![tcx.mk_param_from_def(&type_param)],
overflows: vec![], overflows: vec![],
}; };
debug!("dtorck_constraint: {:?} => {:?}", def, result); debug!("dtorck_constraint: {:?} => {:?}", def, result);

View file

@ -10,7 +10,7 @@
use rustc::hir::{self, ImplItemKind, TraitItemKind}; use rustc::hir::{self, ImplItemKind, TraitItemKind};
use rustc::infer::{self, InferOk}; use rustc::infer::{self, InferOk};
use rustc::ty::{self, TyCtxt}; use rustc::ty::{self, TyCtxt, GenericParamDef};
use rustc::ty::util::ExplicitSelf; use rustc::ty::util::ExplicitSelf;
use rustc::traits::{self, ObligationCause, ObligationCauseCode, Reveal}; use rustc::traits::{self, ObligationCause, ObligationCauseCode, Reveal};
use rustc::ty::error::{ExpectedFound, TypeError}; use rustc::ty::error::{ExpectedFound, TypeError};
@ -728,8 +728,18 @@ fn compare_synthetic_generics<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
let mut error_found = false; let mut error_found = false;
let impl_m_generics = tcx.generics_of(impl_m.def_id); let impl_m_generics = tcx.generics_of(impl_m.def_id);
let trait_m_generics = tcx.generics_of(trait_m.def_id); let trait_m_generics = tcx.generics_of(trait_m.def_id);
let impl_m_type_params = impl_m_generics.params.iter().filter_map(|param| param.get_type()); let impl_m_type_params = impl_m_generics.params.iter().filter_map(|param| {
let trait_m_type_params = trait_m_generics.params.iter().filter_map(|param| param.get_type()); match *param {
GenericParamDef::Type(ty) => Some(ty),
GenericParamDef::Lifetime(_) => None,
}
});
let trait_m_type_params = trait_m_generics.params.iter().filter_map(|param| {
match *param {
GenericParamDef::Type(ty) => Some(ty),
GenericParamDef::Lifetime(_) => None,
}
});
for (impl_ty, trait_ty) in impl_m_type_params.zip(trait_m_type_params) { for (impl_ty, trait_ty) in impl_m_type_params.zip(trait_m_type_params) {
if impl_ty.synthetic != trait_ty.synthetic { if impl_ty.synthetic != trait_ty.synthetic {
let impl_node_id = tcx.hir.as_local_node_id(impl_ty.def_id).unwrap(); let impl_node_id = tcx.hir.as_local_node_id(impl_ty.def_id).unwrap();

View file

@ -860,7 +860,7 @@ impl<'a, 'tcx, 'rcx> AutoTraitFinder<'a, 'tcx, 'rcx> {
ty.bounds.insert(0, TyParamBound::maybe_sized(self.cx)); ty.bounds.insert(0, TyParamBound::maybe_sized(self.cx));
} }
} }
_ => {} GenericParamDef::Lifetime(_) => {}
} }
} }

View file

@ -1725,10 +1725,9 @@ pub enum GenericParamDef {
impl GenericParamDef { impl GenericParamDef {
pub fn is_synthetic_type_param(&self) -> bool { pub fn is_synthetic_type_param(&self) -> bool {
if let GenericParamDef::Type(ref t) = *self { match self {
t.synthetic.is_some() GenericParamDef::Type(ty) => ty.synthetic.is_some(),
} else { GenericParamDef::Lifetime(_) => false,
false
} }
} }
} }

View file

@ -1437,9 +1437,12 @@ impl DocFolder for Cache {
impl<'a> Cache { impl<'a> Cache {
fn generics(&mut self, generics: &clean::Generics) { fn generics(&mut self, generics: &clean::Generics) {
for param in &generics.params { for param in &generics.params {
if let clean::GenericParamDef::Type(ref typ) = *param { match *param {
clean::GenericParamDef::Type(ref typ) => {
self.typarams.insert(typ.did, typ.name.clone()); self.typarams.insert(typ.did, typ.name.clone());
} }
clean::GenericParamDef::Lifetime(_) => {}
}
} }
} }