Skip to content

Wip 5898 #7351

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 30 additions & 18 deletions src/librustc/middle/typeck/check/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ impl<'self> LookupContext<'self> {

// Prepare the list of candidates
self.push_inherent_candidates(self_ty);
self.push_extension_candidates();
self.push_extension_candidates(self_ty);

let mut enum_dids = ~[];
let mut self_ty = self_ty;
Expand Down Expand Up @@ -295,7 +295,7 @@ impl<'self> LookupContext<'self> {
ty_trait(did, ref substs, store, _, _) => {
self.push_inherent_candidates_from_trait(
self_ty, did, substs, store);
self.push_inherent_impl_candidates_for_type(did);
self.push_inherent_impl_candidates_for_type(self_ty, did);
}
ty_self(self_did) => {
// Call is of the form "self.foo()" and appears in one
Expand All @@ -310,7 +310,8 @@ impl<'self> LookupContext<'self> {
}
ty_enum(did, _) | ty_struct(did, _) => {
if self.check_traits == CheckTraitsAndInherentMethods {
self.push_inherent_impl_candidates_for_type(did);
self.push_inherent_impl_candidates_for_type(self_ty,
did);
}
}
_ => { /* No inherent methods in these types */ }
Expand All @@ -326,7 +327,7 @@ impl<'self> LookupContext<'self> {
}
}

pub fn push_extension_candidates(&self) {
pub fn push_extension_candidates(&self, self_ty: ty::t) {
// If the method being called is associated with a trait, then
// find all the impls of that trait. Each of those are
// candidates.
Expand All @@ -342,7 +343,7 @@ impl<'self> LookupContext<'self> {
for opt_impl_infos.iter().advance |impl_infos| {
for impl_infos.iter().advance |impl_info| {
self.push_candidates_from_impl(
self.extension_candidates, *impl_info);
self_ty, self.extension_candidates, *impl_info);

}
}
Expand Down Expand Up @@ -523,18 +524,21 @@ impl<'self> LookupContext<'self> {
}
}

pub fn push_inherent_impl_candidates_for_type(&self, did: def_id) {
pub fn push_inherent_impl_candidates_for_type(&self,
self_ty: ty::t,
did: def_id) {
let opt_impl_infos =
self.fcx.ccx.coherence_info.inherent_methods.find(&did);
for opt_impl_infos.iter().advance |impl_infos| {
for impl_infos.iter().advance |impl_info| {
self.push_candidates_from_impl(
self.inherent_candidates, *impl_info);
self_ty, self.inherent_candidates, *impl_info);
}
}
}

pub fn push_candidates_from_impl(&self,
self_ty: ty::t,
candidates: &mut ~[Candidate],
impl_info: &resolve::Impl) {
if !self.impl_dups.insert(impl_info.did) {
Expand All @@ -561,17 +565,25 @@ impl<'self> LookupContext<'self> {
ccx: self.fcx.ccx,
infcx: self.fcx.infcx()
};
let ty::ty_param_substs_and_ty {
substs: impl_substs,
ty: impl_ty
} = impl_self_ty(&vcx, location_info, impl_info.did);

candidates.push(Candidate {
rcvr_ty: impl_ty,
rcvr_substs: impl_substs,
method_ty: method,
origin: method_static(method.def_id)
});
match impl_self_ty(&vcx, location_info, impl_info.did, Some(self_ty)) {
Some(ty::ty_param_substs_and_ty {
substs: impl_substs,
ty: impl_ty
}) => {
candidates.push(Candidate {
rcvr_ty: impl_ty,
rcvr_substs: impl_substs,
method_ty: method,
origin: method_static(method.def_id)
});
},
None => {
debug!("push_candidates_from_impl match failed: %s %s %s",
self.m_name.repr(self.tcx()),
impl_info.ident.repr(self.tcx()),
impl_info.methods.map(|m| m.ident).repr(self.tcx()));
}
}
}

// ______________________________________________________________________
Expand Down
54 changes: 50 additions & 4 deletions src/librustc/middle/typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1077,12 +1077,13 @@ pub fn check_expr(fcx: @mut FnCtxt, expr: @ast::expr) {
pub fn impl_self_ty(vcx: &VtableContext,
location_info: &LocationInfo, // (potential) receiver for
// this impl
did: ast::def_id)
-> ty_param_substs_and_ty {
did: ast::def_id,
self_ty: Option<ty::t>)
-> Option<ty_param_substs_and_ty> {
let tcx = vcx.tcx();
let ity = ty::lookup_item_type(tcx, did);

let (n_tps, region_param, raw_ty) = {
let ity = ty::lookup_item_type(tcx, did);
(ity.generics.type_param_defs.len(), ity.generics.region_param, ity.ty)
};

Expand All @@ -1096,7 +1097,52 @@ pub fn impl_self_ty(vcx: &VtableContext,
let substs = substs { self_r: self_r, self_ty: None, tps: tps };
let substd_ty = ty::subst(tcx, &substs, raw_ty);

ty_param_substs_and_ty { substs: substs, ty: substd_ty }
match substs.tps.iter().position_(|&ty| ty == substd_ty) {
// Check trait bounds on self
Some(i) => match self_ty {
Some(self_ty) => {
for ity.generics.type_param_defs[i].bounds
.trait_bounds.iter().advance |&trait_ref| {
// Search for traits implementations in scope
match vcx.ccx.coherence_info.extension_methods
.find(&trait_ref.def_id) {
Some(implementations) => {
let mut found = false;
for implementations.iter().advance |im| {
let ty::ty_param_substs_and_ty {
substs: substs,
ty: for_ty
} = impl_self_ty(vcx,
location_info,
im.did,
None)
.expect("Implementation");
match infer::mk_subty(vcx.infcx,
false,
location_info.span,
self_ty,
for_ty) {
result::Ok(()) => { found = true; break; },
result::Err(_) => ()
}
}
if !found { return None; }
},

// No implementations - no chance this impl could be
// applied.
None => { return None; }
}
}
},
None => ()
},

// No trait bounds on self
None => ()
}

Some(ty_param_substs_and_ty { substs: substs, ty: substd_ty })
}

// Only for fields! Returns <none> for methods>
Expand Down
3 changes: 2 additions & 1 deletion src/librustc/middle/typeck/check/vtable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,8 @@ fn lookup_vtable(vcx: &VtableContext,
let ty::ty_param_substs_and_ty {
substs: substs,
ty: for_ty
} = impl_self_ty(vcx, location_info, im.did);
} = impl_self_ty(vcx, location_info, im.did, None)
.expect("Implementation");
match infer::mk_subty(vcx.infcx,
false,
location_info.span,
Expand Down