about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMazdak Farrokhzad <twingoow@gmail.com>2019-08-29 09:35:37 +0200
committerMazdak Farrokhzad <twingoow@gmail.com>2019-08-31 06:07:34 +0200
commit8af33b325377ea64fb9bbdc10073f9063293c9a6 (patch)
treecfa9481a83a4fade8dde10a71f6469bf8cc63864
parentc1d440070e088c6b239bcf5f9f22dfc6d5b2b639 (diff)
downloadrust-8af33b325377ea64fb9bbdc10073f9063293c9a6.tar.gz
rust-8af33b325377ea64fb9bbdc10073f9063293c9a6.zip
qualify_consts: extract check_non_thread_local_static_is_sync
-rw-r--r--src/librustc_mir/transform/qualify_consts.rs49
1 files changed, 25 insertions, 24 deletions
diff --git a/src/librustc_mir/transform/qualify_consts.rs b/src/librustc_mir/transform/qualify_consts.rs
index b052d1e9bb4..dbb994183e9 100644
--- a/src/librustc_mir/transform/qualify_consts.rs
+++ b/src/librustc_mir/transform/qualify_consts.rs
@@ -1687,31 +1687,9 @@ impl<'tcx> MirPass<'tcx> for QualifyAndPromoteConstants<'tcx> {
             }
         }
 
-        // Statics must be Sync.
         if let Mode::Static = mode {
-            // `#[thread_local]` statics don't have to be `Sync`.
-            for attr in &tcx.get_attrs(def_id)[..] {
-                if attr.check_name(sym::thread_local) {
-                    return;
-                }
-            }
-            let ty = body.return_ty();
-            tcx.infer_ctxt().enter(|infcx| {
-                let param_env = ty::ParamEnv::empty();
-                let cause = traits::ObligationCause::new(body.span, hir_id, traits::SharedStatic);
-                let mut fulfillment_cx = traits::FulfillmentContext::new();
-                fulfillment_cx.register_bound(&infcx,
-                                              param_env,
-                                              ty,
-                                              tcx.require_lang_item(
-                                                  lang_items::SyncTraitLangItem,
-                                                  Some(body.span)
-                                              ),
-                                              cause);
-                if let Err(err) = fulfillment_cx.select_all_or_error(&infcx) {
-                    infcx.report_fulfillment_errors(&err, None, false);
-                }
-            });
+            // `static`s (not `static mut`s) which are not `#[thread_local]` must be `Sync`.
+            check_non_thread_local_static_is_sync(tcx, body, def_id, hir_id);
         }
     }
 }
@@ -1760,6 +1738,29 @@ fn check_short_circuiting_in_const_local(tcx: TyCtxt<'_>, body: &mut Body<'tcx>,
     }
 }
 
+fn check_non_thread_local_static_is_sync(
+    tcx: TyCtxt<'tcx>,
+    body: &mut Body<'tcx>,
+    def_id: DefId,
+    hir_id: HirId,
+) {
+    // `#[thread_local]` statics don't have to be `Sync`.
+    if tcx.has_attr(def_id, sym::thread_local) {
+        return;
+    }
+
+    let ty = body.return_ty();
+    tcx.infer_ctxt().enter(|infcx| {
+        let cause = traits::ObligationCause::new(body.span, hir_id, traits::SharedStatic);
+        let mut fulfillment_cx = traits::FulfillmentContext::new();
+        let sync_def_id = tcx.require_lang_item(lang_items::SyncTraitLangItem, Some(body.span));
+        fulfillment_cx.register_bound(&infcx, ty::ParamEnv::empty(), ty, sync_def_id, cause);
+        if let Err(err) = fulfillment_cx.select_all_or_error(&infcx) {
+            infcx.report_fulfillment_errors(&err, None, false);
+        }
+    });
+}
+
 fn args_required_const(tcx: TyCtxt<'_>, def_id: DefId) -> Option<FxHashSet<usize>> {
     let attrs = tcx.get_attrs(def_id);
     let attr = attrs.iter().find(|a| a.check_name(sym::rustc_args_required_const))?;