2013-04-29 14:56:05 -07:00
|
|
|
// Copyright 2012 The Rust Project Developers. See the COPYRIGHT
|
|
|
|
// file at the top-level directory of this distribution and at
|
|
|
|
// http://rust-lang.org/COPYRIGHT.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
2013-05-17 15:28:44 -07:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
use hir::map as hir_map;
|
2016-03-29 12:54:26 +03:00
|
|
|
use hir::def_id::{CRATE_DEF_INDEX};
|
2014-11-15 20:30:33 -05:00
|
|
|
use session::{config, Session};
|
2015-07-31 00:04:06 -07:00
|
|
|
use syntax::ast::NodeId;
|
2015-09-14 21:58:20 +12:00
|
|
|
use syntax::attr;
|
2015-08-23 14:12:39 -04:00
|
|
|
use syntax::entry::EntryPointType;
|
2016-06-21 18:08:13 -04:00
|
|
|
use syntax_pos::Span;
|
2016-12-04 04:21:06 +02:00
|
|
|
use hir::{Item, ItemFn, ImplItem, TraitItem};
|
2016-11-02 18:22:59 -04:00
|
|
|
use hir::itemlikevisit::ItemLikeVisitor;
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2015-11-17 18:54:21 -05:00
|
|
|
struct EntryContext<'a, 'tcx: 'a> {
|
2014-03-05 16:36:01 +02:00
|
|
|
session: &'a Session,
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
map: &'a hir_map::Map<'tcx>,
|
2013-04-29 16:16:58 -07:00
|
|
|
|
|
|
|
// The top-level function called 'main'
|
2013-08-31 18:13:04 +02:00
|
|
|
main_fn: Option<(NodeId, Span)>,
|
2013-04-29 16:16:58 -07:00
|
|
|
|
2013-04-29 14:56:05 -07:00
|
|
|
// The function that has attribute named 'main'
|
2013-08-31 18:13:04 +02:00
|
|
|
attr_main_fn: Option<(NodeId, Span)>,
|
2013-04-29 14:56:05 -07:00
|
|
|
|
|
|
|
// The function that has the attribute 'start' on it
|
2013-08-31 18:13:04 +02:00
|
|
|
start_fn: Option<(NodeId, Span)>,
|
2013-04-29 16:16:58 -07:00
|
|
|
|
|
|
|
// The functions that one might think are 'main' but aren't, e.g.
|
|
|
|
// main functions not defined at the top level. For diagnostics.
|
2014-03-04 10:02:49 -08:00
|
|
|
non_main_fns: Vec<(NodeId, Span)> ,
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
|
|
|
|
2016-11-02 18:22:59 -04:00
|
|
|
impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
|
2015-11-17 18:54:21 -05:00
|
|
|
fn visit_item(&mut self, item: &'tcx Item) {
|
|
|
|
let def_id = self.map.local_def_id(item.id);
|
|
|
|
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 13:04:47 +02:00
|
|
|
}
|
2016-11-04 18:20:15 -04:00
|
|
|
|
2016-12-04 04:21:06 +02:00
|
|
|
fn visit_trait_item(&mut self, _trait_item: &'tcx TraitItem) {
|
|
|
|
// entry fn is never a trait item
|
|
|
|
}
|
2016-10-28 22:58:32 +02:00
|
|
|
|
2016-11-04 18:20:15 -04:00
|
|
|
fn visit_impl_item(&mut self, _impl_item: &'tcx ImplItem) {
|
|
|
|
// entry fn is never an impl item
|
|
|
|
}
|
2013-08-13 13:04:47 +02:00
|
|
|
}
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2018-03-03 12:26:18 -08:00
|
|
|
pub fn find_entry_point(session: &Session,
|
|
|
|
hir_map: &hir_map::Map,
|
|
|
|
crate_name: &str) {
|
2014-05-02 15:26:45 -07:00
|
|
|
let any_exe = session.crate_types.borrow().iter().any(|ty| {
|
2014-05-06 23:38:01 +12:00
|
|
|
*ty == config::CrateTypeExecutable
|
2014-05-02 15:26:45 -07:00
|
|
|
});
|
2015-02-11 19:23:06 +02:00
|
|
|
if !any_exe {
|
2013-04-29 16:16:58 -07:00
|
|
|
// No need to find a main function
|
2018-04-01 08:17:53 +02:00
|
|
|
session.entry_fn.set(None);
|
2014-05-02 15:26:45 -07:00
|
|
|
return
|
2013-04-29 16:16:58 -07:00
|
|
|
}
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2013-08-03 19:59:46 -07:00
|
|
|
// If the user wants no main function at all, then stop here.
|
2017-01-26 03:21:50 +02:00
|
|
|
if attr::contains_name(&hir_map.krate().attrs, "no_main") {
|
2018-04-01 08:17:53 +02:00
|
|
|
session.entry_fn.set(None);
|
2013-08-03 19:59:46 -07:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2013-09-10 11:24:04 +02:00
|
|
|
let mut ctxt = EntryContext {
|
2017-07-03 11:19:51 -07:00
|
|
|
session,
|
2017-01-26 03:21:50 +02:00
|
|
|
map: hir_map,
|
2013-04-29 16:16:58 -07:00
|
|
|
main_fn: None,
|
2013-04-29 14:56:05 -07:00
|
|
|
attr_main_fn: None,
|
|
|
|
start_fn: None,
|
2014-03-04 10:02:49 -08:00
|
|
|
non_main_fns: Vec::new(),
|
2013-04-29 14:56:05 -07:00
|
|
|
};
|
|
|
|
|
2017-01-26 03:21:50 +02:00
|
|
|
hir_map.krate().visit_all_item_likes(&mut ctxt);
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2018-03-03 12:26:18 -08:00
|
|
|
configure_main(&mut ctxt, crate_name);
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
// Beware, this is duplicated in libsyntax/entry.rs, make sure to keep
|
|
|
|
// them in sync.
|
2015-11-17 18:54:21 -05:00
|
|
|
fn entry_point_type(item: &Item, at_root: bool) -> EntryPointType {
|
2015-07-31 00:04:06 -07:00
|
|
|
match item.node {
|
|
|
|
ItemFn(..) => {
|
|
|
|
if attr::contains_name(&item.attrs, "start") {
|
|
|
|
EntryPointType::Start
|
|
|
|
} else if attr::contains_name(&item.attrs, "main") {
|
|
|
|
EntryPointType::MainAttr
|
2016-11-17 14:04:20 +00:00
|
|
|
} else if item.name == "main" {
|
2015-11-17 18:54:21 -05:00
|
|
|
if at_root {
|
2015-07-31 00:04:06 -07:00
|
|
|
// This is a top-level function so can be 'main'
|
|
|
|
EntryPointType::MainNamed
|
|
|
|
} else {
|
|
|
|
EntryPointType::OtherMain
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
EntryPointType::None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_ => EntryPointType::None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-11-17 18:54:21 -05:00
|
|
|
fn find_item(item: &Item, ctxt: &mut EntryContext, at_root: bool) {
|
|
|
|
match entry_point_type(item, at_root) {
|
2015-08-23 14:12:39 -04:00
|
|
|
EntryPointType::MainNamed => {
|
|
|
|
if ctxt.main_fn.is_none() {
|
|
|
|
ctxt.main_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
|
|
|
span_err!(ctxt.session, item.span, E0136,
|
|
|
|
"multiple 'main' functions");
|
2013-04-29 16:16:58 -07:00
|
|
|
}
|
2015-08-23 14:12:39 -04:00
|
|
|
},
|
|
|
|
EntryPointType::OtherMain => {
|
|
|
|
ctxt.non_main_fns.push((item.id, item.span));
|
|
|
|
},
|
|
|
|
EntryPointType::MainAttr => {
|
|
|
|
if ctxt.attr_main_fn.is_none() {
|
|
|
|
ctxt.attr_main_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
2016-08-04 20:27:11 +01:00
|
|
|
struct_span_err!(ctxt.session, item.span, E0137,
|
|
|
|
"multiple functions with a #[main] attribute")
|
2017-05-04 14:17:23 +02:00
|
|
|
.span_label(item.span, "additional #[main] function")
|
|
|
|
.span_label(ctxt.attr_main_fn.unwrap().1, "first #[main] function")
|
2016-08-04 20:27:11 +01:00
|
|
|
.emit();
|
2013-04-29 16:16:58 -07:00
|
|
|
}
|
2015-08-23 14:12:39 -04:00
|
|
|
},
|
|
|
|
EntryPointType::Start => {
|
|
|
|
if ctxt.start_fn.is_none() {
|
|
|
|
ctxt.start_fn = Some((item.id, item.span));
|
|
|
|
} else {
|
2016-08-11 00:38:12 +08:00
|
|
|
struct_span_err!(
|
|
|
|
ctxt.session, item.span, E0138,
|
|
|
|
"multiple 'start' functions")
|
|
|
|
.span_label(ctxt.start_fn.unwrap().1,
|
2017-05-04 14:17:23 +02:00
|
|
|
"previous `start` function here")
|
|
|
|
.span_label(item.span, "multiple `start` functions")
|
2016-08-11 00:38:12 +08:00
|
|
|
.emit();
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
2015-08-23 14:12:39 -04:00
|
|
|
},
|
|
|
|
EntryPointType::None => ()
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 12:26:18 -08:00
|
|
|
fn configure_main(this: &mut EntryContext, crate_name: &str) {
|
2018-04-01 08:17:53 +02:00
|
|
|
if let Some((node_id, span)) = this.start_fn {
|
|
|
|
this.session.entry_fn.set(Some((node_id, span, config::EntryStart)));
|
|
|
|
} else if let Some((node_id, span)) = this.attr_main_fn {
|
|
|
|
this.session.entry_fn.set(Some((node_id, span, config::EntryMain)));
|
|
|
|
} else if let Some((node_id, span)) = this.main_fn {
|
|
|
|
this.session.entry_fn.set(Some((node_id, span, config::EntryMain)));
|
2013-04-29 16:16:58 -07:00
|
|
|
} else {
|
2014-05-02 15:26:45 -07:00
|
|
|
// No main function
|
2018-04-01 08:17:53 +02:00
|
|
|
this.session.entry_fn.set(None);
|
2018-03-03 12:26:18 -08:00
|
|
|
let mut err = struct_err!(this.session, E0601,
|
2018-03-13 11:22:43 -07:00
|
|
|
"`main` function not found in crate `{}`", crate_name);
|
2014-05-02 15:26:45 -07:00
|
|
|
if !this.non_main_fns.is_empty() {
|
|
|
|
// There were some functions named 'main' though. Try to give the user a hint.
|
2015-12-21 10:00:43 +13: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.");
|
2015-01-31 12:20:46 -05:00
|
|
|
for &(_, span) in &this.non_main_fns {
|
2015-12-21 10:00:43 +13:00
|
|
|
err.span_note(span, "here is a function named 'main'");
|
2013-04-29 16:16:58 -07:00
|
|
|
}
|
2015-12-21 10:00:43 +13:00
|
|
|
err.emit();
|
2013-05-06 18:48:24 -07:00
|
|
|
this.session.abort_if_errors();
|
2015-12-21 10:00:43 +13:00
|
|
|
} else {
|
2018-03-12 13:23:12 -07:00
|
|
|
if let Some(ref filename) = this.session.local_crate_source_file {
|
2018-03-13 11:22:43 -07:00
|
|
|
err.note(&format!("consider adding a `main` function to `{}`", filename.display()));
|
2018-03-12 13:23:12 -07:00
|
|
|
}
|
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 21:44:04 -08:00
|
|
|
if this.session.teach(&err.get_code().unwrap()) {
|
|
|
|
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-21 10:00:43 +13:00
|
|
|
err.emit();
|
2013-04-29 16:16:58 -07:00
|
|
|
}
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
|
|
|
}
|