Refactor to address comments

This commit is contained in:
varkor 2018-04-18 17:15:53 +01:00
parent df1c256a2b
commit d62fc236f9
9 changed files with 60 additions and 75 deletions

View file

@ -387,7 +387,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
}
pub(super) fn is_object_safe_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
trait_def_id: DefId)
-> bool {
trait_def_id: DefId) -> bool {
tcx.object_safety_violations(trait_def_id).is_empty()
}

View file

@ -767,7 +767,14 @@ impl GenericParamDef {
pub fn index(&self) -> u32 {
match self {
GenericParamDef::Lifetime(lt) => lt.index,
GenericParamDef::Type(ty) => ty.index,
GenericParamDef::Type(ty) => ty.index,
}
}
pub fn def_id(&self) -> DefId {
match self {
GenericParamDef::Lifetime(lt) => lt.def_id,
GenericParamDef::Type(ty) => ty.def_id,
}
}
@ -795,7 +802,7 @@ pub struct Generics {
pub parent_count: usize,
pub params: Vec<GenericParamDef>,
/// Reverse map to each `TypeParamDef`'s `index` field
/// Reverse map to the `index` field of each `GenericParamDef`'s inner type
pub param_def_id_to_index: FxHashMap<DefId, u32>,
pub has_self: bool,
@ -826,18 +833,6 @@ impl<'a, 'gcx, 'tcx> Generics {
param_counts
}
pub fn type_params_without_defaults(&self) -> usize {
let mut count = 0;
for param in self.params.iter() {
if let GenericParamDef::Type(ty) = param {
if !ty.has_default {
count += 1
}
}
}
count
}
pub fn requires_monomorphization(&self, tcx: TyCtxt<'a, 'gcx, 'tcx>) -> bool {
if self.params.iter().any(|p| p.get_type().is_some()) {
return true;

View file

@ -241,25 +241,12 @@ impl<'a, 'gcx, 'tcx> Substs<'tcx> {
mk_type: &mut FT)
where FR: FnMut(&ty::RegionParamDef, &[Kind<'tcx>]) -> ty::Region<'tcx>,
FT: FnMut(&ty::TypeParamDef, &[Kind<'tcx>]) -> Ty<'tcx> {
// Handle Self first, before all regions.
let mut skip_self = defs.parent.is_none() && defs.has_self;
if skip_self {
let def = defs.params.iter().find_map(|p| p.get_type()).unwrap();
let ty = mk_type(&def, substs);
assert_eq!(def.index as usize, substs.len());
substs.push(ty.into());
}
for def in &defs.params {
let param = match def {
ty::GenericParamDef::Lifetime(ref lt) => {
mk_region(lt, substs).into()
}
ty::GenericParamDef::Type(ref ty) => {
if skip_self {
skip_self = false;
continue
}
mk_type(ty, substs).into()
}
};

View file

@ -280,12 +280,7 @@ crate fn adt_dtorck_constraint<'a, 'tcx>(
if def.is_phantom_data() {
let result = DtorckConstraint {
outlives: vec![],
dtorck_types: vec![tcx.mk_param_from_def(
&tcx.generics_of(def_id)
.params
.iter()
.find_map(|p| p.get_type())
.expect("should be at least one type parameter"))],
dtorck_types: vec![tcx.mk_param_from_def(&tcx.generics_of(def_id).params[0].get_type().unwrap())],
overflows: vec![],
};
debug!("dtorck_constraint: {:?} => {:?}", def, result);

View file

@ -25,7 +25,7 @@ use llvm::{ModuleRef, ContextRef, ValueRef};
use llvm::debuginfo::{DIFile, DIType, DIScope, DIBuilderRef, DISubprogram, DIArray, DIFlags};
use rustc::hir::TransFnAttrFlags;
use rustc::hir::def_id::{DefId, CrateNum};
use rustc::ty::subst::Substs;
use rustc::ty::subst::{Substs, UnpackedKind};
use rustc::ty::GenericParamDef;
use abi::Abi;
@ -197,12 +197,6 @@ pub fn finalize(cx: &CodegenCx) {
};
}
#[derive(PartialEq, Eq, Hash)]
pub enum Kind {
Lifetime,
Type,
}
/// Creates the function-specific debug context.
///
/// Returns the FunctionDebugContext for the function which holds state needed
@ -398,27 +392,25 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
// Again, only create type information if full debuginfo is enabled
let template_params: Vec<_> = if cx.sess().opts.debuginfo == FullDebugInfo {
let names = get_parameter_names(cx, generics);
let names = names.iter().flat_map(|(kind, param)| {
if kind == &Kind::Type {
Some(param)
substs.iter().zip(names).filter_map(|(kind, name)| {
if let UnpackedKind::Type(ty) = kind.unpack() {
let actual_type = cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
let actual_type_metadata =
type_metadata(cx, actual_type, syntax_pos::DUMMY_SP);
let name = CString::new(name.as_str().as_bytes()).unwrap();
Some(unsafe {
llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
DIB(cx),
ptr::null_mut(),
name.as_ptr(),
actual_type_metadata,
file_metadata,
0,
0)
})
} else {
None
}
});
substs.types().zip(names).map(|(ty, name)| {
let actual_type = cx.tcx.normalize_erasing_regions(ParamEnv::reveal_all(), ty);
let actual_type_metadata = type_metadata(cx, actual_type, syntax_pos::DUMMY_SP);
let name = CString::new(name.as_str().as_bytes()).unwrap();
unsafe {
llvm::LLVMRustDIBuilderCreateTemplateTypeParameter(
DIB(cx),
ptr::null_mut(),
name.as_ptr(),
actual_type_metadata,
file_metadata,
0,
0)
}
}).collect()
} else {
vec![]
@ -429,14 +421,14 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
fn get_parameter_names(cx: &CodegenCx,
generics: &ty::Generics)
-> Vec<(Kind, InternedString)> {
-> Vec<InternedString> {
let mut names = generics.parent.map_or(vec![], |def_id| {
get_parameter_names(cx, cx.tcx.generics_of(def_id))
});
names.extend(generics.params.iter().map(|param| {
match param {
GenericParamDef::Lifetime(lt) => (Kind::Lifetime, lt.name),
GenericParamDef::Type(ty) => (Kind::Type, ty.name),
GenericParamDef::Lifetime(lt) => lt.name,
GenericParamDef::Type(ty) => ty.name,
}
}));
names

View file

@ -225,11 +225,23 @@ impl<'o, 'gcx: 'tcx, 'tcx> AstConv<'gcx, 'tcx>+'o {
let type_params_offset = self_ty.is_some() as usize;
let ty_param_defs = param_counts.types - type_params_offset;
if !infer_types || num_types_provided > ty_param_defs {
let type_params_without_defaults = {
let mut count = 0;
for param in decl_generics.params.iter() {
if let ty::GenericParamDef::Type(ty) = param {
if !ty.has_default {
count += 1
}
}
}
count
};
check_type_argument_count(tcx,
span,
num_types_provided,
ty_param_defs,
decl_generics.type_params_without_defaults() - type_params_offset);
type_params_without_defaults - type_params_offset);
}
let is_object = self_ty.map_or(false, |ty| ty.sty == TRAIT_OBJECT_DUMMY_SELF);

View file

@ -4925,8 +4925,19 @@ impl<'a, 'gcx, 'tcx> FnCtxt<'a, 'gcx, 'tcx> {
let type_params_offset
= (generics.parent.is_none() && generics.has_self) as usize;
let type_params = param_counts.types - type_params_offset;
let type_params_without_defaults = {
let mut count = 0;
for param in generics.params.iter() {
if let ty::GenericParamDef::Type(ty) = param {
if !ty.has_default {
count += 1
}
}
}
count
};
let type_params_barring_defaults =
generics.type_params_without_defaults() - type_params_offset;
type_params_without_defaults - type_params_offset;
(type_params_barring_defaults, type_params, param_counts.lifetimes)
});

View file

@ -662,7 +662,7 @@ fn reject_shadowing_parameters(tcx: TyCtxt, def_id: DefId) {
.collect();
for method_param in generics.params.iter() {
// Shadowing is currently permitted with lifetimes.
// Shadowing is checked in resolve_lifetime.
if let GenericParamDef::Lifetime(_) = method_param {
continue;
}

View file

@ -973,15 +973,9 @@ fn generics_of<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
.chain(types)
.collect();
let param_def_id_to_index =
params.iter()
.map(|param| {
match param {
ty::GenericParamDef::Lifetime(lt) => (lt.def_id, lt.index),
ty::GenericParamDef::Type(ty) => (ty.def_id, ty.index),
}
})
.collect();
let param_def_id_to_index = params.iter()
.map(|param| (param.def_id(), param.index()))
.collect();
tcx.alloc_generics(ty::Generics {
parent: parent_def_id,