From ad9986605f5f743ee8cc3da8189a68815313ab07 Mon Sep 17 00:00:00 2001 From: Ariel Ben-Yehuda Date: Sun, 1 Oct 2017 23:03:48 +0200 Subject: [PATCH] fix handling of Self --- src/librustc/infer/error_reporting/mod.rs | 3 +-- src/librustc/ty/mod.rs | 12 ++++++++++-- src/librustc/ty/util.rs | 4 +++- 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 72f24b62e682..3c3c53950119 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 c57ecb9b69c4..46d2f65f34e6 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 c8037ce081a7..e4abe5ee6151 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.