Skip to content

Commit 8f132d8

Browse files
committed
Run the resolver after TyCtxt construction
1 parent 6924e3c commit 8f132d8

File tree

6 files changed

+69
-57
lines changed

6 files changed

+69
-57
lines changed

Cargo.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4180,6 +4180,7 @@ dependencies = [
41804180
"rustc_hir_analysis",
41814181
"rustc_hir_typeck",
41824182
"rustc_incremental",
4183+
"rustc_index",
41834184
"rustc_lint",
41844185
"rustc_macros",
41854186
"rustc_metadata",

compiler/rustc_interface/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ rustc_middle = { path = "../rustc_middle" }
2424
rustc_ast_lowering = { path = "../rustc_ast_lowering" }
2525
rustc_ast_passes = { path = "../rustc_ast_passes" }
2626
rustc_incremental = { path = "../rustc_incremental" }
27+
rustc_index = { path = "../rustc_index" }
2728
rustc_traits = { path = "../rustc_traits" }
2829
rustc_data_structures = { path = "../rustc_data_structures" }
2930
rustc_codegen_ssa = { path = "../rustc_codegen_ssa" }

compiler/rustc_interface/src/queries.rs

Lines changed: 53 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,20 @@ use rustc_codegen_ssa::traits::CodegenBackend;
77
use rustc_codegen_ssa::CodegenResults;
88
use rustc_data_structures::steal::Steal;
99
use rustc_data_structures::svh::Svh;
10-
use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
11-
use rustc_hir::def_id::LOCAL_CRATE;
10+
use rustc_data_structures::sync::{Lrc, OnceCell, RwLock, WorkerLocal};
11+
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
12+
use rustc_hir::definitions::Definitions;
1213
use rustc_incremental::DepGraphFuture;
14+
use rustc_index::vec::IndexVec;
1315
use rustc_lint::LintStore;
16+
use rustc_metadata::creader::CStore;
1417
use rustc_middle::arena::Arena;
1518
use rustc_middle::dep_graph::DepGraph;
1619
use rustc_middle::ty::{self, GlobalCtxt, TyCtxt};
1720
use rustc_query_impl::Queries as TcxQueries;
1821
use rustc_resolve::Resolver;
1922
use rustc_session::config::{self, OutputFilenames, OutputType};
23+
use rustc_session::cstore::Untracked;
2024
use rustc_session::{output::find_crate_name, Session};
2125
use rustc_span::symbol::sym;
2226
use rustc_span::Symbol;
@@ -187,40 +191,20 @@ impl<'tcx> Queries<'tcx> {
187191
self.gcx.compute(|| {
188192
let crate_name = *self.crate_name()?.borrow();
189193
let (krate, lint_store) = self.register_plugins()?.steal();
190-
let (krate, resolver_outputs) = {
191-
let _timer = self.session().timer("configure_and_expand");
192-
let sess = self.session();
193-
194-
let arenas = Resolver::arenas();
195-
let mut resolver = Resolver::new(
196-
sess,
197-
&krate,
198-
crate_name,
199-
self.codegen_backend().metadata_loader(),
200-
&arenas,
201-
);
202-
let krate = passes::configure_and_expand(
203-
sess,
204-
&lint_store,
205-
krate,
206-
crate_name,
207-
&mut resolver,
208-
)?;
209-
(Lrc::new(krate), resolver.into_outputs())
210-
};
211-
212-
let ty::ResolverOutputs {
213-
untracked,
214-
global_ctxt: untracked_resolutions,
215-
ast_lowering: untracked_resolver_for_lowering,
216-
} = resolver_outputs;
217194

218-
// Make sure we don't mutate the cstore from here on.
219-
std::mem::forget(untracked.cstore.read());
195+
let sess = self.session();
220196

221-
let gcx = passes::create_global_ctxt(
197+
let cstore = RwLock::new(Box::new(CStore::new(sess)) as _);
198+
let definitions = RwLock::new(Definitions::new(sess.local_stable_crate_id()));
199+
let mut source_span = IndexVec::default();
200+
let _id = source_span.push(krate.spans.inner_span);
201+
debug_assert_eq!(_id, CRATE_DEF_ID);
202+
let source_span = RwLock::new(source_span);
203+
let untracked = Untracked { cstore, source_span, definitions };
204+
205+
let qcx = passes::create_global_ctxt(
222206
self.compiler,
223-
lint_store,
207+
lint_store.clone(),
224208
self.dep_graph()?.steal(),
225209
untracked,
226210
&self.queries,
@@ -229,17 +213,48 @@ impl<'tcx> Queries<'tcx> {
229213
&self.hir_arena,
230214
);
231215

232-
gcx.enter(|tcx| {
216+
qcx.enter(|tcx| {
217+
let feed = tcx.feed_local_crate();
218+
feed.crate_name(crate_name);
219+
let (krate, resolver_outputs) = {
220+
let _timer = sess.timer("configure_and_expand");
221+
222+
let arenas = Resolver::arenas();
223+
let mut resolver = Resolver::new(
224+
sess,
225+
&krate,
226+
crate_name,
227+
self.codegen_backend().metadata_loader(),
228+
&arenas,
229+
tcx.untracked(),
230+
);
231+
let krate = passes::configure_and_expand(
232+
sess,
233+
&lint_store,
234+
krate,
235+
crate_name,
236+
&mut resolver,
237+
)?;
238+
239+
// Make sure we don't mutate the cstore from here on.
240+
tcx.untracked().cstore.leak();
241+
(Lrc::new(krate), resolver.into_outputs())
242+
};
243+
244+
let ty::ResolverOutputs {
245+
global_ctxt: untracked_resolutions,
246+
ast_lowering: untracked_resolver_for_lowering,
247+
} = resolver_outputs;
248+
233249
let feed = tcx.feed_unit_query();
234250
feed.resolver_for_lowering(
235251
tcx.arena.alloc(Steal::new((untracked_resolver_for_lowering, krate))),
236252
);
237253
feed.resolutions(tcx.arena.alloc(untracked_resolutions));
238254
feed.features_query(tcx.sess.features_untracked());
239-
let feed = tcx.feed_local_crate();
240-
feed.crate_name(crate_name);
241-
});
242-
Ok(gcx)
255+
Ok(())
256+
})?;
257+
Ok(qcx)
243258
})
244259
}
245260

compiler/rustc_middle/src/ty/context.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1015,6 +1015,10 @@ impl<'tcx> TyCtxt<'tcx> {
10151015
ReadGuard::map(self.untracked.cstore.read(), |c| &**c)
10161016
}
10171017

1018+
/// Give out access to the untracked data without any sanity checks.
1019+
pub fn untracked(self) -> &'tcx Untracked {
1020+
&self.untracked
1021+
}
10181022
/// Note that this is *untracked* and should only be used within the query
10191023
/// system if the result is otherwise tracked through queries
10201024
#[inline]

compiler/rustc_middle/src/ty/mod.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ use rustc_index::vec::IndexVec;
4343
use rustc_macros::HashStable;
4444
use rustc_query_system::ich::StableHashingContext;
4545
use rustc_serialize::{Decodable, Encodable};
46-
use rustc_session::cstore::Untracked;
4746
use rustc_span::hygiene::MacroKind;
4847
use rustc_span::symbol::{kw, sym, Ident, Symbol};
4948
use rustc_span::{ExpnId, ExpnKind, Span};
@@ -157,7 +156,6 @@ pub type RegisteredTools = FxHashSet<Ident>;
157156
pub struct ResolverOutputs {
158157
pub global_ctxt: ResolverGlobalCtxt,
159158
pub ast_lowering: ResolverAstLowering,
160-
pub untracked: Untracked,
161159
}
162160

163161
#[derive(Debug)]

compiler/rustc_resolve/src/lib.rs

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,14 @@ use rustc_ast::{self as ast, NodeId, CRATE_NODE_ID};
2727
use rustc_ast::{AngleBracketedArg, Crate, Expr, ExprKind, GenericArg, GenericArgs, LitKind, Path};
2828
use rustc_data_structures::fx::{FxHashMap, FxHashSet, FxIndexMap, FxIndexSet};
2929
use rustc_data_structures::intern::Interned;
30-
use rustc_data_structures::sync::{Lrc, MappedReadGuard, ReadGuard, RwLock};
30+
use rustc_data_structures::sync::{Lrc, MappedReadGuard, ReadGuard};
3131
use rustc_errors::{Applicability, DiagnosticBuilder, ErrorGuaranteed};
3232
use rustc_expand::base::{DeriveResolutions, SyntaxExtension, SyntaxExtensionKind};
3333
use rustc_hir::def::Namespace::{self, *};
3434
use rustc_hir::def::{self, CtorOf, DefKind, DocLinkResMap, LifetimeRes, PartialRes, PerNS};
3535
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LocalDefId};
3636
use rustc_hir::def_id::{CRATE_DEF_ID, LOCAL_CRATE};
37-
use rustc_hir::definitions::{DefPathData, Definitions};
37+
use rustc_hir::definitions::DefPathData;
3838
use rustc_hir::TraitCandidate;
3939
use rustc_index::vec::IndexVec;
4040
use rustc_metadata::creader::{CStore, CrateLoader};
@@ -962,7 +962,7 @@ pub struct Resolver<'a, 'tcx> {
962962

963963
local_crate_name: Symbol,
964964
metadata_loader: Box<MetadataLoaderDyn>,
965-
untracked: Untracked,
965+
untracked: &'tcx Untracked,
966966
used_extern_options: FxHashSet<Symbol>,
967967
macro_names: FxHashSet<Ident>,
968968
builtin_macros: FxHashMap<Symbol, BuiltinMacroState>,
@@ -1211,6 +1211,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12111211
crate_name: Symbol,
12121212
metadata_loader: Box<MetadataLoaderDyn>,
12131213
arenas: &'a ResolverArenas<'a>,
1214+
untracked: &'tcx Untracked,
12141215
) -> Resolver<'a, 'tcx> {
12151216
let tcx = TyCtxt { sess: session };
12161217

@@ -1233,8 +1234,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12331234
&mut FxHashMap::default(),
12341235
);
12351236

1236-
let definitions = Definitions::new(session.local_stable_crate_id());
1237-
12381237
let mut visibilities = FxHashMap::default();
12391238
visibilities.insert(CRATE_DEF_ID, ty::Visibility::Public);
12401239

@@ -1246,10 +1245,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
12461245
let mut invocation_parents = FxHashMap::default();
12471246
invocation_parents.insert(LocalExpnId::ROOT, (CRATE_DEF_ID, ImplTraitContext::Existential));
12481247

1249-
let mut source_span = IndexVec::default();
1250-
let _id = source_span.push(krate.spans.inner_span);
1251-
debug_assert_eq!(_id, CRATE_DEF_ID);
1252-
12531248
let mut extern_prelude: FxHashMap<Ident, ExternPreludeEntry<'_>> = session
12541249
.opts
12551250
.externs
@@ -1327,11 +1322,7 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
13271322
metadata_loader,
13281323
local_crate_name: crate_name,
13291324
used_extern_options: Default::default(),
1330-
untracked: Untracked {
1331-
cstore: RwLock::new(Box::new(CStore::new(session))),
1332-
source_span: RwLock::new(source_span),
1333-
definitions: RwLock::new(definitions),
1334-
},
1325+
untracked,
13351326
macro_names: FxHashSet::default(),
13361327
builtin_macros: Default::default(),
13371328
builtin_macro_kinds: Default::default(),
@@ -1436,7 +1427,6 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14361427
let main_def = self.main_def;
14371428
let confused_type_with_std_module = self.confused_type_with_std_module;
14381429
let effective_visibilities = self.effective_visibilities;
1439-
let untracked = self.untracked;
14401430
let global_ctxt = ResolverGlobalCtxt {
14411431
expn_that_defined,
14421432
visibilities,
@@ -1475,11 +1465,11 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
14751465
builtin_macro_kinds: self.builtin_macro_kinds,
14761466
lifetime_elision_allowed: self.lifetime_elision_allowed,
14771467
};
1478-
ResolverOutputs { global_ctxt, ast_lowering, untracked }
1468+
ResolverOutputs { global_ctxt, ast_lowering }
14791469
}
14801470

14811471
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {
1482-
StableHashingContext::new(self.tcx.sess, &self.untracked)
1472+
StableHashingContext::new(self.tcx.sess, self.untracked)
14831473
}
14841474

14851475
fn crate_loader<T>(&mut self, f: impl FnOnce(&mut CrateLoader<'_>) -> T) -> T {
@@ -1543,6 +1533,9 @@ impl<'a, 'tcx> Resolver<'a, 'tcx> {
15431533
.sess
15441534
.time("resolve_postprocess", || self.crate_loader(|c| c.postprocess(krate)));
15451535
});
1536+
1537+
// Make sure we don't mutate the cstore from here on.
1538+
self.untracked.cstore.leak();
15461539
}
15471540

15481541
fn traits_in_scope(

0 commit comments

Comments
 (0)