about summary refs log tree commit diff
diff options
context:
space:
mode:
authorAriel Ben-Yehuda <ariel.byd@gmail.com>2017-10-01 23:03:48 +0200
committerAriel Ben-Yehuda <ariel.byd@gmail.com>2017-10-01 23:03:48 +0200
commitad9986605f5f743ee8cc3da8189a68815313ab07 (patch)
tree16e57c433ed758f198e1616492a7ec16a93ffa14
parent622a78cd54a090f3f53f3a0f6872073245c80bc5 (diff)
downloadrust-ad9986605f5f743ee8cc3da8189a68815313ab07.tar.gz
rust-ad9986605f5f743ee8cc3da8189a68815313ab07.zip
fix handling of Self
-rw-r--r--src/librustc/infer/error_reporting/mod.rs3
-rw-r--r--src/librustc/ty/mod.rs12
-rw-r--r--src/librustc/ty/util.rs4
3 files changed, 14 insertions, 5 deletions
diff --git a/src/librustc/infer/error_reporting/mod.rs b/src/librustc/infer/error_reporting/mod.rs
index 72f24b62e68..3c3c5395011 100644
--- a/src/librustc/infer/error_reporting/mod.rs
+++ b/src/librustc/infer/error_reporting/mod.rs
@@ -794,8 +794,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
                     let generics = self.tcx.generics_of(did);
                     // Account for the case where `did` corresponds to `Self`, which doesn't have
                     // the expected type argument.
-                    if generics.types.len() > 0 {
-                        let type_param = generics.type_param(param, self.tcx);
+                    if let Some(type_param) = generics.type_param(param, self.tcx) {
                         let hir = &self.tcx.hir;
                         hir.as_local_node_id(type_param.def_id).map(|id| {
                             // Get the `hir::TyParam` to verify wether it already has any bounds.
diff --git a/src/librustc/ty/mod.rs b/src/librustc/ty/mod.rs
index c57ecb9b69c..46d2f65f34e 100644
--- a/src/librustc/ty/mod.rs
+++ b/src/librustc/ty/mod.rs
@@ -755,11 +755,19 @@ impl<'a, 'gcx, 'tcx> Generics {
         }
     }
 
+    /// Returns the `TypeParameterDef` associated with this `ParamTy`, or `None`
+    /// if `param` is `self`.
     pub fn type_param(&'tcx self,
                       param: &ParamTy,
-                      tcx: TyCtxt<'a, 'gcx, 'tcx>) -> &TypeParameterDef {
+                      tcx: TyCtxt<'a, 'gcx, 'tcx>)
+                      -> Option<&TypeParameterDef> {
         if let Some(idx) = param.idx.checked_sub(self.parent_count() as u32) {
-            &self.types[idx as usize - self.has_self as usize - self.regions.len()]
+            let type_param_start = (self.has_self as usize) + self.regions.len();
+            if let Some(idx) = (idx as usize).checked_sub(type_param_start) {
+                Some(&self.types[idx])
+            } else {
+                None
+            }
         } else {
             tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
                 .type_param(param, tcx)
diff --git a/src/librustc/ty/util.rs b/src/librustc/ty/util.rs
index c8037ce081a..e4abe5ee615 100644
--- a/src/librustc/ty/util.rs
+++ b/src/librustc/ty/util.rs
@@ -519,7 +519,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
                 } else if let Some(&ty::TyS {
                     sty: ty::TypeVariants::TyParam(ref pt), ..
                 }) = k.as_type() {
-                    !impl_generics.type_param(pt, self).pure_wrt_drop
+                    !impl_generics.type_param(pt, self)
+                        .expect("drop impl param doesn't have a ParameterDef?")
+                        .pure_wrt_drop
                 } else {
                     // not a type or region param - this should be reported
                     // as an error.