Permit method calls based on where clauses.
This commit is contained in:
parent
c7dd3c4d69
commit
2a1ba2f1ac
3 changed files with 78 additions and 3 deletions
|
@ -22,7 +22,7 @@ use super::TupleArgumentsFlag;
|
|||
use super::write_call;
|
||||
|
||||
use middle::infer;
|
||||
use middle::ty::{mod, Ty};
|
||||
use middle::ty::{self, Ty};
|
||||
use syntax::ast;
|
||||
use syntax::codemap::Span;
|
||||
use syntax::parse::token;
|
||||
|
|
|
@ -442,7 +442,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
|
|||
|
||||
fn assemble_extension_candidates_for_trait(&mut self,
|
||||
trait_def_id: ast::DefId) {
|
||||
debug!("assemble_extension_candidates_for_trait: trait_def_id={}",
|
||||
debug!("assemble_extension_candidates_for_trait(trait_def_id={})",
|
||||
trait_def_id.repr(self.tcx()));
|
||||
|
||||
// Check whether `trait_def_id` defines a method with suitable name:
|
||||
|
@ -471,8 +471,12 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
|
|||
matching_index);
|
||||
|
||||
self.assemble_unboxed_closure_candidates(trait_def_id,
|
||||
method,
|
||||
method.clone(),
|
||||
matching_index);
|
||||
|
||||
self.assemble_where_clause_candidates(trait_def_id,
|
||||
method,
|
||||
matching_index);
|
||||
}
|
||||
|
||||
fn assemble_extension_candidates_for_trait_impls(&mut self,
|
||||
|
@ -599,6 +603,35 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
|
|||
}
|
||||
}
|
||||
|
||||
fn assemble_where_clause_candidates(&mut self,
|
||||
trait_def_id: ast::DefId,
|
||||
method_ty: Rc<ty::Method<'tcx>>,
|
||||
method_index: uint)
|
||||
{
|
||||
debug!("assemble_where_clause_candidates(trait_def_id={})",
|
||||
trait_def_id.repr(self.tcx()));
|
||||
|
||||
// Check whether there are any where-clauses pertaining to this trait.
|
||||
let caller_predicates =
|
||||
self.fcx.inh.param_env.caller_bounds.predicates.as_slice().to_vec();
|
||||
for bound in traits::elaborate_predicates(self.tcx(), caller_predicates)
|
||||
.filter_map(|p| p.to_opt_poly_trait_ref())
|
||||
.filter(|b| b.def_id() == trait_def_id)
|
||||
{
|
||||
let xform_self_ty = self.xform_self_ty(&method_ty, bound.substs());
|
||||
|
||||
debug!("assemble_where_clause_candidates: bound={} xform_self_ty={}",
|
||||
bound.repr(self.tcx()),
|
||||
xform_self_ty.repr(self.tcx()));
|
||||
|
||||
self.extension_candidates.push(Candidate {
|
||||
xform_self_ty: xform_self_ty,
|
||||
method_ty: method_ty.clone(),
|
||||
kind: WhereClauseCandidate(bound, method_index)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////
|
||||
// THE ACTUAL SEARCH
|
||||
|
||||
|
|
42
src/test/run-pass/method-where-clause.rs
Normal file
42
src/test/run-pass/method-where-clause.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
|
||||
// file at the top-level directory of this distribution and at
|
||||
// http://rust-lang.org/COPYRIGHT.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
||||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
||||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
||||
// option. This file may not be copied, modified, or distributed
|
||||
// except according to those terms.
|
||||
|
||||
// Test that we can use method notation to call methods based on a
|
||||
// where clause type, and not only type parameters.
|
||||
|
||||
trait Foo {
|
||||
fn foo(&self) -> int;
|
||||
}
|
||||
|
||||
impl Foo for Option<int>
|
||||
{
|
||||
fn foo(&self) -> int {
|
||||
self.unwrap_or(22)
|
||||
}
|
||||
}
|
||||
|
||||
impl Foo for Option<uint>
|
||||
{
|
||||
fn foo(&self) -> int {
|
||||
self.unwrap_or(22) as int
|
||||
}
|
||||
}
|
||||
|
||||
fn check<T>(x: Option<T>) -> (int, int)
|
||||
where Option<T> : Foo
|
||||
{
|
||||
let y: Option<T> = None;
|
||||
(x.foo(), y.foo())
|
||||
}
|
||||
|
||||
fn main() {
|
||||
assert_eq!(check(Some(23u)), (23i, 22i));
|
||||
assert_eq!(check(Some(23i)), (23i, 22i));
|
||||
}
|
Loading…
Add table
Reference in a new issue