Skip to content

Commit d7bb01e

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 d93921b commit d7bb01e

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
@@ -27,6 +27,7 @@ use super::{VtableBuiltin, VtableImpl, VtableParam, VtableUnboxedClosure};
2727
use super::{VtableImplData, VtableParamData, VtableBuiltinData};
2828
use super::{util};
2929

30+
use middle::fast_reject;
3031
use middle::mem_categorization::Typer;
3132
use middle::subst::{Subst, Substs, VecPerParamSpace};
3233
use middle::ty;
@@ -1767,12 +1768,20 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17671768
obligation: &Obligation)
17681769
-> Result<Substs, ()>
17691770
{
1771+
let impl_trait_ref = ty::impl_trait_ref(self.tcx(),
1772+
impl_def_id).unwrap();
1773+
1774+
// Before we create the substitutions and everything, first
1775+
// consider a "quick reject". This avoids creating more types
1776+
// and so forth that we need to.
1777+
if self.fast_reject_trait_refs(obligation, &*impl_trait_ref) {
1778+
return Err(());
1779+
}
1780+
17701781
let impl_substs = util::fresh_substs_for_impl(self.infcx,
17711782
obligation.cause.span,
17721783
impl_def_id);
17731784

1774-
let impl_trait_ref = ty::impl_trait_ref(self.tcx(),
1775-
impl_def_id).unwrap();
17761785
let impl_trait_ref = impl_trait_ref.subst(self.tcx(),
17771786
&impl_substs);
17781787

@@ -1782,6 +1791,29 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17821791
}
17831792
}
17841793

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

0 commit comments

Comments
 (0)