Skip to content

Commit 87f4623

Browse files
committed
Turn parsing into a query
1 parent 95f0ca1 commit 87f4623

File tree

15 files changed

+170
-163
lines changed

15 files changed

+170
-163
lines changed

src/librustc/query/mod.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,28 @@ use syntax_pos::symbol::InternedString;
3030
// as they will raise an fatal error on query cycles instead.
3131
rustc_queries! {
3232
Other {
33+
query parse(_: LocalCrate) -> Result<Lrc<Steal<ast::Crate>>, ErrorReported> {
34+
no_hash
35+
eval_always
36+
desc { "parsing crate" }
37+
}
38+
39+
query register_plugins(
40+
_: LocalCrate
41+
) -> Result<Lrc<Steal<(ast::Crate, ty::PluginInfo)>>, ErrorReported> {
42+
no_hash
43+
eval_always
44+
desc { "registering plugins" }
45+
}
46+
47+
/// The definite name of the current crate after taking into account
48+
/// attributes, commandline parameters, etc.
49+
query early_crate_name(_: LocalCrate) -> Result<Symbol, ErrorReported> {
50+
no_hash
51+
eval_always
52+
desc { "finding the crate name" }
53+
}
54+
3355
query expand_macros(_: LocalCrate) -> Result<Lrc<ty::ExpansionResult>, ErrorReported> {
3456
no_hash
3557
eval_always
@@ -734,6 +756,7 @@ rustc_queries! {
734756
eval_always
735757
desc { "looking up the hash a crate" }
736758
}
759+
// FIXME: Remove this as it's the same as `crate_name`
737760
query original_crate_name(_: CrateNum) -> Symbol {
738761
eval_always
739762
desc { "looking up the original name a crate" }

src/librustc/session/config.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,9 @@ top_level_options!(
400400
// try to not rely on this too much.
401401
actually_rustdoc: bool [TRACKED],
402402

403+
// Replaces bodies with loops
404+
everybody_loops: bool [TRACKED],
405+
403406
// Specifications of codegen units / ThinLTO which are forced as a
404407
// result of parsing command line options. These are not necessarily
405408
// what rustc was invoked with, but massaged a bit to agree with
@@ -623,6 +626,7 @@ impl Default for Options {
623626
unstable_features: UnstableFeatures::Disallow,
624627
debug_assertions: true,
625628
actually_rustdoc: false,
629+
everybody_loops: false,
626630
cli_forced_codegen_units: None,
627631
cli_forced_thinlto_off: false,
628632
remap_path_prefix: Vec::new(),
@@ -2366,6 +2370,7 @@ pub fn build_session_options_and_crate_config(
23662370
unstable_features: UnstableFeatures::from_environment(),
23672371
debug_assertions,
23682372
actually_rustdoc: false,
2373+
everybody_loops: false,
23692374
cli_forced_codegen_units: codegen_units,
23702375
cli_forced_thinlto_off: disable_thinlto,
23712376
remap_path_prefix,

src/librustc/ty/context.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
5151
StableVec};
5252
use arena::{TypedArena, SyncDroplessArena};
5353
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
54-
use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal, AtomicOnce, Once, OneThread};
54+
use rustc_data_structures::sync::{self, Lrc, Lock, WorkerLocal, AtomicOnce, Once};
5555
use std::any::Any;
5656
use std::borrow::Borrow;
5757
use std::cmp::Ordering;
@@ -70,7 +70,7 @@ use syntax::attr;
7070
use syntax::source_map::MultiSpan;
7171
use syntax::edition::Edition;
7272
use syntax::feature_gate;
73-
use syntax::symbol::{Symbol, keywords, InternedString};
73+
use syntax::symbol::{keywords, InternedString};
7474
use syntax_pos::Span;
7575

7676
use crate::hir;
@@ -1021,13 +1021,10 @@ pub struct GlobalCtxt<'tcx> {
10211021

10221022
/// This stores a `Lrc<CStore>`, but that type depends on
10231023
/// rustc_metadata, so it cannot be used here.
1024-
pub cstore_rc: OneThread<Steal<Box<dyn Any>>>,
1024+
pub cstore_rc: &'tcx (dyn Any + sync::Sync),
10251025

10261026
pub sess_rc: Lrc<Session>,
10271027

1028-
/// The AST after registering plugins.
1029-
pub ast_crate: Steal<(ast::Crate, ty::PluginInfo)>,
1030-
10311028
lowered_hir: AtomicOnce<&'tcx hir::LoweredHir>,
10321029
hir_map: AtomicOnce<&'tcx hir_map::Map<'tcx>>,
10331030

@@ -1047,9 +1044,7 @@ pub struct GlobalCtxt<'tcx> {
10471044
/// Merge this with `selection_cache`?
10481045
pub evaluation_cache: traits::EvaluationCache<'tcx>,
10491046

1050-
/// The definite name of the current crate after taking into account
1051-
/// attributes, commandline parameters, etc.
1052-
pub crate_name: Symbol,
1047+
pub crate_name_override: Option<String>,
10531048

10541049
/// Data layout specification for the current target.
10551050
pub data_layout: TargetDataLayout,
@@ -1210,15 +1205,13 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12101205
pub fn create_global_ctxt(
12111206
s: &'tcx Lrc<Session>,
12121207
cstore: &'tcx CrateStoreDyn,
1213-
cstore_rc: Box<dyn Any>,
1208+
cstore_rc: &'tcx (dyn Any + sync::Sync),
12141209
local_providers: ty::query::Providers<'tcx>,
12151210
extern_providers: ty::query::Providers<'tcx>,
12161211
arenas: &'tcx AllArenas<'tcx>,
12171212
dep_graph: DepGraph,
1218-
ast_crate: ast::Crate,
1219-
plugin_info: ty::PluginInfo,
12201213
on_disk_query_result_cache: query::OnDiskCache<'tcx>,
1221-
crate_name: &str,
1214+
crate_name: Option<String>,
12221215
tx: mpsc::Sender<Box<dyn Any + Send>>,
12231216
io: InputsAndOutputs,
12241217
) -> GlobalCtxt<'tcx> {
@@ -1234,13 +1227,12 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12341227
GlobalCtxt {
12351228
sess: &**s,
12361229
cstore,
1237-
cstore_rc: OneThread::new(Steal::new(cstore_rc)),
1230+
cstore_rc,
12381231
sess_rc: s.clone(),
12391232
global_arenas: &arenas.global,
12401233
global_interners: interners,
12411234
dep_graph,
12421235
types: common_types,
1243-
ast_crate: Steal::new((ast_crate, plugin_info)),
12441236
lowered_hir: AtomicOnce::new(),
12451237
hir_map: AtomicOnce::new(),
12461238
metadata_dep_nodes: Once::new(),
@@ -1252,7 +1244,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
12521244
rcache: Default::default(),
12531245
selection_cache: Default::default(),
12541246
evaluation_cache: Default::default(),
1255-
crate_name: Symbol::intern(crate_name),
1247+
crate_name_override: crate_name,
12561248
data_layout,
12571249
layout_interner: Default::default(),
12581250
stability_interner: Default::default(),
@@ -1361,7 +1353,7 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
13611353
// statements within the query system and we'd run into endless
13621354
// recursion otherwise.
13631355
let (crate_name, crate_disambiguator) = if def_id.is_local() {
1364-
(self.crate_name.clone(),
1356+
(self.crate_name(LOCAL_CRATE),
13651357
self.sess.local_crate_disambiguator())
13661358
} else {
13671359
(self.cstore.crate_name_untracked(def_id.krate),
@@ -3008,7 +3000,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
30083000
};
30093001
providers.crate_name = |tcx, id| {
30103002
assert_eq!(id, LOCAL_CRATE);
3011-
tcx.crate_name
3003+
tcx.early_crate_name(LocalCrate).unwrap()
30123004
};
30133005
providers.get_lib_features = |tcx, id| {
30143006
assert_eq!(id, LOCAL_CRATE);

src/librustc/ty/mod.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3294,8 +3294,7 @@ fn crate_disambiguator<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
32943294

32953295
fn original_crate_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
32963296
crate_num: CrateNum) -> Symbol {
3297-
assert_eq!(crate_num, LOCAL_CRATE);
3298-
tcx.crate_name.clone()
3297+
tcx.crate_name(crate_num)
32993298
}
33003299

33013300
fn crate_hash<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,

src/librustc_codegen_llvm/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ impl CodegenBackend for LlvmCodegenBackend {
275275
target_features(sess)
276276
}
277277

278-
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
278+
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync + Send> {
279279
box metadata::LlvmMetadataLoader
280280
}
281281

src/librustc_codegen_utils/codegen_backend.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ pub trait CodegenBackend {
3131
fn print_version(&self) {}
3232
fn diagnostics(&self) -> &[(&'static str, &'static str)] { &[] }
3333

34-
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync>;
34+
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync + Send>;
3535
fn provide(&self, _providers: &mut Providers<'_>);
3636
fn provide_extern(&self, _providers: &mut Providers<'_>);
3737
fn codegen_crate<'a, 'tcx>(

src/librustc_driver/lib.rs

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ pub fn run_compiler(
226226

227227
callbacks.config(&mut config);
228228

229+
let pretty_info = parse_pretty(&mut config.opts, &matches);
230+
229231
interface::run_compiler(config, |compiler| {
230232
let sess = compiler.session();
231233
let should_stop = RustcDefaultCalls::print_crate_info(
@@ -245,14 +247,9 @@ pub fn run_compiler(
245247
return sess.compile_status();
246248
}
247249

248-
let pretty_info = parse_pretty(sess, &matches);
249-
250-
compiler.parse()?;
251-
252250
if let Some((ppm, opt_uii)) = pretty_info {
253251
if ppm.needs_ast_map(&opt_uii) {
254-
pretty::visit_crate(sess, &mut compiler.parse()?.peek_mut(), ppm);
255-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
252+
compiler.enter(|tcx| {
256253
let expansion_result = tcx.expand_macros(LocalCrate)?;
257254
pretty::print_after_hir_lowering(
258255
tcx,
@@ -266,19 +263,24 @@ pub fn run_compiler(
266263
})?;
267264
return sess.compile_status();
268265
} else {
269-
let mut krate = compiler.parse()?.take();
270-
pretty::visit_crate(sess, &mut krate, ppm);
271-
pretty::print_after_parsing(
272-
sess,
273-
&compiler.input(),
274-
&krate,
275-
ppm,
276-
compiler.output_file().as_ref().map(|p| &**p),
277-
);
266+
compiler.enter(|tcx| {
267+
let krate = tcx.parse(LocalCrate)?;
268+
let krate = krate.borrow();
269+
pretty::print_after_parsing(
270+
sess,
271+
&compiler.input(),
272+
&krate,
273+
ppm,
274+
compiler.output_file().as_ref().map(|p| &**p),
275+
);
276+
Ok(())
277+
})?;
278278
return sess.compile_status();
279279
}
280280
}
281281

282+
compiler.enter(|tcx| tcx.parse(LocalCrate))?;
283+
282284
if !callbacks.after_parsing(compiler) {
283285
return sess.compile_status();
284286
}
@@ -289,15 +291,15 @@ pub fn run_compiler(
289291
return sess.compile_status();
290292
}
291293

292-
compiler.register_plugins()?;
294+
compiler.enter(|tcx| tcx.register_plugins(LocalCrate))?;
293295

294296
// Lint plugins are registered; now we can process command line flags.
295297
if sess.opts.describe_lints {
296298
describe_lints(&sess, &sess.lint_store.borrow(), true);
297299
return sess.compile_status();
298300
}
299301

300-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
302+
compiler.enter(|tcx| {
301303
tcx.prepare_outputs(LocalCrate)?;
302304
Ok(())
303305
})?;
@@ -308,7 +310,7 @@ pub fn run_compiler(
308310
return sess.compile_status();
309311
}
310312

311-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
313+
compiler.enter(|tcx| {
312314
tcx.lower_ast_to_hir(LocalCrate)?;
313315
Ok(())
314316
})?;
@@ -319,10 +321,10 @@ pub fn run_compiler(
319321
}
320322

321323
if sess.opts.debugging_opts.save_analysis {
322-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
324+
compiler.enter(|tcx| {
323325
let expansion_result = tcx.expand_macros(LocalCrate)?;
324326
let result = tcx.analysis(LOCAL_CRATE);
325-
let crate_name = &tcx.crate_name.as_str();
327+
let crate_name = &tcx.crate_name(LOCAL_CRATE).as_str();
326328

327329
time(sess, "save analysis", || {
328330
save::process_crate(
@@ -340,20 +342,20 @@ pub fn run_compiler(
340342
// (needed by the RLS)
341343
})?;
342344
} else {
343-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
345+
compiler.enter(|tcx| {
344346
// Drop AST after lowering HIR to free memory
345347
mem::drop(tcx.expand_macros(LocalCrate).unwrap().ast_crate.steal());
346348
});
347349
}
348350

349-
compiler.global_ctxt()?.peek_mut().enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
351+
compiler.enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
350352

351353
if !callbacks.after_analysis(compiler) {
352354
return sess.compile_status();
353355
}
354356

355357
if sess.opts.debugging_opts.save_analysis {
356-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
358+
compiler.enter(|tcx| {
357359
// Drop AST after lowering HIR to free memory
358360
mem::drop(tcx.expand_macros(LocalCrate).unwrap().ast_crate.steal());
359361
});
@@ -426,22 +428,22 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option
426428
}
427429
}
428430

429-
fn parse_pretty(sess: &Session,
431+
fn parse_pretty(opts: &mut config::Options,
430432
matches: &getopts::Matches)
431433
-> Option<(PpMode, Option<UserIdentifiedItem>)> {
432-
let pretty = if sess.opts.debugging_opts.unstable_options {
434+
let pretty = if opts.debugging_opts.unstable_options {
433435
matches.opt_default("pretty", "normal").map(|a| {
434436
// stable pretty-print variants only
435-
pretty::parse_pretty(sess, &a, false)
437+
pretty::parse_pretty(opts, &a, false)
436438
})
437439
} else {
438440
None
439441
};
440442

441443
if pretty.is_none() {
442-
sess.opts.debugging_opts.unpretty.as_ref().map(|a| {
444+
opts.debugging_opts.unpretty.clone().map(|a| {
443445
// extended with unstable pretty-print variants
444-
pretty::parse_pretty(sess, &a, true)
446+
pretty::parse_pretty(opts, &a, true)
445447
})
446448
} else {
447449
pretty

0 commit comments

Comments
 (0)