Skip to content

Commit cead47c

Browse files
committed
Add a "match" relation that can be used to make recursion check during
trait matching more tailored. We now detect recursion where the obligations "match" -- meaning basically that they are the same for some substitution of any unbound type variables.
1 parent 8403b82 commit cead47c

File tree

3 files changed

+109
-1
lines changed

3 files changed

+109
-1
lines changed

src/librustc/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ pub mod middle {
122122
pub mod traits;
123123
pub mod ty;
124124
pub mod ty_fold;
125+
pub mod ty_match;
125126
pub mod ty_relate;
126127
pub mod ty_walk;
127128
pub mod weak_lang_items;

src/librustc/middle/traits/select.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use middle::ty::{self, RegionEscape, ToPolyTraitRef, Ty};
3838
use middle::infer;
3939
use middle::infer::{InferCtxt, TypeFreshener};
4040
use middle::ty_fold::TypeFoldable;
41+
use middle::ty_match;
42+
use middle::ty_relate::TypeRelation;
4143
use std::cell::RefCell;
4244
use std::rc::Rc;
4345
use syntax::{abi, ast};
@@ -472,7 +474,8 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
472474
unbound_input_types &&
473475
(self.intercrate ||
474476
stack.iter().skip(1).any(
475-
|prev| stack.fresh_trait_ref.def_id() == prev.fresh_trait_ref.def_id()))
477+
|prev| self.match_fresh_trait_refs(&stack.fresh_trait_ref,
478+
&prev.fresh_trait_ref)))
476479
{
477480
debug!("evaluate_stack({}) --> unbound argument, recursion --> ambiguous",
478481
stack.fresh_trait_ref.repr(self.tcx()));
@@ -2475,6 +2478,15 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
24752478
///////////////////////////////////////////////////////////////////////////
24762479
// Miscellany
24772480

2481+
fn match_fresh_trait_refs(&self,
2482+
previous: &ty::PolyTraitRef<'tcx>,
2483+
current: &ty::PolyTraitRef<'tcx>)
2484+
-> bool
2485+
{
2486+
let mut matcher = ty_match::Match::new(self.tcx());
2487+
matcher.relate(previous, current).is_ok()
2488+
}
2489+
24782490
fn push_stack<'o,'s:'o>(&mut self,
24792491
previous_stack: Option<&'s TraitObligationStack<'s, 'tcx>>,
24802492
obligation: &'o TraitObligation<'tcx>)

src/librustc/middle/ty_match.rs

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright 2012 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+
use middle::ty::{self, Ty};
12+
use middle::ty_relate::{self, Relate, TypeRelation, RelateResult};
13+
use util::ppaux::Repr;
14+
15+
/// A type "A" *matches* "B" if the fresh types in B could be
16+
/// substituted with values so as to make it equal to A. Matching is
17+
/// intended to be used only on freshened types, and it basically
18+
/// indicates if the non-freshened versions of A and B could have been
19+
/// unified.
20+
///
21+
/// It is only an approximation. If it yields false, unification would
22+
/// definitely fail, but a true result doesn't mean unification would
23+
/// succeed. This is because we don't track the "side-constraints" on
24+
/// type variables, nor do we track if the same freshened type appears
25+
/// more than once. To some extent these approximations could be
26+
/// fixed, given effort.
27+
///
28+
/// Like subtyping, matching is really a binary relation, so the only
29+
/// important thing about the result is Ok/Err. Also, matching never
30+
/// affects any type variables or unification state.
31+
pub struct Match<'a, 'tcx: 'a> {
32+
tcx: &'a ty::ctxt<'tcx>
33+
}
34+
35+
impl<'a, 'tcx> Match<'a, 'tcx> {
36+
pub fn new(tcx: &'a ty::ctxt<'tcx>) -> Match<'a, 'tcx> {
37+
Match { tcx: tcx }
38+
}
39+
}
40+
41+
impl<'a, 'tcx> TypeRelation<'a, 'tcx> for Match<'a, 'tcx> {
42+
fn tag(&self) -> &'static str { "Match" }
43+
fn tcx(&self) -> &'a ty::ctxt<'tcx> { self.tcx }
44+
fn a_is_expected(&self) -> bool { true } // irrelevant
45+
46+
fn relate_with_variance<T:Relate<'a,'tcx>>(&mut self,
47+
_: ty::Variance,
48+
a: &T,
49+
b: &T)
50+
-> RelateResult<'tcx, T>
51+
{
52+
self.relate(a, b)
53+
}
54+
55+
fn regions(&mut self, a: ty::Region, b: ty::Region) -> RelateResult<'tcx, ty::Region> {
56+
debug!("{}.regions({}, {})",
57+
self.tag(),
58+
a.repr(self.tcx()),
59+
b.repr(self.tcx()));
60+
Ok(a)
61+
}
62+
63+
fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
64+
debug!("{}.tys({}, {})", self.tag(),
65+
a.repr(self.tcx()), b.repr(self.tcx()));
66+
if a == b { return Ok(a); }
67+
68+
match (&a.sty, &b.sty) {
69+
(_, &ty::ty_infer(ty::FreshTy(_))) |
70+
(_, &ty::ty_infer(ty::FreshIntTy(_))) => {
71+
Ok(a)
72+
}
73+
74+
(&ty::ty_infer(_), _) |
75+
(_, &ty::ty_infer(_)) => {
76+
Err(ty::terr_sorts(ty_relate::expected_found(self, &a, &b)))
77+
}
78+
79+
(&ty::ty_err, _) | (_, &ty::ty_err) => {
80+
Ok(self.tcx().types.err)
81+
}
82+
83+
_ => {
84+
ty_relate::super_relate_tys(self, a, b)
85+
}
86+
}
87+
}
88+
89+
fn binders<T>(&mut self, a: &ty::Binder<T>, b: &ty::Binder<T>)
90+
-> RelateResult<'tcx, ty::Binder<T>>
91+
where T: Relate<'a,'tcx>
92+
{
93+
Ok(ty::Binder(try!(self.relate(a.skip_binder(), b.skip_binder()))))
94+
}
95+
}

0 commit comments

Comments
 (0)