Skip to content

Commit 6c43dfb

Browse files
committed
Cleanup type resolution to use type folding infrastructure and not
have such a silly over-engineered interface.
1 parent 4799098 commit 6c43dfb

File tree

8 files changed

+155
-426
lines changed

8 files changed

+155
-426
lines changed

src/librustc/middle/typeck/check/_match.rs

Lines changed: 18 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use middle::subst::{Subst, Substs};
1414
use middle::ty::{mod, Ty};
1515
use middle::typeck::check::{check_expr, check_expr_has_type, demand, FnCtxt};
1616
use middle::typeck::check::{instantiate_path, structurally_resolved_type, valid_range_bounds};
17-
use middle::typeck::infer::{mod, resolve};
17+
use middle::typeck::infer;
1818
use middle::typeck::require_same_types;
1919
use util::nodemap::FnvHashMap;
2020

@@ -142,11 +142,8 @@ pub fn check_pat<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
142142
ast::PatRegion(ref inner) => {
143143
let inner_ty = fcx.infcx().next_ty_var();
144144

145-
let mutbl = infer::resolve_type(
146-
fcx.infcx(), Some(pat.span),
147-
expected, resolve::try_resolve_tvar_shallow)
148-
.ok()
149-
.and_then(|t| ty::deref(t, true))
145+
let mutbl =
146+
ty::deref(fcx.infcx().shallow_resolve(expected), true)
150147
.map_or(ast::MutImmutable, |mt| mt.mutbl);
151148

152149
let mt = ty::mt { ty: inner_ty, mutbl: mutbl };
@@ -213,23 +210,21 @@ pub fn check_dereferencable<'a, 'tcx>(pcx: &pat_ctxt<'a, 'tcx>,
213210
inner: &ast::Pat) -> bool {
214211
let fcx = pcx.fcx;
215212
let tcx = pcx.fcx.ccx.tcx;
216-
match infer::resolve_type(
217-
fcx.infcx(), Some(span),
218-
expected, resolve::try_resolve_tvar_shallow) {
219-
Ok(t) if pat_is_binding(&tcx.def_map, inner) => {
220-
ty::deref(t, true).map_or(true, |mt| match mt.ty.sty {
221-
ty::ty_trait(_) => {
222-
// This is "x = SomeTrait" being reduced from
223-
// "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
224-
span_err!(tcx.sess, span, E0033,
225-
"type `{}` cannot be dereferenced",
226-
fcx.infcx().ty_to_string(t));
227-
false
228-
}
229-
_ => true
230-
})
231-
}
232-
_ => true
213+
if pat_is_binding(&tcx.def_map, inner) {
214+
let expected = fcx.infcx().shallow_resolve(expected);
215+
ty::deref(expected, true).map_or(true, |mt| match mt.ty.sty {
216+
ty::ty_trait(_) => {
217+
// This is "x = SomeTrait" being reduced from
218+
// "let &x = &SomeTrait" or "let box x = Box<SomeTrait>", an error.
219+
span_err!(tcx.sess, span, E0033,
220+
"type `{}` cannot be dereferenced",
221+
fcx.infcx().ty_to_string(expected));
222+
false
223+
}
224+
_ => true
225+
})
226+
} else {
227+
true
233228
}
234229
}
235230

src/librustc/middle/typeck/check/demand.rs

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@
1212
use middle::ty::{mod, Ty};
1313
use middle::typeck::check::FnCtxt;
1414
use middle::typeck::infer;
15-
use middle::typeck::infer::resolve_type;
16-
use middle::typeck::infer::resolve::try_resolve_tvar_shallow;
1715

1816
use std::result::{Err, Ok};
1917
use std::result;
@@ -68,12 +66,7 @@ pub fn coerce<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
6866
debug!("demand::coerce(expected = {}, expr_ty = {})",
6967
expected.repr(fcx.ccx.tcx),
7068
expr_ty.repr(fcx.ccx.tcx));
71-
let expected = if ty::type_needs_infer(expected) {
72-
resolve_type(fcx.infcx(),
73-
None,
74-
expected,
75-
try_resolve_tvar_shallow).unwrap_or(expected)
76-
} else { expected };
69+
let expected = fcx.infcx().resolve_type_vars_if_possible(&expected);
7770
match fcx.mk_assignty(expr, expr_ty, expected) {
7871
result::Ok(()) => { /* ok */ }
7972
result::Err(ref err) => {

src/librustc/middle/typeck/check/regionck.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,8 +124,6 @@ use middle::typeck::astconv::AstConv;
124124
use middle::typeck::check::FnCtxt;
125125
use middle::typeck::check::regionmanip;
126126
use middle::typeck::check::vtable;
127-
use middle::typeck::infer::resolve_and_force_all_but_regions;
128-
use middle::typeck::infer::resolve_type;
129127
use middle::typeck::infer;
130128
use middle::typeck::MethodCall;
131129
use middle::pat_util;
@@ -295,11 +293,7 @@ impl<'a, 'tcx> Rcx<'a, 'tcx> {
295293
/// of b will be `&<R0>.int` and then `*b` will require that `<R0>` be bigger than the let and
296294
/// the `*b` expression, so we will effectively resolve `<R0>` to be the block B.
297295
pub fn resolve_type(&self, unresolved_ty: Ty<'tcx>) -> Ty<'tcx> {
298-
match resolve_type(self.fcx.infcx(), None, unresolved_ty,
299-
resolve_and_force_all_but_regions) {
300-
Ok(t) => t,
301-
Err(_) => ty::mk_err()
302-
}
296+
self.fcx.infcx().resolve_type_vars_if_possible(&unresolved_ty)
303297
}
304298

305299
/// Try to resolve the type for the given node.

src/librustc/middle/typeck/check/writeback.rs

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@ use middle::ty::{mod, Ty};
1919
use middle::ty_fold::{TypeFolder,TypeFoldable};
2020
use middle::typeck::astconv::AstConv;
2121
use middle::typeck::check::FnCtxt;
22-
use middle::typeck::infer::{force_all, resolve_all, resolve_region};
23-
use middle::typeck::infer::resolve_type;
2422
use middle::typeck::infer;
2523
use middle::typeck::{MethodCall, MethodCallee};
2624
use middle::typeck::write_substs_to_tcx;
@@ -339,8 +337,8 @@ impl<'cx, 'tcx> WritebackCx<'cx, 'tcx> {
339337
}
340338
}
341339

342-
fn resolve<T:ResolveIn<'tcx>>(&self, t: &T, reason: ResolveReason) -> T {
343-
t.resolve_in(&mut Resolver::new(self.fcx, reason))
340+
fn resolve<T:TypeFoldable<'tcx>>(&self, t: &T, reason: ResolveReason) -> T {
341+
t.fold_with(&mut Resolver::new(self.fcx, reason))
344342
}
345343
}
346344

@@ -375,19 +373,6 @@ impl ResolveReason {
375373
}
376374
}
377375

378-
///////////////////////////////////////////////////////////////////////////
379-
// Convenience methods for resolving different kinds of things.
380-
381-
trait ResolveIn<'tcx> {
382-
fn resolve_in<'a>(&self, resolver: &mut Resolver<'a, 'tcx>) -> Self;
383-
}
384-
385-
impl<'tcx, T: TypeFoldable<'tcx>> ResolveIn<'tcx> for T {
386-
fn resolve_in<'a>(&self, resolver: &mut Resolver<'a, 'tcx>) -> T {
387-
self.fold_with(resolver)
388-
}
389-
}
390-
391376
///////////////////////////////////////////////////////////////////////////
392377
// The Resolver. This is the type folding engine that detects
393378
// unresolved types and so forth.
@@ -465,21 +450,19 @@ impl<'cx, 'tcx> TypeFolder<'tcx> for Resolver<'cx, 'tcx> {
465450
}
466451

467452
fn fold_ty(&mut self, t: Ty<'tcx>) -> Ty<'tcx> {
468-
if !ty::type_needs_infer(t) {
469-
return t;
470-
}
471-
472-
match resolve_type(self.infcx, None, t, resolve_all | force_all) {
453+
match self.infcx.fully_resolve(&t) {
473454
Ok(t) => t,
474455
Err(e) => {
456+
debug!("Resolver::fold_ty: input type `{}` not fully resolvable",
457+
t.repr(self.tcx));
475458
self.report_error(e);
476459
ty::mk_err()
477460
}
478461
}
479462
}
480463

481464
fn fold_region(&mut self, r: ty::Region) -> ty::Region {
482-
match resolve_region(self.infcx, r, resolve_all | force_all) {
465+
match self.infcx.fully_resolve(&r) {
483466
Ok(r) => r,
484467
Err(e) => {
485468
self.report_error(e);

src/librustc/middle/typeck/coherence/mod.rs

Lines changed: 20 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,12 @@ use middle::ty::{ty_param, Polytype, ty_ptr};
2828
use middle::ty::{ty_rptr, ty_struct, ty_trait, ty_tup};
2929
use middle::ty::{ty_uint, ty_unboxed_closure, ty_uniq, ty_bare_fn};
3030
use middle::ty::{ty_closure};
31-
use middle::ty::type_is_ty_var;
3231
use middle::subst::Subst;
3332
use middle::ty;
3433
use middle::typeck::CrateCtxt;
3534
use middle::typeck::infer::combine::Combine;
3635
use middle::typeck::infer::InferCtxt;
37-
use middle::typeck::infer::{new_infer_ctxt, resolve_ivar, resolve_type};
36+
use middle::typeck::infer::new_infer_ctxt;
3837
use std::collections::{HashSet};
3938
use std::cell::RefCell;
4039
use std::rc::Rc;
@@ -54,80 +53,35 @@ use util::ppaux::Repr;
5453
mod orphan;
5554
mod overlap;
5655

57-
fn get_base_type<'a, 'tcx>(inference_context: &InferCtxt<'a, 'tcx>,
58-
span: Span,
59-
original_type: Ty<'tcx>)
60-
-> Option<Ty<'tcx>> {
61-
let resolved_type = match resolve_type(inference_context,
62-
Some(span),
63-
original_type,
64-
resolve_ivar) {
65-
Ok(resulting_type) if !type_is_ty_var(resulting_type) => resulting_type,
66-
_ => {
67-
inference_context.tcx.sess.span_fatal(span,
68-
"the type of this value must be known in order \
69-
to determine the base type");
70-
}
71-
};
72-
73-
match resolved_type.sty {
74-
ty_enum(..) | ty_struct(..) | ty_unboxed_closure(..) => {
75-
debug!("(getting base type) found base type");
76-
Some(resolved_type)
56+
// Returns the def ID of the base type, if there is one.
57+
fn get_base_type_def_id<'a, 'tcx>(inference_context: &InferCtxt<'a, 'tcx>,
58+
span: Span,
59+
ty: Ty<'tcx>)
60+
-> Option<DefId> {
61+
match ty.sty {
62+
ty_enum(def_id, _) |
63+
ty_struct(def_id, _) => {
64+
Some(def_id)
7765
}
7866

79-
_ if ty::type_is_trait(resolved_type) => {
80-
debug!("(getting base type) found base type (trait)");
81-
Some(resolved_type)
67+
ty_trait(ref t) => {
68+
Some(t.principal.def_id)
8269
}
8370

8471
ty_bool | ty_char | ty_int(..) | ty_uint(..) | ty_float(..) |
8572
ty_str(..) | ty_vec(..) | ty_bare_fn(..) | ty_closure(..) | ty_tup(..) |
86-
ty_infer(..) | ty_param(..) | ty_err | ty_open(..) | ty_uniq(_) |
73+
ty_param(..) | ty_err | ty_open(..) | ty_uniq(_) |
8774
ty_ptr(_) | ty_rptr(_, _) => {
88-
debug!("(getting base type) no base type; found {}",
89-
original_type.sty);
9075
None
9176
}
92-
ty_trait(..) => panic!("should have been caught")
93-
}
94-
}
9577

96-
// Returns the def ID of the base type, if there is one.
97-
fn get_base_type_def_id<'a, 'tcx>(inference_context: &InferCtxt<'a, 'tcx>,
98-
span: Span,
99-
original_type: Ty<'tcx>)
100-
-> Option<DefId> {
101-
match get_base_type(inference_context, span, original_type) {
102-
None => None,
103-
Some(base_type) => {
104-
match base_type.sty {
105-
ty_enum(def_id, _) |
106-
ty_struct(def_id, _) |
107-
ty_unboxed_closure(def_id, _, _) => {
108-
Some(def_id)
109-
}
110-
ty_ptr(ty::mt {ty, ..}) |
111-
ty_rptr(_, ty::mt {ty, ..}) |
112-
ty_uniq(ty) => {
113-
match ty.sty {
114-
ty_trait(box ty::TyTrait { ref principal, .. }) => {
115-
Some(principal.def_id)
116-
}
117-
_ => {
118-
panic!("get_base_type() returned a type that wasn't an \
119-
enum, struct, or trait");
120-
}
121-
}
122-
}
123-
ty_trait(box ty::TyTrait { ref principal, .. }) => {
124-
Some(principal.def_id)
125-
}
126-
_ => {
127-
panic!("get_base_type() returned a type that wasn't an \
128-
enum, struct, or trait");
129-
}
130-
}
78+
ty_infer(..) | ty_unboxed_closure(..) => {
79+
// `ty` comes from a user declaration so we should only expect types
80+
// that the user can type
81+
inference_context.tcx.sess.span_bug(
82+
span,
83+
format!("coherence encountered unexpected type searching for base type: {}",
84+
ty.repr(inference_context.tcx))[]);
13185
}
13286
}
13387
}

src/librustc/middle/typeck/infer/coercion.rs

Lines changed: 4 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,9 @@ use middle::subst;
6464
use middle::ty::{AutoPtr, AutoDerefRef, AdjustDerefRef, AutoUnsize, AutoUnsafe};
6565
use middle::ty::{mt};
6666
use middle::ty::{mod, Ty};
67-
use middle::typeck::infer::{CoerceResult, resolve_type, Coercion};
67+
use middle::typeck::infer::{CoerceResult, Coercion};
6868
use middle::typeck::infer::combine::{CombineFields, Combine};
6969
use middle::typeck::infer::sub::Sub;
70-
use middle::typeck::infer::resolve::try_resolve_tvar_shallow;
7170
use util::ppaux;
7271
use util::ppaux::Repr;
7372

@@ -194,19 +193,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
194193
}
195194

196195
pub fn unpack_actual_value<T>(&self, a: Ty<'tcx>, f: |&ty::sty<'tcx>| -> T)
197-
-> T {
198-
match resolve_type(self.get_ref().infcx, None,
199-
a, try_resolve_tvar_shallow) {
200-
Ok(t) => {
201-
f(&t.sty)
202-
}
203-
Err(e) => {
204-
self.get_ref().infcx.tcx.sess.span_bug(
205-
self.get_ref().trace.origin.span(),
206-
format!("failed to resolve even without \
207-
any force options: {}", e).as_slice());
208-
}
209-
}
196+
-> T
197+
{
198+
f(&self.get_ref().infcx.shallow_resolve(a).sty)
210199
}
211200

212201
// ~T -> &T or &mut T -> &T (including where T = [U] or str)

0 commit comments

Comments
 (0)