diff options
| author | varkor <github@varkor.com> | 2018-04-18 17:15:53 +0100 |
|---|---|---|
| committer | varkor <github@varkor.com> | 2018-05-15 14:21:31 +0100 |
| commit | d62fc236f9b87671955921ef0207c61056fe8af8 (patch) | |
| tree | 2d5bb7cc721f74f197c249e8057d9b0bf8c57034 | |
| parent | df1c256a2b1e7c68f343f40a870fa762a63a0a05 (diff) | |
| download | rust-d62fc236f9b87671955921ef0207c61056fe8af8.tar.gz rust-d62fc236f9b87671955921ef0207c61056fe8af8.zip | |
Refactor to address comments
| -rw-r--r-- | src/librustc/traits/object_safety.rs | 3 | ||||
| -rw-r--r-- | src/librustc/ty/mod.rs | 23 | ||||
| -rw-r--r-- | src/librustc/ty/subst.rs | 13 | ||||
| -rw-r--r-- | src/librustc_traits/dropck_outlives.rs | 7 | ||||
| -rw-r--r-- | src/librustc_trans/debuginfo/mod.rs | 48 | ||||
| -rw-r--r-- | src/librustc_typeck/astconv.rs | 14 | ||||
| -rw-r--r-- | src/librustc_typeck/check/mod.rs | 13 | ||||
| -rw-r--r-- | src/librustc_typeck/check/wfcheck.rs | 2 | ||||
| -rw-r--r-- | src/librustc_typeck/collect.rs | 12 |
9 files changed, 60 insertions, 75 deletions
diff --git a/src/librustc/traits/object_safety.rs b/src/librustc/traits/object_safety.rs index d659d235cd1..7c68d5522fc 100644 --- a/src/librustc/traits/object_safety.rs +++ b/src/librustc/traits/object_safety.rs @@ -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() } diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs index 35a44bcf403..635713a7866 100644 --- a/src/librustc/ty/mod.rs +++ b/src/librustc/ty/mod.rs @@ -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; diff --git a/src/librustc/ty/subst.rs b/src/librustc/ty/subst.rs index a9077eb193f..0808cc84bf7 100644 --- a/src/librustc/ty/subst.rs +++ b/src/librustc/ty/subst.rs @@ -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() } }; diff --git a/src/librustc_traits/dropck_outlives.rs b/src/librustc_traits/dropck_outlives.rs index c9c0902940e..f3339688292 100644 --- a/src/librustc_traits/dropck_outlives.rs +++ b/src/librustc_traits/dropck_outlives.rs @@ -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); diff --git a/src/librustc_trans/debuginfo/mod.rs b/src/librustc_trans/debuginfo/mod.rs index 0990c1f1714..31ef1eddfcb 100644 --- a/src/librustc_trans/debuginfo/mod.rs +++ b/src/librustc_trans/debuginfo/mod.rs @@ -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 diff --git a/src/librustc_typeck/astconv.rs b/src/librustc_typeck/astconv.rs index 2c9995b25a1..84d94539f74 100644 --- a/src/librustc_typeck/astconv.rs +++ b/src/librustc_typeck/astconv.rs @@ -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); diff --git a/src/librustc_typeck/check/mod.rs b/src/librustc_typeck/check/mod.rs index 0726650b054..f860c1358a6 100644 --- a/src/librustc_typeck/check/mod.rs +++ b/src/librustc_typeck/check/mod.rs @@ -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) }); diff --git a/src/librustc_typeck/check/wfcheck.rs b/src/librustc_typeck/check/wfcheck.rs index 9eb53f2fdae..75380c685da 100644 --- a/src/librustc_typeck/check/wfcheck.rs +++ b/src/librustc_typeck/check/wfcheck.rs @@ -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; } diff --git a/src/librustc_typeck/collect.rs b/src/librustc_typeck/collect.rs index 452b76e1b70..b8c78c7b36a 100644 --- a/src/librustc_typeck/collect.rs +++ b/src/librustc_typeck/collect.rs @@ -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, |
