2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::map as hir_map;
|
|
|
|
use crate::hir::def_id::{CrateNum, CRATE_DEF_INDEX, DefId, LOCAL_CRATE};
|
|
|
|
use crate::session::{config, Session};
|
|
|
|
use crate::session::config::EntryFnType;
|
2015-09-14 04:58:20 -05:00
|
|
|
use syntax::attr;
|
2015-08-23 13:12:39 -05:00
|
|
|
use syntax::entry::EntryPointType;
|
2019-05-07 22:21:18 -05:00
|
|
|
use syntax::symbol::sym;
|
2016-06-21 17:08:13 -05:00
|
|
|
use syntax_pos::Span;
|
2019-02-27 10:35:24 -06:00
|
|
|
use crate::hir::{HirId, Item, ItemKind, ImplItem, TraitItem};
|
2019-02-05 11:20:45 -06:00
|
|
|
use crate::hir::itemlikevisit::ItemLikeVisitor;
|
|
|
|
use crate::ty::TyCtxt;
|
|
|
|
use crate::ty::query::Providers;
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2019-06-14 11:39:39 -05:00
|
|
|
struct EntryContext<'a, 'tcx> {
|
2014-03-05 08:36:01 -06:00
|
|
|
session: &'a Session,
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2017-01-25 19:21:50 -06:00
|
|
|
map: &'a hir_map::Map<'tcx>,
|
2013-04-29 18:16:58 -05:00
|
|
|
|
2019-09-05 21:57:44 -05:00
|
|
|
/// The top-level function called 'main'.
|
2019-02-27 10:35:24 -06:00
|
|
|
main_fn: Option<(HirId, Span)>,
|
2013-04-29 18:16:58 -05:00
|
|
|
|
2019-09-05 21:57:44 -05:00
|
|
|
/// The function that has attribute named 'main'.
|
2019-02-27 10:35:24 -06:00
|
|
|
attr_main_fn: Option<(HirId, Span)>,
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2019-09-05 21:57:44 -05:00
|
|
|
/// The function that has the attribute 'start' on it.
|
2019-02-27 10:35:24 -06:00
|
|
|
start_fn: Option<(HirId, Span)>,
|
2013-04-29 18:16:58 -05:00
|
|
|
|
2019-09-05 21:57:44 -05:00
|
|
|
/// The functions that one might think are 'main' but aren't, e.g.
|
|
|
|
/// main functions not defined at the top level. For diagnostics.
|
2019-02-27 10:35:24 -06:00
|
|
|
non_main_fns: Vec<(HirId, Span)> ,
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2016-11-02 17:22:59 -05:00
|
|
|
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
|
2015-11-17 17:54:21 -06:00
|
|
|
fn visit_item(&mut self, item: &'tcx Item) {
|
2019-06-27 04:28:14 -05:00
|
|
|
let def_id = self.map.local_def_id(item.hir_id);
|
2015-11-17 17:54:21 -06:00
|
|
|
let def_key = self.map.def_key(def_id);
|
|
|
|
let at_root = def_key.parent == Some(CRATE_DEF_INDEX);
|
|
|
|
find_item(item, self, at_root);
|
2013-08-13 06:04:47 -05:00
|
|
|
}
|
2016-11-04 17:20:15 -05:00
|
|
|
|
2016-12-03 20:21:06 -06:00
|
|
|
fn visit_trait_item(&mut self, _trait_item: &'tcx TraitItem) {
|
2019-09-05 21:57:44 -05:00
|
|
|
// Entry fn is never a trait item.
|
2016-12-03 20:21:06 -06:00
|
|
|
}
|
2016-10-28 15:58:32 -05:00
|
|
|
|
2016-11-04 17:20:15 -05:00
|
|
|
fn visit_impl_item(&mut self, _impl_item: &'tcx ImplItem) {
|
2019-09-05 21:57:44 -05:00
|
|
|
// Entry fn is never a trait item.
|
2016-11-04 17:20:15 -05:00
|
|
|
}
|
2013-08-13 06:04:47 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2019-06-13 16:48:52 -05:00
|
|
|
fn entry_fn(tcx: TyCtxt<'_>, cnum: CrateNum) -> Option<(DefId, EntryFnType)> {
|
2019-01-13 06:06:26 -06:00
|
|
|
assert_eq!(cnum, LOCAL_CRATE);
|
|
|
|
|
|
|
|
let any_exe = tcx.sess.crate_types.borrow().iter().any(|ty| {
|
2018-07-26 12:13:11 -05:00
|
|
|
*ty == config::CrateType::Executable
|
2014-05-02 17:26:45 -05:00
|
|
|
});
|
2015-02-11 11:23:06 -06:00
|
|
|
if !any_exe {
|
2019-09-05 21:57:44 -05:00
|
|
|
// No need to find a main function.
|
2019-01-13 06:06:26 -06:00
|
|
|
return None;
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2013-08-03 21:59:46 -05:00
|
|
|
// If the user wants no main function at all, then stop here.
|
2019-05-07 22:21:18 -05:00
|
|
|
if attr::contains_name(&tcx.hir().krate().attrs, sym::no_main) {
|
2019-01-13 06:06:26 -06:00
|
|
|
return None;
|
2013-08-03 21:59:46 -05:00
|
|
|
}
|
|
|
|
|
2013-09-10 04:24:04 -05:00
|
|
|
let mut ctxt = EntryContext {
|
2019-01-13 06:06:26 -06:00
|
|
|
session: tcx.sess,
|
|
|
|
map: tcx.hir(),
|
2013-04-29 18:16:58 -05:00
|
|
|
main_fn: None,
|
2013-04-29 16:56:05 -05:00
|
|
|
attr_main_fn: None,
|
|
|
|
start_fn: None,
|
2014-03-04 12:02:49 -06:00
|
|
|
non_main_fns: Vec::new(),
|
2013-04-29 16:56:05 -05:00
|
|
|
};
|
|
|
|
|
2019-01-13 06:06:26 -06:00
|
|
|
tcx.hir().krate().visit_all_item_likes(&mut ctxt);
|
2013-04-29 16:56:05 -05:00
|
|
|
|
2019-01-13 06:06:26 -06:00
|
|
|
configure_main(tcx, &ctxt)
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
|
2018-11-26 20:59:49 -06:00
|
|
|
// Beware, this is duplicated in `libsyntax/entry.rs`, so make sure to keep
|
2015-07-31 02:04:06 -05:00
|
|
|
// them in sync.
|
2015-11-17 17:54:21 -06:00
|
|
|
fn entry_point_type(item: &Item, at_root: bool) -> EntryPointType {
|
2015-07-31 02:04:06 -05:00
|
|
|
match item.node {
|
2018-07-11 10:36:06 -05:00
|
|
|
ItemKind::Fn(..) => {
|
2019-05-07 22:21:18 -05:00
|
|
|
if attr::contains_name(&item.attrs, sym::start) {
|
2015-07-31 02:04:06 -05:00
|
|
|
EntryPointType::Start
|
2019-05-07 22:21:18 -05:00
|
|
|
} else if attr::contains_name(&item.attrs, sym::main) {
|
2015-07-31 02:04:06 -05:00
|
|
|
EntryPointType::MainAttr
|
2019-05-07 01:03:44 -05:00
|
|
|
} else if item.ident.name == sym::main {
|
2015-11-17 17:54:21 -06:00
|
|
|
if at_root {
|
2018-11-26 20:59:49 -06:00
|
|
|
// This is a top-level function so can be 'main'.
|
2015-07-31 02:04:06 -05:00
|
|
|
EntryPointType::MainNamed
|
|
|
|
} else {
|
|
|
|
EntryPointType::OtherMain
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EntryPointType::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => EntryPointType::None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-08-30 00:02:42 -05:00
|
|
|
fn find_item(item: &Item, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
|
2015-11-17 17:54:21 -06:00
|
|
|
match entry_point_type(item, at_root) {
|
2015-08-23 13:12:39 -05:00
|
|
|
EntryPointType::MainNamed => {
|
|
|
|
if ctxt.main_fn.is_none() {
|
2019-02-27 10:35:24 -06:00
|
|
|
ctxt.main_fn = Some((item.hir_id, item.span));
|
2015-08-23 13:12:39 -05:00
|
|
|
} else {
|
|
|
|
span_err!(ctxt.session, item.span, E0136,
|
|
|
|
"multiple 'main' functions");
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2015-08-23 13:12:39 -05:00
|
|
|
},
|
|
|
|
EntryPointType::OtherMain => {
|
2019-02-27 10:35:24 -06:00
|
|
|
ctxt.non_main_fns.push((item.hir_id, item.span));
|
2015-08-23 13:12:39 -05:00
|
|
|
},
|
|
|
|
EntryPointType::MainAttr => {
|
|
|
|
if ctxt.attr_main_fn.is_none() {
|
2019-02-27 10:35:24 -06:00
|
|
|
ctxt.attr_main_fn = Some((item.hir_id, item.span));
|
2015-08-23 13:12:39 -05:00
|
|
|
} else {
|
2016-08-04 14:27:11 -05:00
|
|
|
struct_span_err!(ctxt.session, item.span, E0137,
|
2019-07-23 14:26:01 -05:00
|
|
|
"multiple functions with a `#[main]` attribute")
|
|
|
|
.span_label(item.span, "additional `#[main]` function")
|
|
|
|
.span_label(ctxt.attr_main_fn.unwrap().1, "first `#[main]` function")
|
2016-08-04 14:27:11 -05:00
|
|
|
.emit();
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2015-08-23 13:12:39 -05:00
|
|
|
},
|
|
|
|
EntryPointType::Start => {
|
|
|
|
if ctxt.start_fn.is_none() {
|
2019-02-27 10:35:24 -06:00
|
|
|
ctxt.start_fn = Some((item.hir_id, item.span));
|
2015-08-23 13:12:39 -05:00
|
|
|
} else {
|
2018-10-02 11:29:48 -05:00
|
|
|
struct_span_err!(ctxt.session, item.span, E0138, "multiple 'start' functions")
|
|
|
|
.span_label(ctxt.start_fn.unwrap().1, "previous `start` function here")
|
2017-05-04 07:17:23 -05:00
|
|
|
.span_label(item.span, "multiple `start` functions")
|
2016-08-10 11:38:12 -05:00
|
|
|
.emit();
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
2019-01-13 06:06:26 -06:00
|
|
|
}
|
|
|
|
EntryPointType::None => (),
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-13 17:32:15 -05:00
|
|
|
fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(DefId, EntryFnType)> {
|
2019-02-27 10:35:24 -06:00
|
|
|
if let Some((hir_id, _)) = visitor.start_fn {
|
2019-06-27 04:28:14 -05:00
|
|
|
Some((tcx.hir().local_def_id(hir_id), EntryFnType::Start))
|
2019-02-27 10:35:24 -06:00
|
|
|
} else if let Some((hir_id, _)) = visitor.attr_main_fn {
|
2019-06-27 04:28:14 -05:00
|
|
|
Some((tcx.hir().local_def_id(hir_id), EntryFnType::Main))
|
2019-02-27 10:35:24 -06:00
|
|
|
} else if let Some((hir_id, _)) = visitor.main_fn {
|
2019-06-27 04:28:14 -05:00
|
|
|
Some((tcx.hir().local_def_id(hir_id), EntryFnType::Main))
|
2013-04-29 18:16:58 -05:00
|
|
|
} else {
|
2019-09-05 21:57:44 -05:00
|
|
|
// There is no main function.
|
2019-01-13 06:06:26 -06:00
|
|
|
let mut err = struct_err!(tcx.sess, E0601,
|
|
|
|
"`main` function not found in crate `{}`", tcx.crate_name(LOCAL_CRATE));
|
|
|
|
if !visitor.non_main_fns.is_empty() {
|
2014-05-02 17:26:45 -05:00
|
|
|
// There were some functions named 'main' though. Try to give the user a hint.
|
2015-12-20 15:00:43 -06:00
|
|
|
err.note("the main function must be defined at the crate level \
|
|
|
|
but you have one or more functions named 'main' that are not \
|
|
|
|
defined at the crate level. Either move the definition or \
|
|
|
|
attach the `#[main]` attribute to override this behavior.");
|
2019-01-13 06:06:26 -06:00
|
|
|
for &(_, span) in &visitor.non_main_fns {
|
2015-12-20 15:00:43 -06:00
|
|
|
err.span_note(span, "here is a function named 'main'");
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2015-12-20 15:00:43 -06:00
|
|
|
err.emit();
|
|
|
|
} else {
|
2019-01-13 06:06:26 -06:00
|
|
|
if let Some(ref filename) = tcx.sess.local_crate_source_file {
|
2018-03-13 13:22:43 -05:00
|
|
|
err.note(&format!("consider adding a `main` function to `{}`", filename.display()));
|
2018-03-12 15:23:12 -05:00
|
|
|
}
|
2019-01-13 06:06:26 -06:00
|
|
|
if tcx.sess.teach(&err.get_code().unwrap()) {
|
Add `-Zteach` documentation
Add extra inline documentation to E0019, E0016, E0013, E0396, E0017,
E0018, E0010, E0022, E0030, E0029, E0033, E0026 and E0027.
2018-01-28 23:44:04 -06:00
|
|
|
err.note("If you don't know the basics of Rust, you can go look to the Rust Book \
|
|
|
|
to get started: https://doc.rust-lang.org/book/");
|
|
|
|
}
|
2015-12-20 15:00:43 -06:00
|
|
|
err.emit();
|
2013-04-29 18:16:58 -05:00
|
|
|
}
|
2019-01-13 06:06:26 -06:00
|
|
|
|
|
|
|
None
|
2013-04-29 16:56:05 -05:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 06:06:26 -06:00
|
|
|
|
2019-06-13 16:48:52 -05:00
|
|
|
pub fn find_entry_point(tcx: TyCtxt<'_>) -> Option<(DefId, EntryFnType)> {
|
2019-01-13 06:06:26 -06:00
|
|
|
tcx.entry_fn(LOCAL_CRATE)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn provide(providers: &mut Providers<'_>) {
|
|
|
|
*providers = Providers {
|
|
|
|
entry_fn,
|
|
|
|
..*providers
|
|
|
|
};
|
|
|
|
}
|