Skip to content

Commit 94433a6

Browse files
authored
Rollup merge of #71465 - oli-obk:is_thread_local_cleanup, r=matthewjasper
Add a convenience method on `TyCtxt` for checking for thread locals This PR extracts the cleanup part of #71192 r? @bjorn3
2 parents 4adebb9 + 8079dd8 commit 94433a6

File tree

7 files changed

+17
-13
lines changed

7 files changed

+17
-13
lines changed

src/librustc_codegen_llvm/consts.rs

+1
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/mir/interpret/error.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -438,10 +438,7 @@ impl fmt::Debug for UndefinedBehaviorInfo {
438438
/// Error information for when the program did something that might (or might not) be correct
439439
/// to do according to the Rust spec, but due to limitations in the interpreter, the
440440
/// operation could not be carried out. These limitations can differ between CTFE and the
441-
/// Miri engine, e.g., CTFE does not support casting pointers to "real" integers.
442-
///
443-
/// Currently, we also use this as fall-back error kind for errors that have not been
444-
/// categorized yet.
441+
/// Miri engine, e.g., CTFE does not support dereferencing pointers at integral addresses.
445442
pub enum UnsupportedOpInfo {
446443
/// Free-form case. Only for errors that are never caught!
447444
Unsupported(String),
@@ -451,6 +448,9 @@ pub enum UnsupportedOpInfo {
451448
NoMirFor(DefId),
452449
/// Encountered a pointer where we needed raw bytes.
453450
ReadPointerAsBytes,
451+
//
452+
// The variants below are only reachable from CTFE/const prop, miri will never emit them.
453+
//
454454
/// Encountered raw bytes where we needed a pointer.
455455
ReadBytesAsPointer,
456456
}

src/librustc_middle/ty/util.rs

+6
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

+2-4
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

+1-1
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

+2-3
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
}

src/test/ui/linkage-attr/linkage3.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
// FIXME https://github.com/rust-lang/rust/issues/59774
22

3-
// build-fail
3+
// check-fail
44
// normalize-stderr-test "thread.*panicked.*Metadata module not compiled.*\n" -> ""
55
// normalize-stderr-test "note:.*RUST_BACKTRACE=1.*\n" -> ""
66

0 commit comments

Comments
 (0)