Skip to content

Commit 8b8afe7

Browse files
authored
Rollup merge of #103520 - petrochenkov:resout, r=cjgillot
rustc_middle: Rearrange resolver outputs structures slightly Addresses #98106 (comment). I also haven't seen the motivation for moving `cstore` from its old place, so I moved it back in this PR. r? ``@cjgillot``
2 parents 5a8e481 + 919673e commit 8b8afe7

File tree

5 files changed

+48
-47
lines changed

5 files changed

+48
-47
lines changed

compiler/rustc_interface/src/passes.rs

+3-11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ use rustc_data_structures::sync::{Lrc, OnceCell, WorkerLocal};
1616
use rustc_errors::{ErrorGuaranteed, PResult};
1717
use rustc_expand::base::{ExtCtxt, LintStoreExpand, ResolverExpand};
1818
use rustc_hir::def_id::StableCrateId;
19-
use rustc_hir::definitions::Definitions;
2019
use rustc_lint::{BufferedEarlyLint, EarlyCheckNode, LintStore};
2120
use rustc_metadata::creader::CStore;
2221
use rustc_middle::arena::Arena;
@@ -30,7 +29,7 @@ use rustc_plugin_impl as plugin;
3029
use rustc_query_impl::{OnDiskCache, Queries as TcxQueries};
3130
use rustc_resolve::{Resolver, ResolverArenas};
3231
use rustc_session::config::{CrateType, Input, OutputFilenames, OutputType};
33-
use rustc_session::cstore::{CrateStoreDyn, MetadataLoader, MetadataLoaderDyn};
32+
use rustc_session::cstore::{MetadataLoader, MetadataLoaderDyn};
3433
use rustc_session::output::filename_for_input;
3534
use rustc_session::search_paths::PathKind;
3635
use rustc_session::{Limit, Session};
@@ -135,10 +134,7 @@ mod boxed_resolver {
135134
f((&mut *resolver).as_mut().unwrap())
136135
}
137136

138-
pub fn to_resolver_outputs(
139-
resolver: Rc<RefCell<BoxedResolver>>,
140-
) -> (Definitions, Box<CrateStoreDyn>, ty::ResolverOutputs, ty::ResolverAstLowering)
141-
{
137+
pub fn to_resolver_outputs(resolver: Rc<RefCell<BoxedResolver>>) -> ty::ResolverOutputs {
142138
match Rc::try_unwrap(resolver) {
143139
Ok(resolver) => {
144140
let mut resolver = resolver.into_inner();
@@ -788,8 +784,7 @@ pub fn create_global_ctxt<'tcx>(
788784
// incr. comp. yet.
789785
dep_graph.assert_ignored();
790786

791-
let (definitions, cstore, resolver_outputs, resolver_for_lowering) =
792-
BoxedResolver::to_resolver_outputs(resolver);
787+
let resolver_outputs = BoxedResolver::to_resolver_outputs(resolver);
793788

794789
let sess = &compiler.session();
795790
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
@@ -816,10 +811,7 @@ pub fn create_global_ctxt<'tcx>(
816811
lint_store,
817812
arena,
818813
hir_arena,
819-
definitions,
820-
cstore,
821814
resolver_outputs,
822-
resolver_for_lowering,
823815
krate,
824816
dep_graph,
825817
queries.on_disk_cache.as_ref().map(OnDiskCache::as_dyn),

compiler/rustc_middle/src/query/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ rustc_queries! {
2323
desc { "triggering a delay span bug" }
2424
}
2525

26-
query resolutions(_: ()) -> &'tcx ty::ResolverOutputs {
26+
query resolutions(_: ()) -> &'tcx ty::ResolverGlobalCtxt {
2727
eval_always
2828
no_hash
2929
desc { "getting the resolver outputs" }

compiler/rustc_middle/src/ty/context.rs

+21-20
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ use std::mem;
7979
use std::ops::{Bound, Deref};
8080
use std::sync::Arc;
8181

82-
use super::{ImplPolarity, RvalueScopes};
82+
use super::{ImplPolarity, ResolverOutputs, RvalueScopes};
8383

8484
pub trait OnDiskCache<'tcx>: rustc_data_structures::sync::Sync {
8585
/// Creates a new `OnDiskCache` instance from the serialized data in `data`.
@@ -1067,10 +1067,9 @@ pub struct GlobalCtxt<'tcx> {
10671067
pub consts: CommonConsts<'tcx>,
10681068

10691069
definitions: RwLock<Definitions>,
1070-
cstore: Box<CrateStoreDyn>,
10711070

10721071
/// Output of the resolver.
1073-
pub(crate) untracked_resolutions: ty::ResolverOutputs,
1072+
pub(crate) untracked_resolutions: ty::ResolverGlobalCtxt,
10741073
untracked_resolver_for_lowering: Steal<ty::ResolverAstLowering>,
10751074
/// The entire crate as AST. This field serves as the input for the hir_crate query,
10761075
/// which lowers it from AST to HIR. It must not be read or used by anything else.
@@ -1233,10 +1232,7 @@ impl<'tcx> TyCtxt<'tcx> {
12331232
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
12341233
arena: &'tcx WorkerLocal<Arena<'tcx>>,
12351234
hir_arena: &'tcx WorkerLocal<hir::Arena<'tcx>>,
1236-
definitions: Definitions,
1237-
cstore: Box<CrateStoreDyn>,
1238-
untracked_resolutions: ty::ResolverOutputs,
1239-
untracked_resolver_for_lowering: ty::ResolverAstLowering,
1235+
resolver_outputs: ResolverOutputs,
12401236
krate: Lrc<ast::Crate>,
12411237
dep_graph: DepGraph,
12421238
on_disk_cache: Option<&'tcx dyn OnDiskCache<'tcx>>,
@@ -1245,6 +1241,11 @@ impl<'tcx> TyCtxt<'tcx> {
12451241
crate_name: &str,
12461242
output_filenames: OutputFilenames,
12471243
) -> GlobalCtxt<'tcx> {
1244+
let ResolverOutputs {
1245+
definitions,
1246+
global_ctxt: untracked_resolutions,
1247+
ast_lowering: untracked_resolver_for_lowering,
1248+
} = resolver_outputs;
12481249
let data_layout = TargetDataLayout::parse(&s.target).unwrap_or_else(|err| {
12491250
s.emit_fatal(err);
12501251
});
@@ -1253,7 +1254,7 @@ impl<'tcx> TyCtxt<'tcx> {
12531254
&interners,
12541255
s,
12551256
&definitions,
1256-
&*cstore,
1257+
&*untracked_resolutions.cstore,
12571258
// This is only used to create a stable hashing context.
12581259
&untracked_resolutions.source_span,
12591260
);
@@ -1268,7 +1269,6 @@ impl<'tcx> TyCtxt<'tcx> {
12681269
interners,
12691270
dep_graph,
12701271
definitions: RwLock::new(definitions),
1271-
cstore,
12721272
prof: s.prof.clone(),
12731273
types: common_types,
12741274
lifetimes: common_lifetimes,
@@ -1369,7 +1369,7 @@ impl<'tcx> TyCtxt<'tcx> {
13691369
if let Some(id) = id.as_local() {
13701370
self.definitions_untracked().def_key(id)
13711371
} else {
1372-
self.cstore.def_key(id)
1372+
self.untracked_resolutions.cstore.def_key(id)
13731373
}
13741374
}
13751375

@@ -1383,7 +1383,7 @@ impl<'tcx> TyCtxt<'tcx> {
13831383
if let Some(id) = id.as_local() {
13841384
self.definitions_untracked().def_path(id)
13851385
} else {
1386-
self.cstore.def_path(id)
1386+
self.untracked_resolutions.cstore.def_path(id)
13871387
}
13881388
}
13891389

@@ -1393,7 +1393,7 @@ impl<'tcx> TyCtxt<'tcx> {
13931393
if let Some(def_id) = def_id.as_local() {
13941394
self.definitions_untracked().def_path_hash(def_id)
13951395
} else {
1396-
self.cstore.def_path_hash(def_id)
1396+
self.untracked_resolutions.cstore.def_path_hash(def_id)
13971397
}
13981398
}
13991399

@@ -1402,7 +1402,7 @@ impl<'tcx> TyCtxt<'tcx> {
14021402
if crate_num == LOCAL_CRATE {
14031403
self.sess.local_stable_crate_id()
14041404
} else {
1405-
self.cstore.stable_crate_id(crate_num)
1405+
self.untracked_resolutions.cstore.stable_crate_id(crate_num)
14061406
}
14071407
}
14081408

@@ -1413,7 +1413,7 @@ impl<'tcx> TyCtxt<'tcx> {
14131413
if stable_crate_id == self.sess.local_stable_crate_id() {
14141414
LOCAL_CRATE
14151415
} else {
1416-
self.cstore.stable_crate_id_to_crate_num(stable_crate_id)
1416+
self.untracked_resolutions.cstore.stable_crate_id_to_crate_num(stable_crate_id)
14171417
}
14181418
}
14191419

@@ -1432,8 +1432,9 @@ impl<'tcx> TyCtxt<'tcx> {
14321432
} else {
14331433
// If this is a DefPathHash from an upstream crate, let the CrateStore map
14341434
// it to a DefId.
1435-
let cnum = self.cstore.stable_crate_id_to_crate_num(stable_crate_id);
1436-
self.cstore.def_path_hash_to_def_id(cnum, hash)
1435+
let cstore = &*self.untracked_resolutions.cstore;
1436+
let cnum = cstore.stable_crate_id_to_crate_num(stable_crate_id);
1437+
cstore.def_path_hash_to_def_id(cnum, hash)
14371438
}
14381439
}
14391440

@@ -1445,7 +1446,7 @@ impl<'tcx> TyCtxt<'tcx> {
14451446
let (crate_name, stable_crate_id) = if def_id.is_local() {
14461447
(self.crate_name, self.sess.local_stable_crate_id())
14471448
} else {
1448-
let cstore = &self.cstore;
1449+
let cstore = &*self.untracked_resolutions.cstore;
14491450
(cstore.crate_name(def_id.krate), cstore.stable_crate_id(def_id.krate))
14501451
};
14511452

@@ -1520,7 +1521,7 @@ impl<'tcx> TyCtxt<'tcx> {
15201521
/// Note that this is *untracked* and should only be used within the query
15211522
/// system if the result is otherwise tracked through queries
15221523
pub fn cstore_untracked(self) -> &'tcx CrateStoreDyn {
1523-
&*self.cstore
1524+
&*self.untracked_resolutions.cstore
15241525
}
15251526

15261527
/// Note that this is *untracked* and should only be used within the query
@@ -1546,7 +1547,7 @@ impl<'tcx> TyCtxt<'tcx> {
15461547
let hcx = StableHashingContext::new(
15471548
self.sess,
15481549
&*definitions,
1549-
&*self.cstore,
1550+
&*self.untracked_resolutions.cstore,
15501551
&self.untracked_resolutions.source_span,
15511552
);
15521553
f(hcx)
@@ -2364,7 +2365,7 @@ impl<'tcx> TyCtxt<'tcx> {
23642365
st,
23652366
self.sess,
23662367
&self.definitions.read(),
2367-
&*self.cstore,
2368+
&*self.untracked_resolutions.cstore,
23682369
// This is only used to create a stable hashing context.
23692370
&self.untracked_resolutions.source_span,
23702371
)

compiler/rustc_middle/src/ty/mod.rs

+10-1
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,13 @@ use rustc_data_structures::tagged_ptr::CopyTaggedPtr;
3838
use rustc_hir as hir;
3939
use rustc_hir::def::{CtorKind, CtorOf, DefKind, LifetimeRes, Res};
4040
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, LocalDefIdMap};
41+
use rustc_hir::definitions::Definitions;
4142
use rustc_hir::Node;
4243
use rustc_index::vec::IndexVec;
4344
use rustc_macros::HashStable;
4445
use rustc_query_system::ich::StableHashingContext;
4546
use rustc_serialize::{Decodable, Encodable};
47+
use rustc_session::cstore::CrateStoreDyn;
4648
use rustc_span::hygiene::MacroKind;
4749
use rustc_span::symbol::{kw, sym, Ident, Symbol};
4850
use rustc_span::{ExpnId, Span};
@@ -142,8 +144,15 @@ mod sty;
142144

143145
pub type RegisteredTools = FxHashSet<Ident>;
144146

145-
#[derive(Debug)]
146147
pub struct ResolverOutputs {
148+
pub definitions: Definitions,
149+
pub global_ctxt: ResolverGlobalCtxt,
150+
pub ast_lowering: ResolverAstLowering,
151+
}
152+
153+
#[derive(Debug)]
154+
pub struct ResolverGlobalCtxt {
155+
pub cstore: Box<CrateStoreDyn>,
147156
pub visibilities: FxHashMap<LocalDefId, Visibility>,
148157
/// This field is used to decide whether we should make `PRIVATE_IN_PUBLIC` a hard error.
149158
pub has_pub_restricted: bool,

compiler/rustc_resolve/src/lib.rs

+13-14
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,10 @@ use rustc_metadata::creader::{CStore, CrateLoader};
4242
use rustc_middle::metadata::ModChild;
4343
use rustc_middle::middle::privacy::AccessLevels;
4444
use rustc_middle::span_bug;
45-
use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools, ResolverOutputs};
45+
use rustc_middle::ty::{self, DefIdTree, MainDefinition, RegisteredTools};
46+
use rustc_middle::ty::{ResolverGlobalCtxt, ResolverOutputs};
4647
use rustc_query_system::ich::StableHashingContext;
47-
use rustc_session::cstore::{CrateStore, CrateStoreDyn, MetadataLoaderDyn};
48+
use rustc_session::cstore::{CrateStore, MetadataLoaderDyn};
4849
use rustc_session::lint::LintBuffer;
4950
use rustc_session::Session;
5051
use rustc_span::hygiene::{ExpnId, LocalExpnId, MacroKind, SyntaxContext, Transparency};
@@ -1376,9 +1377,7 @@ impl<'a> Resolver<'a> {
13761377
Default::default()
13771378
}
13781379

1379-
pub fn into_outputs(
1380-
self,
1381-
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
1380+
pub fn into_outputs(self) -> ResolverOutputs {
13821381
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
13831382
let definitions = self.definitions;
13841383
let cstore = Box::new(self.crate_loader.into_cstore());
@@ -1394,7 +1393,8 @@ impl<'a> Resolver<'a> {
13941393
let main_def = self.main_def;
13951394
let confused_type_with_std_module = self.confused_type_with_std_module;
13961395
let access_levels = self.access_levels;
1397-
let resolutions = ResolverOutputs {
1396+
let global_ctxt = ResolverGlobalCtxt {
1397+
cstore,
13981398
source_span,
13991399
expn_that_defined,
14001400
visibilities,
@@ -1416,7 +1416,7 @@ impl<'a> Resolver<'a> {
14161416
confused_type_with_std_module,
14171417
registered_tools: self.registered_tools,
14181418
};
1419-
let resolutions_lowering = ty::ResolverAstLowering {
1419+
let ast_lowering = ty::ResolverAstLowering {
14201420
legacy_const_generic_args: self.legacy_const_generic_args,
14211421
partial_res_map: self.partial_res_map,
14221422
import_res_map: self.import_res_map,
@@ -1429,16 +1429,15 @@ impl<'a> Resolver<'a> {
14291429
trait_map: self.trait_map,
14301430
builtin_macro_kinds: self.builtin_macro_kinds,
14311431
};
1432-
(definitions, cstore, resolutions, resolutions_lowering)
1432+
ResolverOutputs { definitions, global_ctxt, ast_lowering }
14331433
}
14341434

1435-
pub fn clone_outputs(
1436-
&self,
1437-
) -> (Definitions, Box<CrateStoreDyn>, ResolverOutputs, ty::ResolverAstLowering) {
1435+
pub fn clone_outputs(&self) -> ResolverOutputs {
14381436
let proc_macros = self.proc_macros.iter().map(|id| self.local_def_id(*id)).collect();
14391437
let definitions = self.definitions.clone();
14401438
let cstore = Box::new(self.cstore().clone());
1441-
let resolutions = ResolverOutputs {
1439+
let global_ctxt = ResolverGlobalCtxt {
1440+
cstore,
14421441
source_span: self.source_span.clone(),
14431442
expn_that_defined: self.expn_that_defined.clone(),
14441443
visibilities: self.visibilities.clone(),
@@ -1460,7 +1459,7 @@ impl<'a> Resolver<'a> {
14601459
registered_tools: self.registered_tools.clone(),
14611460
access_levels: self.access_levels.clone(),
14621461
};
1463-
let resolutions_lowering = ty::ResolverAstLowering {
1462+
let ast_lowering = ty::ResolverAstLowering {
14641463
legacy_const_generic_args: self.legacy_const_generic_args.clone(),
14651464
partial_res_map: self.partial_res_map.clone(),
14661465
import_res_map: self.import_res_map.clone(),
@@ -1473,7 +1472,7 @@ impl<'a> Resolver<'a> {
14731472
trait_map: self.trait_map.clone(),
14741473
builtin_macro_kinds: self.builtin_macro_kinds.clone(),
14751474
};
1476-
(definitions, cstore, resolutions, resolutions_lowering)
1475+
ResolverOutputs { definitions, global_ctxt, ast_lowering }
14771476
}
14781477

14791478
fn create_stable_hashing_context(&self) -> StableHashingContext<'_> {

0 commit comments

Comments
 (0)