Skip to content
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
71 changes: 22 additions & 49 deletions src/librustc/infer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -386,33 +386,6 @@ pub fn normalizing_infer_ctxt<'a, 'tcx>(tcx: &'a TyCtxt<'tcx>,
infcx
}

/// Computes the least upper-bound of `a` and `b`. If this is not possible, reports an error and
/// returns ty::err.
pub fn common_supertype<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
origin: TypeOrigin,
a_is_expected: bool,
a: Ty<'tcx>,
b: Ty<'tcx>)
-> Ty<'tcx>
{
debug!("common_supertype({:?}, {:?})",
a, b);

let trace = TypeTrace {
origin: origin,
values: Types(expected_found(a_is_expected, a, b))
};

let result = cx.commit_if_ok(|_| cx.lub(a_is_expected, trace.clone()).relate(&a, &b));
match result {
Ok(t) => t,
Err(ref err) => {
cx.report_and_explain_type_error(trace, err).emit();
cx.tcx.types.err
}
}
}

pub fn mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
a_is_expected: bool,
origin: TypeOrigin,
Expand All @@ -434,7 +407,7 @@ pub fn can_mk_subty<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
origin: TypeOrigin::Misc(codemap::DUMMY_SP),
values: Types(expected_found(true, a, b))
};
cx.sub(true, trace).relate(&a, &b).map(|_| ())
cx.sub(true, trace, &a, &b).map(|_| ())
})
}

Expand Down Expand Up @@ -695,32 +668,32 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
cause: None}
}

// public so that it can be used from the rustc_driver unit tests
pub fn equate(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
-> equate::Equate<'a, 'tcx>
pub fn equate<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T)
-> RelateResult<'tcx, T>
where T: Relate<'a, 'tcx>
{
self.combine_fields(a_is_expected, trace).equate()
self.combine_fields(a_is_expected, trace).equate().relate(a, b)
}

// public so that it can be used from the rustc_driver unit tests
pub fn sub(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
-> sub::Sub<'a, 'tcx>
pub fn sub<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T)
-> RelateResult<'tcx, T>
where T: Relate<'a, 'tcx>
{
self.combine_fields(a_is_expected, trace).sub()
self.combine_fields(a_is_expected, trace).sub().relate(a, b)
}

// public so that it can be used from the rustc_driver unit tests
pub fn lub(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
-> lub::Lub<'a, 'tcx>
pub fn lub<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T)
-> RelateResult<'tcx, T>
where T: Relate<'a, 'tcx>
{
self.combine_fields(a_is_expected, trace).lub()
self.combine_fields(a_is_expected, trace).lub().relate(a, b)
}

// public so that it can be used from the rustc_driver unit tests
pub fn glb(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>)
-> glb::Glb<'a, 'tcx>
pub fn glb<T>(&'a self, a_is_expected: bool, trace: TypeTrace<'tcx>, a: &T, b: &T)
-> RelateResult<'tcx, T>
where T: Relate<'a, 'tcx>
{
self.combine_fields(a_is_expected, trace).glb()
self.combine_fields(a_is_expected, trace).glb().relate(a, b)
}

fn start_snapshot(&self) -> CombinedSnapshot {
Expand Down Expand Up @@ -861,7 +834,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
debug!("sub_types({:?} <: {:?})", a, b);
self.commit_if_ok(|_| {
let trace = TypeTrace::types(origin, a_is_expected, a, b);
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
self.sub(a_is_expected, trace, &a, &b).map(|_| ())
})
}

Expand All @@ -874,7 +847,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
{
self.commit_if_ok(|_| {
let trace = TypeTrace::types(origin, a_is_expected, a, b);
self.equate(a_is_expected, trace).relate(&a, &b).map(|_| ())
self.equate(a_is_expected, trace, &a, &b).map(|_| ())
})
}

Expand All @@ -893,7 +866,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
origin: origin,
values: TraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
};
self.equate(a_is_expected, trace).relate(&a, &b).map(|_| ())
self.equate(a_is_expected, trace, &a, &b).map(|_| ())
})
}

Expand All @@ -912,7 +885,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
origin: origin,
values: PolyTraitRefs(expected_found(a_is_expected, a.clone(), b.clone()))
};
self.sub(a_is_expected, trace).relate(&a, &b).map(|_| ())
self.sub(a_is_expected, trace, &a, &b).map(|_| ())
})
}

Expand Down Expand Up @@ -1461,7 +1434,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
origin: TypeOrigin::Misc(codemap::DUMMY_SP),
values: Types(expected_found(true, e, e))
};
self.equate(true, trace).relate(a, b)
self.equate(true, trace, a, b)
}).map(|_| ())
}

Expand Down
11 changes: 11 additions & 0 deletions src/librustc/ty/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,17 @@ impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::ClosureSubsts<'tcx> {
}
}

impl<'a,'tcx:'a> Relate<'a,'tcx> for Substs<'tcx> {
fn relate<R>(relation: &mut R,
a: &Substs<'tcx>,
b: &Substs<'tcx>)
-> RelateResult<'tcx, Substs<'tcx>>
where R: TypeRelation<'a,'tcx>
{
relate_substs(relation, None, a, b)
}
}

impl<'a,'tcx:'a> Relate<'a,'tcx> for ty::Region {
fn relate<R>(relation: &mut R,
a: &ty::Region,
Expand Down
25 changes: 11 additions & 14 deletions src/librustc_driver/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ use rustc::ty::subst;
use rustc::ty::subst::Subst;
use rustc::traits::ProjectionMode;
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
use rustc::ty::relate::TypeRelation;
use rustc::ty::relate::{TypeRelation, RelateResult};
use rustc::infer::{self, TypeOrigin};
use rustc::infer::lub::Lub;
use rustc::infer::glb::Glb;
use rustc::infer::sub::Sub;
use rustc_metadata::cstore::CStore;
use rustc::front::map as hir_map;
use rustc::session::{self, config};
Expand Down Expand Up @@ -358,25 +355,25 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
infer::TypeTrace::dummy(self.tcx())
}

pub fn sub(&self) -> Sub<'a, 'tcx> {
pub fn sub(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
let trace = self.dummy_type_trace();
self.infcx.sub(true, trace)
self.infcx.sub(true, trace, t1, t2)
}

pub fn lub(&self) -> Lub<'a, 'tcx> {
pub fn lub(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
let trace = self.dummy_type_trace();
self.infcx.lub(true, trace)
self.infcx.lub(true, trace, t1, t2)
}

pub fn glb(&self) -> Glb<'a, 'tcx> {
pub fn glb(&self, t1: &Ty<'tcx>, t2: &Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
let trace = self.dummy_type_trace();
self.infcx.glb(true, trace)
self.infcx.glb(true, trace, t1, t2)
}

/// Checks that `t1 <: t2` is true (this may register additional
/// region checks).
pub fn check_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
match self.sub().relate(&t1, &t2) {
match self.sub(&t1, &t2) {
Ok(_) => {}
Err(ref e) => {
panic!("unexpected error computing sub({:?},{:?}): {}", t1, t2, e);
Expand All @@ -387,7 +384,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
/// Checks that `t1 <: t2` is false (this may register additional
/// region checks).
pub fn check_not_sub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) {
match self.sub().relate(&t1, &t2) {
match self.sub(&t1, &t2) {
Err(_) => {}
Ok(_) => {
panic!("unexpected success computing sub({:?},{:?})", t1, t2);
Expand All @@ -397,7 +394,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> {

/// Checks that `LUB(t1,t2) == t_lub`
pub fn check_lub(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_lub: Ty<'tcx>) {
match self.lub().relate(&t1, &t2) {
match self.lub(&t1, &t2) {
Ok(t) => {
self.assert_eq(t, t_lub);
}
Expand All @@ -410,7 +407,7 @@ impl<'a, 'tcx> Env<'a, 'tcx> {
/// Checks that `GLB(t1,t2) == t_glb`
pub fn check_glb(&self, t1: Ty<'tcx>, t2: Ty<'tcx>, t_glb: Ty<'tcx>) {
debug!("check_glb(t1={}, t2={}, t_glb={})", t1, t2, t_glb);
match self.glb().relate(&t1, &t2) {
match self.glb(&t1, &t2) {
Err(e) => {
panic!("unexpected error computing LUB: {:?}", e)
}
Expand Down
19 changes: 11 additions & 8 deletions src/librustc_typeck/check/coercion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ use rustc::ty::adjustment::{AdjustUnsafeFnPointer, AdjustMutToConstPointer};
use rustc::ty::{self, LvaluePreference, TypeAndMut, Ty, TyCtxt};
use rustc::ty::fold::TypeFoldable;
use rustc::ty::error::TypeError;
use rustc::ty::relate::{relate_substs, Relate, RelateResult, TypeRelation};
use rustc::ty::relate::{RelateResult, TypeRelation};
use util::common::indent;

use std::cell::RefCell;
Expand Down Expand Up @@ -117,9 +117,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
infcx.commit_if_ok(|_| {
let trace = TypeTrace::types(self.origin, false, a, b);
if self.use_lub {
infcx.lub(false, trace).relate(&a, &b)
infcx.lub(false, trace, &a, &b)
} else {
infcx.sub(false, trace).relate(&a, &b)
infcx.sub(false, trace, &a, &b)
}
})
}
Expand Down Expand Up @@ -649,20 +649,19 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>,
debug!("coercion::try_find_lub({:?}, {:?})", prev_ty, new_ty);

let trace = TypeTrace::types(origin, true, prev_ty, new_ty);
let mut lub = fcx.infcx().lub(true, trace);

// Special-case that coercion alone cannot handle:
// Two function item types of differing IDs or Substs.
match (&prev_ty.sty, &new_ty.sty) {
(&ty::TyFnDef(a_def_id, a_substs, a_fty),
&ty::TyFnDef(b_def_id, b_substs, b_fty)) => {
// The signature must always match.
let fty = lub.relate(a_fty, b_fty)?;
let fty = fcx.infcx().lub(true, trace.clone(), a_fty, b_fty)?;

if a_def_id == b_def_id {
// Same function, maybe the parameters match.
let substs = fcx.infcx().commit_if_ok(|_| {
relate_substs(&mut lub, None, a_substs, b_substs)
fcx.infcx().lub(true, trace.clone(), a_substs, b_substs)
}).map(|s| fcx.tcx().mk_substs(s));

if let Ok(substs) = substs {
Expand Down Expand Up @@ -724,7 +723,9 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>,
};

if !noop {
return fcx.infcx().commit_if_ok(|_| lub.relate(&prev_ty, &new_ty));
return fcx.infcx().commit_if_ok(|_| {
fcx.infcx().lub(true, trace.clone(), &prev_ty, &new_ty)
});
}
}

Expand All @@ -734,7 +735,9 @@ pub fn try_find_lub<'a, 'b, 'tcx, E, I>(fcx: &FnCtxt<'a, 'tcx>,
if let Some(e) = first_error {
Err(e)
} else {
fcx.infcx().commit_if_ok(|_| lub.relate(&prev_ty, &new_ty))
fcx.infcx().commit_if_ok(|_| {
fcx.infcx().lub(true, trace, &prev_ty, &new_ty)
})
}
}
Ok((ty, adjustment)) => {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_typeck/check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2904,7 +2904,7 @@ fn check_expr_with_expectation_and_lvalue_pref<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
} else {
fcx.infcx().commit_if_ok(|_| {
let trace = TypeTrace::types(origin, true, then_ty, else_ty);
fcx.infcx().lub(true, trace).relate(&then_ty, &else_ty)
fcx.infcx().lub(true, trace, &then_ty, &else_ty)
})
};
(origin, then_ty, else_ty, result)
Expand Down