Skip to content

Commit 391fc05

Browse files
committed
Turn parsing into a query
1 parent 65506bd commit 391fc05

File tree

14 files changed

+167
-160
lines changed

14 files changed

+167
-160
lines changed

src/librustc/query/mod.rs

+23
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,28 @@ use syntax_pos::symbol::InternedString;
3131
// as they will raise an fatal error on query cycles instead.
3232
rustc_queries! {
3333
Other {
34+
query parse(_: ()) -> Result<Lrc<Steal<ast::Crate>>, ErrorReported> {
35+
no_hash
36+
eval_always
37+
desc { "parsing crate" }
38+
}
39+
40+
query register_plugins(
41+
_: ()
42+
) -> Result<Lrc<Steal<(ast::Crate, ty::PluginInfo)>>, ErrorReported> {
43+
no_hash
44+
eval_always
45+
desc { "registering plugins" }
46+
}
47+
48+
/// The definite name of the current crate after taking into account
49+
/// attributes, commandline parameters, etc.
50+
query early_crate_name(_: ()) -> Result<Symbol, ErrorReported> {
51+
no_hash
52+
eval_always
53+
desc { "finding the crate name" }
54+
}
55+
3456
query expand_macros(_: ()) -> Result<Lrc<ty::ExpansionResult>, ErrorReported> {
3557
no_hash
3658
eval_always
@@ -771,6 +793,7 @@ rustc_queries! {
771793
eval_always
772794
desc { "looking up the hash a crate" }
773795
}
796+
// FIXME: Remove this as it's the same as `crate_name`
774797
query original_crate_name(_: CrateNum) -> Symbol {
775798
eval_always
776799
desc { "looking up the original name a crate" }

src/librustc/session/config.rs

+5
Original file line numberDiff line numberDiff line change
@@ -426,6 +426,9 @@ top_level_options!(
426426
// try to not rely on this too much.
427427
actually_rustdoc: bool [TRACKED],
428428

429+
// Replaces bodies with loops
430+
everybody_loops: bool [TRACKED],
431+
429432
// Specifications of codegen units / ThinLTO which are forced as a
430433
// result of parsing command line options. These are not necessarily
431434
// what rustc was invoked with, but massaged a bit to agree with
@@ -630,6 +633,7 @@ impl Default for Options {
630633
unstable_features: UnstableFeatures::Disallow,
631634
debug_assertions: true,
632635
actually_rustdoc: false,
636+
everybody_loops: false,
633637
cli_forced_codegen_units: None,
634638
cli_forced_thinlto_off: false,
635639
remap_path_prefix: Vec::new(),
@@ -2449,6 +2453,7 @@ pub fn build_session_options_and_crate_config(
24492453
unstable_features: UnstableFeatures::from_environment(),
24502454
debug_assertions,
24512455
actually_rustdoc: false,
2456+
everybody_loops: false,
24522457
cli_forced_codegen_units: codegen_units,
24532458
cli_forced_thinlto_off: disable_thinlto,
24542459
remap_path_prefix,

src/librustc/ty/context.rs

+9-17
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ use rustc_data_structures::stable_hasher::{HashStable, hash_stable_hashmap,
5353
StableVec};
5454
use arena::SyncDroplessArena;
5555
use rustc_data_structures::indexed_vec::{Idx, IndexVec};
56-
use rustc_data_structures::sync::{Lrc, Lock, WorkerLocal, AtomicOnce, Once, OneThread};
56+
use rustc_data_structures::sync::{self, Lrc, Lock, WorkerLocal, AtomicOnce, Once};
5757
use std::any::Any;
5858
use std::borrow::Borrow;
5959
use std::cmp::Ordering;
@@ -1011,13 +1011,10 @@ pub struct GlobalCtxt<'tcx> {
10111011

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

10161016
pub sess_rc: Lrc<Session>,
10171017

1018-
/// The AST after registering plugins.
1019-
pub ast_crate: Steal<(ast::Crate, ty::PluginInfo)>,
1020-
10211018
lowered_hir: AtomicOnce<&'tcx hir::LoweredHir>,
10221019
hir_map: AtomicOnce<&'tcx hir_map::Map<'tcx>>,
10231020

@@ -1037,9 +1034,7 @@ pub struct GlobalCtxt<'tcx> {
10371034
/// Merge this with `selection_cache`?
10381035
pub evaluation_cache: traits::EvaluationCache<'tcx>,
10391036

1040-
/// The definite name of the current crate after taking into account
1041-
/// attributes, commandline parameters, etc.
1042-
pub crate_name: Symbol,
1037+
pub crate_name_override: Option<String>,
10431038

10441039
/// Data layout specification for the current target.
10451040
pub data_layout: TargetDataLayout,
@@ -1179,15 +1174,13 @@ impl<'tcx> TyCtxt<'tcx> {
11791174
pub fn create_global_ctxt(
11801175
s: &'tcx Lrc<Session>,
11811176
cstore: &'tcx CrateStoreDyn,
1182-
cstore_rc: Box<dyn Any>,
1177+
cstore_rc: &'tcx (dyn Any + sync::Sync),
11831178
local_providers: ty::query::Providers<'tcx>,
11841179
extern_providers: ty::query::Providers<'tcx>,
11851180
arenas: &'tcx AllArenas,
11861181
dep_graph: DepGraph,
1187-
ast_crate: ast::Crate,
1188-
plugin_info: ty::PluginInfo,
11891182
on_disk_query_result_cache: query::OnDiskCache<'tcx>,
1190-
crate_name: &str,
1183+
crate_name: Option<String>,
11911184
tx: mpsc::Sender<Box<dyn Any + Send>>,
11921185
io: InputsAndOutputs,
11931186
) -> GlobalCtxt<'tcx> {
@@ -1212,15 +1205,14 @@ impl<'tcx> TyCtxt<'tcx> {
12121205
sess: &**s,
12131206
arena: WorkerLocal::new(|_| Arena::default()),
12141207
cstore,
1215-
cstore_rc: OneThread::new(Steal::new(cstore_rc)),
1208+
cstore_rc,
12161209
sess_rc: s.clone(),
12171210
interners,
12181211
dep_graph,
12191212
common,
12201213
types: common_types,
12211214
lifetimes: common_lifetimes,
12221215
consts: common_consts,
1223-
ast_crate: Steal::new((ast_crate, plugin_info)),
12241216
lowered_hir: AtomicOnce::new(),
12251217
hir_map: AtomicOnce::new(),
12261218
metadata_dep_nodes: Once::new(),
@@ -1232,7 +1224,7 @@ impl<'tcx> TyCtxt<'tcx> {
12321224
rcache: Default::default(),
12331225
selection_cache: Default::default(),
12341226
evaluation_cache: Default::default(),
1235-
crate_name: Symbol::intern(crate_name),
1227+
crate_name_override: crate_name,
12361228
data_layout,
12371229
layout_interner: Default::default(),
12381230
stability_interner: Default::default(),
@@ -1351,7 +1343,7 @@ impl<'tcx> TyCtxt<'tcx> {
13511343
// statements within the query system and we'd run into endless
13521344
// recursion otherwise.
13531345
let (crate_name, crate_disambiguator) = if def_id.is_local() {
1354-
(self.crate_name.clone(),
1346+
(self.crate_name(LOCAL_CRATE),
13551347
self.sess.local_crate_disambiguator())
13561348
} else {
13571349
(self.cstore.crate_name_untracked(def_id.krate),
@@ -2874,7 +2866,7 @@ pub fn provide(providers: &mut ty::query::Providers<'_>) {
28742866
};
28752867
providers.crate_name = |tcx, id| {
28762868
assert_eq!(id, LOCAL_CRATE);
2877-
tcx.crate_name
2869+
tcx.early_crate_name(()).unwrap()
28782870
};
28792871
providers.get_lib_features = |tcx, id| {
28802872
assert_eq!(id, LOCAL_CRATE);

src/librustc/ty/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3279,8 +3279,7 @@ fn crate_disambiguator(tcx: TyCtxt<'_>, crate_num: CrateNum) -> CrateDisambiguat
32793279
}
32803280

32813281
fn original_crate_name(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Symbol {
3282-
assert_eq!(crate_num, LOCAL_CRATE);
3283-
tcx.crate_name.clone()
3282+
tcx.crate_name(crate_num)
32843283
}
32853284

32863285
fn crate_hash(tcx: TyCtxt<'_>, crate_num: CrateNum) -> Svh {

src/librustc_codegen_llvm/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ impl CodegenBackend for LlvmCodegenBackend {
265265
target_features(sess)
266266
}
267267

268-
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync> {
268+
fn metadata_loader(&self) -> Box<dyn MetadataLoader + Sync + Send> {
269269
box metadata::LlvmMetadataLoader
270270
}
271271

src/librustc_codegen_utils/codegen_backend.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ pub trait CodegenBackend {
3030
fn print_version(&self) {}
3131
fn diagnostics(&self) -> &[(&'static str, &'static str)] { &[] }
3232

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

src/librustc_driver/lib.rs

+30-28
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ pub fn run_compiler(
241241

242242
callbacks.config(&mut config);
243243

244+
let pretty_info = parse_pretty(&mut config.opts, &matches);
245+
244246
interface::run_compiler(config, |compiler| {
245247
let sess = compiler.session();
246248
let should_stop = RustcDefaultCalls::print_crate_info(
@@ -260,14 +262,9 @@ pub fn run_compiler(
260262
return sess.compile_status();
261263
}
262264

263-
let pretty_info = parse_pretty(sess, &matches);
264-
265-
compiler.parse()?;
266-
267265
if let Some((ppm, opt_uii)) = pretty_info {
268266
if ppm.needs_ast_map(&opt_uii) {
269-
pretty::visit_crate(sess, &mut compiler.parse()?.peek_mut(), ppm);
270-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
267+
compiler.enter(|tcx| {
271268
let expansion_result = tcx.expand_macros(())?;
272269
pretty::print_after_hir_lowering(
273270
tcx,
@@ -281,19 +278,24 @@ pub fn run_compiler(
281278
})?;
282279
return sess.compile_status();
283280
} else {
284-
let mut krate = compiler.parse()?.take();
285-
pretty::visit_crate(sess, &mut krate, ppm);
286-
pretty::print_after_parsing(
287-
sess,
288-
&compiler.input(),
289-
&krate,
290-
ppm,
291-
compiler.output_file().as_ref().map(|p| &**p),
292-
);
281+
compiler.enter(|tcx| {
282+
let krate = tcx.parse(())?;
283+
let krate = krate.borrow();
284+
pretty::print_after_parsing(
285+
sess,
286+
&compiler.input(),
287+
&krate,
288+
ppm,
289+
compiler.output_file().as_ref().map(|p| &**p),
290+
);
291+
Ok(())
292+
})?;
293293
return sess.compile_status();
294294
}
295295
}
296296

297+
compiler.enter(|tcx| tcx.parse(()))?;
298+
297299
if !callbacks.after_parsing(compiler) {
298300
return sess.compile_status();
299301
}
@@ -304,15 +306,15 @@ pub fn run_compiler(
304306
return sess.compile_status();
305307
}
306308

307-
compiler.register_plugins()?;
309+
compiler.enter(|tcx| tcx.register_plugins(()))?;
308310

309311
// Lint plugins are registered; now we can process command line flags.
310312
if sess.opts.describe_lints {
311313
describe_lints(&sess, &sess.lint_store.borrow(), true);
312314
return sess.compile_status();
313315
}
314316

315-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
317+
compiler.enter(|tcx| {
316318
tcx.prepare_outputs(())?;
317319
Ok(())
318320
})?;
@@ -323,7 +325,7 @@ pub fn run_compiler(
323325
return sess.compile_status();
324326
}
325327

326-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
328+
compiler.enter(|tcx| {
327329
tcx.lower_ast_to_hir(())?;
328330
Ok(())
329331
})?;
@@ -334,10 +336,10 @@ pub fn run_compiler(
334336
}
335337

336338
if sess.opts.debugging_opts.save_analysis {
337-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
339+
compiler.enter(|tcx| {
338340
let expansion_result = tcx.expand_macros(())?;
339341
let result = tcx.analysis(LOCAL_CRATE);
340-
let crate_name = &tcx.crate_name.as_str();
342+
let crate_name = &tcx.crate_name(LOCAL_CRATE).as_str();
341343

342344
time(sess, "save analysis", || {
343345
save::process_crate(
@@ -355,20 +357,20 @@ pub fn run_compiler(
355357
// (needed by the RLS)
356358
})?;
357359
} else {
358-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
360+
compiler.enter(|tcx| {
359361
// Drop AST after lowering HIR to free memory
360362
mem::drop(tcx.expand_macros(()).unwrap().ast_crate.steal());
361363
});
362364
}
363365

364-
compiler.global_ctxt()?.peek_mut().enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
366+
compiler.enter(|tcx| tcx.analysis(LOCAL_CRATE))?;
365367

366368
if !callbacks.after_analysis(compiler) {
367369
return sess.compile_status();
368370
}
369371

370372
if sess.opts.debugging_opts.save_analysis {
371-
compiler.global_ctxt()?.peek_mut().enter(|tcx| {
373+
compiler.enter(|tcx| {
372374
// Drop AST after lowering HIR to free memory
373375
mem::drop(tcx.expand_macros(()).unwrap().ast_crate.steal());
374376
});
@@ -441,22 +443,22 @@ fn make_input(free_matches: &[String]) -> Option<(Input, Option<PathBuf>, Option
441443
}
442444
}
443445

444-
fn parse_pretty(sess: &Session,
446+
fn parse_pretty(opts: &mut config::Options,
445447
matches: &getopts::Matches)
446448
-> Option<(PpMode, Option<UserIdentifiedItem>)> {
447-
let pretty = if sess.opts.debugging_opts.unstable_options {
449+
let pretty = if opts.debugging_opts.unstable_options {
448450
matches.opt_default("pretty", "normal").map(|a| {
449451
// stable pretty-print variants only
450-
pretty::parse_pretty(sess, &a, false)
452+
pretty::parse_pretty(opts, &a, false)
451453
})
452454
} else {
453455
None
454456
};
455457

456458
if pretty.is_none() {
457-
sess.opts.debugging_opts.unpretty.as_ref().map(|a| {
459+
opts.debugging_opts.unpretty.clone().map(|a| {
458460
// extended with unstable pretty-print variants
459-
pretty::parse_pretty(sess, &a, true)
461+
pretty::parse_pretty(opts, &a, true)
460462
})
461463
} else {
462464
pretty

0 commit comments

Comments
 (0)