Skip to content

Commit 2e10944

Browse files
authored
Rollup merge of rust-lang#57570 - Xanewok:querify-some, r=Zoxc
Querify local `plugin_registrar_fn` and `proc_macro_decls_static` Instead of calculating them as part of the `Session`, we do that in the query system. It's also nice that these queries are already defined for external crates - here, we provide the queries for the local crate. r? @nikomatsakis
2 parents 2f7a226 + 707a9a0 commit 2e10944

File tree

8 files changed

+66
-41
lines changed

8 files changed

+66
-41
lines changed

src/librustc/session/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,6 @@ pub struct Session {
6969
pub parse_sess: ParseSess,
7070
/// For a library crate, this is always none
7171
pub entry_fn: Once<Option<(NodeId, Span, config::EntryFnType)>>,
72-
pub plugin_registrar_fn: Once<Option<ast::NodeId>>,
73-
pub proc_macro_decls_static: Once<Option<ast::NodeId>>,
7472
pub sysroot: PathBuf,
7573
/// The name of the root source file of the crate, in the local file system.
7674
/// `None` means that there is no source file.
@@ -1177,8 +1175,6 @@ pub fn build_session_(
11771175
parse_sess: p_s,
11781176
// For a library crate, this is always none
11791177
entry_fn: Once::new(),
1180-
plugin_registrar_fn: Once::new(),
1181-
proc_macro_decls_static: Once::new(),
11821178
sysroot,
11831179
local_crate_source_file,
11841180
working_dir,

src/librustc_codegen_ssa/back/symbol_export.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -147,14 +147,12 @@ fn reachable_non_generics_provider<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
147147
})
148148
.collect();
149149

150-
if let Some(id) = *tcx.sess.proc_macro_decls_static.get() {
151-
let def_id = tcx.hir().local_def_id(id);
152-
reachable_non_generics.insert(def_id, SymbolExportLevel::C);
150+
if let Some(id) = tcx.proc_macro_decls_static(LOCAL_CRATE) {
151+
reachable_non_generics.insert(id, SymbolExportLevel::C);
153152
}
154153

155-
if let Some(id) = *tcx.sess.plugin_registrar_fn.get() {
156-
let def_id = tcx.hir().local_def_id(id);
157-
reachable_non_generics.insert(def_id, SymbolExportLevel::C);
154+
if let Some(id) = tcx.plugin_registrar_fn(LOCAL_CRATE) {
155+
reachable_non_generics.insert(id, SymbolExportLevel::C);
158156
}
159157

160158
Lrc::new(reachable_non_generics)

src/librustc_codegen_utils/symbol_names.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -242,12 +242,12 @@ fn compute_symbol_name<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, instance: Instance
242242

243243
let node_id = tcx.hir().as_local_node_id(def_id);
244244

245-
if let Some(id) = node_id {
246-
if *tcx.sess.plugin_registrar_fn.get() == Some(id) {
245+
if def_id.is_local() {
246+
if tcx.plugin_registrar_fn(LOCAL_CRATE) == Some(def_id) {
247247
let disambiguator = tcx.sess.local_crate_disambiguator();
248248
return tcx.sess.generate_plugin_registrar_symbol(disambiguator);
249249
}
250-
if *tcx.sess.proc_macro_decls_static.get() == Some(id) {
250+
if tcx.proc_macro_decls_static(LOCAL_CRATE) == Some(def_id) {
251251
let disambiguator = tcx.sess.local_crate_disambiguator();
252252
return tcx.sess.generate_proc_macro_decls_symbol(disambiguator);
253253
}

src/librustc_driver/driver.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -1158,6 +1158,8 @@ where
11581158
}
11591159

11601160
pub fn default_provide(providers: &mut ty::query::Providers) {
1161+
proc_macro_decls::provide(providers);
1162+
plugin::build::provide(providers);
11611163
hir::provide(providers);
11621164
borrowck::provide(providers);
11631165
mir::provide(providers);
@@ -1212,13 +1214,6 @@ where
12121214
middle::entry::find_entry_point(sess, &hir_map, name)
12131215
});
12141216

1215-
sess.plugin_registrar_fn
1216-
.set(time(sess, "looking for plugin registrar", || {
1217-
plugin::build::find_plugin_registrar(sess.diagnostic(), &hir_map)
1218-
}));
1219-
sess.proc_macro_decls_static
1220-
.set(proc_macro_decls::find(&hir_map));
1221-
12221217
let mut local_providers = ty::query::Providers::default();
12231218
default_provide(&mut local_providers);
12241219
codegen_backend.provide(&mut local_providers);
@@ -1248,6 +1243,14 @@ where
12481243
// tcx available.
12491244
time(sess, "dep graph tcx init", || rustc_incremental::dep_graph_tcx_init(tcx));
12501245

1246+
time(sess, "looking for plugin registrar", || {
1247+
plugin::build::find_plugin_registrar(tcx)
1248+
});
1249+
1250+
time(sess, "looking for derive registrar", || {
1251+
proc_macro_decls::find(tcx)
1252+
});
1253+
12511254
time(sess, "loop checking", || loops::check_crate(tcx));
12521255

12531256
time(sess, "attribute checking", || {

src/librustc_driver/proc_macro_decls.rs

+21-5
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,25 @@
11
use rustc::hir::itemlikevisit::ItemLikeVisitor;
2-
use rustc::hir::map::Map;
2+
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
33
use rustc::hir;
4+
use rustc::ty::TyCtxt;
5+
use rustc::ty::query::Providers;
46
use syntax::ast;
57
use syntax::attr;
68

7-
pub fn find(hir_map: &Map) -> Option<ast::NodeId> {
8-
let krate = hir_map.krate();
9+
pub fn find<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> Option<DefId> {
10+
tcx.proc_macro_decls_static(LOCAL_CRATE)
11+
}
12+
13+
fn proc_macro_decls_static<'tcx>(
14+
tcx: TyCtxt<'_, 'tcx, 'tcx>,
15+
cnum: CrateNum,
16+
) -> Option<DefId> {
17+
assert_eq!(cnum, LOCAL_CRATE);
918

1019
let mut finder = Finder { decls: None };
11-
krate.visit_all_item_likes(&mut finder);
12-
finder.decls
20+
tcx.hir().krate().visit_all_item_likes(&mut finder);
21+
22+
finder.decls.map(|id| tcx.hir().local_def_id(id))
1323
}
1424

1525
struct Finder {
@@ -30,3 +40,9 @@ impl<'v> ItemLikeVisitor<'v> for Finder {
3040
}
3141
}
3242

43+
pub(crate) fn provide(providers: &mut Providers<'_>) {
44+
*providers = Providers {
45+
proc_macro_decls_static,
46+
..*providers
47+
};
48+
}

src/librustc_lint/builtin.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
//! a `pub fn new()`.
2020
2121
use rustc::hir::def::Def;
22-
use rustc::hir::def_id::DefId;
22+
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
2323
use rustc::ty::{self, Ty};
2424
use hir::Node;
2525
use util::nodemap::NodeSet;
@@ -860,7 +860,7 @@ impl LintPass for PluginAsLibrary {
860860

861861
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for PluginAsLibrary {
862862
fn check_item(&mut self, cx: &LateContext, it: &hir::Item) {
863-
if cx.sess().plugin_registrar_fn.get().is_some() {
863+
if cx.tcx.plugin_registrar_fn(LOCAL_CRATE).is_some() {
864864
// We're compiling a plugin; it's fine to link other plugins.
865865
return;
866866
}

src/librustc_metadata/encoder.rs

+3-6
Original file line numberDiff line numberDiff line change
@@ -482,13 +482,10 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
482482
has_global_allocator: has_global_allocator,
483483
has_panic_handler: has_panic_handler,
484484
has_default_lib_allocator: has_default_lib_allocator,
485-
plugin_registrar_fn: tcx.sess
486-
.plugin_registrar_fn
487-
.get()
488-
.map(|id| tcx.hir().local_def_id(id).index),
485+
plugin_registrar_fn: tcx.plugin_registrar_fn(LOCAL_CRATE).map(|id| id.index),
489486
proc_macro_decls_static: if is_proc_macro {
490-
let id = tcx.sess.proc_macro_decls_static.get().unwrap();
491-
Some(tcx.hir().local_def_id(id).index)
487+
let id = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap();
488+
Some(id.index)
492489
} else {
493490
None
494491
},

src/librustc_plugin/build.rs

+23-8
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@
22
33
use syntax::ast;
44
use syntax::attr;
5-
use errors;
65
use syntax_pos::Span;
7-
use rustc::hir::map::Map;
86
use rustc::hir::itemlikevisit::ItemLikeVisitor;
97
use rustc::hir;
8+
use rustc::hir::def_id::{CrateNum, DefId, LOCAL_CRATE};
9+
use rustc::ty::TyCtxt;
10+
use rustc::ty::query::Providers;
1011

1112
struct RegistrarFinder {
1213
registrars: Vec<(ast::NodeId, Span)> ,
@@ -30,21 +31,27 @@ impl<'v> ItemLikeVisitor<'v> for RegistrarFinder {
3031
}
3132

3233
/// Find the function marked with `#[plugin_registrar]`, if any.
33-
pub fn find_plugin_registrar(diagnostic: &errors::Handler,
34-
hir_map: &Map)
35-
-> Option<ast::NodeId> {
36-
let krate = hir_map.krate();
34+
pub fn find_plugin_registrar<'tcx>(tcx: TyCtxt<'_, 'tcx, 'tcx>) -> Option<DefId> {
35+
tcx.plugin_registrar_fn(LOCAL_CRATE)
36+
}
37+
38+
fn plugin_registrar_fn<'tcx>(
39+
tcx: TyCtxt<'_, 'tcx, 'tcx>,
40+
cnum: CrateNum,
41+
) -> Option<DefId> {
42+
assert_eq!(cnum, LOCAL_CRATE);
3743

3844
let mut finder = RegistrarFinder { registrars: Vec::new() };
39-
krate.visit_all_item_likes(&mut finder);
45+
tcx.hir().krate().visit_all_item_likes(&mut finder);
4046

4147
match finder.registrars.len() {
4248
0 => None,
4349
1 => {
4450
let (node_id, _) = finder.registrars.pop().unwrap();
45-
Some(node_id)
51+
Some(tcx.hir().local_def_id(node_id))
4652
},
4753
_ => {
54+
let diagnostic = tcx.sess.diagnostic();
4855
let mut e = diagnostic.struct_err("multiple plugin registration functions found");
4956
for &(_, span) in &finder.registrars {
5057
e.span_note(span, "one is here");
@@ -55,3 +62,11 @@ pub fn find_plugin_registrar(diagnostic: &errors::Handler,
5562
}
5663
}
5764
}
65+
66+
67+
pub fn provide(providers: &mut Providers<'_>) {
68+
*providers = Providers {
69+
plugin_registrar_fn,
70+
..*providers
71+
};
72+
}

0 commit comments

Comments
 (0)