Skip to content

Commit 8c8cb99

Browse files
committed
Introduce a tcx() helper method to cleanup this mess.
1 parent 34d6800 commit 8c8cb99

File tree

1 file changed

+29
-27
lines changed

1 file changed

+29
-27
lines changed

src/librustc/middle/infer/coercion.rs

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,14 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
8484
let Coerce(ref v) = *self; v
8585
}
8686

87+
fn tcx(&self) -> &ty::ctxt<'tcx> {
88+
self.get_ref().infcx.tcx
89+
}
90+
8791
pub fn tys(&self, a: Ty<'tcx>, b: Ty<'tcx>) -> CoerceResult<'tcx> {
8892
debug!("Coerce.tys({} => {})",
89-
a.repr(self.get_ref().infcx.tcx),
90-
b.repr(self.get_ref().infcx.tcx));
93+
a.repr(self.tcx()),
94+
b.repr(self.tcx()));
9195

9296
// Consider coercing the subtype to a DST
9397
let unsize = self.unpack_actual_value(a, |a| {
@@ -170,13 +174,11 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
170174

171175
self.unpack_actual_value(a, |a| {
172176
match a.sty {
173-
ty::ty_bare_fn(ref a_f) => {
174-
// Bare functions are coercible to any closure type.
175-
//
176-
// FIXME(#3320) this should go away and be
177-
// replaced with proper inference, got a patch
178-
// underway - ndm
179-
self.coerce_from_bare_fn(a, a_f, b)
177+
ty::ty_bare_fn(Some(a_def_id), ref a_f) => {
178+
// Function items are coercible to any closure
179+
// type; function pointers are not (that would
180+
// require double indirection).
181+
self.coerce_from_fn_item(a, a_def_id, a_f, b)
180182
}
181183
_ => {
182184
// Otherwise, just use subtyping rules.
@@ -206,8 +208,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
206208
mutbl_b: ast::Mutability)
207209
-> CoerceResult<'tcx> {
208210
debug!("coerce_borrowed_pointer(a={}, b={})",
209-
a.repr(self.get_ref().infcx.tcx),
210-
b.repr(self.get_ref().infcx.tcx));
211+
a.repr(self.tcx()),
212+
b.repr(self.tcx()));
211213

212214
// If we have a parameter of type `&M T_a` and the value
213215
// provided is `expr`, we will be adding an implicit borrow,
@@ -227,7 +229,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
227229
}
228230
};
229231

230-
let a_borrowed = ty::mk_rptr(self.get_ref().infcx.tcx,
232+
let a_borrowed = ty::mk_rptr(self.tcx(),
231233
r_borrow,
232234
mt {ty: inner_ty, mutbl: mutbl_b});
233235
try!(sub.tys(a_borrowed, b));
@@ -247,8 +249,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
247249
b: Ty<'tcx>)
248250
-> CoerceResult<'tcx> {
249251
debug!("coerce_unsized(a={}, b={})",
250-
a.repr(self.get_ref().infcx.tcx),
251-
b.repr(self.get_ref().infcx.tcx));
252+
a.repr(self.tcx()),
253+
b.repr(self.tcx()));
252254

253255
// Note, we want to avoid unnecessary unsizing. We don't want to coerce to
254256
// a DST unless we have to. This currently comes out in the wash since
@@ -268,7 +270,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
268270

269271
let coercion = Coercion(self.get_ref().trace.clone());
270272
let r_borrow = self.get_ref().infcx.next_region_var(coercion);
271-
let ty = ty::mk_rptr(self.get_ref().infcx.tcx,
273+
let ty = ty::mk_rptr(self.tcx(),
272274
r_borrow,
273275
ty::mt{ty: ty, mutbl: mt_b.mutbl});
274276
try!(self.get_ref().infcx.try(|_| sub.tys(ty, b)));
@@ -292,7 +294,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
292294
return Err(ty::terr_mutability);
293295
}
294296

295-
let ty = ty::mk_ptr(self.get_ref().infcx.tcx,
297+
let ty = ty::mk_ptr(self.tcx(),
296298
ty::mt{ty: ty, mutbl: mt_b.mutbl});
297299
try!(self.get_ref().infcx.try(|_| sub.tys(ty, b)));
298300
debug!("Success, coerced with AutoDerefRef(1, \
@@ -311,7 +313,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
311313
self.unpack_actual_value(t_a, |a| {
312314
match self.unsize_ty(t_a, a, t_b) {
313315
Some((ty, kind)) => {
314-
let ty = ty::mk_uniq(self.get_ref().infcx.tcx, ty);
316+
let ty = ty::mk_uniq(self.tcx(), ty);
315317
try!(self.get_ref().infcx.try(|_| sub.tys(ty, b)));
316318
debug!("Success, coerced with AutoDerefRef(1, \
317319
AutoUnsizeUniq({}))", kind);
@@ -336,9 +338,9 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
336338
a: Ty<'tcx>,
337339
ty_b: Ty<'tcx>)
338340
-> Option<(Ty<'tcx>, ty::UnsizeKind<'tcx>)> {
339-
debug!("unsize_ty(a={}, ty_b={})", a, ty_b.repr(self.get_ref().infcx.tcx));
341+
debug!("unsize_ty(a={}, ty_b={})", a, ty_b.repr(self.tcx()));
340342

341-
let tcx = self.get_ref().infcx.tcx;
343+
let tcx = self.tcx();
342344

343345
self.unpack_actual_value(ty_b, |b|
344346
match (&a.sty, &b.sty) {
@@ -412,7 +414,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
412414
b: Ty<'tcx>,
413415
b_mutbl: ast::Mutability) -> CoerceResult<'tcx>
414416
{
415-
let tcx = self.get_ref().infcx.tcx;
417+
let tcx = self.tcx();
416418

417419
debug!("coerce_borrowed_object(a={}, b={}, b_mutbl={})",
418420
a.repr(tcx),
@@ -431,7 +433,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
431433
b: Ty<'tcx>,
432434
b_mutbl: ast::Mutability) -> CoerceResult<'tcx>
433435
{
434-
let tcx = self.get_ref().infcx.tcx;
436+
let tcx = self.tcx();
435437

436438
debug!("coerce_unsafe_object(a={}, b={}, b_mutbl={})",
437439
a.repr(tcx),
@@ -451,7 +453,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
451453
F: FnOnce(Ty<'tcx>) -> Ty<'tcx>,
452454
G: FnOnce() -> ty::AutoRef<'tcx>,
453455
{
454-
let tcx = self.get_ref().infcx.tcx;
456+
let tcx = self.tcx();
455457

456458
match a.sty {
457459
ty::ty_rptr(_, ty::mt{ty, mutbl}) => match ty.sty {
@@ -480,8 +482,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
480482
b: Ty<'tcx>)
481483
-> CoerceResult<'tcx> {
482484
debug!("coerce_borrowed_fn(a={}, b={})",
483-
a.repr(self.get_ref().infcx.tcx),
484-
b.repr(self.get_ref().infcx.tcx));
485+
a.repr(self.tcx()),
486+
b.repr(self.tcx()));
485487

486488
match a.sty {
487489
ty::ty_bare_fn(ref f) => {
@@ -528,8 +530,8 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
528530
mutbl_b: ast::Mutability)
529531
-> CoerceResult<'tcx> {
530532
debug!("coerce_unsafe_ptr(a={}, b={})",
531-
a.repr(self.get_ref().infcx.tcx),
532-
b.repr(self.get_ref().infcx.tcx));
533+
a.repr(self.tcx()),
534+
b.repr(self.tcx()));
533535

534536
let mt_a = match a.sty {
535537
ty::ty_rptr(_, mt) | ty::ty_ptr(mt) => mt,
@@ -539,7 +541,7 @@ impl<'f, 'tcx> Coerce<'f, 'tcx> {
539541
};
540542

541543
// Check that the types which they point at are compatible.
542-
let a_unsafe = ty::mk_ptr(self.get_ref().infcx.tcx, ty::mt{ mutbl: mutbl_b, ty: mt_a.ty });
544+
let a_unsafe = ty::mk_ptr(self.tcx(), ty::mt{ mutbl: mutbl_b, ty: mt_a.ty });
543545
try!(self.subtype(a_unsafe, b));
544546
if !can_coerce_mutbls(mt_a.mutbl, mutbl_b) {
545547
return Err(ty::terr_mutability);

0 commit comments

Comments
 (0)