2020-02-29 20:37:32 +03:00
|
|
|
use rustc_ast::entry::EntryPointType;
|
2020-01-09 11:18:47 +01:00
|
|
|
use rustc_errors::struct_span_err;
|
2022-04-15 19:27:53 +02:00
|
|
|
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir::itemlikevisit::ItemLikeVisitor;
|
2021-12-05 13:07:51 +01:00
|
|
|
use rustc_hir::{ForeignItem, ImplItem, Item, ItemKind, Node, TraitItem, CRATE_HIR_ID};
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::ty::query::Providers;
|
2022-04-15 19:27:53 +02:00
|
|
|
use rustc_middle::ty::{DefIdTree, TyCtxt};
|
2020-05-02 01:30:23 +03:00
|
|
|
use rustc_session::config::{CrateType, EntryFnType};
|
2021-04-26 01:09:35 +08:00
|
|
|
use rustc_session::parse::feature_err;
|
2020-05-02 01:30:23 +03:00
|
|
|
use rustc_session::Session;
|
2020-01-01 19:30:57 +01:00
|
|
|
use rustc_span::symbol::sym;
|
2019-12-31 21:25:16 +01:00
|
|
|
use rustc_span::{Span, DUMMY_SP};
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
struct EntryContext<'tcx> {
|
|
|
|
tcx: TyCtxt<'tcx>,
|
2013-04-29 16:16:58 -07:00
|
|
|
|
2019-09-08 13:06:49 -04:00
|
|
|
/// The function that has attribute named `main`.
|
2021-12-05 13:07:51 +01:00
|
|
|
attr_main_fn: Option<(LocalDefId, Span)>,
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2019-09-06 03:57:44 +01:00
|
|
|
/// The function that has the attribute 'start' on it.
|
2021-12-05 13:07:51 +01:00
|
|
|
start_fn: Option<(LocalDefId, Span)>,
|
2013-04-29 16:16:58 -07:00
|
|
|
|
2019-09-08 13:06:49 -04:00
|
|
|
/// The functions that one might think are `main` but aren't, e.g.
|
2019-09-06 03:57:44 +01:00
|
|
|
/// main functions not defined at the top level. For diagnostics.
|
2021-12-05 13:07:51 +01:00
|
|
|
non_main_fns: Vec<Span>,
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
impl<'tcx> ItemLikeVisitor<'tcx> for EntryContext<'tcx> {
|
2019-11-28 19:28:50 +01:00
|
|
|
fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
|
2022-04-25 22:08:45 +03:00
|
|
|
let at_root = self.tcx.opt_local_parent(item.def_id) == Some(CRATE_DEF_ID);
|
2015-11-17 18:54:21 -05:00
|
|
|
find_item(item, self, at_root);
|
2013-08-13 13:04:47 +02:00
|
|
|
}
|
2016-11-04 18:20:15 -04:00
|
|
|
|
2019-11-28 21:47:10 +01:00
|
|
|
fn visit_trait_item(&mut self, _trait_item: &'tcx TraitItem<'tcx>) {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Entry fn is never a trait item.
|
2016-12-04 04:21:06 +02:00
|
|
|
}
|
2016-10-28 22:58:32 +02:00
|
|
|
|
2019-11-28 22:16:44 +01:00
|
|
|
fn visit_impl_item(&mut self, _impl_item: &'tcx ImplItem<'tcx>) {
|
2019-09-06 03:57:44 +01:00
|
|
|
// Entry fn is never a trait item.
|
2016-11-04 18:20:15 -04:00
|
|
|
}
|
2020-11-11 21:57:54 +01:00
|
|
|
|
2020-11-26 19:11:43 +01:00
|
|
|
fn visit_foreign_item(&mut self, _: &'tcx ForeignItem<'tcx>) {
|
|
|
|
// Entry fn is never a foreign item.
|
|
|
|
}
|
2013-08-13 13:04:47 +02:00
|
|
|
}
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2021-05-11 12:00:59 +02:00
|
|
|
fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> {
|
2020-05-15 21:44:28 -07:00
|
|
|
let any_exe = tcx.sess.crate_types().iter().any(|ty| *ty == CrateType::Executable);
|
2015-02-11 19:23:06 +02:00
|
|
|
if !any_exe {
|
2019-09-06 03:57:44 +01:00
|
|
|
// No need to find a main function.
|
2019-01-13 13:06:26 +01:00
|
|
|
return None;
|
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.
|
2020-11-26 23:38:53 +01:00
|
|
|
if tcx.sess.contains_name(&tcx.hir().attrs(CRATE_HIR_ID), sym::no_main) {
|
2019-01-13 13:06:26 +01:00
|
|
|
return None;
|
2013-08-03 19:59:46 -07:00
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
let mut ctxt =
|
|
|
|
EntryContext { tcx, attr_main_fn: None, start_fn: None, non_main_fns: Vec::new() };
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2021-09-12 01:11:22 +02:00
|
|
|
tcx.hir().visit_all_item_likes(&mut ctxt);
|
2013-04-29 14:56:05 -07:00
|
|
|
|
2019-01-13 13:06:26 +01:00
|
|
|
configure_main(tcx, &ctxt)
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
|
|
|
|
2020-07-30 11:27:50 +10:00
|
|
|
// Beware, this is duplicated in `librustc_builtin_macros/test_harness.rs`
|
|
|
|
// (with `ast::Item`), so make sure to keep them in sync.
|
2022-04-15 19:27:53 +02:00
|
|
|
fn entry_point_type(ctxt: &EntryContext<'_>, item: &Item<'_>, at_root: bool) -> EntryPointType {
|
|
|
|
let attrs = ctxt.tcx.hir().attrs(item.hir_id());
|
|
|
|
if ctxt.tcx.sess.contains_name(attrs, sym::start) {
|
2020-10-03 20:45:39 +02:00
|
|
|
EntryPointType::Start
|
2022-04-15 19:27:53 +02:00
|
|
|
} else if ctxt.tcx.sess.contains_name(attrs, sym::rustc_main) {
|
2020-10-03 20:45:39 +02:00
|
|
|
EntryPointType::MainAttr
|
|
|
|
} else if item.ident.name == sym::main {
|
|
|
|
if at_root {
|
|
|
|
// This is a top-level function so can be `main`.
|
|
|
|
EntryPointType::MainNamed
|
|
|
|
} else {
|
|
|
|
EntryPointType::OtherMain
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
2020-10-03 20:45:39 +02:00
|
|
|
} else {
|
|
|
|
EntryPointType::None
|
2015-07-31 00:04:06 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-03 20:45:39 +02:00
|
|
|
fn throw_attr_err(sess: &Session, span: Span, attr: &str) {
|
|
|
|
sess.struct_span_err(span, &format!("`{}` attribute can only be used on functions", attr))
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_>, at_root: bool) {
|
2021-01-24 13:17:54 +01:00
|
|
|
match entry_point_type(ctxt, item, at_root) {
|
2020-10-03 20:45:39 +02:00
|
|
|
EntryPointType::None => (),
|
|
|
|
_ if !matches!(item.kind, ItemKind::Fn(..)) => {
|
2022-04-15 19:27:53 +02:00
|
|
|
let attrs = ctxt.tcx.hir().attrs(item.hir_id());
|
|
|
|
if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym::start) {
|
|
|
|
throw_attr_err(&ctxt.tcx.sess, attr.span, "start");
|
2020-10-03 20:45:39 +02:00
|
|
|
}
|
2022-04-15 19:27:53 +02:00
|
|
|
if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym::rustc_main) {
|
|
|
|
throw_attr_err(&ctxt.tcx.sess, attr.span, "rustc_main");
|
2020-10-03 20:45:39 +02:00
|
|
|
}
|
|
|
|
}
|
2021-04-26 01:09:35 +08:00
|
|
|
EntryPointType::MainNamed => (),
|
2015-08-23 14:12:39 -04:00
|
|
|
EntryPointType::OtherMain => {
|
2021-12-05 13:07:51 +01:00
|
|
|
ctxt.non_main_fns.push(item.span);
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2015-08-23 14:12:39 -04:00
|
|
|
EntryPointType::MainAttr => {
|
|
|
|
if ctxt.attr_main_fn.is_none() {
|
2021-12-05 13:07:51 +01:00
|
|
|
ctxt.attr_main_fn = Some((item.def_id, item.span));
|
2015-08-23 14:12:39 -04:00
|
|
|
} else {
|
2019-12-22 17:42:04 -05:00
|
|
|
struct_span_err!(
|
2022-04-15 19:27:53 +02:00
|
|
|
ctxt.tcx.sess,
|
2019-12-22 17:42:04 -05:00
|
|
|
item.span,
|
|
|
|
E0137,
|
|
|
|
"multiple functions with a `#[main]` attribute"
|
|
|
|
)
|
2019-07-23 21:26:01 +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
|
|
|
}
|
2019-12-22 17:42:04 -05:00
|
|
|
}
|
2015-08-23 14:12:39 -04:00
|
|
|
EntryPointType::Start => {
|
|
|
|
if ctxt.start_fn.is_none() {
|
2021-12-05 13:07:51 +01:00
|
|
|
ctxt.start_fn = Some((item.def_id, item.span));
|
2015-08-23 14:12:39 -04:00
|
|
|
} else {
|
2022-04-15 19:27:53 +02:00
|
|
|
struct_span_err!(ctxt.tcx.sess, item.span, E0138, "multiple `start` functions")
|
2020-01-05 00:17:46 +00:00
|
|
|
.span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here")
|
2017-05-04 14:17:23 +02:00
|
|
|
.span_label(item.span, "multiple `start` functions")
|
2016-08-11 00:38:12 +08:00
|
|
|
.emit();
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
2019-01-13 13:06:26 +01:00
|
|
|
}
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) -> Option<(DefId, EntryFnType)> {
|
2021-12-05 13:07:51 +01:00
|
|
|
if let Some((def_id, _)) = visitor.start_fn {
|
|
|
|
Some((def_id.to_def_id(), EntryFnType::Start))
|
|
|
|
} else if let Some((def_id, _)) = visitor.attr_main_fn {
|
|
|
|
Some((def_id.to_def_id(), EntryFnType::Main))
|
2021-04-04 14:40:35 +02:00
|
|
|
} else {
|
2022-03-18 22:48:21 +09:00
|
|
|
if let Some(main_def) = tcx.resolutions(()).main_def && let Some(def_id) = main_def.opt_fn_def_id() {
|
|
|
|
// non-local main imports are handled below
|
|
|
|
if let Some(def_id) = def_id.as_local() && matches!(tcx.hir().find_by_def_id(def_id), Some(Node::ForeignItem(_))) {
|
|
|
|
tcx.sess
|
|
|
|
.struct_span_err(
|
|
|
|
tcx.def_span(def_id),
|
|
|
|
"the `main` function cannot be declared in an `extern` block",
|
2021-06-09 23:11:35 -04:00
|
|
|
)
|
|
|
|
.emit();
|
2022-03-18 22:48:21 +09:00
|
|
|
return None;
|
2021-06-09 23:11:35 -04:00
|
|
|
}
|
2022-03-18 22:48:21 +09:00
|
|
|
|
|
|
|
if main_def.is_import && !tcx.features().imported_main {
|
|
|
|
let span = main_def.span;
|
|
|
|
feature_err(
|
|
|
|
&tcx.sess.parse_sess,
|
|
|
|
sym::imported_main,
|
|
|
|
span,
|
|
|
|
"using an imported function as entry point `main` is experimental",
|
|
|
|
)
|
|
|
|
.emit();
|
|
|
|
}
|
|
|
|
return Some((def_id, EntryFnType::Main));
|
2021-06-09 23:11:35 -04:00
|
|
|
}
|
2019-09-08 13:06:49 -04:00
|
|
|
no_main_err(tcx, visitor);
|
2019-01-13 13:06:26 +01:00
|
|
|
None
|
2013-04-29 14:56:05 -07:00
|
|
|
}
|
|
|
|
}
|
2019-01-13 13:06:26 +01:00
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) {
|
2021-09-12 01:11:22 +02:00
|
|
|
let sp = tcx.def_span(CRATE_DEF_ID);
|
2019-10-28 17:44:20 -07:00
|
|
|
if *tcx.sess.parse_sess.reached_eof.borrow() {
|
|
|
|
// There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about
|
|
|
|
// the missing `fn main()` then as it might have been hidden inside an unclosed block.
|
|
|
|
tcx.sess.delay_span_bug(sp, "`main` not found, but expected unclosed brace error");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-09-08 13:06:49 -04:00
|
|
|
// There is no main function.
|
2019-12-31 21:25:16 +01:00
|
|
|
let mut err = struct_span_err!(
|
2019-12-22 17:42:04 -05:00
|
|
|
tcx.sess,
|
2019-12-31 21:25:16 +01:00
|
|
|
DUMMY_SP,
|
2019-12-22 17:42:04 -05:00
|
|
|
E0601,
|
|
|
|
"`main` function not found in crate `{}`",
|
|
|
|
tcx.crate_name(LOCAL_CRATE)
|
|
|
|
);
|
2019-09-08 13:06:49 -04:00
|
|
|
let filename = &tcx.sess.local_crate_source_file;
|
|
|
|
let note = if !visitor.non_main_fns.is_empty() {
|
2021-12-05 13:07:51 +01:00
|
|
|
for &span in &visitor.non_main_fns {
|
2019-09-08 13:06:49 -04:00
|
|
|
err.span_note(span, "here is a function named `main`");
|
|
|
|
}
|
|
|
|
err.note("you have one or more functions named `main` not defined at the crate level");
|
2021-04-08 21:37:38 +08:00
|
|
|
err.help("consider moving the `main` function definitions");
|
2019-09-08 13:06:49 -04:00
|
|
|
// There were some functions named `main` though. Try to give the user a hint.
|
2019-12-22 17:42:04 -05:00
|
|
|
format!(
|
|
|
|
"the main function must be defined at the crate level{}",
|
|
|
|
filename.as_ref().map(|f| format!(" (in `{}`)", f.display())).unwrap_or_default()
|
|
|
|
)
|
2019-09-08 13:06:49 -04:00
|
|
|
} else if let Some(filename) = filename {
|
|
|
|
format!("consider adding a `main` function to `{}`", filename.display())
|
|
|
|
} else {
|
|
|
|
String::from("consider adding a `main` function at the crate level")
|
|
|
|
};
|
|
|
|
// The file may be empty, which leads to the diagnostic machinery not emitting this
|
|
|
|
// note. This is a relatively simple way to detect that case and emit a span-less
|
|
|
|
// note instead.
|
2022-01-21 00:15:39 +00:00
|
|
|
if tcx.sess.source_map().lookup_line(sp.hi()).is_ok() {
|
|
|
|
err.set_span(sp.shrink_to_hi());
|
|
|
|
err.span_label(sp.shrink_to_hi(), ¬e);
|
2019-09-08 13:06:49 -04:00
|
|
|
} else {
|
|
|
|
err.note(¬e);
|
|
|
|
}
|
2021-04-26 01:09:35 +08:00
|
|
|
|
2022-03-18 22:48:21 +09:00
|
|
|
if let Some(main_def) = tcx.resolutions(()).main_def && main_def.opt_fn_def_id().is_none(){
|
|
|
|
// There is something at `crate::main`, but it is not a function definition.
|
|
|
|
err.span_label(main_def.span, "non-function item at `crate::main` is found");
|
2021-04-26 01:09:35 +08:00
|
|
|
}
|
|
|
|
|
2019-09-08 13:06:49 -04:00
|
|
|
if tcx.sess.teach(&err.get_code().unwrap()) {
|
2019-12-22 17:42:04 -05: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/",
|
|
|
|
);
|
2019-09-08 13:06:49 -04:00
|
|
|
}
|
|
|
|
err.emit();
|
|
|
|
}
|
|
|
|
|
2020-07-05 23:00:14 +03:00
|
|
|
pub fn provide(providers: &mut Providers) {
|
2019-12-22 17:42:04 -05:00
|
|
|
*providers = Providers { entry_fn, ..*providers };
|
2019-01-13 13:06:26 +01:00
|
|
|
}
|