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 ( ) => {
@@ -649,25 +650,23 @@ fn check_adjustments<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Exp
649
650
Some ( & ty:: adjustment:: AdjustUnsafeFnPointer ) |
650
651
Some ( & ty:: adjustment:: AdjustMutToConstPointer ) => { }
651
652
652
- Some ( & ty:: adjustment:: AdjustDerefRef (
653
- ty:: adjustment:: AutoDerefRef { autoderefs, .. }
654
- ) ) => {
655
- if ( 0 ..autoderefs as u32 ) . any ( |autoderef| {
656
- v. tcx . is_overloaded_autoderef ( e. id , autoderef)
657
- } ) {
653
+ Some ( & ty:: adjustment:: AdjustDerefRef ( ty:: adjustment:: AutoDerefRef { autoderefs, .. } ) ) => {
654
+ if ( 0 ..autoderefs as u32 )
655
+ . any ( |autoderef| v. tcx . is_overloaded_autoderef ( e. id , autoderef) ) {
658
656
v. add_qualif ( ConstQualif :: NOT_CONST ) ;
659
657
}
660
658
}
661
659
}
662
660
}
663
661
664
662
pub fn check_crate < ' a , ' tcx > ( tcx : TyCtxt < ' a , ' tcx , ' tcx > ) {
665
- tcx. visit_all_items_in_krate ( DepNode :: CheckConst , & mut CheckCrateVisitor {
666
- tcx : tcx,
667
- mode : Mode :: Var ,
668
- qualif : ConstQualif :: NOT_CONST ,
669
- rvalue_borrows : NodeMap ( )
670
- } ) ;
663
+ tcx. visit_all_items_in_krate ( DepNode :: CheckConst ,
664
+ & mut CheckCrateVisitor {
665
+ tcx : tcx,
666
+ mode : Mode :: Var ,
667
+ qualif : ConstQualif :: NOT_CONST ,
668
+ rvalue_borrows : NodeMap ( ) ,
669
+ } ) ;
671
670
tcx. sess . abort_if_errors ( ) ;
672
671
}
673
672
@@ -689,7 +688,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
689
688
690
689
Categorization :: Rvalue ( ..) |
691
690
Categorization :: Upvar ( ..) |
692
- Categorization :: Local ( ..) => break
691
+ Categorization :: Local ( ..) => break ,
693
692
}
694
693
}
695
694
}
@@ -699,8 +698,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
699
698
cmt : mc:: cmt < ' tcx > ,
700
699
_loan_region : ty:: Region ,
701
700
bk : ty:: BorrowKind ,
702
- loan_cause : euv:: LoanCause )
703
- {
701
+ loan_cause : euv:: LoanCause ) {
704
702
// Kind of hacky, but we allow Unsafe coercions in constants.
705
703
// These occur when we convert a &T or *T to a *U, as well as
706
704
// when making a thin pointer (e.g., `*T`) into a fat pointer
@@ -709,7 +707,7 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
709
707
euv:: LoanCause :: AutoUnsafe => {
710
708
return ;
711
709
}
712
- _ => { }
710
+ _ => { }
713
711
}
714
712
715
713
let mut cur = & cmt;
@@ -746,27 +744,20 @@ impl<'a, 'gcx, 'tcx> euv::Delegate<'tcx> for CheckCrateVisitor<'a, 'gcx> {
746
744
}
747
745
748
746
Categorization :: Upvar ( ..) |
749
- Categorization :: Local ( ..) => break
747
+ Categorization :: Local ( ..) => break ,
750
748
}
751
749
}
752
750
}
753
751
754
- fn decl_without_init ( & mut self ,
755
- _id : ast:: NodeId ,
756
- _span : Span ) { }
752
+ fn decl_without_init ( & mut self , _id : ast:: NodeId , _span : Span ) { }
757
753
fn mutate ( & mut self ,
758
754
_assignment_id : ast:: NodeId ,
759
755
_assignment_span : Span ,
760
756
_assignee_cmt : mc:: cmt ,
761
- _mode : euv:: MutateMode ) { }
757
+ _mode : euv:: MutateMode ) {
758
+ }
762
759
763
- fn matched_pat ( & mut self ,
764
- _: & hir:: Pat ,
765
- _: mc:: cmt ,
766
- _: euv:: MatchMode ) { }
760
+ fn matched_pat ( & mut self , _: & hir:: Pat , _: mc:: cmt , _: euv:: MatchMode ) { }
767
761
768
- fn consume_pat ( & mut self ,
769
- _consume_pat : & hir:: Pat ,
770
- _cmt : mc:: cmt ,
771
- _mode : euv:: ConsumeMode ) { }
762
+ fn consume_pat ( & mut self , _consume_pat : & hir:: Pat , _cmt : mc:: cmt , _mode : euv:: ConsumeMode ) { }
772
763
}
0 commit comments