@@ -56,6 +56,7 @@ use std::cmp::Ordering;
56
56
struct CheckCrateVisitor < ' a , ' tcx : ' a > {
57
57
tcx : TyCtxt < ' a , ' tcx , ' tcx > ,
58
58
in_fn : bool ,
59
+ in_static : bool ,
59
60
promotable : bool ,
60
61
mut_rvalue_borrows : NodeSet ,
61
62
param_env : ty:: ParamEnv < ' tcx > ,
@@ -128,10 +129,16 @@ impl<'a, 'tcx> Visitor<'tcx> for CheckCrateVisitor<'a, 'tcx> {
128
129
let outer_param_env = self . param_env ;
129
130
let outer_identity_substs = self . identity_substs ;
130
131
131
- self . in_fn = match MirSource :: from_node ( self . tcx , item_id) {
132
- MirSource :: Fn ( _) => true ,
133
- _ => false
132
+ self . in_fn = false ;
133
+ self . in_static = false ;
134
+
135
+ match MirSource :: from_node ( self . tcx , item_id) {
136
+ MirSource :: Fn ( _) => self . in_fn = true ,
137
+ MirSource :: Static ( _, _) => self . in_static = true ,
138
+ _ => { }
134
139
} ;
140
+
141
+
135
142
self . tables = self . tcx . typeck_tables_of ( item_def_id) ;
136
143
self . param_env = self . tcx . param_env ( item_def_id) ;
137
144
self . identity_substs = Substs :: identity_for_item ( self . tcx , item_def_id) ;
@@ -333,20 +340,28 @@ fn check_expr<'a, 'tcx>(v: &mut CheckCrateVisitor<'a, 'tcx>, e: &hir::Expr, node
333
340
// with the exception of "#[thread_loca]" statics.
334
341
// The latter may not outlive the current function
335
342
Def :: Static ( did, _) => {
336
- let mut thread_local = false ;
337
-
338
- for attr in & v. tcx . get_attrs ( did) [ ..] {
339
- if attr. check_name ( "thread_local" ) {
340
- debug ! ( "Static(id={:?}) is unpromotable \
341
- due to a #[thread_local] attribute", did) ;
342
- v. promotable = false ;
343
- thread_local = true ;
344
- break ;
343
+
344
+ if v. in_static {
345
+ let mut thread_local = false ;
346
+
347
+ for attr in & v. tcx . get_attrs ( did) [ ..] {
348
+ if attr. check_name ( "thread_local" ) {
349
+ debug ! ( "Reference to Static(id={:?}) is unpromotable \
350
+ due to a #[thread_local] attribute", did) ;
351
+ v. promotable = false ;
352
+ thread_local = true ;
353
+ break ;
354
+ }
345
355
}
346
- }
347
356
348
- if !thread_local {
349
- debug ! ( "Allowing promotion of reference to Static(id={:?})" , did) ;
357
+ if !thread_local {
358
+ debug ! ( "Allowing promotion of reference to Static(id={:?})" , did) ;
359
+ }
360
+ } else {
361
+ debug ! ( "Reference to Static(id={:?}) is unpromotable as it is not \
362
+ referenced from a static", did) ;
363
+ v. promotable = false ;
364
+
350
365
}
351
366
}
352
367
@@ -502,6 +517,7 @@ pub fn check_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
502
517
tcx,
503
518
tables : & ty:: TypeckTables :: empty ( None ) ,
504
519
in_fn : false ,
520
+ in_static : false ,
505
521
promotable : false ,
506
522
mut_rvalue_borrows : NodeSet ( ) ,
507
523
param_env : ty:: ParamEnv :: empty ( Reveal :: UserFacing ) ,
0 commit comments