Skip to content

Commit 612d001

Browse files
committed
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).
1 parent 1902021 commit 612d001

File tree

4 files changed

+38
-27
lines changed

4 files changed

+38
-27
lines changed

src/librustc/middle/infer/mod.rs

+19-7
Original file line numberDiff line numberDiff line change
@@ -474,14 +474,26 @@ pub fn mk_eqty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
474474
cx.commit_if_ok(|_| cx.eq_types(a_is_expected, origin, a, b))
475475
}
476476

477-
pub fn mk_sub_poly_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
477+
pub fn mk_eq_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
478478
a_is_expected: bool,
479479
origin: TypeOrigin,
480-
a: ty::PolyTraitRef<'tcx>,
481-
b: ty::PolyTraitRef<'tcx>)
480+
a: ty::TraitRef<'tcx>,
481+
b: ty::TraitRef<'tcx>)
482482
-> UnitResult<'tcx>
483483
{
484-
debug!("mk_sub_trait_refs({:?} <: {:?})",
484+
debug!("mk_eq_trait_refs({:?} <: {:?})",
485+
a, b);
486+
cx.commit_if_ok(|_| cx.eq_trait_refs(a_is_expected, origin, a.clone(), b.clone()))
487+
}
488+
489+
pub fn mk_sub_poly_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
490+
a_is_expected: bool,
491+
origin: TypeOrigin,
492+
a: ty::PolyTraitRef<'tcx>,
493+
b: ty::PolyTraitRef<'tcx>)
494+
-> UnitResult<'tcx>
495+
{
496+
debug!("mk_sub_poly_trait_refs({:?} <: {:?})",
485497
a, b);
486498
cx.commit_if_ok(|_| cx.sub_poly_trait_refs(a_is_expected, origin, a.clone(), b.clone()))
487499
}
@@ -857,22 +869,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
857869
})
858870
}
859871

860-
pub fn sub_trait_refs(&self,
872+
pub fn eq_trait_refs(&self,
861873
a_is_expected: bool,
862874
origin: TypeOrigin,
863875
a: ty::TraitRef<'tcx>,
864876
b: ty::TraitRef<'tcx>)
865877
-> UnitResult<'tcx>
866878
{
867-
debug!("sub_trait_refs({:?} <: {:?})",
879+
debug!("eq_trait_refs({:?} <: {:?})",
868880
a,
869881
b);
870882
self.commit_if_ok(|_| {
871883
let trace = TypeTrace {
872884
origin: origin,
873885
values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
874886
};
875-
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
887+
self.equate(a_is_expected, trace).relate(&a, &b).map(|_| ())
876888
})
877889
}
878890

src/librustc/middle/traits/coherence.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use super::util;
2020
use metadata::cstore::LOCAL_CRATE;
2121
use middle::def_id::DefId;
2222
use middle::subst::{Subst, Substs, TypeSpace};
23-
use middle::ty::{self, ToPolyTraitRef, Ty};
23+
use middle::ty::{self, Ty};
2424
use middle::infer::{self, InferCtxt};
2525
use syntax::codemap::{DUMMY_SP, Span};
2626

@@ -41,12 +41,11 @@ pub fn overlapping_impls(infcx: &InferCtxt,
4141

4242
let selcx = &mut SelectionContext::intercrate(infcx);
4343
infcx.probe(|_| {
44-
overlap(selcx, impl1_def_id, impl2_def_id) || overlap(selcx, impl2_def_id, impl1_def_id)
44+
overlap(selcx, impl1_def_id, impl2_def_id)
4545
})
4646
}
4747

48-
/// Can the types from impl `a` be used to satisfy impl `b`?
49-
/// (Including all conditions)
48+
/// Can both impl `a` and impl `b` be satisfied by a common type (including `where` clauses)?
5049
fn overlap(selcx: &mut SelectionContext,
5150
a_def_id: DefId,
5251
b_def_id: DefId)
@@ -68,16 +67,16 @@ fn overlap(selcx: &mut SelectionContext,
6867

6968
debug!("overlap: b_trait_ref={:?}", b_trait_ref);
7069

71-
// Does `a <: b` hold? If not, no overlap.
72-
if let Err(_) = infer::mk_sub_poly_trait_refs(selcx.infcx(),
73-
true,
74-
infer::Misc(DUMMY_SP),
75-
a_trait_ref.to_poly_trait_ref(),
76-
b_trait_ref.to_poly_trait_ref()) {
70+
// Do `a` and `b` unify? If not, no overlap.
71+
if let Err(_) = infer::mk_eq_trait_refs(selcx.infcx(),
72+
true,
73+
infer::Misc(DUMMY_SP),
74+
a_trait_ref,
75+
b_trait_ref) {
7776
return false;
7877
}
7978

80-
debug!("overlap: subtraitref check succeeded");
79+
debug!("overlap: unification check succeeded");
8180

8281
// Are any of the obligations unsatisfiable? If so, no overlap.
8382
let infcx = selcx.infcx();

src/librustc/middle/traits/project.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -902,10 +902,10 @@ fn confirm_param_env_candidate<'cx,'tcx>(
902902
obligation.predicate.item_name);
903903

904904
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
905-
match infcx.sub_trait_refs(false,
906-
origin,
907-
obligation.predicate.trait_ref.clone(),
908-
projection.projection_ty.trait_ref.clone()) {
905+
match infcx.eq_trait_refs(false,
906+
origin,
907+
obligation.predicate.trait_ref.clone(),
908+
projection.projection_ty.trait_ref.clone()) {
909909
Ok(()) => { }
910910
Err(e) => {
911911
selcx.tcx().sess.span_bug(

src/librustc/middle/traits/select.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -2695,11 +2695,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
26952695
skol_obligation_trait_ref);
26962696

26972697
let origin = infer::RelateOutputImplTypes(obligation.cause.span);
2698-
if let Err(e) = self.infcx.sub_trait_refs(false,
2699-
origin,
2700-
impl_trait_ref.value.clone(),
2701-
skol_obligation_trait_ref) {
2702-
debug!("match_impl: failed sub_trait_refs due to `{}`", e);
2698+
if let Err(e) = self.infcx.eq_trait_refs(false,
2699+
origin,
2700+
impl_trait_ref.value.clone(),
2701+
skol_obligation_trait_ref) {
2702+
debug!("match_impl: failed eq_trait_refs due to `{}`", e);
27032703
return Err(());
27042704
}
27052705

0 commit comments

Comments
 (0)