]> git.lizzy.rs Git - rust.git/commitdiff
Refactor to address comments
authorvarkor <github@varkor.com>
Wed, 18 Apr 2018 16:15:53 +0000 (17:15 +0100)
committervarkor <github@varkor.com>
Tue, 15 May 2018 13:21:31 +0000 (14:21 +0100)
src/librustc/traits/object_safety.rs
src/librustc/ty/mod.rs
src/librustc/ty/subst.rs
src/librustc_traits/dropck_outlives.rs
src/librustc_trans/debuginfo/mod.rs
src/librustc_typeck/astconv.rs
src/librustc_typeck/check/mod.rs
src/librustc_typeck/check/wfcheck.rs
src/librustc_typeck/collect.rs

index d659d235cd13abd05092ffc509d171e4b6cc74ed..7c68d5522fcc39cb6e1cc6124ea0f2cde2c25d6f 100644 (file)
@@ -387,7 +387,6 @@ fn contains_illegal_self_type_reference(self,
 }
 
 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()
 }
index 35a44bcf40309049a187ec0554451f5ae0a11154..635713a7866508e4a36d91d766a569e076b7634b 100644 (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 @@ pub fn param_counts(&self) -> GenericParamCount {
         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;
index a9077eb193f38f4a6e31bc2cd613cc4a8e656d4d..0808cc84bf7aaf9fe61abf10b1bfa86ae6404429 100644 (file)
@@ -241,25 +241,12 @@ fn fill_single<FR, FT>(substs: &mut Vec<Kind<'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()
                 }
             };
index c9c0902940e54069ae76056af4dfd406d56d0e56..f333968829286af0eb6cf7d49b8a61e9ef6143f6 100644 (file)
@@ -280,12 +280,7 @@ fn dtorck_constraint_for_ty<'a, 'gcx, '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);
index 0990c1f1714c9e2d66286ec470dbc62d68eca270..31ef1eddfcbf5a648083709615fdb8b3d19f159f 100644 (file)
@@ -25,7 +25,7 @@
 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 @@ fn get_template_parameters<'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 @@ fn get_template_parameters<'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
index 2c9995b25a1bdd9403eb269057c3873d6fbe53cb..84d94539f743681e16898dd7c5b1879c765e392e 100644 (file)
@@ -225,11 +225,23 @@ fn create_substs_for_ast_path(&self,
         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);
index 0726650b05480ee1827554ba695f8f8bc1539984..f860c1358a6daaeded71a6610acac6f021d8ef07 100644 (file)
@@ -4925,8 +4925,19 @@ fn check_path_parameter_count(&self,
                 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)
             });
index 9eb53f2fdaecfc76c4cbab51958a5773b725bce3..75380c685daa391b33bf5251a04769ccbd3a4540 100644 (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;
         }
index 452b76e1b702f7c769a8da8cfdd2391c4d0e140d..b8c78c7b36a5e0c597721d8f6f97ba827c078b04 100644 (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,