Skip to content

Commit ff19a53

Browse files
committed
Querify entry_fn
1 parent 33e6df4 commit ff19a53

File tree

17 files changed

+116
-110
lines changed

17 files changed

+116
-110
lines changed

src/librustc/dep_graph/dep_node.rs

+1
Original file line numberDiff line numberDiff line change
@@ -583,6 +583,7 @@ define_dep_nodes!( <'tcx>
583583
[] CheckImplItemWellFormed(DefId),
584584
[] ReachableNonGenerics(CrateNum),
585585
[] NativeLibraries(CrateNum),
586+
[] EntryFn(CrateNum),
586587
[] PluginRegistrarFn(CrateNum),
587588
[] ProcMacroDeclsStatic(CrateNum),
588589
[input] CrateDisambiguator(CrateNum),

src/librustc/middle/dead.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ fn create_and_seed_worklist<'a, 'tcx>(
409409
}
410410
}).chain(
411411
// Seed entry point
412-
tcx.sess.entry_fn.borrow().map(|(id, _, _)| id)
412+
tcx.entry_fn(LOCAL_CRATE).map(|(def_id, _)| tcx.hir().as_local_node_id(def_id).unwrap())
413413
).collect::<Vec<_>>();
414414

415415
// Seed implemented trait items

src/librustc/middle/entry.rs

+46-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use hir::map as hir_map;
2-
use hir::def_id::{CRATE_DEF_INDEX};
2+
use hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
33
use session::{config, Session};
44
use session::config::EntryFnType;
55
use syntax::ast::NodeId;
@@ -8,6 +8,8 @@ use syntax::entry::EntryPointType;
88
use syntax_pos::Span;
99
use hir::{Item, ItemKind, ImplItem, TraitItem};
1010
use hir::itemlikevisit::ItemLikeVisitor;
11+
use ty::TyCtxt;
12+
use ty::query::Providers;
1113

1214
struct EntryContext<'a, 'tcx: 'a> {
1315
session: &'a Session,
@@ -45,36 +47,34 @@ impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
4547
}
4648
}
4749

48-
pub fn find_entry_point(session: &Session,
49-
hir_map: &hir_map::Map<'_>,
50-
crate_name: &str) {
51-
let any_exe = session.crate_types.borrow().iter().any(|ty| {
50+
fn entry_fn(tcx: TyCtxt<'_, '_, '_>, cnum: CrateNum) -> Option<(DefId, EntryFnType)> {
51+
assert_eq!(cnum, LOCAL_CRATE);
52+
53+
let any_exe = tcx.sess.crate_types.borrow().iter().any(|ty| {
5254
*ty == config::CrateType::Executable
5355
});
5456
if !any_exe {
5557
// No need to find a main function
56-
session.entry_fn.set(None);
57-
return
58+
return None;
5859
}
5960

6061
// If the user wants no main function at all, then stop here.
61-
if attr::contains_name(&hir_map.krate().attrs, "no_main") {
62-
session.entry_fn.set(None);
63-
return
62+
if attr::contains_name(&tcx.hir().krate().attrs, "no_main") {
63+
return None;
6464
}
6565

6666
let mut ctxt = EntryContext {
67-
session,
68-
map: hir_map,
67+
session: tcx.sess,
68+
map: tcx.hir(),
6969
main_fn: None,
7070
attr_main_fn: None,
7171
start_fn: None,
7272
non_main_fns: Vec::new(),
7373
};
7474

75-
hir_map.krate().visit_all_item_likes(&mut ctxt);
75+
tcx.hir().krate().visit_all_item_likes(&mut ctxt);
7676

77-
configure_main(&mut ctxt, crate_name);
77+
configure_main(tcx, &ctxt)
7878
}
7979

8080
// Beware, this is duplicated in `libsyntax/entry.rs`, so make sure to keep
@@ -135,43 +135,58 @@ fn find_item(item: &Item, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
135135
.span_label(item.span, "multiple `start` functions")
136136
.emit();
137137
}
138-
},
139-
EntryPointType::None => ()
138+
}
139+
EntryPointType::None => (),
140140
}
141141
}
142142

143-
fn configure_main(this: &mut EntryContext<'_, '_>, crate_name: &str) {
144-
if let Some((node_id, span)) = this.start_fn {
145-
this.session.entry_fn.set(Some((node_id, span, EntryFnType::Start)));
146-
} else if let Some((node_id, span)) = this.attr_main_fn {
147-
this.session.entry_fn.set(Some((node_id, span, EntryFnType::Main)));
148-
} else if let Some((node_id, span)) = this.main_fn {
149-
this.session.entry_fn.set(Some((node_id, span, EntryFnType::Main)));
143+
fn configure_main(
144+
tcx: TyCtxt<'_, '_, '_>,
145+
visitor: &EntryContext<'_, '_>,
146+
) -> Option<(DefId, EntryFnType)> {
147+
if let Some((node_id, _)) = visitor.start_fn {
148+
Some((tcx.hir().local_def_id(node_id), EntryFnType::Start))
149+
} else if let Some((node_id, _)) = visitor.attr_main_fn {
150+
Some((tcx.hir().local_def_id(node_id), EntryFnType::Main))
151+
} else if let Some((node_id, _)) = visitor.main_fn {
152+
Some((tcx.hir().local_def_id(node_id), EntryFnType::Main))
150153
} else {
151154
// No main function
152-
this.session.entry_fn.set(None);
153-
let mut err = struct_err!(this.session, E0601,
154-
"`main` function not found in crate `{}`", crate_name);
155-
if !this.non_main_fns.is_empty() {
155+
let mut err = struct_err!(tcx.sess, E0601,
156+
"`main` function not found in crate `{}`", tcx.crate_name(LOCAL_CRATE));
157+
if !visitor.non_main_fns.is_empty() {
156158
// There were some functions named 'main' though. Try to give the user a hint.
157159
err.note("the main function must be defined at the crate level \
158160
but you have one or more functions named 'main' that are not \
159161
defined at the crate level. Either move the definition or \
160162
attach the `#[main]` attribute to override this behavior.");
161-
for &(_, span) in &this.non_main_fns {
163+
for &(_, span) in &visitor.non_main_fns {
162164
err.span_note(span, "here is a function named 'main'");
163165
}
164166
err.emit();
165-
this.session.abort_if_errors();
167+
tcx.sess.abort_if_errors();
166168
} else {
167-
if let Some(ref filename) = this.session.local_crate_source_file {
169+
if let Some(ref filename) = tcx.sess.local_crate_source_file {
168170
err.note(&format!("consider adding a `main` function to `{}`", filename.display()));
169171
}
170-
if this.session.teach(&err.get_code().unwrap()) {
172+
if tcx.sess.teach(&err.get_code().unwrap()) {
171173
err.note("If you don't know the basics of Rust, you can go look to the Rust Book \
172174
to get started: https://doc.rust-lang.org/book/");
173175
}
174176
err.emit();
175177
}
178+
179+
None
176180
}
177181
}
182+
183+
pub fn find_entry_point(tcx: TyCtxt<'_, '_, '_>) -> Option<(DefId, EntryFnType)> {
184+
tcx.entry_fn(LOCAL_CRATE)
185+
}
186+
187+
pub fn provide(providers: &mut Providers<'_>) {
188+
*providers = Providers {
189+
entry_fn,
190+
..*providers
191+
};
192+
}

src/librustc/session/config.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -649,15 +649,15 @@ impl Options {
649649
}
650650
}
651651

652-
// The type of entry function, so
653-
// users can have their own entry
654-
// functions
655-
#[derive(Copy, Clone, PartialEq)]
652+
// The type of entry function, so users can have their own entry functions
653+
#[derive(Copy, Clone, PartialEq, Hash, Debug)]
656654
pub enum EntryFnType {
657655
Main,
658656
Start,
659657
}
660658

659+
impl_stable_hash_via_hash!(EntryFnType);
660+
661661
#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug)]
662662
pub enum CrateType {
663663
Executable,

src/librustc/session/mod.rs

-4
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,6 @@ pub struct Session {
6767
/// This is `None` if the host and target are the same.
6868
pub target_tlib_path: Option<SearchPath>,
6969
pub parse_sess: ParseSess,
70-
/// For a library crate, this is always none
71-
pub entry_fn: Once<Option<(NodeId, Span, config::EntryFnType)>>,
7270
pub sysroot: PathBuf,
7371
/// The name of the root source file of the crate, in the local file system.
7472
/// `None` means that there is no source file.
@@ -1173,8 +1171,6 @@ pub fn build_session_(
11731171
host_tlib_path,
11741172
target_tlib_path,
11751173
parse_sess: p_s,
1176-
// For a library crate, this is always none
1177-
entry_fn: Once::new(),
11781174
sysroot,
11791175
local_crate_source_file,
11801176
working_dir,

src/librustc/ty/query/config.rs

+6
Original file line numberDiff line numberDiff line change
@@ -629,6 +629,12 @@ impl<'tcx> QueryDescription<'tcx> for queries::foreign_modules<'tcx> {
629629
}
630630
}
631631

632+
impl<'tcx> QueryDescription<'tcx> for queries::entry_fn<'tcx> {
633+
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
634+
"looking up the entry function of a crate".into()
635+
}
636+
}
637+
632638
impl<'tcx> QueryDescription<'tcx> for queries::plugin_registrar_fn<'tcx> {
633639
fn describe(_tcx: TyCtxt<'_, '_, '_>, _: CrateNum) -> Cow<'static, str> {
634640
"looking up the plugin registrar for a crate".into()

src/librustc/ty/query/mod.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use mir::mono::CodegenUnit;
2222
use mir;
2323
use mir::interpret::GlobalId;
2424
use session::{CompileResult, CrateDisambiguator};
25-
use session::config::OutputFilenames;
25+
use session::config::{EntryFnType, OutputFilenames};
2626
use traits::{self, Vtable};
2727
use traits::query::{
2828
CanonicalPredicateGoal, CanonicalProjectionGoal,
@@ -476,6 +476,8 @@ define_queries! { <'tcx>
476476

477477
[] fn foreign_modules: ForeignModules(CrateNum) -> Lrc<Vec<ForeignModule>>,
478478

479+
// For a library crate, this is always none
480+
[] fn entry_fn: EntryFn(CrateNum) -> Option<(DefId, EntryFnType)>,
479481
[] fn plugin_registrar_fn: PluginRegistrarFn(CrateNum) -> Option<DefId>,
480482
[] fn proc_macro_decls_static: ProcMacroDeclsStatic(CrateNum) -> Option<DefId>,
481483
[] fn crate_disambiguator: CrateDisambiguator(CrateNum) -> CrateDisambiguator,

src/librustc/ty/query/plumbing.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1362,6 +1362,7 @@ pub fn force_from_dep_node<'a, 'gcx, 'lcx>(tcx: TyCtxt<'a, 'gcx, 'lcx>,
13621362
DepKind::CheckImplItemWellFormed => { force!(check_impl_item_well_formed, def_id!()); }
13631363
DepKind::ReachableNonGenerics => { force!(reachable_non_generics, krate!()); }
13641364
DepKind::NativeLibraries => { force!(native_libraries, krate!()); }
1365+
DepKind::EntryFn => { force!(entry_fn, krate!()); }
13651366
DepKind::PluginRegistrarFn => { force!(plugin_registrar_fn, krate!()); }
13661367
DepKind::ProcMacroDeclsStatic => { force!(proc_macro_decls_static, krate!()); }
13671368
DepKind::CrateDisambiguator => { force!(crate_disambiguator, krate!()); }

src/librustc_codegen_llvm/debuginfo/mod.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use llvm;
1414
use llvm::debuginfo::{DIFile, DIType, DIScope, DIBuilder, DISubprogram, DIArray, DIFlags,
1515
DILexicalBlock};
1616
use rustc::hir::CodegenFnAttrFlags;
17-
use rustc::hir::def_id::{DefId, CrateNum};
17+
use rustc::hir::def_id::{DefId, CrateNum, LOCAL_CRATE};
1818
use rustc::ty::subst::{Substs, UnpackedKind};
1919

2020
use abi::Abi;
@@ -290,9 +290,8 @@ impl DebugInfoMethods<'tcx> for CodegenCx<'ll, 'tcx> {
290290

291291
let mut flags = DIFlags::FlagPrototyped;
292292

293-
let local_id = self.tcx().hir().as_local_node_id(def_id);
294-
if let Some((id, _, _)) = *self.sess().entry_fn.borrow() {
295-
if local_id == Some(id) {
293+
if let Some((id, _)) = self.tcx.entry_fn(LOCAL_CRATE) {
294+
if id == def_id {
296295
flags |= DIFlags::FlagMainSubprogram;
297296
}
298297
}

src/librustc_codegen_ssa/back/symbol_export.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ fn exported_symbols_provider_local<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
194194
})
195195
.collect();
196196

197-
if tcx.sess.entry_fn.borrow().is_some() {
197+
if tcx.entry_fn(LOCAL_CRATE).is_some() {
198198
let exported_symbol = ExportedSymbol::NoDefId(SymbolName::new("main"));
199199

200200
symbols.push((exported_symbol, SymbolExportLevel::C));

src/librustc_codegen_ssa/base.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,8 @@ pub fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
441441
pub fn maybe_create_entry_wrapper<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
442442
cx: &'a Bx::CodegenCx
443443
) {
444-
let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
445-
Some((id, span, _)) => {
446-
(cx.tcx().hir().local_def_id(id), span)
447-
}
444+
let (main_def_id, span) = match cx.tcx().entry_fn(LOCAL_CRATE) {
445+
Some((def_id, _)) => { (def_id, cx.tcx().def_span(def_id)) },
448446
None => return,
449447
};
450448

@@ -458,7 +456,7 @@ pub fn maybe_create_entry_wrapper<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
458456

459457
let main_llfn = cx.get_fn(instance);
460458

461-
let et = cx.sess().entry_fn.get().map(|e| e.2);
459+
let et = cx.tcx().entry_fn(LOCAL_CRATE).map(|e| e.1);
462460
match et {
463461
Some(EntryFnType::Main) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, true),
464462
Some(EntryFnType::Start) => create_entry_fn::<Bx>(cx, span, main_llfn, main_def_id, false),

src/librustc_codegen_utils/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ extern crate syntax_pos;
3131
#[macro_use] extern crate rustc_data_structures;
3232

3333
use rustc::ty::TyCtxt;
34+
use rustc::hir::def_id::LOCAL_CRATE;
3435

3536
pub mod link;
3637
pub mod codegen_backend;
@@ -42,11 +43,9 @@ pub mod symbol_names_test;
4243
/// that actually test that compilation succeeds without
4344
/// reporting an error.
4445
pub fn check_for_rustc_errors_attr(tcx: TyCtxt) {
45-
if let Some((id, span, _)) = *tcx.sess.entry_fn.borrow() {
46-
let main_def_id = tcx.hir().local_def_id(id);
47-
48-
if tcx.has_attr(main_def_id, "rustc_error") {
49-
tcx.sess.span_fatal(span, "compilation successful");
46+
if let Some((def_id, _)) = tcx.entry_fn(LOCAL_CRATE) {
47+
if tcx.has_attr(def_id, "rustc_error") {
48+
tcx.sess.span_fatal(tcx.def_span(def_id), "compilation successful");
5049
}
5150
}
5251
}

src/librustc_driver/driver.rs

+5-4
Original file line numberDiff line numberDiff line change
@@ -1174,6 +1174,7 @@ pub fn default_provide(providers: &mut ty::query::Providers) {
11741174
rustc_passes::provide(providers);
11751175
rustc_traits::provide(providers);
11761176
middle::region::provide(providers);
1177+
middle::entry::provide(providers);
11771178
cstore::provide(providers);
11781179
lint::provide(providers);
11791180
}
@@ -1210,10 +1211,6 @@ where
12101211
rustc_incremental::load_query_result_cache(sess)
12111212
});
12121213

1213-
time(sess, "looking for entry point", || {
1214-
middle::entry::find_entry_point(sess, &hir_map, name)
1215-
});
1216-
12171214
let mut local_providers = ty::query::Providers::default();
12181215
default_provide(&mut local_providers);
12191216
codegen_backend.provide(&mut local_providers);
@@ -1243,6 +1240,10 @@ where
12431240
// tcx available.
12441241
time(sess, "dep graph tcx init", || rustc_incremental::dep_graph_tcx_init(tcx));
12451242

1243+
time(sess, "looking for entry point", || {
1244+
middle::entry::find_entry_point(tcx)
1245+
});
1246+
12461247
time(sess, "looking for plugin registrar", || {
12471248
plugin::build::find_plugin_registrar(tcx)
12481249
});

0 commit comments

Comments
 (0)