Skip to content

[DO NOT MERGE] Checking for #[thread_local] crash #50210

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/librustc/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
#![feature(slice_sort_by_cached_key)]
#![feature(specialization)]
#![feature(unboxed_closures)]
#![feature(thread_local)]
#![feature(trace_macros)]
#![feature(trusted_len)]
#![feature(catch_expr)]
Expand Down
11 changes: 7 additions & 4 deletions src/librustc/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1762,17 +1762,20 @@ pub mod tls {
}

// A thread local value which stores a pointer to the current ImplicitCtxt
thread_local!(static TLV: Cell<usize> = Cell::new(0));
#[thread_local]
static TLV: Cell<usize> = Cell::new(0);

#[inline(always)]
fn set_tlv<F: FnOnce() -> R, R>(value: usize, f: F) -> R {
let old = get_tlv();
let _reset = OnDrop(move || TLV.with(|tlv| tlv.set(old)));
TLV.with(|tlv| tlv.set(value));
let _reset = OnDrop(move || TLV.set(old));
TLV.set(value);
f()
}

#[inline(always)]
fn get_tlv() -> usize {
TLV.with(|tlv| tlv.get())
TLV.get()
}

/// This is a callback from libsyntax as it cannot access the implicit state
Expand Down
28 changes: 23 additions & 5 deletions src/librustc/ty/maps/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,24 +9,42 @@
// except according to those terms.

use dep_graph::SerializedDepNodeIndex;
use dep_graph::DepNode;
use hir::def_id::{CrateNum, DefId, DefIndex};
use mir::interpret::{GlobalId};
use traits::query::{CanonicalProjectionGoal, CanonicalTyGoal};
use ty::{self, ParamEnvAnd, Ty, TyCtxt};
use ty::subst::Substs;
use ty::maps::queries;
use ty::maps::Query;
use ty::maps::QueryMap;

use std::hash::Hash;
use std::fmt::Debug;
use syntax_pos::symbol::InternedString;
use rustc_data_structures::sync::Lock;
use rustc_data_structures::stable_hasher::HashStable;
use ich::StableHashingContext;

/// Query configuration and description traits.

pub trait QueryConfig {
type Key: Eq + Hash + Clone;
type Value;
pub trait QueryConfig<'tcx> {
const NAME: &'static str;

type Key: Eq + Hash + Clone + Debug;
type Value: Clone + for<'a> HashStable<StableHashingContext<'a>>;

fn query(key: Self::Key) -> Query<'tcx>;
fn query_map<'a>(tcx: TyCtxt<'a, 'tcx, '_>) -> &'a Lock<QueryMap<'tcx, Self>>;

fn to_dep_node(tcx: TyCtxt<'_, 'tcx, '_>, key: &Self::Key) -> DepNode;

fn compute(tcx: TyCtxt<'_, 'tcx, '_>, key: Self::Key) -> Self::Value;

fn handle_cycle_error(tcx: TyCtxt<'_, 'tcx, '_>) -> Self::Value;
}

pub(super) trait QueryDescription<'tcx>: QueryConfig {
pub trait QueryDescription<'tcx>: QueryConfig<'tcx> {
fn describe(tcx: TyCtxt, key: Self::Key) -> String;

#[inline]
Expand All @@ -41,7 +59,7 @@ pub(super) trait QueryDescription<'tcx>: QueryConfig {
}
}

impl<'tcx, M: QueryConfig<Key=DefId>> QueryDescription<'tcx> for M {
impl<'tcx, M: QueryConfig<'tcx, Key=DefId>> QueryDescription<'tcx> for M {
default fn describe(tcx: TyCtxt, def_id: DefId) -> String {
if !tcx.sess.verbose() {
format!("processing `{}`", tcx.item_path_str(def_id))
Expand Down
4 changes: 1 addition & 3 deletions src/librustc/ty/maps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
// except according to those terms.

use dep_graph::{DepConstructor, DepNode};
use errors::DiagnosticBuilder;
use hir::def_id::{CrateNum, DefId, DefIndex};
use hir::def::{Def, Export};
use hir::{self, TraitCandidate, ItemLocalId, TransFnAttrs};
Expand Down Expand Up @@ -43,7 +42,7 @@ use ty::{self, CrateInherentImpls, ParamEnvAnd, Slice, Ty, TyCtxt};
use ty::steal::Steal;
use ty::subst::Substs;
use util::nodemap::{DefIdSet, DefIdMap, ItemLocalSet};
use util::common::{profq_msg, ErrorReported, ProfileQueriesMsg};
use util::common::{ErrorReported};

use rustc_data_structures::indexed_set::IdxSetBuf;
use rustc_back::PanicStrategy;
Expand All @@ -68,7 +67,6 @@ pub use self::plumbing::force_from_dep_node;

mod job;
pub use self::job::{QueryJob, QueryInfo};
use self::job::QueryResult;

mod keys;
pub use self::keys::Key;
Expand Down
8 changes: 4 additions & 4 deletions src/librustc/ty/maps/on_disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -239,8 +239,8 @@ impl<'sess> OnDiskCache<'sess> {
encode_query_results::<specialization_graph_of, _>(tcx, enc, qri)?;

// const eval is special, it only encodes successfully evaluated constants
use ty::maps::plumbing::GetCacheInternal;
for (key, entry) in const_eval::get_cache_internal(tcx).map.iter() {
use ty::maps::QueryConfig;
for (key, entry) in const_eval::query_map(tcx).borrow().map.iter() {
use ty::maps::config::QueryDescription;
if const_eval::cache_on_disk(key.clone()) {
let entry = match *entry {
Expand Down Expand Up @@ -1124,7 +1124,7 @@ fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
encoder: &mut CacheEncoder<'enc, 'a, 'tcx, E>,
query_result_index: &mut EncodedQueryResultIndex)
-> Result<(), E::Error>
where Q: super::plumbing::GetCacheInternal<'tcx>,
where Q: super::config::QueryDescription<'tcx>,
E: 'enc + TyEncoder,
Q::Value: Encodable,
{
Expand All @@ -1133,7 +1133,7 @@ fn encode_query_results<'enc, 'a, 'tcx, Q, E>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

time(tcx.sess, desc, || {

for (key, entry) in Q::get_cache_internal(tcx).map.iter() {
for (key, entry) in Q::query_map(tcx).borrow().map.iter() {
if Q::cache_on_disk(key.clone()) {
let entry = match *entry {
QueryResult::Complete(ref v) => v,
Expand Down
Loading