38
38
//! expression, `e as U2` is not necessarily so (in fact it will only be valid if
39
39
//! `U1` coerces to `U2`).
40
40
41
- use super :: coercion;
42
- use super :: demand;
43
41
use super :: FnCtxt ;
44
- use super :: structurally_resolved_type;
45
42
46
43
use lint;
47
44
use hir:: def_id:: DefId ;
@@ -75,19 +72,18 @@ enum UnsizeKind<'tcx> {
75
72
OfParam ( & ' tcx ty:: ParamTy )
76
73
}
77
74
75
+ impl < ' a , ' tcx > FnCtxt < ' a , ' tcx > {
78
76
/// Returns the kind of unsize information of t, or None
79
77
/// if t is sized or it is unknown.
80
- fn unsize_kind < ' a , ' tcx > ( fcx : & FnCtxt < ' a , ' tcx > ,
81
- t : Ty < ' tcx > )
82
- -> Option < UnsizeKind < ' tcx > > {
78
+ fn unsize_kind ( & self , t : Ty < ' tcx > ) -> Option < UnsizeKind < ' tcx > > {
83
79
match t. sty {
84
80
ty:: TySlice ( _) | ty:: TyStr => Some ( UnsizeKind :: Length ) ,
85
81
ty:: TyTrait ( ref tty) => Some ( UnsizeKind :: Vtable ( tty. principal_def_id ( ) ) ) ,
86
82
ty:: TyStruct ( def, substs) => {
87
83
// FIXME(arielb1): do some kind of normalization
88
84
match def. struct_variant ( ) . fields . last ( ) {
89
85
None => None ,
90
- Some ( f) => unsize_kind ( fcx , f. ty ( fcx . tcx ( ) , substs) )
86
+ Some ( f) => self . unsize_kind ( f. ty ( self . tcx ( ) , substs) )
91
87
}
92
88
}
93
89
// We should really try to normalize here.
@@ -96,6 +92,7 @@ fn unsize_kind<'a,'tcx>(fcx: &FnCtxt<'a, 'tcx>,
96
92
_ => None
97
93
}
98
94
}
95
+ }
99
96
100
97
#[ derive( Copy , Clone ) ]
101
98
enum CastError {
@@ -112,14 +109,14 @@ enum CastError {
112
109
NonScalar ,
113
110
}
114
111
115
- impl < ' tcx > CastCheck < ' tcx > {
116
- pub fn new < ' a > ( fcx : & FnCtxt < ' a , ' tcx > ,
117
- expr : & ' tcx hir:: Expr ,
118
- expr_ty : Ty < ' tcx > ,
119
- cast_ty : Ty < ' tcx > ,
120
- cast_span : Span ,
121
- span : Span )
122
- -> Result < CastCheck < ' tcx > , ErrorReported > {
112
+ impl < ' a , ' tcx > CastCheck < ' tcx > {
113
+ pub fn new ( fcx : & FnCtxt < ' a , ' tcx > ,
114
+ expr : & ' tcx hir:: Expr ,
115
+ expr_ty : Ty < ' tcx > ,
116
+ cast_ty : Ty < ' tcx > ,
117
+ cast_span : Span ,
118
+ span : Span )
119
+ -> Result < CastCheck < ' tcx > , ErrorReported > {
123
120
let check = CastCheck {
124
121
expr : expr,
125
122
expr_ty : expr_ty,
@@ -142,9 +139,7 @@ impl<'tcx> CastCheck<'tcx> {
142
139
}
143
140
}
144
141
145
- fn report_cast_error < ' a > ( & self ,
146
- fcx : & FnCtxt < ' a , ' tcx > ,
147
- e : CastError ) {
142
+ fn report_cast_error ( & self , fcx : & FnCtxt < ' a , ' tcx > , e : CastError ) {
148
143
match e {
149
144
CastError :: NeedViaPtr |
150
145
CastError :: NeedViaThinPtr |
@@ -207,8 +202,7 @@ impl<'tcx> CastCheck<'tcx> {
207
202
}
208
203
}
209
204
210
- fn report_cast_to_unsized_type < ' a > ( & self ,
211
- fcx : & FnCtxt < ' a , ' tcx > ) {
205
+ fn report_cast_to_unsized_type ( & self , fcx : & FnCtxt < ' a , ' tcx > ) {
212
206
if
213
207
self . cast_ty . references_error ( ) ||
214
208
self . expr_ty . references_error ( )
@@ -262,7 +256,7 @@ impl<'tcx> CastCheck<'tcx> {
262
256
err. emit ( ) ;
263
257
}
264
258
265
- fn trivial_cast_lint < ' a > ( & self , fcx : & FnCtxt < ' a , ' tcx > ) {
259
+ fn trivial_cast_lint ( & self , fcx : & FnCtxt < ' a , ' tcx > ) {
266
260
let t_cast = self . cast_ty ;
267
261
let t_expr = self . expr_ty ;
268
262
if t_cast. is_numeric ( ) && t_expr. is_numeric ( ) {
@@ -287,9 +281,9 @@ impl<'tcx> CastCheck<'tcx> {
287
281
288
282
}
289
283
290
- pub fn check < ' a > ( mut self , fcx : & FnCtxt < ' a , ' tcx > ) {
291
- self . expr_ty = structurally_resolved_type ( fcx , self . span , self . expr_ty ) ;
292
- self . cast_ty = structurally_resolved_type ( fcx , self . span , self . cast_ty ) ;
284
+ pub fn check ( mut self , fcx : & FnCtxt < ' a , ' tcx > ) {
285
+ self . expr_ty = fcx . structurally_resolved_type ( self . span , self . expr_ty ) ;
286
+ self . cast_ty = fcx . structurally_resolved_type ( self . span , self . cast_ty ) ;
293
287
294
288
debug ! ( "check_cast({}, {:?} as {:?})" , self . expr. id, self . expr_ty,
295
289
self . cast_ty) ;
@@ -315,7 +309,7 @@ impl<'tcx> CastCheck<'tcx> {
315
309
/// Check a cast, and report an error if one exists. In some cases, this
316
310
/// can return Ok and create type errors in the fcx rather than returning
317
311
/// directly. coercion-cast is handled in check instead of here.
318
- fn do_check < ' a > ( & self , fcx : & FnCtxt < ' a , ' tcx > ) -> Result < CastKind , CastError > {
312
+ fn do_check ( & self , fcx : & FnCtxt < ' a , ' tcx > ) -> Result < CastKind , CastError > {
319
313
use rustc:: ty:: cast:: IntTy :: * ;
320
314
use rustc:: ty:: cast:: CastTy :: * ;
321
315
@@ -326,8 +320,8 @@ impl<'tcx> CastCheck<'tcx> {
326
320
( None , Some ( t_cast) ) => {
327
321
if let ty:: TyFnDef ( _, _, f) = self . expr_ty . sty {
328
322
// Attempt a coercion to a fn pointer type.
329
- let res = coercion :: try ( fcx, self . expr ,
330
- fcx. tcx ( ) . mk_ty ( ty:: TyFnPtr ( f) ) ) ;
323
+ let res = fcx. try_coerce ( self . expr ,
324
+ fcx. tcx ( ) . mk_ty ( ty:: TyFnPtr ( f) ) ) ;
331
325
if !res. is_ok ( ) {
332
326
return Err ( CastError :: NonScalar ) ;
333
327
}
@@ -382,11 +376,11 @@ impl<'tcx> CastCheck<'tcx> {
382
376
}
383
377
}
384
378
385
- fn check_ptr_ptr_cast < ' a > ( & self ,
386
- fcx : & FnCtxt < ' a , ' tcx > ,
387
- m_expr : & ' tcx ty:: TypeAndMut < ' tcx > ,
388
- m_cast : & ' tcx ty:: TypeAndMut < ' tcx > )
389
- -> Result < CastKind , CastError >
379
+ fn check_ptr_ptr_cast ( & self ,
380
+ fcx : & FnCtxt < ' a , ' tcx > ,
381
+ m_expr : & ' tcx ty:: TypeAndMut < ' tcx > ,
382
+ m_cast : & ' tcx ty:: TypeAndMut < ' tcx > )
383
+ -> Result < CastKind , CastError >
390
384
{
391
385
debug ! ( "check_ptr_ptr_cast m_expr={:?} m_cast={:?}" ,
392
386
m_expr, m_cast) ;
@@ -403,16 +397,16 @@ impl<'tcx> CastCheck<'tcx> {
403
397
}
404
398
405
399
// vtable kinds must match
406
- match ( unsize_kind ( fcx , m_cast. ty ) , unsize_kind ( fcx , m_expr. ty ) ) {
400
+ match ( fcx . unsize_kind ( m_cast. ty ) , fcx . unsize_kind ( m_expr. ty ) ) {
407
401
( Some ( a) , Some ( b) ) if a == b => Ok ( CastKind :: PtrPtrCast ) ,
408
402
_ => Err ( CastError :: DifferingKinds )
409
403
}
410
404
}
411
405
412
- fn check_fptr_ptr_cast < ' a > ( & self ,
413
- fcx : & FnCtxt < ' a , ' tcx > ,
414
- m_cast : & ' tcx ty:: TypeAndMut < ' tcx > )
415
- -> Result < CastKind , CastError >
406
+ fn check_fptr_ptr_cast ( & self ,
407
+ fcx : & FnCtxt < ' a , ' tcx > ,
408
+ m_cast : & ' tcx ty:: TypeAndMut < ' tcx > )
409
+ -> Result < CastKind , CastError >
416
410
{
417
411
// fptr-ptr cast. must be to sized ptr
418
412
@@ -423,10 +417,10 @@ impl<'tcx> CastCheck<'tcx> {
423
417
}
424
418
}
425
419
426
- fn check_ptr_addr_cast < ' a > ( & self ,
427
- fcx : & FnCtxt < ' a , ' tcx > ,
428
- m_expr : & ' tcx ty:: TypeAndMut < ' tcx > )
429
- -> Result < CastKind , CastError >
420
+ fn check_ptr_addr_cast ( & self ,
421
+ fcx : & FnCtxt < ' a , ' tcx > ,
422
+ m_expr : & ' tcx ty:: TypeAndMut < ' tcx > )
423
+ -> Result < CastKind , CastError >
430
424
{
431
425
// ptr-addr cast. must be from sized ptr
432
426
@@ -437,11 +431,11 @@ impl<'tcx> CastCheck<'tcx> {
437
431
}
438
432
}
439
433
440
- fn check_ref_cast < ' a > ( & self ,
441
- fcx : & FnCtxt < ' a , ' tcx > ,
442
- m_expr : & ' tcx ty:: TypeAndMut < ' tcx > ,
443
- m_cast : & ' tcx ty:: TypeAndMut < ' tcx > )
444
- -> Result < CastKind , CastError >
434
+ fn check_ref_cast ( & self ,
435
+ fcx : & FnCtxt < ' a , ' tcx > ,
436
+ m_expr : & ' tcx ty:: TypeAndMut < ' tcx > ,
437
+ m_cast : & ' tcx ty:: TypeAndMut < ' tcx > )
438
+ -> Result < CastKind , CastError >
445
439
{
446
440
// array-ptr-cast.
447
441
@@ -455,18 +449,18 @@ impl<'tcx> CastCheck<'tcx> {
455
449
// from a region pointer to a vector.
456
450
457
451
// this will report a type mismatch if needed
458
- demand :: eqtype ( fcx, self . span , ety, m_cast. ty ) ;
452
+ fcx. demand_eqtype ( self . span , ety, m_cast. ty ) ;
459
453
return Ok ( CastKind :: ArrayPtrCast ) ;
460
454
}
461
455
}
462
456
463
457
Err ( CastError :: IllegalCast )
464
458
}
465
459
466
- fn check_addr_ptr_cast < ' a > ( & self ,
467
- fcx : & FnCtxt < ' a , ' tcx > ,
468
- m_cast : & ' tcx ty:: TypeAndMut < ' tcx > )
469
- -> Result < CastKind , CastError >
460
+ fn check_addr_ptr_cast ( & self ,
461
+ fcx : & FnCtxt < ' a , ' tcx > ,
462
+ m_cast : & ' tcx ty:: TypeAndMut < ' tcx > )
463
+ -> Result < CastKind , CastError >
470
464
{
471
465
// ptr-addr cast. pointer must be thin.
472
466
if fcx. type_is_known_to_be_sized ( m_cast. ty , self . span ) {
@@ -476,8 +470,8 @@ impl<'tcx> CastCheck<'tcx> {
476
470
}
477
471
}
478
472
479
- fn try_coercion_cast < ' a > ( & self , fcx : & FnCtxt < ' a , ' tcx > ) -> bool {
480
- coercion :: try ( fcx, self . expr , self . cast_ty ) . is_ok ( )
473
+ fn try_coercion_cast ( & self , fcx : & FnCtxt < ' a , ' tcx > ) -> bool {
474
+ fcx. try_coerce ( self . expr , self . cast_ty ) . is_ok ( )
481
475
}
482
476
483
477
}
0 commit comments