Skip to content

Commit ad99866

Browse files
committed
fix handling of Self
1 parent 622a78c commit ad99866

File tree

3 files changed

+14
-5
lines changed

3 files changed

+14
-5
lines changed

src/librustc/infer/error_reporting/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -794,8 +794,7 @@ impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
794794
let generics = self.tcx.generics_of(did);
795795
// Account for the case where `did` corresponds to `Self`, which doesn't have
796796
// the expected type argument.
797-
if generics.types.len() > 0 {
798-
let type_param = generics.type_param(param, self.tcx);
797+
if let Some(type_param) = generics.type_param(param, self.tcx) {
799798
let hir = &self.tcx.hir;
800799
hir.as_local_node_id(type_param.def_id).map(|id| {
801800
// Get the `hir::TyParam` to verify wether it already has any bounds.

src/librustc/ty/mod.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -755,11 +755,19 @@ impl<'a, 'gcx, 'tcx> Generics {
755755
}
756756
}
757757

758+
/// Returns the `TypeParameterDef` associated with this `ParamTy`, or `None`
759+
/// if `param` is `self`.
758760
pub fn type_param(&'tcx self,
759761
param: &ParamTy,
760-
tcx: TyCtxt<'a, 'gcx, 'tcx>) -> &TypeParameterDef {
762+
tcx: TyCtxt<'a, 'gcx, 'tcx>)
763+
-> Option<&TypeParameterDef> {
761764
if let Some(idx) = param.idx.checked_sub(self.parent_count() as u32) {
762-
&self.types[idx as usize - self.has_self as usize - self.regions.len()]
765+
let type_param_start = (self.has_self as usize) + self.regions.len();
766+
if let Some(idx) = (idx as usize).checked_sub(type_param_start) {
767+
Some(&self.types[idx])
768+
} else {
769+
None
770+
}
763771
} else {
764772
tcx.generics_of(self.parent.expect("parent_count>0 but no parent?"))
765773
.type_param(param, tcx)

src/librustc/ty/util.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -519,7 +519,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
519519
} else if let Some(&ty::TyS {
520520
sty: ty::TypeVariants::TyParam(ref pt), ..
521521
}) = k.as_type() {
522-
!impl_generics.type_param(pt, self).pure_wrt_drop
522+
!impl_generics.type_param(pt, self)
523+
.expect("drop impl param doesn't have a ParameterDef?")
524+
.pure_wrt_drop
523525
} else {
524526
// not a type or region param - this should be reported
525527
// as an error.

0 commit comments

Comments
 (0)