Skip to content

Commit 521a6e6

Browse files
author
Jorge Aparicio
committed
librustc_typeck: use unboxed closures
1 parent 888f249 commit 521a6e6

File tree

5 files changed

+72
-61
lines changed

5 files changed

+72
-61
lines changed

src/librustc_typeck/check/demand.rs

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,14 @@ pub fn suptype<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
2828
|sp, e, a, s| { fcx.report_mismatched_types(sp, e, a, s) })
2929
}
3030

31-
pub fn suptype_with_fn<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
32-
sp: Span,
33-
b_is_expected: bool,
34-
ty_a: Ty<'tcx>,
35-
ty_b: Ty<'tcx>,
36-
handle_err: |Span, Ty<'tcx>, Ty<'tcx>, &ty::type_err<'tcx>|) {
31+
pub fn suptype_with_fn<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
32+
sp: Span,
33+
b_is_expected: bool,
34+
ty_a: Ty<'tcx>,
35+
ty_b: Ty<'tcx>,
36+
handle_err: F) where
37+
F: FnOnce(Span, Ty<'tcx>, Ty<'tcx>, &ty::type_err<'tcx>),
38+
{
3739
// n.b.: order of actual, expected is reversed
3840
match infer::mk_subty(fcx.infcx(), b_is_expected, infer::Misc(sp),
3941
ty_b, ty_a) {

src/librustc_typeck/check/method/confirm.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -286,11 +286,8 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
286286
}
287287
}
288288

289-
fn extract_trait_ref<R>(&mut self,
290-
self_ty: Ty<'tcx>,
291-
closure: |&mut ConfirmContext<'a,'tcx>,
292-
Ty<'tcx>, &ty::TyTrait<'tcx>| -> R)
293-
-> R
289+
fn extract_trait_ref<R, F>(&mut self, self_ty: Ty<'tcx>, mut closure: F) -> R where
290+
F: FnMut(&mut ConfirmContext<'a, 'tcx>, Ty<'tcx>, &ty::TyTrait<'tcx>) -> R,
294291
{
295292
// If we specified that this is an object method, then the
296293
// self-type ought to be something that can be dereferenced to
@@ -665,9 +662,11 @@ impl<'a,'tcx> ConfirmContext<'a,'tcx> {
665662
}
666663
}
667664

668-
fn wrap_autoref<'tcx>(mut deref: ty::AutoDerefRef<'tcx>,
669-
base_fn: |Option<Box<ty::AutoRef<'tcx>>>| -> ty::AutoRef<'tcx>)
670-
-> ty::AutoDerefRef<'tcx> {
665+
fn wrap_autoref<'tcx, F>(mut deref: ty::AutoDerefRef<'tcx>,
666+
base_fn: F)
667+
-> ty::AutoDerefRef<'tcx> where
668+
F: FnOnce(Option<Box<ty::AutoRef<'tcx>>>) -> ty::AutoRef<'tcx>,
669+
{
671670
let autoref = mem::replace(&mut deref.autoref, None);
672671
let autoref = autoref.map(|r| box r);
673672
deref.autoref = Some(base_fn(autoref));

src/librustc_typeck/check/method/probe.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -710,10 +710,12 @@ impl<'a,'tcx> ProbeContext<'a,'tcx> {
710710
mutbl: m }))
711711
}
712712

713-
fn search_mutabilities(&mut self,
714-
mk_adjustment: |ast::Mutability| -> PickAdjustment,
715-
mk_autoref_ty: |ast::Mutability, ty::Region| -> Ty<'tcx>)
716-
-> Option<PickResult<'tcx>>
713+
fn search_mutabilities<F, G>(&mut self,
714+
mut mk_adjustment: F,
715+
mut mk_autoref_ty: G)
716+
-> Option<PickResult<'tcx>> where
717+
F: FnMut(ast::Mutability) -> PickAdjustment,
718+
G: FnMut(ast::Mutability, ty::Region) -> Ty<'tcx>,
717719
{
718720
// In general, during probing we erase regions. See
719721
// `impl_self_ty()` for an explanation.

src/librustc_typeck/check/mod.rs

Lines changed: 41 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1885,9 +1885,11 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
18851885
self.inh.item_substs.borrow()
18861886
}
18871887

1888-
pub fn opt_node_ty_substs(&self,
1889-
id: ast::NodeId,
1890-
f: |&ty::ItemSubsts<'tcx>|) {
1888+
pub fn opt_node_ty_substs<F>(&self,
1889+
id: ast::NodeId,
1890+
f: F) where
1891+
F: FnOnce(&ty::ItemSubsts<'tcx>),
1892+
{
18911893
match self.inh.item_substs.borrow().get(&id) {
18921894
Some(s) => { f(s) }
18931895
None => { }
@@ -2027,12 +2029,14 @@ impl Copy for LvaluePreference {}
20272029
///
20282030
/// Note: this method does not modify the adjustments table. The caller is responsible for
20292031
/// inserting an AutoAdjustment record into the `fcx` using one of the suitable methods.
2030-
pub fn autoderef<'a, 'tcx, T>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
2031-
base_ty: Ty<'tcx>,
2032-
expr_id: Option<ast::NodeId>,
2033-
mut lvalue_pref: LvaluePreference,
2034-
should_stop: |Ty<'tcx>, uint| -> Option<T>)
2035-
-> (Ty<'tcx>, uint, Option<T>) {
2032+
pub fn autoderef<'a, 'tcx, T, F>(fcx: &FnCtxt<'a, 'tcx>, sp: Span,
2033+
base_ty: Ty<'tcx>,
2034+
expr_id: Option<ast::NodeId>,
2035+
mut lvalue_pref: LvaluePreference,
2036+
mut should_stop: F)
2037+
-> (Ty<'tcx>, uint, Option<T>) where
2038+
F: FnMut(Ty<'tcx>, uint) -> Option<T>,
2039+
{
20362040
let mut t = base_ty;
20372041
for autoderefs in range(0, fcx.tcx().sess.recursion_limit.get()) {
20382042
let resolved_t = structurally_resolved_type(fcx, sp, t);
@@ -2194,12 +2198,13 @@ fn make_overloaded_lvalue_return_type<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
21942198
}
21952199
}
21962200

2197-
fn autoderef_for_index<'a, 'tcx, T>(fcx: &FnCtxt<'a, 'tcx>,
2198-
base_expr: &ast::Expr,
2199-
base_ty: Ty<'tcx>,
2200-
lvalue_pref: LvaluePreference,
2201-
step: |Ty<'tcx>, ty::AutoDerefRef<'tcx>| -> Option<T>)
2202-
-> Option<T>
2201+
fn autoderef_for_index<'a, 'tcx, T, F>(fcx: &FnCtxt<'a, 'tcx>,
2202+
base_expr: &ast::Expr,
2203+
base_ty: Ty<'tcx>,
2204+
lvalue_pref: LvaluePreference,
2205+
mut step: F)
2206+
-> Option<T> where
2207+
F: FnMut(Ty<'tcx>, ty::AutoDerefRef<'tcx>) -> Option<T>,
22032208
{
22042209
// FIXME(#18741) -- this is almost but not quite the same as the
22052210
// autoderef that normal method probing does. They could likely be
@@ -2938,11 +2943,12 @@ enum TupleArgumentsFlag {
29382943
/// Note that inspecting a type's structure *directly* may expose the fact
29392944
/// that there are actually multiple representations for `ty_err`, so avoid
29402945
/// that when err needs to be handled differently.
2941-
fn check_expr_with_unifier<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
2942-
expr: &ast::Expr,
2943-
expected: Expectation<'tcx>,
2944-
lvalue_pref: LvaluePreference,
2945-
unifier: ||)
2946+
fn check_expr_with_unifier<'a, 'tcx, F>(fcx: &FnCtxt<'a, 'tcx>,
2947+
expr: &ast::Expr,
2948+
expected: Expectation<'tcx>,
2949+
lvalue_pref: LvaluePreference,
2950+
unifier: F) where
2951+
F: FnOnce(),
29462952
{
29472953
debug!(">> typechecking: expr={} expected={}",
29482954
expr.repr(fcx.tcx()), expected.repr(fcx.tcx()));
@@ -3117,14 +3123,16 @@ fn check_expr_with_unifier<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
31173123
fcx.write_ty(id, if_ty);
31183124
}
31193125

3120-
fn lookup_op_method<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>,
3121-
op_ex: &ast::Expr,
3122-
lhs_ty: Ty<'tcx>,
3123-
opname: ast::Name,
3124-
trait_did: Option<ast::DefId>,
3125-
lhs: &'a ast::Expr,
3126-
rhs: Option<&P<ast::Expr>>,
3127-
unbound_method: ||) -> Ty<'tcx> {
3126+
fn lookup_op_method<'a, 'tcx, F>(fcx: &'a FnCtxt<'a, 'tcx>,
3127+
op_ex: &ast::Expr,
3128+
lhs_ty: Ty<'tcx>,
3129+
opname: ast::Name,
3130+
trait_did: Option<ast::DefId>,
3131+
lhs: &'a ast::Expr,
3132+
rhs: Option<&P<ast::Expr>>,
3133+
unbound_method: F) -> Ty<'tcx> where
3134+
F: FnOnce(),
3135+
{
31283136
let method = match trait_did {
31293137
Some(trait_did) => {
31303138
// We do eager coercions to make using operators
@@ -4376,19 +4384,17 @@ impl<'tcx> Expectation<'tcx> {
43764384
}
43774385
}
43784386

4379-
fn map<'a>(self, fcx: &FnCtxt<'a, 'tcx>,
4380-
unpack: |&ty::sty<'tcx>| -> Expectation<'tcx>)
4381-
-> Expectation<'tcx> {
4387+
fn map<'a, F>(self, fcx: &FnCtxt<'a, 'tcx>, unpack: F) -> Expectation<'tcx> where
4388+
F: FnOnce(&ty::sty<'tcx>) -> Expectation<'tcx>
4389+
{
43824390
match self.resolve(fcx) {
43834391
NoExpectation => NoExpectation,
43844392
ExpectCastableToType(t) | ExpectHasType(t) => unpack(&t.sty),
43854393
}
43864394
}
43874395

4388-
fn map_to_option<'a, O>(self,
4389-
fcx: &FnCtxt<'a, 'tcx>,
4390-
unpack: |&ty::sty<'tcx>| -> Option<O>)
4391-
-> Option<O>
4396+
fn map_to_option<'a, O, F>(self, fcx: &FnCtxt<'a, 'tcx>, unpack: F) -> Option<O> where
4397+
F: FnOnce(&ty::sty<'tcx>) -> Option<O>,
43924398
{
43934399
match self.resolve(fcx) {
43944400
NoExpectation => None,

src/librustc_typeck/lib.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,16 @@ fn no_params<'tcx>(t: Ty<'tcx>) -> ty::Polytype<'tcx> {
170170
}
171171
}
172172

173-
fn require_same_types<'a, 'tcx>(tcx: &ty::ctxt<'tcx>,
174-
maybe_infcx: Option<&infer::InferCtxt<'a, 'tcx>>,
175-
t1_is_expected: bool,
176-
span: Span,
177-
t1: Ty<'tcx>,
178-
t2: Ty<'tcx>,
179-
msg: || -> String)
180-
-> bool {
173+
fn require_same_types<'a, 'tcx, M>(tcx: &ty::ctxt<'tcx>,
174+
maybe_infcx: Option<&infer::InferCtxt<'a, 'tcx>>,
175+
t1_is_expected: bool,
176+
span: Span,
177+
t1: Ty<'tcx>,
178+
t2: Ty<'tcx>,
179+
msg: M)
180+
-> bool where
181+
M: FnOnce() -> String,
182+
{
181183
let result = match maybe_infcx {
182184
None => {
183185
let infcx = infer::new_infer_ctxt(tcx);

0 commit comments

Comments
 (0)