25
25
// by borrowck::gather_loans
26
26
27
27
use rustc:: dep_graph:: DepNode ;
28
- use rustc:: ty:: cast:: { CastKind } ;
28
+ use rustc:: ty:: cast:: CastKind ;
29
29
use rustc_const_eval:: { ConstEvalErr , lookup_const_fn_by_id, compare_lit_exprs} ;
30
30
use rustc_const_eval:: { eval_const_expr_partial, lookup_const_by_id} ;
31
31
use rustc_const_eval:: ErrKind :: { IndexOpFeatureGated , UnimplementedConstVal , MiscCatchAll , Math } ;
@@ -71,12 +71,12 @@ struct CheckCrateVisitor<'a, 'tcx: 'a> {
71
71
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
72
72
mode : Mode ,
73
73
qualif : ConstQualif ,
74
- rvalue_borrows : NodeMap < hir:: Mutability >
74
+ rvalue_borrows : NodeMap < hir:: Mutability > ,
75
75
}
76
76
77
77
impl < ' a , ' gcx > CheckCrateVisitor < ' a , ' gcx > {
78
- fn with_mode < F , R > ( & mut self , mode : Mode , f : F ) -> R where
79
- F : FnOnce ( & mut CheckCrateVisitor < ' a , ' gcx > ) -> R ,
78
+ fn with_mode < F , R > ( & mut self , mode : Mode , f : F ) -> R
79
+ where F : FnOnce ( & mut CheckCrateVisitor < ' a , ' gcx > ) -> R
80
80
{
81
81
let ( old_mode, old_qualif) = ( self . mode , self . qualif ) ;
82
82
self . mode = mode;
@@ -87,17 +87,17 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
87
87
r
88
88
}
89
89
90
- fn with_euv < F , R > ( & mut self , item_id : Option < ast:: NodeId > , f : F ) -> R where
91
- F : for <' b , ' tcx > FnOnce ( & mut euv:: ExprUseVisitor < ' b , ' gcx , ' tcx > ) -> R ,
90
+ fn with_euv < F , R > ( & mut self , item_id : Option < ast:: NodeId > , f : F ) -> R
91
+ where F : for <' b , ' tcx > FnOnce ( & mut euv:: ExprUseVisitor < ' b , ' gcx , ' tcx > ) -> R
92
92
{
93
93
let param_env = match item_id {
94
94
Some ( item_id) => ty:: ParameterEnvironment :: for_item ( self . tcx , item_id) ,
95
- None => self . tcx . empty_parameter_environment ( )
95
+ None => self . tcx . empty_parameter_environment ( ) ,
96
96
} ;
97
97
98
- self . tcx . infer_ctxt ( None , Some ( param_env ) , ProjectionMode :: AnyFinal ) . enter ( |infcx| {
99
- f ( & mut euv :: ExprUseVisitor :: new ( self , & infcx ) )
100
- } )
98
+ self . tcx
99
+ . infer_ctxt ( None , Some ( param_env ) , ProjectionMode :: AnyFinal )
100
+ . enter ( |infcx| f ( & mut euv :: ExprUseVisitor :: new ( self , & infcx ) ) )
101
101
}
102
102
103
103
fn global_expr ( & mut self , mode : Mode , expr : & hir:: Expr ) -> ConstQualif {
@@ -111,13 +111,17 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
111
111
}
112
112
if let Err ( err) = eval_const_expr_partial ( self . tcx , expr, ExprTypeChecked , None ) {
113
113
match err. kind {
114
- UnimplementedConstVal ( _) => { } ,
115
- IndexOpFeatureGated => { } ,
116
- ErroneousReferencedConstant ( _) => { } ,
117
- _ => self . tcx . sess . add_lint ( CONST_ERR , expr. id , expr. span ,
118
- format ! ( "constant evaluation error: {}. This will \
119
- become a HARD ERROR in the future",
120
- err. description( ) . into_oneline( ) ) ) ,
114
+ UnimplementedConstVal ( _) => { }
115
+ IndexOpFeatureGated => { }
116
+ ErroneousReferencedConstant ( _) => { }
117
+ _ => {
118
+ self . tcx . sess . add_lint ( CONST_ERR ,
119
+ expr. id ,
120
+ expr. span ,
121
+ format ! ( "constant evaluation error: {}. This will \
122
+ become a HARD ERROR in the future",
123
+ err. description( ) . into_oneline( ) ) )
124
+ }
121
125
}
122
126
}
123
127
self . with_mode ( mode, |this| {
@@ -143,17 +147,15 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
143
147
}
144
148
145
149
let mode = match fk {
146
- FnKind :: ItemFn ( _, _, _, hir:: Constness :: Const , _, _, _) => {
147
- Mode :: ConstFn
148
- }
150
+ FnKind :: ItemFn ( _, _, _, hir:: Constness :: Const , _, _, _) => Mode :: ConstFn ,
149
151
FnKind :: Method ( _, m, _, _) => {
150
152
if m. constness == hir:: Constness :: Const {
151
153
Mode :: ConstFn
152
154
} else {
153
155
Mode :: Var
154
156
}
155
157
}
156
- _ => Mode :: Var
158
+ _ => Mode :: Var ,
157
159
} ;
158
160
159
161
let qualif = self . with_mode ( mode, |this| {
@@ -175,11 +177,7 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
175
177
}
176
178
177
179
/// Returns true if the call is to a const fn or method.
178
- fn handle_const_fn_call ( & mut self ,
179
- _expr : & hir:: Expr ,
180
- def_id : DefId ,
181
- ret_ty : Ty < ' gcx > )
182
- -> bool {
180
+ fn handle_const_fn_call ( & mut self , _expr : & hir:: Expr , def_id : DefId , ret_ty : Ty < ' gcx > ) -> bool {
183
181
if let Some ( fn_like) = lookup_const_fn_by_id ( self . tcx , def_id) {
184
182
let qualif = self . fn_like ( fn_like. kind ( ) ,
185
183
fn_like. decl ( ) ,
@@ -285,13 +283,15 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
285
283
Ok ( Ordering :: Less ) |
286
284
Ok ( Ordering :: Equal ) => { }
287
285
Ok ( Ordering :: Greater ) => {
288
- span_err ! ( self . tcx. sess, start. span, E0030 ,
289
- "lower range bound must be less than or equal to upper" ) ;
286
+ span_err ! ( self . tcx. sess,
287
+ start. span,
288
+ E0030 ,
289
+ "lower range bound must be less than or equal to upper" ) ;
290
290
}
291
291
Err ( ErrorReported ) => { }
292
292
}
293
293
}
294
- _ => intravisit:: walk_pat ( self , p)
294
+ _ => intravisit:: walk_pat ( self , p) ,
295
295
}
296
296
}
297
297
@@ -301,13 +301,13 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
301
301
match stmt. node {
302
302
hir:: StmtDecl ( ref decl, _) => {
303
303
match decl. node {
304
- hir:: DeclLocal ( _) => { } ,
304
+ hir:: DeclLocal ( _) => { }
305
305
// Item statements are allowed
306
- hir:: DeclItem ( _) => continue
306
+ hir:: DeclItem ( _) => continue ,
307
307
}
308
308
}
309
- hir:: StmtExpr ( _, _) => { } ,
310
- hir:: StmtSemi ( _, _) => { } ,
309
+ hir:: StmtExpr ( _, _) => { }
310
+ hir:: StmtSemi ( _, _) => { }
311
311
}
312
312
self . add_qualif ( ConstQualif :: NOT_CONST ) ;
313
313
}
@@ -340,7 +340,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
340
340
// The count is checked elsewhere (typeck).
341
341
let count = match node_ty. sty {
342
342
ty:: TyArray ( _, n) => n,
343
- _ => bug ! ( )
343
+ _ => bug ! ( ) ,
344
344
} ;
345
345
// [element; 0] is always zero-sized.
346
346
if count == 0 {
@@ -354,7 +354,8 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
354
354
for pat in arms. iter ( ) . flat_map ( |arm| & arm. pats ) {
355
355
let pat_borrow = self . rvalue_borrows . remove ( & pat. id ) ;
356
356
match ( borrow, pat_borrow) {
357
- ( None , _) | ( _, Some ( hir:: MutMutable ) ) => {
357
+ ( None , _) |
358
+ ( _, Some ( hir:: MutMutable ) ) => {
358
359
borrow = pat_borrow;
359
360
}
360
361
_ => { }
@@ -365,7 +366,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
365
366
}
366
367
intravisit:: walk_expr ( self , ex) ;
367
368
}
368
- _ => intravisit:: walk_expr ( self , ex)
369
+ _ => intravisit:: walk_expr ( self , ex) ,
369
370
}
370
371
371
372
// Handle borrows on (or inside the autorefs of) this expression.
@@ -405,17 +406,18 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
405
406
if self . mode == Mode :: Var && !self . qualif . intersects ( ConstQualif :: NOT_CONST ) {
406
407
match eval_const_expr_partial ( self . tcx , ex, ExprTypeChecked , None ) {
407
408
Ok ( _) => { }
408
- Err ( ConstEvalErr { kind : UnimplementedConstVal ( _) , ..} ) |
409
- Err ( ConstEvalErr { kind : MiscCatchAll , ..} ) |
410
- Err ( ConstEvalErr { kind : MiscBinaryOp , ..} ) |
411
- Err ( ConstEvalErr { kind : NonConstPath , ..} ) |
412
- Err ( ConstEvalErr { kind : UnresolvedPath , ..} ) |
413
- Err ( ConstEvalErr { kind : ErroneousReferencedConstant ( _) , ..} ) |
414
- Err ( ConstEvalErr { kind : Math ( ConstMathErr :: Overflow ( Op :: Shr ) ) , ..} ) |
415
- Err ( ConstEvalErr { kind : Math ( ConstMathErr :: Overflow ( Op :: Shl ) ) , ..} ) |
416
- Err ( ConstEvalErr { kind : IndexOpFeatureGated , ..} ) => { } ,
409
+ Err ( ConstEvalErr { kind : UnimplementedConstVal ( _) , .. } ) |
410
+ Err ( ConstEvalErr { kind : MiscCatchAll , .. } ) |
411
+ Err ( ConstEvalErr { kind : MiscBinaryOp , .. } ) |
412
+ Err ( ConstEvalErr { kind : NonConstPath , .. } ) |
413
+ Err ( ConstEvalErr { kind : UnresolvedPath , .. } ) |
414
+ Err ( ConstEvalErr { kind : ErroneousReferencedConstant ( _) , .. } ) |
415
+ Err ( ConstEvalErr { kind : Math ( ConstMathErr :: Overflow ( Op :: Shr ) ) , .. } ) |
416
+ Err ( ConstEvalErr { kind : Math ( ConstMathErr :: Overflow ( Op :: Shl ) ) , .. } ) |
417
+ Err ( ConstEvalErr { kind : IndexOpFeatureGated , .. } ) => { }
417
418
Err ( msg) => {
418
- self . tcx . sess . add_lint ( CONST_ERR , ex. id ,
419
+ self . tcx . sess . add_lint ( CONST_ERR ,
420
+ ex. id ,
419
421
msg. span ,
420
422
msg. description ( ) . into_oneline ( ) . into_owned ( ) )
421
423
}
@@ -434,8 +436,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
434
436
/// every nested expression. If the expression is not part
435
437
/// of a const/static item, it is qualified for promotion
436
438
/// instead of producing errors.
437
- fn check_expr < ' a , ' tcx > ( v : & mut CheckCrateVisitor < ' a , ' tcx > ,
438
- e : & hir:: Expr , node_ty : Ty < ' tcx > ) {
439
+ fn check_expr < ' a , ' tcx > ( v : & mut CheckCrateVisitor < ' a , ' tcx > , e : & hir:: Expr , node_ty : Ty < ' tcx > ) {
439
440
match node_ty. sty {
440
441
ty:: TyStruct ( def, _) |
441
442
ty:: TyEnum ( def, _) if def. has_dtor ( ) => {
@@ -635,25 +636,23 @@ fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Exp
635
636
Some ( & ty:: adjustment:: AdjustUnsafeFnPointer ) |
636
637
Some ( & ty:: adjustment:: AdjustMutToConstPointer ) => { }
637
638
638
- Some ( & ty:: adjustment:: AdjustDerefRef (
639
- ty:: adjustment:: AutoDerefRef { autoderefs, .. }
640
- ) ) => {
641
- if ( 0 ..autoderefs as u32 ) . any ( |autoderef| {
642
- v. tcx . is_overloaded_autoderef ( e. id , autoderef)
643
- } ) {
639
+ Some ( & ty:: adjustment:: AdjustDerefRef ( ty:: adjustment:: AutoDerefRef { autoderefs, .. } ) ) => {
640
+ if ( 0 ..autoderefs as u32 )
641
+ . any ( |autoderef| v. tcx . is_overloaded_autoderef ( e. id , autoderef) ) {
644
642
v. add_qualif ( ConstQualif :: NOT_CONST ) ;
645
643
}
646
644
}
647
645
}
648
646
}
649
647
650
648
pub fn check_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) {
651
- tcx. visit_all_items_in_krate ( DepNode :: CheckConst , & mut CheckCrateVisitor {
652
- tcx : tcx,
653
- mode : Mode :: Var ,
654
- qualif : ConstQualif :: NOT_CONST ,
655
- rvalue_borrows : NodeMap ( )
656
- } ) ;
649
+ tcx. visit_all_items_in_krate ( DepNode :: CheckConst ,
650
+ & mut CheckCrateVisitor {
651
+ tcx : tcx,
652
+ mode : Mode :: Var ,
653
+ qualif : ConstQualif :: NOT_CONST ,
654
+ rvalue_borrows : NodeMap ( ) ,
655
+ } ) ;
657
656
tcx. sess . abort_if_errors ( ) ;
658
657
}
659
658
@@ -675,7 +674,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
675
674
676
675
Categorization :: Rvalue ( ..) |
677
676
Categorization :: Upvar ( ..) |
678
- Categorization :: Local ( ..) => break
677
+ Categorization :: Local ( ..) => break ,
679
678
}
680
679
}
681
680
}
@@ -685,8 +684,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
685
684
cmt : mc:: cmt < ' tcx > ,
686
685
_loan_region : ty:: Region ,
687
686
bk : ty:: BorrowKind ,
688
- loan_cause : euv:: LoanCause )
689
- {
687
+ loan_cause : euv:: LoanCause ) {
690
688
// Kind of hacky, but we allow Unsafe coercions in constants.
691
689
// These occur when we convert a &T or *T to a *U, as well as
692
690
// when making a thin pointer (e.g., `*T`) into a fat pointer
@@ -695,7 +693,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
695
693
euv:: LoanCause :: AutoUnsafe => {
696
694
return ;
697
695
}
698
- _ => { }
696
+ _ => { }
699
697
}
700
698
701
699
let mut cur = & cmt;
@@ -715,7 +713,8 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
715
713
// type of the expression. `&mut [1]` has exactly the
716
714
// same representation as &mut 1.
717
715
match cmt. ty . sty {
718
- ty:: TyArray ( _, _) | ty:: TySlice ( _) => break ,
716
+ ty:: TyArray ( _, _) |
717
+ ty:: TySlice ( _) => break ,
719
718
_ => { }
720
719
}
721
720
}
@@ -732,27 +731,20 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
732
731
}
733
732
734
733
Categorization :: Upvar ( ..) |
735
- Categorization :: Local ( ..) => break
734
+ Categorization :: Local ( ..) => break ,
736
735
}
737
736
}
738
737
}
739
738
740
- fn decl_without_init ( & mut self ,
741
- _id : ast:: NodeId ,
742
- _span : Span ) { }
739
+ fn decl_without_init ( & mut self , _id : ast:: NodeId , _span : Span ) { }
743
740
fn mutate ( & mut self ,
744
741
_assignment_id : ast:: NodeId ,
745
742
_assignment_span : Span ,
746
743
_assignee_cmt : mc:: cmt ,
747
- _mode : euv:: MutateMode ) { }
744
+ _mode : euv:: MutateMode ) {
745
+ }
748
746
749
- fn matched_pat ( & mut self ,
750
- _: & hir:: Pat ,
751
- _: mc:: cmt ,
752
- _: euv:: MatchMode ) { }
747
+ fn matched_pat ( & mut self , _: & hir:: Pat , _: mc:: cmt , _: euv:: MatchMode ) { }
753
748
754
- fn consume_pat ( & mut self ,
755
- _consume_pat : & hir:: Pat ,
756
- _cmt : mc:: cmt ,
757
- _mode : euv:: ConsumeMode ) { }
749
+ fn consume_pat ( & mut self , _consume_pat : & hir:: Pat , _cmt : mc:: cmt , _mode : euv:: ConsumeMode ) { }
758
750
}
0 commit comments