Skip to content

Commit

Permalink
fix handling of Self
Browse files Browse the repository at this point in the history
  • Loading branch information
arielb1 committed Oct 1, 2017
1 parent 622a78c commit ad99866
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 5 deletions.
3 changes: 1 addition & 2 deletions src/librustc/infer/error_reporting/mod.rs
Expand Up @@ -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.
Expand Down
12 changes: 10 additions & 2 deletions src/librustc/ty/mod.rs
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/librustc/ty/util.rs
Expand Up @@ -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.
Expand Down

0 comments on commit ad99866

Please sign in to comment.