Skip to content

Commit 0a4a4ff

Browse files
committed
Auto merge of #56601 - Zoxc:lifetime-killer, r=nikomatsakis
Make the 'a lifetime on TyCtxt useless cc @rust-lang/compiler r? @nikomatsakis
2 parents 6f839fb + d0190d3 commit 0a4a4ff

File tree

9 files changed

+56
-48
lines changed

9 files changed

+56
-48
lines changed

src/librustc/infer/mod.rs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ use ty::error::{ExpectedFound, TypeError, UnconstrainedNumeric};
3737
use ty::fold::TypeFoldable;
3838
use ty::relate::{RelateResult, TraitObjectMode};
3939
use ty::subst::{Kind, Substs};
40-
use ty::{self, GenericParamDefKind, Ty, TyCtxt};
40+
use ty::{self, GenericParamDefKind, Ty, TyCtxt, CtxtInterners};
4141
use ty::{FloatVid, IntVid, TyVid};
4242
use util::nodemap::FxHashMap;
4343

@@ -474,6 +474,7 @@ impl fmt::Display for FixupError {
474474
pub struct InferCtxtBuilder<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
475475
global_tcx: TyCtxt<'a, 'gcx, 'gcx>,
476476
arena: SyncDroplessArena,
477+
interners: Option<CtxtInterners<'tcx>>,
477478
fresh_tables: Option<RefCell<ty::TypeckTables<'tcx>>>,
478479
trait_object_mode: TraitObjectMode,
479480
}
@@ -483,6 +484,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'gcx> {
483484
InferCtxtBuilder {
484485
global_tcx: self,
485486
arena: SyncDroplessArena::default(),
487+
interners: None,
486488
fresh_tables: None,
487489
trait_object_mode: TraitObjectMode::NoSquash,
488490
}
@@ -531,10 +533,13 @@ impl<'a, 'gcx, 'tcx> InferCtxtBuilder<'a, 'gcx, 'tcx> {
531533
global_tcx,
532534
trait_object_mode,
533535
ref arena,
536+
ref mut interners,
534537
ref fresh_tables,
535538
} = *self;
536539
let in_progress_tables = fresh_tables.as_ref();
537-
global_tcx.enter_local(arena, |tcx| {
540+
// Check that we haven't entered before
541+
assert!(interners.is_none());
542+
global_tcx.enter_local(arena, interners, |tcx| {
538543
f(InferCtxt {
539544
tcx,
540545
in_progress_tables,

src/librustc/ty/context.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ use std::ops::{Deref, Bound};
7272
use std::iter;
7373
use std::sync::mpsc;
7474
use std::sync::Arc;
75+
use std::marker::PhantomData;
7576
use rustc_target::spec::abi;
7677
use syntax::ast::{self, NodeId};
7778
use syntax::attr;
@@ -86,13 +87,15 @@ use hir;
8687
pub struct AllArenas<'tcx> {
8788
pub global: WorkerLocal<GlobalArenas<'tcx>>,
8889
pub interner: SyncDroplessArena,
90+
global_ctxt: Option<GlobalCtxt<'tcx>>,
8991
}
9092

9193
impl<'tcx> AllArenas<'tcx> {
9294
pub fn new() -> Self {
9395
AllArenas {
9496
global: WorkerLocal::new(|_| GlobalArenas::default()),
9597
interner: SyncDroplessArena::default(),
98+
global_ctxt: None,
9699
}
97100
}
98101
}
@@ -869,12 +872,13 @@ pub struct FreeRegionInfo {
869872
/// [rustc guide]: https://rust-lang.github.io/rustc-guide/ty.html
870873
#[derive(Copy, Clone)]
871874
pub struct TyCtxt<'a, 'gcx: 'tcx, 'tcx: 'a> {
872-
gcx: &'a GlobalCtxt<'gcx>,
873-
interners: &'a CtxtInterners<'tcx>
875+
gcx: &'gcx GlobalCtxt<'gcx>,
876+
interners: &'tcx CtxtInterners<'tcx>,
877+
dummy: PhantomData<&'a ()>,
874878
}
875879

876-
impl<'a, 'gcx, 'tcx> Deref for TyCtxt<'a, 'gcx, 'tcx> {
877-
type Target = &'a GlobalCtxt<'gcx>;
880+
impl<'gcx> Deref for TyCtxt<'_, 'gcx, '_> {
881+
type Target = &'gcx GlobalCtxt<'gcx>;
878882
#[inline(always)]
879883
fn deref(&self) -> &Self::Target {
880884
&self.gcx
@@ -964,10 +968,11 @@ pub struct GlobalCtxt<'tcx> {
964968
impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
965969
/// Get the global TyCtxt.
966970
#[inline]
967-
pub fn global_tcx(self) -> TyCtxt<'a, 'gcx, 'gcx> {
971+
pub fn global_tcx(self) -> TyCtxt<'gcx, 'gcx, 'gcx> {
968972
TyCtxt {
969973
gcx: self.gcx,
970974
interners: &self.gcx.global_interners,
975+
dummy: PhantomData,
971976
}
972977
}
973978

@@ -1105,7 +1110,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11051110
cstore: &'tcx CrateStoreDyn,
11061111
local_providers: ty::query::Providers<'tcx>,
11071112
extern_providers: ty::query::Providers<'tcx>,
1108-
arenas: &'tcx AllArenas<'tcx>,
1113+
arenas: &'tcx mut AllArenas<'tcx>,
11091114
resolutions: ty::Resolutions,
11101115
hir: hir_map::Map<'tcx>,
11111116
on_disk_query_result_cache: query::OnDiskCache<'tcx>,
@@ -1166,7 +1171,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
11661171
Lrc::new(StableVec::new(v)));
11671172
}
11681173

1169-
let gcx = &GlobalCtxt {
1174+
arenas.global_ctxt = Some(GlobalCtxt {
11701175
sess: s,
11711176
cstore,
11721177
global_arenas: &arenas.global,
@@ -1209,7 +1214,9 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12091214
alloc_map: Lock::new(interpret::AllocMap::new()),
12101215
tx_to_llvm_workers: Lock::new(tx),
12111216
output_filenames: Arc::new(output_filenames.clone()),
1212-
};
1217+
});
1218+
1219+
let gcx = arenas.global_ctxt.as_ref().unwrap();
12131220

12141221
sync::assert_send_val(&gcx);
12151222

@@ -1609,20 +1616,25 @@ impl<'a, 'tcx> TyCtxt<'a, 'tcx, 'tcx> {
16091616
}
16101617
}
16111618

1612-
impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
1619+
impl<'gcx> GlobalCtxt<'gcx> {
16131620
/// Call the closure with a local `TyCtxt` using the given arena.
1614-
pub fn enter_local<F, R>(
1615-
&self,
1621+
/// `interners` is a slot passed so we can create a CtxtInterners
1622+
/// with the same lifetime as `arena`.
1623+
pub fn enter_local<'tcx, F, R>(
1624+
&'gcx self,
16161625
arena: &'tcx SyncDroplessArena,
1626+
interners: &'tcx mut Option<CtxtInterners<'tcx>>,
16171627
f: F
16181628
) -> R
16191629
where
1620-
F: for<'a> FnOnce(TyCtxt<'a, 'gcx, 'tcx>) -> R
1630+
F: FnOnce(TyCtxt<'tcx, 'gcx, 'tcx>) -> R,
1631+
'gcx: 'tcx,
16211632
{
1622-
let interners = CtxtInterners::new(arena);
1633+
*interners = Some(CtxtInterners::new(&arena));
16231634
let tcx = TyCtxt {
16241635
gcx: self,
1625-
interners: &interners,
1636+
interners: interners.as_ref().unwrap(),
1637+
dummy: PhantomData,
16261638
};
16271639
ty::tls::with_related_context(tcx.global_tcx(), |icx| {
16281640
let new_icx = ty::tls::ImplicitCtxt {
@@ -1631,8 +1643,8 @@ impl<'gcx: 'tcx, 'tcx> GlobalCtxt<'gcx> {
16311643
layout_depth: icx.layout_depth,
16321644
task: icx.task,
16331645
};
1634-
ty::tls::enter_context(&new_icx, |new_icx| {
1635-
f(new_icx.tcx)
1646+
ty::tls::enter_context(&new_icx, |_| {
1647+
f(tcx)
16361648
})
16371649
})
16381650
}
@@ -1872,6 +1884,7 @@ pub mod tls {
18721884

18731885
use std::fmt;
18741886
use std::mem;
1887+
use std::marker::PhantomData;
18751888
use syntax_pos;
18761889
use ty::query;
18771890
use errors::{Diagnostic, TRACK_DIAGNOSTICS};
@@ -1891,10 +1904,10 @@ pub mod tls {
18911904
/// you should also have access to an ImplicitCtxt through the functions
18921905
/// in this module.
18931906
#[derive(Clone)]
1894-
pub struct ImplicitCtxt<'a, 'gcx: 'a+'tcx, 'tcx: 'a> {
1907+
pub struct ImplicitCtxt<'a, 'gcx: 'tcx, 'tcx> {
18951908
/// The current TyCtxt. Initially created by `enter_global` and updated
18961909
/// by `enter_local` with a new local interner
1897-
pub tcx: TyCtxt<'a, 'gcx, 'tcx>,
1910+
pub tcx: TyCtxt<'tcx, 'gcx, 'tcx>,
18981911

18991912
/// The current query job, if any. This is updated by start_job in
19001913
/// ty::query::plumbing when executing a query
@@ -2008,8 +2021,8 @@ pub mod tls {
20082021
/// creating a initial TyCtxt and ImplicitCtxt.
20092022
/// This happens once per rustc session and TyCtxts only exists
20102023
/// inside the `f` function.
2011-
pub fn enter_global<'gcx, F, R>(gcx: &GlobalCtxt<'gcx>, f: F) -> R
2012-
where F: for<'a> FnOnce(TyCtxt<'a, 'gcx, 'gcx>) -> R
2024+
pub fn enter_global<'gcx, F, R>(gcx: &'gcx GlobalCtxt<'gcx>, f: F) -> R
2025+
where F: FnOnce(TyCtxt<'gcx, 'gcx, 'gcx>) -> R
20132026
{
20142027
with_thread_locals(|| {
20152028
// Update GCX_PTR to indicate there's a GlobalCtxt available
@@ -2024,6 +2037,7 @@ pub mod tls {
20242037
let tcx = TyCtxt {
20252038
gcx,
20262039
interners: &gcx.global_interners,
2040+
dummy: PhantomData,
20272041
};
20282042
let icx = ImplicitCtxt {
20292043
tcx,
@@ -2053,6 +2067,7 @@ pub mod tls {
20532067
let tcx = TyCtxt {
20542068
gcx,
20552069
interners: &gcx.global_interners,
2070+
dummy: PhantomData,
20562071
};
20572072
let icx = ImplicitCtxt {
20582073
query: None,

src/librustc/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pub use self::binding::BindingMode;
8282
pub use self::binding::BindingMode::*;
8383

8484
pub use self::context::{TyCtxt, FreeRegionInfo, GlobalArenas, AllArenas, tls, keep_local};
85-
pub use self::context::{Lift, TypeckTables};
85+
pub use self::context::{Lift, TypeckTables, CtxtInterners};
8686

8787
pub use self::instance::{Instance, InstanceDef};
8888

src/librustc/ty/query/plumbing.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ impl<'a, 'tcx, Q: QueryDescription<'tcx>> JobOwner<'a, 'tcx, Q> {
197197
let r = tls::with_related_context(tcx, move |current_icx| {
198198
// Update the ImplicitCtxt to point to our new query job
199199
let new_icx = tls::ImplicitCtxt {
200-
tcx,
200+
tcx: tcx.global_tcx(),
201201
query: Some(self.job.clone()),
202202
layout_depth: current_icx.layout_depth,
203203
task: current_icx.task,

src/librustc_driver/driver.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -246,8 +246,6 @@ pub fn compile_input(
246246
}
247247
}
248248

249-
let arenas = AllArenas::new();
250-
251249
// Construct the HIR map
252250
let hir_map = time(sess, "indexing hir", || {
253251
hir_map::map_crate(sess, cstore, &mut hir_forest, &defs)
@@ -263,7 +261,6 @@ pub fn compile_input(
263261
sess,
264262
outdir,
265263
output,
266-
&arenas,
267264
&cstore,
268265
&hir_map,
269266
&analysis,
@@ -284,6 +281,8 @@ pub fn compile_input(
284281
None
285282
};
286283

284+
let mut arenas = AllArenas::new();
285+
287286
phase_3_run_analysis_passes(
288287
&*codegen_backend,
289288
control,
@@ -292,7 +291,7 @@ pub fn compile_input(
292291
hir_map,
293292
analysis,
294293
resolutions,
295-
&arenas,
294+
&mut arenas,
296295
&crate_name,
297296
&outputs,
298297
|tcx, analysis, rx, result| {
@@ -533,7 +532,6 @@ pub struct CompileState<'a, 'tcx: 'a> {
533532
pub output_filenames: Option<&'a OutputFilenames>,
534533
pub out_dir: Option<&'a Path>,
535534
pub out_file: Option<&'a Path>,
536-
pub arenas: Option<&'tcx AllArenas<'tcx>>,
537535
pub expanded_crate: Option<&'a ast::Crate>,
538536
pub hir_crate: Option<&'a hir::Crate>,
539537
pub hir_map: Option<&'a hir_map::Map<'tcx>>,
@@ -549,7 +547,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
549547
session,
550548
out_dir: out_dir.as_ref().map(|s| &**s),
551549
out_file: None,
552-
arenas: None,
553550
krate: None,
554551
registry: None,
555552
cstore: None,
@@ -605,7 +602,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
605602
session: &'tcx Session,
606603
out_dir: &'a Option<PathBuf>,
607604
out_file: &'a Option<PathBuf>,
608-
arenas: &'tcx AllArenas<'tcx>,
609605
cstore: &'tcx CStore,
610606
hir_map: &'a hir_map::Map<'tcx>,
611607
analysis: &'a ty::CrateAnalysis,
@@ -617,7 +613,6 @@ impl<'a, 'tcx> CompileState<'a, 'tcx> {
617613
) -> Self {
618614
CompileState {
619615
crate_name: Some(crate_name),
620-
arenas: Some(arenas),
621616
cstore: Some(cstore),
622617
hir_map: Some(hir_map),
623618
analysis: Some(analysis),
@@ -1216,7 +1211,7 @@ pub fn phase_3_run_analysis_passes<'tcx, F, R>(
12161211
hir_map: hir_map::Map<'tcx>,
12171212
mut analysis: ty::CrateAnalysis,
12181213
resolutions: Resolutions,
1219-
arenas: &'tcx AllArenas<'tcx>,
1214+
arenas: &'tcx mut AllArenas<'tcx>,
12201215
name: &str,
12211216
output_filenames: &OutputFilenames,
12221217
f: F,

src/librustc_driver/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,7 +911,6 @@ impl<'a> CompilerCalls<'a> for RustcDefaultCalls {
911911
&state.expanded_crate.take().unwrap(),
912912
state.crate_name.unwrap(),
913913
ppm,
914-
state.arenas.unwrap(),
915914
state.output_filenames.unwrap(),
916915
opt_uii.clone(),
917916
state.out_file);

0 commit comments

Comments
 (0)