Skip to content

Commit e3e4420

Browse files
committed
Make layout_depth thread-safe
1 parent 7360d6d commit e3e4420

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

src/librustc/ty/context.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ use rustc_data_structures::indexed_vec::IndexVec;
6262
use rustc_data_structures::sync::{Lrc, Lock};
6363
use std::any::Any;
6464
use std::borrow::Borrow;
65-
use std::cell::Cell;
6665
use std::cmp::Ordering;
6766
use std::collections::hash_map::{self, Entry};
6867
use std::hash::{Hash, Hasher};
@@ -915,9 +914,6 @@ pub struct GlobalCtxt<'tcx> {
915914
/// Data layout specification for the current target.
916915
pub data_layout: TargetDataLayout,
917916

918-
/// Used to prevent layout from recursing too deeply.
919-
pub layout_depth: Cell<usize>,
920-
921917
stability_interner: Lock<FxHashSet<&'tcx attr::Stability>>,
922918

923919
pub interpret_interner: InterpretInterner<'tcx>,
@@ -1292,7 +1288,6 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12921288
crate_name: Symbol::intern(crate_name),
12931289
data_layout,
12941290
layout_interner: Lock::new(FxHashSet()),
1295-
layout_depth: Cell::new(0),
12961291
stability_interner: Lock::new(FxHashSet()),
12971292
interpret_interner: Default::default(),
12981293
tx_to_llvm_workers: Lock::new(tx),
@@ -1574,6 +1569,7 @@ impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
15741569
let new_icx = ty::tls::ImplicitCtxt {
15751570
tcx,
15761571
query: icx.query.clone(),
1572+
layout_depth: icx.layout_depth,
15771573
};
15781574
ty::tls::enter_context(&new_icx, |new_icx| {
15791575
f(new_icx.tcx)
@@ -1768,6 +1764,9 @@ pub mod tls {
17681764
/// The current query job, if any. This is updated by start_job in
17691765
/// ty::maps::plumbing when executing a query
17701766
pub query: Option<Lrc<maps::QueryJob<'gcx>>>,
1767+
1768+
/// Used to prevent layout from recursing too deeply.
1769+
pub layout_depth: usize,
17711770
}
17721771

17731772
// A thread local value which stores a pointer to the current ImplicitCtxt
@@ -1853,6 +1852,7 @@ pub mod tls {
18531852
let icx = ImplicitCtxt {
18541853
tcx,
18551854
query: None,
1855+
layout_depth: 0,
18561856
};
18571857
enter_context(&icx, |_| {
18581858
f(tcx)

src/librustc/ty/layout.rs

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -896,21 +896,26 @@ fn layout_raw<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
896896
query: ty::ParamEnvAnd<'tcx, Ty<'tcx>>)
897897
-> Result<&'tcx LayoutDetails, LayoutError<'tcx>>
898898
{
899-
let (param_env, ty) = query.into_parts();
899+
ty::tls::with_related_context(tcx, move |icx| {
900+
let rec_limit = *tcx.sess.recursion_limit.get();
901+
let (param_env, ty) = query.into_parts();
900902

901-
let rec_limit = *tcx.sess.recursion_limit.get();
902-
let depth = tcx.layout_depth.get();
903-
if depth > rec_limit {
904-
tcx.sess.fatal(
905-
&format!("overflow representing the type `{}`", ty));
906-
}
903+
if icx.layout_depth > rec_limit {
904+
tcx.sess.fatal(
905+
&format!("overflow representing the type `{}`", ty));
906+
}
907907

908-
tcx.layout_depth.set(depth+1);
909-
let cx = LayoutCx { tcx, param_env };
910-
let layout = cx.layout_raw_uncached(ty);
911-
tcx.layout_depth.set(depth);
908+
// Update the ImplicitCtxt to increase the layout_depth
909+
let icx = ty::tls::ImplicitCtxt {
910+
layout_depth: icx.layout_depth + 1,
911+
..icx.clone()
912+
};
912913

913-
layout
914+
ty::tls::enter_context(&icx, |_| {
915+
let cx = LayoutCx { tcx, param_env };
916+
cx.layout_raw_uncached(ty)
917+
})
918+
})
914919
}
915920

916921
pub fn provide(providers: &mut ty::maps::Providers) {

src/librustc/ty/maps/plumbing.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ macro_rules! define_maps {
522522
let icx = ty::tls::ImplicitCtxt {
523523
tcx,
524524
query: Some(job.clone()),
525+
layout_depth: icx.layout_depth,
525526
};
526527

527528
// Use the ImplicitCtxt while we execute the query

0 commit comments

Comments
 (0)