Skip to content

Commit 2009f85

Browse files
committed
Use the quick reject mechanism during trait matching as well. Seems to
yield an incremental improvement (type-checking rustc drops from ~9s to ~8s).
1 parent 7c2c232 commit 2009f85

File tree

1 file changed

+34
-2
lines changed

1 file changed

+34
-2
lines changed

src/librustc/middle/traits/select.rs

+34-2
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ use super::{VtableBuiltin, VtableImpl, VtableParam, VtableUnboxedClosure};
2121
use super::{VtableImplData, VtableParamData, VtableBuiltinData};
2222
use super::{util};
2323

24+
use middle::fast_reject;
2425
use middle::mem_categorization::Typer;
2526
use middle::subst::{Subst, Substs, VecPerParamSpace};
2627
use middle::ty;
@@ -1762,12 +1763,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17621763
obligation: &Obligation)
17631764
-> Result<Substs, ()>
17641765
{
1766+
let impl_trait_ref = ty::impl_trait_ref(self.tcx(),
1767+
impl_def_id).unwrap();
1768+
1769+
// Before we create the substitutions and everything, first
1770+
// consider a "quick reject". This avoids creating more types
1771+
// and so forth that we need to.
1772+
if self.fast_reject_trait_refs(obligation, &*impl_trait_ref) {
1773+
return Err(());
1774+
}
1775+
17651776
let impl_substs = util::fresh_substs_for_impl(self.infcx,
17661777
obligation.cause.span,
17671778
impl_def_id);
17681779

1769-
let impl_trait_ref = ty::impl_trait_ref(self.tcx(),
1770-
impl_def_id).unwrap();
17711780
let impl_trait_ref = impl_trait_ref.subst(self.tcx(),
17721781
&impl_substs);
17731782

@@ -1777,6 +1786,29 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17771786
}
17781787
}
17791788

1789+
fn fast_reject_trait_refs(&mut self,
1790+
obligation: &Obligation,
1791+
impl_trait_ref: &ty::TraitRef)
1792+
-> bool
1793+
{
1794+
// We can avoid creating type variables and doing the full
1795+
// substitution if we find that any of the input types, when
1796+
// simplified, do not match.
1797+
1798+
obligation.trait_ref.input_types().iter()
1799+
.zip(impl_trait_ref.input_types().iter())
1800+
.any(|(&obligation_ty, &impl_ty)| {
1801+
let simplified_obligation_ty =
1802+
fast_reject::simplify_type(self.tcx(), obligation_ty, true);
1803+
let simplified_impl_ty =
1804+
fast_reject::simplify_type(self.tcx(), impl_ty, false);
1805+
1806+
simplified_obligation_ty.is_some() &&
1807+
simplified_impl_ty.is_some() &&
1808+
simplified_obligation_ty != simplified_impl_ty
1809+
})
1810+
}
1811+
17801812
fn match_trait_refs(&mut self,
17811813
obligation: &Obligation,
17821814
trait_ref: Rc<ty::TraitRef>)

0 commit comments

Comments
 (0)