Skip to content

Change TraitRef subtyping checks to equality #29960

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions src/librustc/middle/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -474,14 +474,26 @@ pub fn mk_eqty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
cx.commit_if_ok(|_| cx.eq_types(a_is_expected, origin, a, b))
}

pub fn mk_sub_poly_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
pub fn mk_eq_trait_refs<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
a_is_expected: bool,
origin: TypeOrigin,
a: ty::PolyTraitRef<'tcx>,
b: ty::PolyTraitRef<'tcx>)
a: ty::TraitRef<'tcx>,
b: ty::TraitRef<'tcx>)
-> UnitResult<'tcx>
{
debug!("mk_sub_trait_refs({:?} <: {:?})",
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>,
a_is_expected: bool,
origin: TypeOrigin,
a: ty::PolyTraitRef<'tcx>,
b: ty::PolyTraitRef<'tcx>)
-> UnitResult<'tcx>
{
debug!("mk_sub_poly_trait_refs({:?} <: {:?})",
a, b);
cx.commit_if_ok(|_| cx.sub_poly_trait_refs(a_is_expected, origin, a.clone(), b.clone()))
}
Expand Down Expand Up @@ -857,22 +869,22 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
})
}

pub fn sub_trait_refs(&self,
pub fn eq_trait_refs(&self,
a_is_expected: bool,
origin: TypeOrigin,
a: ty::TraitRef<'tcx>,
b: ty::TraitRef<'tcx>)
-> UnitResult<'tcx>
{
debug!("sub_trait_refs({:?} <: {:?})",
debug!("eq_trait_refs({:?} <: {:?})",
a,
b);
self.commit_if_ok(|_| {
let trace = TypeTrace {
origin: origin,
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(|_| ())
})
}

Expand Down
21 changes: 10 additions & 11 deletions src/librustc/middle/traits/coherence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use super::util;
use metadata::cstore::LOCAL_CRATE;
use middle::def_id::DefId;
use middle::subst::{Subst, Substs, TypeSpace};
use middle::ty::{self, ToPolyTraitRef, Ty};
use middle::ty::{self, Ty};
use middle::infer::{self, InferCtxt};
use syntax::codemap::{DUMMY_SP, Span};

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

let selcx = &mut SelectionContext::intercrate(infcx);
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`?
/// (Including all conditions)
/// Can both impl `a` and impl `b` be satisfied by a common type (including `where` clauses)?
fn overlap(selcx: &mut SelectionContext,
a_def_id: DefId,
b_def_id: DefId)
Expand All @@ -68,16 +67,16 @@ fn overlap(selcx: &mut SelectionContext,

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

// Does `a <: b` hold? If not, no overlap.
if let Err(_) = infer::mk_sub_poly_trait_refs(selcx.infcx(),
true,
infer::Misc(DUMMY_SP),
a_trait_ref.to_poly_trait_ref(),
b_trait_ref.to_poly_trait_ref()) {
// Do `a` and `b` unify? If not, no overlap.
if let Err(_) = infer::mk_eq_trait_refs(selcx.infcx(),
true,
infer::Misc(DUMMY_SP),
a_trait_ref,
b_trait_ref) {
return false;
}

debug!("overlap: subtraitref check succeeded");
debug!("overlap: unification check succeeded");

// Are any of the obligations unsatisfiable? If so, no overlap.
let infcx = selcx.infcx();
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/middle/traits/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -902,10 +902,10 @@ fn confirm_param_env_candidate<'cx,'tcx>(
obligation.predicate.item_name);

let origin = infer::RelateOutputImplTypes(obligation.cause.span);
match infcx.sub_trait_refs(false,
origin,
obligation.predicate.trait_ref.clone(),
projection.projection_ty.trait_ref.clone()) {
match infcx.eq_trait_refs(false,
origin,
obligation.predicate.trait_ref.clone(),
projection.projection_ty.trait_ref.clone()) {
Ok(()) => { }
Err(e) => {
selcx.tcx().sess.span_bug(
Expand Down
10 changes: 5 additions & 5 deletions src/librustc/middle/traits/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2695,11 +2695,11 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
skol_obligation_trait_ref);

let origin = infer::RelateOutputImplTypes(obligation.cause.span);
if let Err(e) = self.infcx.sub_trait_refs(false,
origin,
impl_trait_ref.value.clone(),
skol_obligation_trait_ref) {
debug!("match_impl: failed sub_trait_refs due to `{}`", e);
if let Err(e) = self.infcx.eq_trait_refs(false,
origin,
impl_trait_ref.value.clone(),
skol_obligation_trait_ref) {
debug!("match_impl: failed eq_trait_refs due to `{}`", e);
return Err(());
}

Expand Down