Change TraitRef subtyping checks to equality
Trait references are always invariant, so all uses of subtyping between them are equivalent to using equality. Moreover, the overlap check was previously performed twice per impl pair, once in each direction. It is now performed only once, and internally uses the equality check. On glium, a crate that spends some time in coherence, this change sped up coherence checking by a few percent (not very significant).
This commit is contained in:
parent
1902021032
commit
612d001d74
4 changed files with 38 additions and 27 deletions
|
@ -474,6 +474,18 @@ pub fn mk_eqty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
|
||||||
cx.commit_if_ok(|_| cx.eq_types(a_is_expected, origin, a, b))
|
cx.commit_if_ok(|_| cx.eq_types(a_is_expected, origin, a, b))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn mk_eq_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
|
||||||
|
a_is_expected: bool,
|
||||||
|
origin: TypeOrigin,
|
||||||
|
a: ty::TraitRef<'tcx>,
|
||||||
|
b: ty::TraitRef<'tcx>)
|
||||||
|
-> UnitResult<'tcx>
|
||||||
|
{
|
||||||
|
debug!("mk_eq_trait_refs({:?} <: {:?})",
|
||||||
|
a, b);
|
||||||
|
cx.commit_if_ok(|_| cx.eq_trait_refs(a_is_expected, origin, a.clone(), b.clone()))
|
||||||
|
}
|
||||||
|
|
||||||
pub fn mk_sub_poly_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
|
pub fn mk_sub_poly_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
|
||||||
a_is_expected: bool,
|
a_is_expected: bool,
|
||||||
origin: TypeOrigin,
|
origin: TypeOrigin,
|
||||||
|
@ -481,7 +493,7 @@ pub fn mk_sub_poly_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
|
||||||
b: ty::PolyTraitRef<'tcx>)
|
b: ty::PolyTraitRef<'tcx>)
|
||||||
-> UnitResult<'tcx>
|
-> UnitResult<'tcx>
|
||||||
{
|
{
|
||||||
debug!("mk_sub_trait_refs({:?} <: {:?})",
|
debug!("mk_sub_poly_trait_refs({:?} <: {:?})",
|
||||||
a, b);
|
a, b);
|
||||||
cx.commit_if_ok(|_| cx.sub_poly_trait_refs(a_is_expected, origin, a.clone(), b.clone()))
|
cx.commit_if_ok(|_| cx.sub_poly_trait_refs(a_is_expected, origin, a.clone(), b.clone()))
|
||||||
}
|
}
|
||||||
|
@ -857,14 +869,14 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sub_trait_refs(&self,
|
pub fn eq_trait_refs(&self,
|
||||||
a_is_expected: bool,
|
a_is_expected: bool,
|
||||||
origin: TypeOrigin,
|
origin: TypeOrigin,
|
||||||
a: ty::TraitRef<'tcx>,
|
a: ty::TraitRef<'tcx>,
|
||||||
b: ty::TraitRef<'tcx>)
|
b: ty::TraitRef<'tcx>)
|
||||||
-> UnitResult<'tcx>
|
-> UnitResult<'tcx>
|
||||||
{
|
{
|
||||||
debug!("sub_trait_refs({:?} <: {:?})",
|
debug!("eq_trait_refs({:?} <: {:?})",
|
||||||
a,
|
a,
|
||||||
b);
|
b);
|
||||||
self.commit_if_ok(|_| {
|
self.commit_if_ok(|_| {
|
||||||
|
@ -872,7 +884,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
|
||||||
origin: origin,
|
origin: origin,
|
||||||
values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
|
values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
|
||||||
};
|
};
|
||||||
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
|
self.equate(a_is_expected, trace).relate(&a, &b).map(|_| ())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -20,7 +20,7 @@ use super::util;
|
||||||
use metadata::cstore::LOCAL_CRATE;
|
use metadata::cstore::LOCAL_CRATE;
|
||||||
use middle::def_id::DefId;
|
use middle::def_id::DefId;
|
||||||
use middle::subst::{Subst, Substs, TypeSpace};
|
use middle::subst::{Subst, Substs, TypeSpace};
|
||||||
use middle::ty::{self, ToPolyTraitRef, Ty};
|
use middle::ty::{self, Ty};
|
||||||
use middle::infer::{self, InferCtxt};
|
use middle::infer::{self, InferCtxt};
|
||||||
use syntax::codemap::{DUMMY_SP, Span};
|
use syntax::codemap::{DUMMY_SP, Span};
|
||||||
|
|
||||||
|
@ -41,12 +41,11 @@ pub fn overlapping_impls(infcx: &InferCtxt,
|
||||||
|
|
||||||
let selcx = &mut SelectionContext::intercrate(infcx);
|
let selcx = &mut SelectionContext::intercrate(infcx);
|
||||||
infcx.probe(|_| {
|
infcx.probe(|_| {
|
||||||
overlap(selcx, impl1_def_id, impl2_def_id) || overlap(selcx, impl2_def_id, impl1_def_id)
|
overlap(selcx, impl1_def_id, impl2_def_id)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Can the types from impl `a` be used to satisfy impl `b`?
|
/// Can both impl `a` and impl `b` be satisfied by a common type (including `where` clauses)?
|
||||||
/// (Including all conditions)
|
|
||||||
fn overlap(selcx: &mut SelectionContext,
|
fn overlap(selcx: &mut SelectionContext,
|
||||||
a_def_id: DefId,
|
a_def_id: DefId,
|
||||||
b_def_id: DefId)
|
b_def_id: DefId)
|
||||||
|
@ -68,16 +67,16 @@ fn overlap(selcx: &mut SelectionContext,
|
||||||
|
|
||||||
debug!("overlap: b_trait_ref={:?}", b_trait_ref);
|
debug!("overlap: b_trait_ref={:?}", b_trait_ref);
|
||||||
|
|
||||||
// Does `a <: b` hold? If not, no overlap.
|
// Do `a` and `b` unify? If not, no overlap.
|
||||||
if let Err(_) = infer::mk_sub_poly_trait_refs(selcx.infcx(),
|
if let Err(_) = infer::mk_eq_trait_refs(selcx.infcx(),
|
||||||
true,
|
true,
|
||||||
infer::Misc(DUMMY_SP),
|
infer::Misc(DUMMY_SP),
|
||||||
a_trait_ref.to_poly_trait_ref(),
|
a_trait_ref,
|
||||||
b_trait_ref.to_poly_trait_ref()) {
|
b_trait_ref) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
debug!("overlap: subtraitref check succeeded");
|
debug!("overlap: unification check succeeded");
|
||||||
|
|
||||||
// Are any of the obligations unsatisfiable? If so, no overlap.
|
// Are any of the obligations unsatisfiable? If so, no overlap.
|
||||||
let infcx = selcx.infcx();
|
let infcx = selcx.infcx();
|
||||||
|
|
|
@ -902,7 +902,7 @@ fn confirm_param_env_candidate<'cx,'tcx>(
|
||||||
obligation.predicate.item_name);
|
obligation.predicate.item_name);
|
||||||
|
|
||||||
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
|
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
|
||||||
match infcx.sub_trait_refs(false,
|
match infcx.eq_trait_refs(false,
|
||||||
origin,
|
origin,
|
||||||
obligation.predicate.trait_ref.clone(),
|
obligation.predicate.trait_ref.clone(),
|
||||||
projection.projection_ty.trait_ref.clone()) {
|
projection.projection_ty.trait_ref.clone()) {
|
||||||
|
|
|
@ -2695,11 +2695,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
|
||||||
skol_obligation_trait_ref);
|
skol_obligation_trait_ref);
|
||||||
|
|
||||||
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
|
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
|
||||||
if let Err(e) = self.infcx.sub_trait_refs(false,
|
if let Err(e) = self.infcx.eq_trait_refs(false,
|
||||||
origin,
|
origin,
|
||||||
impl_trait_ref.value.clone(),
|
impl_trait_ref.value.clone(),
|
||||||
skol_obligation_trait_ref) {
|
skol_obligation_trait_ref) {
|
||||||
debug!("match_impl: failed sub_trait_refs due to `{}`", e);
|
debug!("match_impl: failed eq_trait_refs due to `{}`", e);
|
||||||
return Err(());
|
return Err(());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue