Skip to content

Commit 27adb31

Browse files
committed
Combine Session.entry_fn and Session.entry_type and make them thread-safe
1 parent 7aa7198 commit 27adb31

File tree

11 files changed

+31
-38
lines changed

11 files changed

+31
-38
lines changed

src/librustc/middle/dead.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ fn create_and_seed_worklist<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
408408
}
409409

410410
// Seed entry point
411-
if let Some((id, _)) = *tcx.sess.entry_fn.borrow() {
411+
if let Some((id, _, _)) = *tcx.sess.entry_fn.borrow() {
412412
worklist.push(id);
413413
}
414414

src/librustc/middle/entry.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -63,12 +63,13 @@ pub fn find_entry_point(session: &Session,
6363
});
6464
if !any_exe {
6565
// No need to find a main function
66+
session.entry_fn.set(None);
6667
return
6768
}
6869

6970
// If the user wants no main function at all, then stop here.
7071
if attr::contains_name(&hir_map.krate().attrs, "no_main") {
71-
session.entry_type.set(Some(config::EntryNone));
72+
session.entry_fn.set(None);
7273
return
7374
}
7475

@@ -153,17 +154,15 @@ fn find_item(item: &Item, ctxt: &mut EntryContext, at_root: bool) {
153154
}
154155

155156
fn configure_main(this: &mut EntryContext, crate_name: &str) {
156-
if this.start_fn.is_some() {
157-
*this.session.entry_fn.borrow_mut() = this.start_fn;
158-
this.session.entry_type.set(Some(config::EntryStart));
159-
} else if this.attr_main_fn.is_some() {
160-
*this.session.entry_fn.borrow_mut() = this.attr_main_fn;
161-
this.session.entry_type.set(Some(config::EntryMain));
162-
} else if this.main_fn.is_some() {
163-
*this.session.entry_fn.borrow_mut() = this.main_fn;
164-
this.session.entry_type.set(Some(config::EntryMain));
157+
if let Some((node_id, span)) = this.start_fn {
158+
this.session.entry_fn.set(Some((node_id, span, config::EntryStart)));
159+
} else if let Some((node_id, span)) = this.attr_main_fn {
160+
this.session.entry_fn.set(Some((node_id, span, config::EntryMain)));
161+
} else if let Some((node_id, span)) = this.main_fn {
162+
this.session.entry_fn.set(Some((node_id, span, config::EntryMain)));
165163
} else {
166164
// No main function
165+
this.session.entry_fn.set(None);
167166
let mut err = struct_err!(this.session, E0601,
168167
"`main` function not found in crate `{}`", crate_name);
169168
if !this.non_main_fns.is_empty() {

src/librustc/session/config.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -614,13 +614,11 @@ impl Options {
614614

615615
// The type of entry function, so
616616
// users can have their own entry
617-
// functions that don't start a
618-
// scheduler
617+
// functions
619618
#[derive(Copy, Clone, PartialEq)]
620619
pub enum EntryFnType {
621620
EntryMain,
622621
EntryStart,
623-
EntryNone,
624622
}
625623

626624
#[derive(Copy, PartialEq, PartialOrd, Clone, Ord, Eq, Hash, Debug)]

src/librustc/session/mod.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,7 @@ pub struct Session {
7070
pub opts: config::Options,
7171
pub parse_sess: ParseSess,
7272
/// For a library crate, this is always none
73-
pub entry_fn: RefCell<Option<(NodeId, Span)>>,
74-
pub entry_type: Cell<Option<config::EntryFnType>>,
73+
pub entry_fn: Once<Option<(NodeId, Span, config::EntryFnType)>>,
7574
pub plugin_registrar_fn: Cell<Option<ast::NodeId>>,
7675
pub derive_registrar_fn: Cell<Option<ast::NodeId>>,
7776
pub default_sysroot: Option<PathBuf>,
@@ -1094,8 +1093,7 @@ pub fn build_session_(
10941093
opts: sopts,
10951094
parse_sess: p_s,
10961095
// For a library crate, this is always none
1097-
entry_fn: RefCell::new(None),
1098-
entry_type: Cell::new(None),
1096+
entry_fn: Once::new(),
10991097
plugin_registrar_fn: Cell::new(None),
11001098
derive_registrar_fn: Cell::new(None),
11011099
default_sysroot,

src/librustc_mir/monomorphize/collector.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ fn collect_roots<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
325325
let mut roots = Vec::new();
326326

327327
{
328-
let entry_fn = tcx.sess.entry_fn.borrow().map(|(node_id, _)| {
328+
let entry_fn = tcx.sess.entry_fn.borrow().map(|(node_id, _, _)| {
329329
tcx.hir.local_def_id(node_id)
330330
});
331331

@@ -1038,7 +1038,7 @@ impl<'b, 'a, 'v> RootCollector<'b, 'a, 'v> {
10381038
/// the return type of `main`. This is not needed when
10391039
/// the user writes their own `start` manually.
10401040
fn push_extra_entry_roots(&mut self) {
1041-
if self.tcx.sess.entry_type.get() != Some(config::EntryMain) {
1041+
if self.tcx.sess.entry_fn.get().map(|e| e.2) != Some(config::EntryMain) {
10421042
return
10431043
}
10441044

src/librustc_mir/monomorphize/item.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub trait MonoItemExt<'a, 'tcx>: fmt::Debug {
9292
match *self.as_mono_item() {
9393
MonoItem::Fn(ref instance) => {
9494
let entry_def_id =
95-
tcx.sess.entry_fn.borrow().map(|(id, _)| tcx.hir.local_def_id(id));
95+
tcx.sess.entry_fn.borrow().map(|(id, _, _)| tcx.hir.local_def_id(id));
9696
// If this function isn't inlined or otherwise has explicit
9797
// linkage, then we'll be creating a globally shared version.
9898
if self.explicit_linkage(tcx).is_some() ||

src/librustc_trans/base.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -517,7 +517,7 @@ pub fn set_link_section(cx: &CodegenCx,
517517
/// users main function.
518518
fn maybe_create_entry_wrapper(cx: &CodegenCx) {
519519
let (main_def_id, span) = match *cx.sess().entry_fn.borrow() {
520-
Some((id, span)) => {
520+
Some((id, span, _)) => {
521521
(cx.tcx.hir.local_def_id(id), span)
522522
}
523523
None => return,
@@ -533,11 +533,11 @@ fn maybe_create_entry_wrapper(cx: &CodegenCx) {
533533

534534
let main_llfn = callee::get_fn(cx, instance);
535535

536-
let et = cx.sess().entry_type.get().unwrap();
536+
let et = cx.sess().entry_fn.get().map(|e| e.2);
537537
match et {
538-
config::EntryMain => create_entry_fn(cx, span, main_llfn, main_def_id, true),
539-
config::EntryStart => create_entry_fn(cx, span, main_llfn, main_def_id, false),
540-
config::EntryNone => {} // Do nothing.
538+
Some(config::EntryMain) => create_entry_fn(cx, span, main_llfn, main_def_id, true),
539+
Some(config::EntryStart) => create_entry_fn(cx, span, main_llfn, main_def_id, false),
540+
None => {} // Do nothing.
541541
}
542542

543543
fn create_entry_fn<'cx>(cx: &'cx CodegenCx,

src/librustc_trans/debuginfo/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ pub fn create_function_debug_context<'a, 'tcx>(cx: &CodegenCx<'a, 'tcx>,
263263

264264
let local_id = cx.tcx.hir.as_local_node_id(def_id);
265265
match *cx.sess().entry_fn.borrow() {
266-
Some((id, _)) => {
266+
Some((id, _, _)) => {
267267
if local_id == Some(id) {
268268
flags = flags | DIFlags::FlagMainSubprogram;
269269
}

src/librustc_trans_utils/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ pub mod symbol_names_test;
5252
/// that actually test that compilation succeeds without
5353
/// reporting an error.
5454
pub fn check_for_rustc_errors_attr(tcx: TyCtxt) {
55-
if let Some((id, span)) = *tcx.sess.entry_fn.borrow() {
55+
if let Some((id, span, _)) = *tcx.sess.entry_fn.borrow() {
5656
let main_def_id = tcx.hir.local_def_id(id);
5757

5858
if tcx.has_attr(main_def_id, "rustc_error") {

src/librustc_typeck/check/mod.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1128,10 +1128,10 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
11281128

11291129
// Check that the main return type implements the termination trait.
11301130
if let Some(term_id) = fcx.tcx.lang_items().termination() {
1131-
if let Some((id, _)) = *fcx.tcx.sess.entry_fn.borrow() {
1131+
if let Some((id, _, entry_type)) = *fcx.tcx.sess.entry_fn.borrow() {
11321132
if id == fn_id {
1133-
match fcx.sess().entry_type.get() {
1134-
Some(config::EntryMain) => {
1133+
match entry_type {
1134+
config::EntryMain => {
11351135
let substs = fcx.tcx.mk_substs(iter::once(Kind::from(ret_ty)));
11361136
let trait_ref = ty::TraitRef::new(term_id, substs);
11371137
let return_ty_span = decl.output.span();
@@ -1142,7 +1142,7 @@ fn check_fn<'a, 'gcx, 'tcx>(inherited: &'a Inherited<'a, 'gcx, 'tcx>,
11421142
traits::Obligation::new(
11431143
cause, param_env, trait_ref.to_predicate()));
11441144
},
1145-
_ => {},
1145+
config::EntryStart => {},
11461146
}
11471147
}
11481148
}

0 commit comments

Comments
 (0)