Skip to content

Commit 78f848c

Browse files
committed
Refactor the Typer interface to separate out UnboxedClosureTyper methods, which are
the only things that trait selection needs.
1 parent 7092af7 commit 78f848c

File tree

11 files changed

+195
-119
lines changed

11 files changed

+195
-119
lines changed

src/librustc/middle/expr_use_visitor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,7 +1229,7 @@ impl<'d,'t,'tcx,TYPER:mc::Typer<'tcx>> ExprUseVisitor<'d,'t,'tcx,TYPER> {
12291229
// inferred by regionbk
12301230
let upvar_id = ty::UpvarId { var_id: id_var,
12311231
closure_expr_id: closure_expr.id };
1232-
let upvar_borrow = self.typer.upvar_borrow(upvar_id);
1232+
let upvar_borrow = self.typer.upvar_borrow(upvar_id).unwrap();
12331233

12341234
self.delegate.borrow(closure_expr.id,
12351235
closure_expr.span,

src/librustc/middle/liveness.rs

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,9 @@ use self::VarKind::*;
111111

112112
use middle::def::*;
113113
use middle::mem_categorization::Typer;
114-
use middle::{pat_util, ty};
114+
use middle::pat_util;
115+
use middle::ty;
116+
use middle::ty::UnboxedClosureTyper;
115117
use lint;
116118
use util::nodemap::NodeMap;
117119

@@ -1515,16 +1517,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
15151517
fn fn_ret(&self, id: NodeId) -> ty::FnOutput<'tcx> {
15161518
let fn_ty = ty::node_id_to_type(self.ir.tcx, id);
15171519
match fn_ty.sty {
1518-
ty::ty_unboxed_closure(closure_def_id, _, _) =>
1519-
self.ir.tcx.unboxed_closures()
1520-
.borrow()
1521-
.get(&closure_def_id)
1522-
.unwrap()
1523-
.closure_type
1524-
.sig
1525-
.0
1526-
.output,
1527-
_ => ty::ty_fn_ret(fn_ty)
1520+
ty::ty_unboxed_closure(closure_def_id, _, substs) =>
1521+
self.ir.tcx.unboxed_closure_type(closure_def_id, substs).sig.0.output,
1522+
_ =>
1523+
ty::ty_fn_ret(fn_ty),
15281524
}
15291525
}
15301526

src/librustc/middle/mem_categorization.rs

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ pub use self::categorization::*;
7474
use middle::def;
7575
use middle::region;
7676
use middle::ty::{mod, Ty};
77-
use util::nodemap::{DefIdMap, NodeMap};
77+
use util::nodemap::{NodeMap};
7878
use util::ppaux::{ty_to_string, Repr};
7979

8080
use syntax::ast::{MutImmutable, MutMutable};
@@ -280,7 +280,7 @@ impl<'t,TYPER:'t> Copy for MemCategorizationContext<'t,TYPER> {}
280280
/// In the borrow checker, in contrast, type checking is complete and we
281281
/// know that no errors have occurred, so we simply consult the tcx and we
282282
/// can be sure that only `Ok` results will occur.
283-
pub trait Typer<'tcx> {
283+
pub trait Typer<'tcx> : ty::UnboxedClosureTyper<'tcx> {
284284
fn tcx<'a>(&'a self) -> &'a ty::ctxt<'tcx>;
285285
fn node_ty(&self, id: ast::NodeId) -> Ty<'tcx>;
286286
fn expr_ty_adjusted(&self, expr: &ast::Expr) -> Ty<'tcx>;
@@ -290,11 +290,9 @@ pub trait Typer<'tcx> {
290290
fn adjustments<'a>(&'a self) -> &'a RefCell<NodeMap<ty::AutoAdjustment<'tcx>>>;
291291
fn is_method_call(&self, id: ast::NodeId) -> bool;
292292
fn temporary_scope(&self, rvalue_id: ast::NodeId) -> Option<region::CodeExtent>;
293-
fn upvar_borrow(&self, upvar_id: ty::UpvarId) -> ty::UpvarBorrow;
293+
fn upvar_borrow(&self, upvar_id: ty::UpvarId) -> Option<ty::UpvarBorrow>;
294294
fn capture_mode(&self, closure_expr_id: ast::NodeId)
295295
-> ast::CaptureClause;
296-
fn unboxed_closures<'a>(&'a self)
297-
-> &'a RefCell<DefIdMap<ty::UnboxedClosure<'tcx>>>;
298296
}
299297

300298
impl MutabilityCategory {
@@ -622,8 +620,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
622620
self.cat_upvar(id, span, var_id, fn_node_id, kind, mode, false)
623621
}
624622
ty::ty_unboxed_closure(closure_id, _, _) => {
625-
let unboxed_closures = self.typer.unboxed_closures().borrow();
626-
let kind = (*unboxed_closures)[closure_id].kind;
623+
let kind = self.typer.unboxed_closure_kind(closure_id);
627624
let mode = self.typer.capture_mode(fn_node_id);
628625
self.cat_upvar(id, span, var_id, fn_node_id, kind, mode, true)
629626
}
@@ -800,7 +797,7 @@ impl<'t,'tcx,TYPER:Typer<'tcx>> MemCategorizationContext<'t,TYPER> {
800797
}
801798

802799
// Look up upvar borrow so we can get its region
803-
let upvar_borrow = self.typer.upvar_borrow(upvar_id);
800+
let upvar_borrow = self.typer.upvar_borrow(upvar_id).unwrap();
804801
let ptr = BorrowedPtr(upvar_borrow.kind, upvar_borrow.region);
805802

806803
Rc::new(cmt_ {

src/librustc/middle/traits/fulfill.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ impl<'tcx> FulfillmentContext<'tcx> {
110110
pub fn normalize_projection_type<'a>(&mut self,
111111
infcx: &InferCtxt<'a,'tcx>,
112112
param_env: &ty::ParameterEnvironment<'tcx>,
113-
typer: &Typer<'tcx>,
113+
typer: &ty::UnboxedClosureTyper<'tcx>,
114114
projection_ty: ty::ProjectionTy<'tcx>,
115115
cause: ObligationCause<'tcx>)
116116
-> Ty<'tcx>
@@ -187,7 +187,7 @@ impl<'tcx> FulfillmentContext<'tcx> {
187187
pub fn select_all_or_error<'a>(&mut self,
188188
infcx: &InferCtxt<'a,'tcx>,
189189
param_env: &ty::ParameterEnvironment<'tcx>,
190-
typer: &Typer<'tcx>)
190+
typer: &ty::UnboxedClosureTyper<'tcx>)
191191
-> Result<(),Vec<FulfillmentError<'tcx>>>
192192
{
193193
try!(self.select_where_possible(infcx, param_env, typer));
@@ -213,7 +213,7 @@ impl<'tcx> FulfillmentContext<'tcx> {
213213
pub fn select_new_obligations<'a>(&mut self,
214214
infcx: &InferCtxt<'a,'tcx>,
215215
param_env: &ty::ParameterEnvironment<'tcx>,
216-
typer: &Typer<'tcx>)
216+
typer: &ty::UnboxedClosureTyper<'tcx>)
217217
-> Result<(),Vec<FulfillmentError<'tcx>>>
218218
{
219219
let mut selcx = SelectionContext::new(infcx, param_env, typer);
@@ -223,7 +223,7 @@ impl<'tcx> FulfillmentContext<'tcx> {
223223
pub fn select_where_possible<'a>(&mut self,
224224
infcx: &InferCtxt<'a,'tcx>,
225225
param_env: &ty::ParameterEnvironment<'tcx>,
226-
typer: &Typer<'tcx>)
226+
typer: &ty::UnboxedClosureTyper<'tcx>)
227227
-> Result<(),Vec<FulfillmentError<'tcx>>>
228228
{
229229
let mut selcx = SelectionContext::new(infcx, param_env, typer);

src/librustc/middle/traits/select.rs

Lines changed: 18 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ use util::ppaux::Repr;
4545
pub struct SelectionContext<'cx, 'tcx:'cx> {
4646
infcx: &'cx InferCtxt<'cx, 'tcx>,
4747
param_env: &'cx ty::ParameterEnvironment<'tcx>,
48-
typer: &'cx (Typer<'tcx>+'cx),
48+
closure_typer: &'cx (ty::UnboxedClosureTyper<'tcx>+'cx),
4949

5050
/// Freshener used specifically for skolemizing entries on the
5151
/// obligation stack. This ensures that all entries on the stack
@@ -178,25 +178,25 @@ enum EvaluationResult<'tcx> {
178178
impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
179179
pub fn new(infcx: &'cx InferCtxt<'cx, 'tcx>,
180180
param_env: &'cx ty::ParameterEnvironment<'tcx>,
181-
typer: &'cx Typer<'tcx>)
181+
closure_typer: &'cx ty::UnboxedClosureTyper<'tcx>)
182182
-> SelectionContext<'cx, 'tcx> {
183183
SelectionContext {
184184
infcx: infcx,
185185
param_env: param_env,
186-
typer: typer,
186+
closure_typer: closure_typer,
187187
freshener: infcx.freshener(),
188188
intercrate: false,
189189
}
190190
}
191191

192192
pub fn intercrate(infcx: &'cx InferCtxt<'cx, 'tcx>,
193193
param_env: &'cx ty::ParameterEnvironment<'tcx>,
194-
typer: &'cx Typer<'tcx>)
194+
closure_typer: &'cx ty::UnboxedClosureTyper<'tcx>)
195195
-> SelectionContext<'cx, 'tcx> {
196196
SelectionContext {
197197
infcx: infcx,
198198
param_env: param_env,
199-
typer: typer,
199+
closure_typer: closure_typer,
200200
freshener: infcx.freshener(),
201201
intercrate: true,
202202
}
@@ -919,15 +919,7 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
919919
kind,
920920
obligation.repr(self.tcx()));
921921

922-
let closure_kind = match self.typer.unboxed_closures().borrow().get(&closure_def_id) {
923-
Some(closure) => closure.kind,
924-
None => {
925-
self.tcx().sess.span_bug(
926-
obligation.cause.span,
927-
format!("No entry for unboxed closure: {}",
928-
closure_def_id.repr(self.tcx()))[]);
929-
}
930-
};
922+
let closure_kind = self.closure_typer.unboxed_closure_kind(closure_def_id);
931923

932924
debug!("closure_kind = {}", closure_kind);
933925

@@ -1399,32 +1391,21 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
13991391
return Ok(ParameterBuiltin);
14001392
}
14011393

1402-
match self.tcx().freevars.borrow().get(&def_id.node) {
1403-
None => {
1404-
// No upvars.
1405-
Ok(If(Vec::new()))
1394+
match self.closure_typer.unboxed_closure_upvars(def_id, substs) {
1395+
Some(upvars) => {
1396+
Ok(If(upvars.iter().map(|c| c.ty).collect()))
14061397
}
1407-
1408-
Some(freevars) => {
1409-
let tys: Vec<Ty> =
1410-
freevars
1411-
.iter()
1412-
.map(|freevar| {
1413-
let freevar_def_id = freevar.def.def_id();
1414-
self.typer.node_ty(freevar_def_id.node).subst(self.tcx(), substs)
1415-
})
1416-
.collect();
1417-
Ok(If(tys))
1398+
None => {
1399+
Ok(AmbiguousBuiltin)
14181400
}
14191401
}
14201402
}
14211403

14221404
ty::ty_struct(def_id, substs) => {
14231405
let types: Vec<Ty> =
1424-
ty::struct_fields(self.tcx(), def_id, substs)
1425-
.iter()
1426-
.map(|f| f.mt.ty)
1427-
.collect();
1406+
ty::struct_fields(self.tcx(), def_id, substs).iter()
1407+
.map(|f| f.mt.ty)
1408+
.collect();
14281409
nominal(self, bound, def_id, types)
14291410
}
14301411

@@ -1799,27 +1780,17 @@ impl<'cx, 'tcx> SelectionContext<'cx, 'tcx> {
17991780
closure_def_id.repr(self.tcx()),
18001781
substs.repr(self.tcx()));
18011782

1802-
let closure_type = match self.typer.unboxed_closures().borrow().get(&closure_def_id) {
1803-
Some(closure) => closure.closure_type.clone(),
1804-
None => {
1805-
self.tcx().sess.span_bug(
1806-
obligation.cause.span,
1807-
format!("No entry for unboxed closure: {}",
1808-
closure_def_id.repr(self.tcx()))[]);
1809-
}
1810-
};
1811-
1783+
let closure_type = self.closure_typer.unboxed_closure_type(closure_def_id, substs);
18121784
let closure_sig = &closure_type.sig;
18131785
let arguments_tuple = closure_sig.0.inputs[0];
1814-
let substs =
1786+
let trait_substs =
18151787
Substs::new_trait(
1816-
vec![arguments_tuple.subst(self.tcx(), substs),
1817-
closure_sig.0.output.unwrap().subst(self.tcx(), substs)],
1788+
vec![arguments_tuple, closure_sig.0.output.unwrap()],
18181789
vec![],
18191790
obligation.self_ty());
18201791
let trait_ref = ty::Binder(Rc::new(ty::TraitRef {
18211792
def_id: obligation.predicate.def_id(),
1822-
substs: self.tcx().mk_substs(substs),
1793+
substs: self.tcx().mk_substs(trait_substs),
18231794
}));
18241795

18251796
debug!("confirm_unboxed_closure_candidate(closure_def_id={}, trait_ref={})",

0 commit comments

Comments
 (0)