Skip to content

Commit b2395a5

Browse files
committed
Add a convenience function for testing whether a static is #[thread_local]
1 parent 582d52f commit b2395a5

File tree

5 files changed

+12
-8
lines changed

5 files changed

+12
-8
lines changed

src/librustc_codegen_llvm/consts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,7 @@ impl CodegenCx<'ll, 'tcx> {
212212
let g = if let Some(def_id) = def_id.as_local() {
213213
let id = self.tcx.hir().as_local_hir_id(def_id);
214214
let llty = self.layout_of(ty).llvm_type(self);
215+
// FIXME: refactor this to work without accessing the HIR
215216
let (g, attrs) = match self.tcx.hir().get(id) {
216217
Node::Item(&hir::Item { attrs, span, kind: hir::ItemKind::Static(..), .. }) => {
217218
let sym_str = sym.as_str();

src/librustc_middle/ty/util.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Miscellaneous type-system utilities that are too small to deserve their own modules.
22
33
use crate::ich::NodeIdHashingMode;
4+
use crate::middle::codegen_fn_attrs::CodegenFnAttrFlags;
45
use crate::mir::interpret::{sign_extend, truncate};
56
use crate::ty::layout::IntegerExt;
67
use crate::ty::query::TyCtxtAt;
@@ -528,6 +529,11 @@ impl<'tcx> TyCtxt<'tcx> {
528529
self.static_mutability(def_id).is_some()
529530
}
530531

532+
/// Returns `true` if this is a `static` item with the `#[thread_local]` attribute.
533+
pub fn is_thread_local_static(&self, def_id: DefId) -> bool {
534+
self.codegen_fn_attrs(def_id).flags.contains(CodegenFnAttrFlags::THREAD_LOCAL)
535+
}
536+
531537
/// Returns `true` if the node pointed to by `def_id` is a mutable `static` item.
532538
pub fn is_mutable_static(&self, def_id: DefId) -> bool {
533539
self.static_mutability(def_id) == Some(hir::Mutability::Mut)

src/librustc_mir/transform/check_consts/validation.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_middle::mir::visit::{MutatingUseContext, NonMutatingUseContext, PlaceC
88
use rustc_middle::mir::*;
99
use rustc_middle::ty::cast::CastTy;
1010
use rustc_middle::ty::{self, Instance, InstanceDef, TyCtxt};
11-
use rustc_span::symbol::sym;
1211
use rustc_span::Span;
1312
use rustc_trait_selection::traits::error_reporting::InferCtxtExt;
1413
use rustc_trait_selection::traits::{self, TraitEngine};
@@ -224,7 +223,7 @@ impl Validator<'mir, 'tcx> {
224223

225224
// Ensure that the end result is `Sync` in a non-thread local `static`.
226225
let should_check_for_sync =
227-
const_kind == Some(ConstKind::Static) && !tcx.has_attr(def_id, sym::thread_local);
226+
const_kind == Some(ConstKind::Static) && !tcx.is_thread_local_static(def_id);
228227

229228
if should_check_for_sync {
230229
let hir_id = tcx.hir().as_local_hir_id(def_id.expect_local());
@@ -267,8 +266,7 @@ impl Validator<'mir, 'tcx> {
267266
}
268267

269268
fn check_static(&mut self, def_id: DefId, span: Span) {
270-
let is_thread_local = self.tcx.has_attr(def_id, sym::thread_local);
271-
if is_thread_local {
269+
if self.tcx.is_thread_local_static(def_id) {
272270
self.check_op_spanned(ops::ThreadLocalAccess, span)
273271
} else {
274272
self.check_op_spanned(ops::StaticAccess, span)

src/librustc_mir/transform/promote_consts.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ impl<'tcx> Validator<'_, 'tcx> {
522522
return Err(Unpromotable);
523523
}
524524

525-
let is_thread_local = self.tcx.has_attr(def_id, sym::thread_local);
525+
let is_thread_local = self.tcx.is_thread_local_static(def_id);
526526
if is_thread_local {
527527
return Err(Unpromotable);
528528
}

src/librustc_mir_build/build/expr/as_temp.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
use crate::build::scope::DropKind;
44
use crate::build::{BlockAnd, BlockAndExtension, Builder};
55
use crate::hair::*;
6+
use rustc_hir as hir;
67
use rustc_middle::middle::region;
78
use rustc_middle::mir::*;
8-
use rustc_hir as hir;
9-
use rustc_span::symbol::sym;
109

1110
impl<'a, 'tcx> Builder<'a, 'tcx> {
1211
/// Compile `expr` into a fresh temporary. This is used when building
@@ -60,7 +59,7 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
6059
local_decl = local_decl.block_tail(tail_info);
6160
}
6261
if let ExprKind::StaticRef { def_id, .. } = expr.kind {
63-
let is_thread_local = this.hir.tcx().has_attr(def_id, sym::thread_local);
62+
let is_thread_local = this.hir.tcx().is_thread_local_static(def_id);
6463
local_decl.internal = true;
6564
local_decl.local_info = LocalInfo::StaticRef { def_id, is_thread_local };
6665
}

0 commit comments

Comments
 (0)