Skip to content

Commit 2a1ba2f

Browse files
committed
Permit method calls based on where clauses.
1 parent c7dd3c4 commit 2a1ba2f

File tree

3 files changed

+78
-3
lines changed

3 files changed

+78
-3
lines changed

src/librustc_typeck/check/callee.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use super::TupleArgumentsFlag;
2222
use super::write_call;
2323

2424
use middle::infer;
25-
use middle::ty::{mod, Ty};
25+
use middle::ty::{self, Ty};
2626
use syntax::ast;
2727
use syntax::codemap::Span;
2828
use syntax::parse::token;

src/librustc_typeck/check/method/probe.rs

+35-2
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
442442

443443
fn assemble_extension_candidates_for_trait(&mut self,
444444
trait_def_id: ast::DefId) {
445-
debug!("assemble_extension_candidates_for_trait: trait_def_id={}",
445+
debug!("assemble_extension_candidates_for_trait(trait_def_id={})",
446446
trait_def_id.repr(self.tcx()));
447447

448448
// Check whether `trait_def_id` defines a method with suitable name:
@@ -471,8 +471,12 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
471471
matching_index);
472472

473473
self.assemble_unboxed_closure_candidates(trait_def_id,
474-
method,
474+
method.clone(),
475475
matching_index);
476+
477+
self.assemble_where_clause_candidates(trait_def_id,
478+
method,
479+
matching_index);
476480
}
477481

478482
fn assemble_extension_candidates_for_trait_impls(&mut self,
@@ -599,6 +603,35 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
599603
}
600604
}
601605

606+
fn assemble_where_clause_candidates(&mut self,
607+
trait_def_id: ast::DefId,
608+
method_ty: Rc<ty::Method<'tcx>>,
609+
method_index: uint)
610+
{
611+
debug!("assemble_where_clause_candidates(trait_def_id={})",
612+
trait_def_id.repr(self.tcx()));
613+
614+
// Check whether there are any where-clauses pertaining to this trait.
615+
let caller_predicates =
616+
self.fcx.inh.param_env.caller_bounds.predicates.as_slice().to_vec();
617+
for bound in traits::elaborate_predicates(self.tcx(), caller_predicates)
618+
.filter_map(|p| p.to_opt_poly_trait_ref())
619+
.filter(|b| b.def_id() == trait_def_id)
620+
{
621+
let xform_self_ty = self.xform_self_ty(&method_ty, bound.substs());
622+
623+
debug!("assemble_where_clause_candidates: bound={} xform_self_ty={}",
624+
bound.repr(self.tcx()),
625+
xform_self_ty.repr(self.tcx()));
626+
627+
self.extension_candidates.push(Candidate {
628+
xform_self_ty: xform_self_ty,
629+
method_ty: method_ty.clone(),
630+
kind: WhereClauseCandidate(bound, method_index)
631+
});
632+
}
633+
}
634+
602635
///////////////////////////////////////////////////////////////////////////
603636
// THE ACTUAL SEARCH
604637

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// Test that we can use method notation to call methods based on a
12+
// where clause type, and not only type parameters.
13+
14+
trait Foo {
15+
fn foo(&self) -> int;
16+
}
17+
18+
impl Foo for Option<int>
19+
{
20+
fn foo(&self) -> int {
21+
self.unwrap_or(22)
22+
}
23+
}
24+
25+
impl Foo for Option<uint>
26+
{
27+
fn foo(&self) -> int {
28+
self.unwrap_or(22) as int
29+
}
30+
}
31+
32+
fn check<T>(x: Option<T>) -> (int, int)
33+
where Option<T> : Foo
34+
{
35+
let y: Option<T> = None;
36+
(x.foo(), y.foo())
37+
}
38+
39+
fn main() {
40+
assert_eq!(check(Some(23u)), (23i, 22i));
41+
assert_eq!(check(Some(23i)), (23i, 22i));
42+
}

0 commit comments

Comments
 (0)