1
Fork 0

infer: Take the origin in report_mismatched_types.

This commit is contained in:
Eduard Burtescu 2016-02-21 18:57:30 +02:00
parent 871a1e1cf8
commit dc37664c94
4 changed files with 19 additions and 47 deletions

View file

@ -1351,18 +1351,18 @@ impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
} }
pub fn report_mismatched_types(&self, pub fn report_mismatched_types(&self,
span: Span, origin: TypeOrigin,
expected: Ty<'tcx>, expected: Ty<'tcx>,
actual: Ty<'tcx>, actual: Ty<'tcx>,
err: &TypeError<'tcx>) { err: TypeError<'tcx>) {
let trace = TypeTrace { let trace = TypeTrace {
origin: TypeOrigin::Misc(span), origin: origin,
values: Types(ExpectedFound { values: Types(ExpectedFound {
expected: expected, expected: expected,
found: actual found: actual
}) })
}; };
self.report_and_explain_type_error(trace, err); self.report_and_explain_type_error(trace, &err);
} }
pub fn report_conflicting_default_types(&self, pub fn report_conflicting_default_types(&self,

View file

@ -10,45 +10,27 @@
use check::{coercion, FnCtxt}; use check::{coercion, FnCtxt};
use middle::ty::{self, Ty}; use middle::ty::Ty;
use middle::infer::{self, TypeOrigin}; use middle::infer::TypeOrigin;
use std::result::Result::{Err, Ok};
use syntax::codemap::Span; use syntax::codemap::Span;
use rustc_front::hir; use rustc_front::hir;
// Requires that the two types unify, and prints an error message if // Requires that the two types unify, and prints an error message if
// they don't. // they don't.
pub fn suptype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span, pub fn suptype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
ty_expected: Ty<'tcx>, ty_actual: Ty<'tcx>) { expected: Ty<'tcx>, actual: Ty<'tcx>) {
suptype_with_fn(fcx, sp, false, ty_expected, ty_actual, let origin = TypeOrigin::Misc(sp);
|sp, e, a, s| { fcx.report_mismatched_types(sp, e, a, s) }) if let Err(e) = fcx.infcx().sub_types(false, origin, actual, expected) {
} fcx.infcx().report_mismatched_types(origin, expected, actual, e);
/// As `suptype`, but call `handle_err` if unification for subtyping fails.
pub fn suptype_with_fn<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
sp: Span,
b_is_expected: bool,
ty_a: Ty<'tcx>,
ty_b: Ty<'tcx>,
handle_err: F) where
F: FnOnce(Span, Ty<'tcx>, Ty<'tcx>, &ty::error::TypeError<'tcx>),
{
// n.b.: order of actual, expected is reversed
match infer::mk_subty(fcx.infcx(), b_is_expected, TypeOrigin::Misc(sp),
ty_b, ty_a) {
Ok(()) => { /* ok */ }
Err(ref err) => {
handle_err(sp, ty_a, ty_b, err);
}
} }
} }
pub fn eqtype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span, pub fn eqtype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
expected: Ty<'tcx>, actual: Ty<'tcx>) { expected: Ty<'tcx>, actual: Ty<'tcx>) {
match infer::mk_eqty(fcx.infcx(), false, TypeOrigin::Misc(sp), actual, expected) { let origin = TypeOrigin::Misc(sp);
Ok(()) => { /* ok */ } if let Err(e) = fcx.infcx().eq_types(false, origin, actual, expected) {
Err(ref err) => { fcx.report_mismatched_types(sp, expected, actual, err); } fcx.infcx().report_mismatched_types(origin, expected, actual, e);
} }
} }
@ -63,10 +45,8 @@ pub fn coerce<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
expr_ty); expr_ty);
let expr_ty = fcx.resolve_type_vars_if_possible(expr_ty); let expr_ty = fcx.resolve_type_vars_if_possible(expr_ty);
let expected = fcx.resolve_type_vars_if_possible(expected); let expected = fcx.resolve_type_vars_if_possible(expected);
match coercion::mk_assignty(fcx, expr, expr_ty, expected) { let origin = TypeOrigin::Misc(sp);
Ok(()) => { /* ok */ } if let Err(e) = coercion::try(fcx, expr, expr_ty, expected) {
Err(ref err) => { fcx.infcx().report_mismatched_types(origin, expected, expr_ty, e);
fcx.report_mismatched_types(sp, expected, expr_ty, err);
}
} }
} }

View file

@ -1622,14 +1622,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.infcx().type_error_struct(sp, mk_msg, actual_ty, err) self.infcx().type_error_struct(sp, mk_msg, actual_ty, err)
} }
pub fn report_mismatched_types(&self,
sp: Span,
e: Ty<'tcx>,
a: Ty<'tcx>,
err: &TypeError<'tcx>) {
self.infcx().report_mismatched_types(sp, e, a, err)
}
/// Registers an obligation for checking later, during regionck, that the type `ty` must /// Registers an obligation for checking later, during regionck, that the type `ty` must
/// outlive the region `r`. /// outlive the region `r`.
pub fn register_region_obligation(&self, pub fn register_region_obligation(&self,

View file

@ -385,11 +385,12 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
let infcx = new_infer_ctxt(tcx, &tcx.tables, Some(param_env)); let infcx = new_infer_ctxt(tcx, &tcx.tables, Some(param_env));
let origin = TypeOrigin::Misc(span);
let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>, mt_b: ty::TypeAndMut<'tcx>, let check_mutbl = |mt_a: ty::TypeAndMut<'tcx>, mt_b: ty::TypeAndMut<'tcx>,
mk_ptr: &Fn(Ty<'tcx>) -> Ty<'tcx>| { mk_ptr: &Fn(Ty<'tcx>) -> Ty<'tcx>| {
if (mt_a.mutbl, mt_b.mutbl) == (hir::MutImmutable, hir::MutMutable) { if (mt_a.mutbl, mt_b.mutbl) == (hir::MutImmutable, hir::MutMutable) {
infcx.report_mismatched_types(span, mk_ptr(mt_b.ty), infcx.report_mismatched_types(origin, mk_ptr(mt_b.ty),
target, &ty::error::TypeError::Mutability); target, ty::error::TypeError::Mutability);
} }
(mt_a.ty, mt_b.ty, unsize_trait, None) (mt_a.ty, mt_b.ty, unsize_trait, None)
}; };
@ -418,7 +419,6 @@ impl<'a, 'tcx> CoherenceChecker<'a, 'tcx> {
return; return;
} }
let origin = TypeOrigin::Misc(span);
let fields = &def_a.struct_variant().fields; let fields = &def_a.struct_variant().fields;
let diff_fields = fields.iter().enumerate().filter_map(|(i, f)| { let diff_fields = fields.iter().enumerate().filter_map(|(i, f)| {
let (a, b) = (f.ty(tcx, substs_a), f.ty(tcx, substs_b)); let (a, b) = (f.ty(tcx, substs_a), f.ty(tcx, substs_b));