Skip to content

Commit cf3683f

Browse files
committed
Address comments
1 parent 524d140 commit cf3683f

File tree

6 files changed

+37
-42
lines changed

6 files changed

+37
-42
lines changed

Diff for: compiler/rustc_middle/src/mir/tcx.rs

+1-11
Original file line numberDiff line numberDiff line change
@@ -164,17 +164,7 @@ impl<'tcx> Rvalue<'tcx> {
164164
Rvalue::Repeat(ref operand, count) => {
165165
tcx.mk_array_with_const_len(operand.ty(local_decls, tcx), count)
166166
}
167-
Rvalue::ThreadLocalRef(did) => {
168-
let static_ty = tcx.type_of(did).subst_identity();
169-
if tcx.is_mutable_static(did) {
170-
tcx.mk_mut_ptr(static_ty)
171-
} else if tcx.is_foreign_item(did) {
172-
tcx.mk_imm_ptr(static_ty)
173-
} else {
174-
// FIXME: These things don't *really* have 'static lifetime.
175-
tcx.mk_imm_ref(tcx.lifetimes.re_static, static_ty)
176-
}
177-
}
167+
Rvalue::ThreadLocalRef(did) => tcx.thread_local_ptr_ty(did),
178168
Rvalue::Ref(reg, bk, ref place) => {
179169
let place_ty = place.ty(local_decls, tcx).ty;
180170
tcx.mk_ref(reg, ty::TypeAndMut { ty: place_ty, mutbl: bk.to_mutbl_lossy() })

Diff for: compiler/rustc_middle/src/ty/instance.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -82,8 +82,9 @@ pub enum InstanceDef<'tcx> {
8282
/// The `DefId` is the ID of the `call_once` method in `FnOnce`.
8383
ClosureOnceShim { call_once: DefId, track_caller: bool },
8484

85-
/// Compiler-generated accessor for thread locals. This is used to export thread locals
86-
/// from dylibs on platforms lacking native support.
85+
/// Compiler-generated accessor for thread locals which returns a reference to the thread local
86+
/// the `DefId` defines. This is used to export thread locals from dylibs on platforms lacking
87+
/// native support.
8788
ThreadLocalShim(DefId),
8889

8990
/// `core::ptr::drop_in_place::<T>`.

Diff for: compiler/rustc_middle/src/ty/util.rs

+13
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,19 @@ impl<'tcx> TyCtxt<'tcx> {
599599
self.static_mutability(def_id) == Some(hir::Mutability::Mut)
600600
}
601601

602+
/// Returns the type a reference to the thread local takes in MIR.
603+
pub fn thread_local_ptr_ty(self, def_id: DefId) -> Ty<'tcx> {
604+
let static_ty = self.type_of(def_id).subst_identity();
605+
if self.is_mutable_static(def_id) {
606+
self.mk_mut_ptr(static_ty)
607+
} else if self.is_foreign_item(def_id) {
608+
self.mk_imm_ptr(static_ty)
609+
} else {
610+
// FIXME: These things don't *really* have 'static lifetime.
611+
self.mk_imm_ref(self.lifetimes.re_static, static_ty)
612+
}
613+
}
614+
602615
/// Get the type of the pointer to the static that we use in MIR.
603616
pub fn static_ptr_ty(self, def_id: DefId) -> Ty<'tcx> {
604617
// Make sure that any constants in the static's type are evaluated.

Diff for: compiler/rustc_mir_transform/src/shim.rs

+1-3
Original file line numberDiff line numberDiff line change
@@ -341,12 +341,10 @@ fn build_thread_local_shim<'tcx>(tcx: TyCtxt<'tcx>, instance: ty::InstanceDef<'t
341341
is_cleanup: false,
342342
});
343343

344-
let ret_ty = Rvalue::ThreadLocalRef(def_id).ty(&IndexVec::new(), tcx);
345-
346344
new_body(
347345
MirSource::from_instance(instance),
348346
blocks,
349-
iter::once(LocalDecl::new(ret_ty, span)).collect(),
347+
IndexVec::from_raw(vec![LocalDecl::new(tcx.thread_local_ptr_ty(def_id), span)]),
350348
0,
351349
span,
352350
)

Diff for: compiler/rustc_monomorphize/src/partitioning/default.rs

+17-20
Original file line numberDiff line numberDiff line change
@@ -392,6 +392,19 @@ fn mono_item_linkage_and_visibility<'tcx>(
392392

393393
type CguNameCache = FxHashMap<(DefId, bool), Symbol>;
394394

395+
fn static_visibility<'tcx>(
396+
tcx: TyCtxt<'tcx>,
397+
can_be_internalized: &mut bool,
398+
def_id: DefId,
399+
) -> Visibility {
400+
if tcx.is_reachable_non_generic(def_id) {
401+
*can_be_internalized = false;
402+
default_visibility(tcx, def_id, false)
403+
} else {
404+
Visibility::Hidden
405+
}
406+
}
407+
395408
fn mono_item_visibility<'tcx>(
396409
tcx: TyCtxt<'tcx>,
397410
mono_item: &MonoItem<'tcx>,
@@ -403,35 +416,19 @@ fn mono_item_visibility<'tcx>(
403416
MonoItem::Fn(instance) => instance,
404417

405418
// Misc handling for generics and such, but otherwise:
406-
MonoItem::Static(def_id) => {
407-
return if tcx.is_reachable_non_generic(*def_id) {
408-
*can_be_internalized = false;
409-
default_visibility(tcx, *def_id, false)
410-
} else {
411-
Visibility::Hidden
412-
};
413-
}
419+
MonoItem::Static(def_id) => return static_visibility(tcx, can_be_internalized, *def_id),
414420
MonoItem::GlobalAsm(item_id) => {
415-
return if tcx.is_reachable_non_generic(item_id.owner_id) {
416-
*can_be_internalized = false;
417-
default_visibility(tcx, item_id.owner_id.to_def_id(), false)
418-
} else {
419-
Visibility::Hidden
420-
};
421+
return static_visibility(tcx, can_be_internalized, item_id.owner_id.to_def_id());
421422
}
422423
};
423424

424425
let def_id = match instance.def {
425426
InstanceDef::Item(def) => def.did,
426427
InstanceDef::DropGlue(def_id, Some(_)) => def_id,
427428

429+
// We match the visiblity of statics here
428430
InstanceDef::ThreadLocalShim(def_id) => {
429-
return if tcx.is_reachable_non_generic(def_id) {
430-
*can_be_internalized = false;
431-
default_visibility(tcx, def_id, false)
432-
} else {
433-
Visibility::Hidden
434-
};
431+
return static_visibility(tcx, can_be_internalized, def_id);
435432
}
436433

437434
// These are all compiler glue and such, never exported, always hidden.

Diff for: compiler/rustc_ty_utils/src/abi.rs

+2-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
use rustc_hir as hir;
22
use rustc_hir::lang_items::LangItem;
3-
use rustc_index::vec::IndexVec;
4-
use rustc_middle::mir::Rvalue;
53
use rustc_middle::ty::layout::{
64
fn_can_unwind, FnAbiError, HasParamEnv, HasTyCtxt, LayoutCx, LayoutOf, TyAndLayout,
75
};
@@ -32,11 +30,9 @@ fn fn_sig_for_fn_abi<'tcx>(
3230
param_env: ty::ParamEnv<'tcx>,
3331
) -> ty::PolyFnSig<'tcx> {
3432
if let InstanceDef::ThreadLocalShim(..) = instance.def {
35-
let ret_ty = Rvalue::ThreadLocalRef(instance.def_id()).ty(&IndexVec::new(), tcx);
36-
3733
return ty::Binder::dummy(tcx.mk_fn_sig(
38-
[].iter(),
39-
&ret_ty,
34+
[],
35+
tcx.thread_local_ptr_ty(instance.def_id()),
4036
false,
4137
hir::Unsafety::Normal,
4238
rustc_target::spec::abi::Abi::Unadjusted,

0 commit comments

Comments
 (0)