25
25
// by borrowck::gather_loans
26
26
27
27
use rustc:: dep_graph:: DepNode ;
28
- use rustc:: ty:: cast:: { CastKind } ;
29
- use rustc_const_eval:: { ConstEvalErr , lookup_const_fn_by_id , compare_lit_exprs } ;
28
+ use rustc:: ty:: cast:: CastKind ;
29
+ use rustc_const_eval:: { ConstEvalErr , compare_lit_exprs , lookup_const_fn_by_id } ;
30
30
use rustc_const_eval:: { eval_const_expr_partial, lookup_const_by_id} ;
31
- use rustc_const_eval:: ErrKind :: { IndexOpFeatureGated , UnimplementedConstVal , MiscCatchAll , Math } ;
31
+ use rustc_const_eval:: ErrKind :: { IndexOpFeatureGated , Math , MiscCatchAll , UnimplementedConstVal } ;
32
32
use rustc_const_eval:: ErrKind :: { ErroneousReferencedConstant , MiscBinaryOp , NonConstPath } ;
33
33
use rustc_const_eval:: ErrKind :: UnresolvedPath ;
34
34
use rustc_const_eval:: EvalHint :: ExprTypeChecked ;
@@ -70,12 +70,12 @@ struct CheckCrateVisitor<'a, 'tcx: 'a> {
70
70
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
71
71
mode : Mode ,
72
72
qualif : ConstQualif ,
73
- rvalue_borrows : NodeMap < hir:: Mutability >
73
+ rvalue_borrows : NodeMap < hir:: Mutability > ,
74
74
}
75
75
76
76
impl < ' a , ' gcx > CheckCrateVisitor < ' a , ' gcx > {
77
- fn with_mode < F , R > ( & mut self , mode : Mode , f : F ) -> R where
78
- F : FnOnce ( & mut CheckCrateVisitor < ' a , ' gcx > ) -> R ,
77
+ fn with_mode < F , R > ( & mut self , mode : Mode , f : F ) -> R
78
+ where F : FnOnce ( & mut CheckCrateVisitor < ' a , ' gcx > ) -> R
79
79
{
80
80
let ( old_mode, old_qualif) = ( self . mode , self . qualif ) ;
81
81
self . mode = mode;
@@ -86,17 +86,17 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
86
86
r
87
87
}
88
88
89
- fn with_euv < F , R > ( & mut self , item_id : Option < ast:: NodeId > , f : F ) -> R where
90
- F : for <' b , ' tcx > FnOnce ( & mut euv:: ExprUseVisitor < ' b , ' gcx , ' tcx > ) -> R ,
89
+ fn with_euv < F , R > ( & mut self , item_id : Option < ast:: NodeId > , f : F ) -> R
90
+ where F : for <' b , ' tcx > FnOnce ( & mut euv:: ExprUseVisitor < ' b , ' gcx , ' tcx > ) -> R
91
91
{
92
92
let param_env = match item_id {
93
93
Some ( item_id) => ty:: ParameterEnvironment :: for_item ( self . tcx , item_id) ,
94
- None => self . tcx . empty_parameter_environment ( )
94
+ None => self . tcx . empty_parameter_environment ( ) ,
95
95
} ;
96
96
97
- self . tcx . infer_ctxt ( None , Some ( param_env ) , ProjectionMode :: AnyFinal ) . enter ( |infcx| {
98
- f ( & mut euv :: ExprUseVisitor :: new ( self , & infcx ) )
99
- } )
97
+ self . tcx
98
+ . infer_ctxt ( None , Some ( param_env ) , ProjectionMode :: AnyFinal )
99
+ . enter ( |infcx| f ( & mut euv :: ExprUseVisitor :: new ( self , & infcx ) ) )
100
100
}
101
101
102
102
fn global_expr ( & mut self , mode : Mode , expr : & hir:: Expr ) -> ConstQualif {
@@ -110,13 +110,17 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
110
110
}
111
111
if let Err ( err) = eval_const_expr_partial ( self . tcx , expr, ExprTypeChecked , None ) {
112
112
match err. kind {
113
- UnimplementedConstVal ( _) => { } ,
114
- IndexOpFeatureGated => { } ,
115
- ErroneousReferencedConstant ( _) => { } ,
116
- _ => self . tcx . sess . add_lint ( CONST_ERR , expr. id , expr. span ,
117
- format ! ( "constant evaluation error: {}. This will \
113
+ UnimplementedConstVal ( _) => { }
114
+ IndexOpFeatureGated => { }
115
+ ErroneousReferencedConstant ( _) => { }
116
+ _ => {
117
+ self . tcx . sess . add_lint ( CONST_ERR ,
118
+ expr. id ,
119
+ expr. span ,
120
+ format ! ( "constant evaluation error: {}. This will \
118
121
become a HARD ERROR in the future",
119
- err. description( ) ) ) ,
122
+ err. description( ) ) )
123
+ }
120
124
}
121
125
}
122
126
self . with_mode ( mode, |this| {
@@ -142,17 +146,15 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
142
146
}
143
147
144
148
let mode = match fk {
145
- FnKind :: ItemFn ( _, _, _, hir:: Constness :: Const , _, _, _) => {
146
- Mode :: ConstFn
147
- }
149
+ FnKind :: ItemFn ( _, _, _, hir:: Constness :: Const , _, _, _) => Mode :: ConstFn ,
148
150
FnKind :: Method ( _, m, _, _) => {
149
151
if m. constness == hir:: Constness :: Const {
150
152
Mode :: ConstFn
151
153
} else {
152
154
Mode :: Var
153
155
}
154
156
}
155
- _ => Mode :: Var
157
+ _ => Mode :: Var ,
156
158
} ;
157
159
158
160
let qualif = self . with_mode ( mode, |this| {
@@ -174,11 +176,7 @@ impl<'a, 'gcx> CheckCrateVisitor<'a, 'gcx> {
174
176
}
175
177
176
178
/// Returns true if the call is to a const fn or method.
177
- fn handle_const_fn_call ( & mut self ,
178
- _expr : & hir:: Expr ,
179
- def_id : DefId ,
180
- ret_ty : Ty < ' gcx > )
181
- -> bool {
179
+ fn handle_const_fn_call ( & mut self , _expr : & hir:: Expr , def_id : DefId , ret_ty : Ty < ' gcx > ) -> bool {
182
180
if let Some ( fn_like) = lookup_const_fn_by_id ( self . tcx , def_id) {
183
181
let qualif = self . fn_like ( fn_like. kind ( ) ,
184
182
fn_like. decl ( ) ,
@@ -293,17 +291,21 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
293
291
Some ( Ordering :: Less ) |
294
292
Some ( Ordering :: Equal ) => { }
295
293
Some ( Ordering :: Greater ) => {
296
- span_err ! ( self . tcx. sess, start. span, E0030 ,
297
- "lower range bound must be less than or equal to upper" ) ;
294
+ span_err ! ( self . tcx. sess,
295
+ start. span,
296
+ E0030 ,
297
+ "lower range bound must be less than or equal to upper" ) ;
298
298
}
299
299
None => {
300
- span_err ! ( self . tcx. sess, p. span, E0014 ,
300
+ span_err ! ( self . tcx. sess,
301
+ p. span,
302
+ E0014 ,
301
303
"paths in {}s may only refer to constants" ,
302
304
self . msg( ) ) ;
303
305
}
304
306
}
305
307
}
306
- _ => intravisit:: walk_pat ( self , p)
308
+ _ => intravisit:: walk_pat ( self , p) ,
307
309
}
308
310
}
309
311
@@ -313,13 +315,13 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
313
315
match stmt. node {
314
316
hir:: StmtDecl ( ref decl, _) => {
315
317
match decl. node {
316
- hir:: DeclLocal ( _) => { } ,
318
+ hir:: DeclLocal ( _) => { }
317
319
// Item statements are allowed
318
- hir:: DeclItem ( _) => continue
320
+ hir:: DeclItem ( _) => continue ,
319
321
}
320
322
}
321
- hir:: StmtExpr ( _, _) => { } ,
322
- hir:: StmtSemi ( _, _) => { } ,
323
+ hir:: StmtExpr ( _, _) => { }
324
+ hir:: StmtSemi ( _, _) => { }
323
325
}
324
326
self . add_qualif ( ConstQualif :: NOT_CONST ) ;
325
327
}
@@ -352,7 +354,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
352
354
// The count is checked elsewhere (typeck).
353
355
let count = match node_ty. sty {
354
356
ty:: TyArray ( _, n) => n,
355
- _ => bug ! ( )
357
+ _ => bug ! ( ) ,
356
358
} ;
357
359
// [element; 0] is always zero-sized.
358
360
if count == 0 {
@@ -377,7 +379,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
377
379
}
378
380
intravisit:: walk_expr ( self , ex) ;
379
381
}
380
- _ => intravisit:: walk_expr ( self , ex)
382
+ _ => intravisit:: walk_expr ( self , ex) ,
381
383
}
382
384
383
385
// Handle borrows on (or inside the autorefs of) this expression.
@@ -417,19 +419,19 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
417
419
if self . mode == Mode :: Var && !self . qualif . intersects ( ConstQualif :: NOT_CONST ) {
418
420
match eval_const_expr_partial ( self . tcx , ex, ExprTypeChecked , None ) {
419
421
Ok ( _) => { }
420
- Err ( ConstEvalErr { kind : UnimplementedConstVal ( _) , ..} ) |
421
- Err ( ConstEvalErr { kind : MiscCatchAll , ..} ) |
422
- Err ( ConstEvalErr { kind : MiscBinaryOp , ..} ) |
423
- Err ( ConstEvalErr { kind : NonConstPath , ..} ) |
424
- Err ( ConstEvalErr { kind : UnresolvedPath , ..} ) |
425
- Err ( ConstEvalErr { kind : ErroneousReferencedConstant ( _) , ..} ) |
426
- Err ( ConstEvalErr { kind : Math ( ConstMathErr :: Overflow ( Op :: Shr ) ) , ..} ) |
427
- Err ( ConstEvalErr { kind : Math ( ConstMathErr :: Overflow ( Op :: Shl ) ) , ..} ) |
428
- Err ( ConstEvalErr { kind : IndexOpFeatureGated , ..} ) => { } ,
422
+ Err ( ConstEvalErr { kind : UnimplementedConstVal ( _) , .. } ) |
423
+ Err ( ConstEvalErr { kind : MiscCatchAll , .. } ) |
424
+ Err ( ConstEvalErr { kind : MiscBinaryOp , .. } ) |
425
+ Err ( ConstEvalErr { kind : NonConstPath , .. } ) |
426
+ Err ( ConstEvalErr { kind : UnresolvedPath , .. } ) |
427
+ Err ( ConstEvalErr { kind : ErroneousReferencedConstant ( _) , .. } ) |
428
+ Err ( ConstEvalErr { kind : Math ( ConstMathErr :: Overflow ( Op :: Shr ) ) , .. } ) |
429
+ Err ( ConstEvalErr { kind : Math ( ConstMathErr :: Overflow ( Op :: Shl ) ) , .. } ) |
430
+ Err ( ConstEvalErr { kind : IndexOpFeatureGated , .. } ) => { }
429
431
Err ( msg) => {
430
- self . tcx . sess . add_lint ( CONST_ERR , ex . id ,
431
- msg . span ,
432
- msg. description ( ) . into_owned ( ) )
432
+ self . tcx
433
+ . sess
434
+ . add_lint ( CONST_ERR , ex . id , msg . span , msg. description ( ) . into_owned ( ) )
433
435
}
434
436
}
435
437
}
@@ -446,8 +448,7 @@ impl<'a, 'tcx, 'v> Visitor<'v> for CheckCrateVisitor<'a, 'tcx> {
446
448
/// every nested expression. If the expression is not part
447
449
/// of a const/static item, it is qualified for promotion
448
450
/// instead of producing errors.
449
- fn check_expr < ' a , ' tcx > ( v : & mut CheckCrateVisitor < ' a , ' tcx > ,
450
- e : & hir:: Expr , node_ty : Ty < ' tcx > ) {
451
+ fn check_expr < ' a , ' tcx > ( v : & mut CheckCrateVisitor < ' a , ' tcx > , e : & hir:: Expr , node_ty : Ty < ' tcx > ) {
451
452
match node_ty. sty {
452
453
ty:: TyStruct ( def, _) |
453
454
ty:: TyEnum ( def, _) if def. has_dtor ( ) => {
@@ -647,25 +648,23 @@ fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Exp
647
648
Some ( & ty:: adjustment:: AdjustUnsafeFnPointer ) |
648
649
Some ( & ty:: adjustment:: AdjustMutToConstPointer ) => { }
649
650
650
- Some ( & ty:: adjustment:: AdjustDerefRef (
651
- ty:: adjustment:: AutoDerefRef { autoderefs, .. }
652
- ) ) => {
653
- if ( 0 ..autoderefs as u32 ) . any ( |autoderef| {
654
- v. tcx . is_overloaded_autoderef ( e. id , autoderef)
655
- } ) {
651
+ Some ( & ty:: adjustment:: AdjustDerefRef ( ty:: adjustment:: AutoDerefRef { autoderefs, .. } ) ) => {
652
+ if ( 0 ..autoderefs as u32 )
653
+ . any ( |autoderef| v. tcx . is_overloaded_autoderef ( e. id , autoderef) ) {
656
654
v. add_qualif ( ConstQualif :: NOT_CONST ) ;
657
655
}
658
656
}
659
657
}
660
658
}
661
659
662
660
pub fn check_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) {
663
- tcx. visit_all_items_in_krate ( DepNode :: CheckConst , & mut CheckCrateVisitor {
664
- tcx : tcx,
665
- mode : Mode :: Var ,
666
- qualif : ConstQualif :: NOT_CONST ,
667
- rvalue_borrows : NodeMap ( )
668
- } ) ;
661
+ tcx. visit_all_items_in_krate ( DepNode :: CheckConst ,
662
+ & mut CheckCrateVisitor {
663
+ tcx : tcx,
664
+ mode : Mode :: Var ,
665
+ qualif : ConstQualif :: NOT_CONST ,
666
+ rvalue_borrows : NodeMap ( ) ,
667
+ } ) ;
669
668
tcx. sess . abort_if_errors ( ) ;
670
669
}
671
670
@@ -687,7 +686,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
687
686
688
687
Categorization :: Rvalue ( ..) |
689
688
Categorization :: Upvar ( ..) |
690
- Categorization :: Local ( ..) => break
689
+ Categorization :: Local ( ..) => break ,
691
690
}
692
691
}
693
692
}
@@ -697,8 +696,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
697
696
cmt : mc:: cmt < ' tcx > ,
698
697
_loan_region : ty:: Region ,
699
698
bk : ty:: BorrowKind ,
700
- loan_cause : euv:: LoanCause )
701
- {
699
+ loan_cause : euv:: LoanCause ) {
702
700
// Kind of hacky, but we allow Unsafe coercions in constants.
703
701
// These occur when we convert a &T or *T to a *U, as well as
704
702
// when making a thin pointer (e.g., `*T`) into a fat pointer
@@ -707,7 +705,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
707
705
euv:: LoanCause :: AutoUnsafe => {
708
706
return ;
709
707
}
710
- _ => { }
708
+ _ => { }
711
709
}
712
710
713
711
let mut cur = & cmt;
@@ -744,27 +742,20 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
744
742
}
745
743
746
744
Categorization :: Upvar ( ..) |
747
- Categorization :: Local ( ..) => break
745
+ Categorization :: Local ( ..) => break ,
748
746
}
749
747
}
750
748
}
751
749
752
- fn decl_without_init ( & mut self ,
753
- _id : ast:: NodeId ,
754
- _span : Span ) { }
750
+ fn decl_without_init ( & mut self , _id : ast:: NodeId , _span : Span ) { }
755
751
fn mutate ( & mut self ,
756
752
_assignment_id : ast:: NodeId ,
757
753
_assignment_span : Span ,
758
754
_assignee_cmt : mc:: cmt ,
759
- _mode : euv:: MutateMode ) { }
755
+ _mode : euv:: MutateMode ) {
756
+ }
760
757
761
- fn matched_pat ( & mut self ,
762
- _: & hir:: Pat ,
763
- _: mc:: cmt ,
764
- _: euv:: MatchMode ) { }
758
+ fn matched_pat ( & mut self , _: & hir:: Pat , _: mc:: cmt , _: euv:: MatchMode ) { }
765
759
766
- fn consume_pat ( & mut self ,
767
- _consume_pat : & hir:: Pat ,
768
- _cmt : mc:: cmt ,
769
- _mode : euv:: ConsumeMode ) { }
760
+ fn consume_pat ( & mut self , _consume_pat : & hir:: Pat , _cmt : mc:: cmt , _mode : euv:: ConsumeMode ) { }
770
761
}
0 commit comments