rustc: move infer::coercion to rustc_typeck.

This commit is contained in:
Eduard Burtescu 2015-01-16 05:48:15 +02:00
parent 265a23320d
commit f9f3ba5920
3 changed files with 43 additions and 60 deletions

View File

@ -33,12 +33,10 @@ use std::rc::Rc;
use syntax::ast;
use syntax::codemap;
use syntax::codemap::Span;
use util::common::indent;
use util::nodemap::FnvHashMap;
use util::ppaux::{ty_to_string};
use util::ppaux::{Repr, UserString};
use self::coercion::Coerce;
use self::combine::{Combine, Combineable, CombineFields};
use self::region_inference::{RegionVarBindings, RegionSnapshot};
use self::equate::Equate;
@ -47,7 +45,6 @@ use self::lub::Lub;
use self::unify::{UnificationTable, InferCtxtMethodsForSimplyUnifiableTypes};
use self::error_reporting::ErrorReporting;
pub mod coercion;
pub mod combine;
pub mod doc;
pub mod equate;
@ -68,7 +65,6 @@ pub type Bound<T> = Option<T>;
pub type cres<'tcx, T> = Result<T,ty::type_err<'tcx>>; // "combine result"
pub type ures<'tcx> = cres<'tcx, ()>; // "unify result"
pub type fres<T> = Result<T, fixup_err>; // "fixup result"
pub type CoerceResult<'tcx> = cres<'tcx, Option<ty::AutoAdjustment<'tcx>>>;
pub struct InferCtxt<'a, 'tcx: 'a> {
pub tcx: &'a ty::ctxt<'tcx>,
@ -409,24 +405,6 @@ fn expected_found<T>(a_is_expected: bool,
}
}
pub fn mk_coercety<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
a_is_expected: bool,
origin: TypeOrigin,
a: Ty<'tcx>,
b: Ty<'tcx>)
-> CoerceResult<'tcx> {
debug!("mk_coercety({} -> {})", a.repr(cx.tcx), b.repr(cx.tcx));
indent(|| {
cx.commit_if_ok(|| {
let trace = TypeTrace {
origin: origin,
values: Types(expected_found(a_is_expected, a, b))
};
Coerce(cx.combine_fields(a_is_expected, trace)).tys(a, b)
})
})
}
trait then<'tcx> {
fn then<T, F>(&self, f: F) -> Result<T, ty::type_err<'tcx>> where
T: Clone,
@ -689,10 +667,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
{
debug!("sub_types({} <: {})", a.repr(self.tcx), b.repr(self.tcx));
self.commit_if_ok(|| {
let trace = TypeTrace {
origin: origin,
values: Types(expected_found(a_is_expected, a, b))
};
let trace = TypeTrace::types(origin, a_is_expected, a, b);
self.sub(a_is_expected, trace).tys(a, b).to_ures()
})
}
@ -705,10 +680,7 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
-> ures<'tcx>
{
self.commit_if_ok(|| {
let trace = TypeTrace {
origin: origin,
values: Types(expected_found(a_is_expected, a, b))
};
let trace = TypeTrace::types(origin, a_is_expected, a, b);
self.equate(a_is_expected, trace).tys(a, b).to_ures()
})
}
@ -1118,6 +1090,17 @@ impl<'tcx> TypeTrace<'tcx> {
self.origin.span()
}
pub fn types(origin: TypeOrigin,
a_is_expected: bool,
a: Ty<'tcx>,
b: Ty<'tcx>)
-> TypeTrace<'tcx> {
TypeTrace {
origin: origin,
values: Types(expected_found(a_is_expected, a, b))
}
}
pub fn dummy(tcx: &ty::ctxt<'tcx>) -> TypeTrace<'tcx> {
TypeTrace {
origin: Misc(codemap::DUMMY_SP),

View File

@ -60,14 +60,15 @@
//! sort of a minor point so I've opted to leave it for later---after all
//! we may want to adjust precisely when coercions occur.
use super::{CoerceResult, Coercion};
use super::combine::{CombineFields, Combine};
use super::sub::Sub;
use middle::infer::{cres, Coercion, InferCtxt, TypeOrigin, TypeTrace};
use middle::infer::combine::{CombineFields, Combine};
use middle::infer::sub::Sub;
use middle::subst;
use middle::ty::{AutoPtr, AutoDerefRef, AdjustDerefRef, AutoUnsize, AutoUnsafe};
use middle::ty::{mt};
use middle::ty::{self, Ty};
use util::common::indent;
use util::ppaux;
use util::ppaux::Repr;
@ -472,24 +473,6 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
}
}
pub fn coerce_borrowed_fn(&self,
a: Ty<'tcx>,
b: Ty<'tcx>)
-> CoerceResult<'tcx> {
debug!("coerce_borrowed_fn(a={}, b={})",
a.repr(self.tcx()),
b.repr(self.tcx()));
match a.sty {
ty::ty_bare_fn(Some(a_def_id), f) => {
self.coerce_from_fn_item(a, a_def_id, f, b)
}
_ => {
self.subtype(a, b)
}
}
}
fn coerce_from_fn_item(&self,
a: Ty<'tcx>,
fn_def_id_a: ast::DefId,
@ -551,6 +534,23 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
}
}
pub type CoerceResult<'tcx> = cres<'tcx, Option<ty::AutoAdjustment<'tcx>>>;
pub fn mk_coercety<'a, 'tcx>(cx: &InferCtxt<'a, 'tcx>,
a_is_expected: bool,
origin: TypeOrigin,
a: Ty<'tcx>,
b: Ty<'tcx>)
-> CoerceResult<'tcx> {
debug!("mk_coercety({} -> {})", a.repr(cx.tcx), b.repr(cx.tcx));
indent(|| {
cx.commit_if_ok(|| {
let trace = TypeTrace::types(origin, a_is_expected, a, b);
Coerce(cx.combine_fields(a_is_expected, trace)).tys(a, b)
})
})
}
fn can_coerce_mutbls(from_mutbl: ast::Mutability,
to_mutbl: ast::Mutability)
-> bool {

View File

@ -132,6 +132,7 @@ pub mod vtable;
pub mod writeback;
pub mod regionmanip;
pub mod regionck;
pub mod coercion;
pub mod demand;
pub mod method;
mod upvar;
@ -1730,18 +1731,17 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
sub: Ty<'tcx>,
sup: Ty<'tcx>)
-> Result<(), ty::type_err<'tcx>> {
match infer::mk_coercety(self.infcx(),
false,
infer::ExprAssignable(expr.span),
sub,
sup) {
Ok(None) => Ok(()),
Err(ref e) => Err((*e)),
Ok(Some(adjustment)) => {
match try!(coercion::mk_coercety(self.infcx(),
false,
infer::ExprAssignable(expr.span),
sub,
sup)) {
None => {}
Some(adjustment) => {
self.write_adjustment(expr.id, expr.span, adjustment);
Ok(())
}
}
Ok(())
}
pub fn mk_eqty(&self,