Skip to content

Commit c8380cb

Browse files
committed
Move almost all code from Queries::global_ctxt into passes::create_global_ctxt
1 parent 5ced3da commit c8380cb

File tree

2 files changed

+78
-95
lines changed

2 files changed

+78
-95
lines changed

compiler/rustc_interface/src/passes.rs

+68-23
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@ use rustc_ast::{self as ast, visit};
77
use rustc_codegen_ssa::traits::CodegenBackend;
88
use rustc_data_structures::parallel;
99
use rustc_data_structures::steal::Steal;
10-
use rustc_data_structures::sync::{Lrc, OnceLock, WorkerLocal};
11-
use rustc_errors::PResult;
10+
use rustc_data_structures::sync::{AppendOnlyIndexVec, FreezeLock, Lrc, OnceLock, WorkerLocal};
1211
use rustc_expand::base::{ExtCtxt, LintStoreExpand};
1312
use rustc_feature::Features;
1413
use rustc_fs_util::try_canonicalize;
15-
use rustc_hir::def_id::{StableCrateId, LOCAL_CRATE};
14+
use rustc_hir::def_id::{StableCrateId, StableCrateIdMap, LOCAL_CRATE};
15+
use rustc_hir::definitions::Definitions;
16+
use rustc_incremental::setup_dep_graph;
1617
use rustc_lint::{unerased_lint_store, BufferedEarlyLint, EarlyCheckNode, LintStore};
1718
use rustc_metadata::creader::CStore;
1819
use rustc_middle::arena::Arena;
19-
use rustc_middle::dep_graph::DepGraph;
2020
use rustc_middle::ty::{self, GlobalCtxt, RegisteredTools, TyCtxt};
2121
use rustc_middle::util::Providers;
2222
use rustc_parse::{
@@ -28,6 +28,7 @@ use rustc_session::code_stats::VTableSizeInfo;
2828
use rustc_session::config::{CrateType, Input, OutFileName, OutputFilenames, OutputType};
2929
use rustc_session::cstore::Untracked;
3030
use rustc_session::output::filename_for_input;
31+
use rustc_session::output::{collect_crate_types, find_crate_name};
3132
use rustc_session::search_paths::PathKind;
3233
use rustc_session::{Limit, Session};
3334
use rustc_span::symbol::{sym, Symbol};
@@ -39,20 +40,22 @@ use std::any::Any;
3940
use std::ffi::OsString;
4041
use std::io::{self, BufWriter, Write};
4142
use std::path::{Path, PathBuf};
42-
use std::sync::LazyLock;
43+
use std::sync::{Arc, LazyLock};
4344
use std::{env, fs, iter};
4445
use tracing::{info, instrument};
4546

46-
pub fn parse<'a>(sess: &'a Session) -> PResult<'a, ast::Crate> {
47-
let krate = sess.time("parse_crate", || {
48-
let mut parser = unwrap_or_emit_fatal(match &sess.io.input {
49-
Input::File(file) => new_parser_from_file(&sess.psess, file, None),
50-
Input::Str { input, name } => {
51-
new_parser_from_source_str(&sess.psess, name.clone(), input.clone())
52-
}
53-
});
54-
parser.parse_crate_mod()
55-
})?;
47+
pub fn parse<'a>(sess: &'a Session) -> Result<ast::Crate> {
48+
let krate = sess
49+
.time("parse_crate", || {
50+
let mut parser = unwrap_or_emit_fatal(match &sess.io.input {
51+
Input::File(file) => new_parser_from_file(&sess.psess, file, None),
52+
Input::Str { input, name } => {
53+
new_parser_from_source_str(&sess.psess, name.clone(), input.clone())
54+
}
55+
});
56+
parser.parse_crate_mod()
57+
})
58+
.map_err(|parse_error| parse_error.emit())?;
5659

5760
if sess.opts.unstable_opts.input_stats {
5861
eprintln!("Lines of code: {}", sess.source_map().count_lines());
@@ -642,20 +645,46 @@ pub static DEFAULT_QUERY_PROVIDERS: LazyLock<Providers> = LazyLock::new(|| {
642645

643646
pub fn create_global_ctxt<'tcx>(
644647
compiler: &'tcx Compiler,
645-
crate_types: Vec<CrateType>,
646-
stable_crate_id: StableCrateId,
647-
dep_graph: DepGraph,
648-
untracked: Untracked,
648+
mut krate: rustc_ast::Crate,
649649
gcx_cell: &'tcx OnceLock<GlobalCtxt<'tcx>>,
650650
arena: &'tcx WorkerLocal<Arena<'tcx>>,
651651
hir_arena: &'tcx WorkerLocal<rustc_hir::Arena<'tcx>>,
652-
) -> &'tcx GlobalCtxt<'tcx> {
652+
) -> Result<&'tcx GlobalCtxt<'tcx>> {
653+
let sess = &compiler.sess;
654+
655+
rustc_builtin_macros::cmdline_attrs::inject(
656+
&mut krate,
657+
&sess.psess,
658+
&sess.opts.unstable_opts.crate_attr,
659+
);
660+
661+
let pre_configured_attrs = rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);
662+
663+
// parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
664+
let crate_name = find_crate_name(sess, &pre_configured_attrs);
665+
let crate_types = collect_crate_types(sess, &pre_configured_attrs);
666+
let stable_crate_id = StableCrateId::new(
667+
crate_name,
668+
crate_types.contains(&CrateType::Executable),
669+
sess.opts.cg.metadata.clone(),
670+
sess.cfg_version,
671+
);
672+
let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
673+
let dep_graph = setup_dep_graph(sess)?;
674+
675+
let cstore =
676+
FreezeLock::new(Box::new(CStore::new(compiler.codegen_backend.metadata_loader())) as _);
677+
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
678+
679+
let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
680+
let untracked =
681+
Untracked { cstore, source_span: AppendOnlyIndexVec::new(), definitions, stable_crate_ids };
682+
653683
// We're constructing the HIR here; we don't care what we will
654684
// read, since we haven't even constructed the *input* to
655685
// incr. comp. yet.
656686
dep_graph.assert_ignored();
657687

658-
let sess = &compiler.sess;
659688
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
660689

661690
let codegen_backend = &compiler.codegen_backend;
@@ -669,7 +698,7 @@ pub fn create_global_ctxt<'tcx>(
669698
let incremental = dep_graph.is_fully_enabled();
670699

671700
sess.time("setup_global_ctxt", || {
672-
gcx_cell.get_or_init(move || {
701+
let qcx = gcx_cell.get_or_init(move || {
673702
TyCtxt::create_global_ctxt(
674703
sess,
675704
crate_types,
@@ -688,7 +717,23 @@ pub fn create_global_ctxt<'tcx>(
688717
providers.hooks,
689718
compiler.current_gcx.clone(),
690719
)
691-
})
720+
});
721+
722+
qcx.enter(|tcx| {
723+
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
724+
assert_eq!(feed.key(), LOCAL_CRATE);
725+
feed.crate_name(crate_name);
726+
727+
let feed = tcx.feed_unit_query();
728+
feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
729+
sess,
730+
&pre_configured_attrs,
731+
crate_name,
732+
)));
733+
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
734+
feed.output_filenames(Arc::new(outputs));
735+
});
736+
Ok(qcx)
692737
})
693738
}
694739

compiler/rustc_interface/src/queries.rs

+10-72
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,19 @@
11
use crate::errors::{FailedWritingFile, RustcErrorFatal, RustcErrorUnexpectedAnnotation};
22
use crate::interface::{Compiler, Result};
3-
use crate::{errors, passes, util};
3+
use crate::{errors, passes};
44

55
use rustc_ast as ast;
66
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::{AppendOnlyIndexVec, FreezeLock, OnceLock, WorkerLocal};
11-
use rustc_hir::def_id::{StableCrateId, StableCrateIdMap, LOCAL_CRATE};
12-
use rustc_hir::definitions::Definitions;
13-
use rustc_incremental::setup_dep_graph;
14-
use rustc_metadata::creader::CStore;
10+
use rustc_data_structures::sync::{OnceLock, WorkerLocal};
11+
use rustc_hir::def_id::LOCAL_CRATE;
1512
use rustc_middle::arena::Arena;
1613
use rustc_middle::dep_graph::DepGraph;
1714
use rustc_middle::ty::{GlobalCtxt, TyCtxt};
1815
use rustc_serialize::opaque::FileEncodeResult;
19-
use rustc_session::config::{self, CrateType, OutputFilenames, OutputType};
20-
use rustc_session::cstore::Untracked;
21-
use rustc_session::output::{collect_crate_types, find_crate_name};
16+
use rustc_session::config::{self, OutputFilenames, OutputType};
2217
use rustc_session::Session;
2318
use rustc_span::symbol::sym;
2419
use std::any::Any;
@@ -106,77 +101,20 @@ impl<'tcx> Queries<'tcx> {
106101
}
107102

108103
pub fn parse(&self) -> Result<QueryResult<'_, ast::Crate>> {
109-
self.parse.compute(|| {
110-
passes::parse(&self.compiler.sess).map_err(|parse_error| parse_error.emit())
111-
})
104+
self.parse.compute(|| passes::parse(&self.compiler.sess))
112105
}
113106

114107
pub fn global_ctxt(&'tcx self) -> Result<QueryResult<'_, &'tcx GlobalCtxt<'tcx>>> {
115108
self.gcx.compute(|| {
116-
let sess = &self.compiler.sess;
117-
118-
let mut krate = self.parse()?.steal();
119-
120-
rustc_builtin_macros::cmdline_attrs::inject(
121-
&mut krate,
122-
&sess.psess,
123-
&sess.opts.unstable_opts.crate_attr,
124-
);
125-
126-
let pre_configured_attrs =
127-
rustc_expand::config::pre_configure_attrs(sess, &krate.attrs);
128-
129-
// parse `#[crate_name]` even if `--crate-name` was passed, to make sure it matches.
130-
let crate_name = find_crate_name(sess, &pre_configured_attrs);
131-
let crate_types = collect_crate_types(sess, &pre_configured_attrs);
132-
let stable_crate_id = StableCrateId::new(
133-
crate_name,
134-
crate_types.contains(&CrateType::Executable),
135-
sess.opts.cg.metadata.clone(),
136-
sess.cfg_version,
137-
);
138-
let outputs = util::build_output_filenames(&pre_configured_attrs, sess);
139-
let dep_graph = setup_dep_graph(sess)?;
140-
141-
let cstore = FreezeLock::new(Box::new(CStore::new(
142-
self.compiler.codegen_backend.metadata_loader(),
143-
)) as _);
144-
let definitions = FreezeLock::new(Definitions::new(stable_crate_id));
145-
146-
let stable_crate_ids = FreezeLock::new(StableCrateIdMap::default());
147-
let untracked = Untracked {
148-
cstore,
149-
source_span: AppendOnlyIndexVec::new(),
150-
definitions,
151-
stable_crate_ids,
152-
};
153-
154-
let qcx = passes::create_global_ctxt(
109+
let krate = self.parse()?.steal();
110+
111+
passes::create_global_ctxt(
155112
self.compiler,
156-
crate_types,
157-
stable_crate_id,
158-
dep_graph,
159-
untracked,
113+
krate,
160114
&self.gcx_cell,
161115
&self.arena,
162116
&self.hir_arena,
163-
);
164-
165-
qcx.enter(|tcx| {
166-
let feed = tcx.create_crate_num(stable_crate_id).unwrap();
167-
assert_eq!(feed.key(), LOCAL_CRATE);
168-
feed.crate_name(crate_name);
169-
170-
let feed = tcx.feed_unit_query();
171-
feed.features_query(tcx.arena.alloc(rustc_expand::config::features(
172-
sess,
173-
&pre_configured_attrs,
174-
crate_name,
175-
)));
176-
feed.crate_for_resolver(tcx.arena.alloc(Steal::new((krate, pre_configured_attrs))));
177-
feed.output_filenames(Arc::new(outputs));
178-
});
179-
Ok(qcx)
117+
)
180118
})
181119
}
182120

0 commit comments

Comments
 (0)