summaryrefslogtreecommitdiffstats
path: root/compiler/rustc_passes/src/entry.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--compiler/rustc_passes/src/entry.rs187
1 files changed, 90 insertions, 97 deletions
diff --git a/compiler/rustc_passes/src/entry.rs b/compiler/rustc_passes/src/entry.rs
index 7381019a6..5885f45ae 100644
--- a/compiler/rustc_passes/src/entry.rs
+++ b/compiler/rustc_passes/src/entry.rs
@@ -1,14 +1,19 @@
-use rustc_ast::{entry::EntryPointType, Attribute};
-use rustc_errors::struct_span_err;
+use rustc_ast::entry::EntryPointType;
+use rustc_errors::error_code;
use rustc_hir::def::DefKind;
use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
use rustc_hir::{ItemId, Node, CRATE_HIR_ID};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{DefIdTree, TyCtxt};
-use rustc_session::config::{CrateType, EntryFnType};
+use rustc_session::config::{sigpipe, CrateType, EntryFnType};
use rustc_session::parse::feature_err;
use rustc_span::symbol::sym;
-use rustc_span::{Span, Symbol, DUMMY_SP};
+use rustc_span::{Span, Symbol};
+
+use crate::errors::{
+ AttrOnlyInFunctions, AttrOnlyOnMain, AttrOnlyOnRootMain, ExternMain, MultipleRustcMain,
+ MultipleStartFunctions, NoMainErr, UnixSigpipeValues,
+};
struct EntryContext<'tcx> {
tcx: TyCtxt<'tcx>,
@@ -57,7 +62,7 @@ fn entry_point_type(ctxt: &EntryContext<'_>, id: ItemId, at_root: bool) -> Entry
} else if ctxt.tcx.sess.contains_name(attrs, sym::rustc_main) {
EntryPointType::RustcMainAttr
} else {
- if let Some(name) = ctxt.tcx.opt_item_name(id.def_id.to_def_id())
+ if let Some(name) = ctxt.tcx.opt_item_name(id.owner_id.to_def_id())
&& name == sym::main {
if at_root {
// This is a top-level function so can be `main`.
@@ -71,63 +76,57 @@ fn entry_point_type(ctxt: &EntryContext<'_>, id: ItemId, at_root: bool) -> Entry
}
}
-fn err_if_attr_found(ctxt: &EntryContext<'_>, attrs: &[Attribute], sym: Symbol) {
- if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym) {
- ctxt.tcx
- .sess
- .struct_span_err(
- attr.span,
- &format!("`{}` attribute can only be used on functions", sym),
- )
- .emit();
- }
+fn attr_span_by_symbol(ctxt: &EntryContext<'_>, id: ItemId, sym: Symbol) -> Option<Span> {
+ let attrs = ctxt.tcx.hir().attrs(id.hir_id());
+ ctxt.tcx.sess.find_by_name(attrs, sym).map(|attr| attr.span)
}
fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
- let at_root = ctxt.tcx.opt_local_parent(id.def_id) == Some(CRATE_DEF_ID);
+ let at_root = ctxt.tcx.opt_local_parent(id.owner_id.def_id) == Some(CRATE_DEF_ID);
match entry_point_type(ctxt, id, at_root) {
- EntryPointType::None => (),
- _ if !matches!(ctxt.tcx.def_kind(id.def_id), DefKind::Fn) => {
- let attrs = ctxt.tcx.hir().attrs(id.hir_id());
- err_if_attr_found(ctxt, attrs, sym::start);
- err_if_attr_found(ctxt, attrs, sym::rustc_main);
+ EntryPointType::None => {
+ if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) {
+ ctxt.tcx.sess.emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe });
+ }
+ }
+ _ if !matches!(ctxt.tcx.def_kind(id.owner_id), DefKind::Fn) => {
+ for attr in [sym::start, sym::rustc_main] {
+ if let Some(span) = attr_span_by_symbol(ctxt, id, attr) {
+ ctxt.tcx.sess.emit_err(AttrOnlyInFunctions { span, attr });
+ }
+ }
}
EntryPointType::MainNamed => (),
EntryPointType::OtherMain => {
- ctxt.non_main_fns.push(ctxt.tcx.def_span(id.def_id));
+ if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) {
+ ctxt.tcx.sess.emit_err(AttrOnlyOnRootMain { span, attr: sym::unix_sigpipe });
+ }
+ ctxt.non_main_fns.push(ctxt.tcx.def_span(id.owner_id));
}
EntryPointType::RustcMainAttr => {
if ctxt.attr_main_fn.is_none() {
- ctxt.attr_main_fn = Some((id.def_id, ctxt.tcx.def_span(id.def_id.to_def_id())));
+ ctxt.attr_main_fn = Some((id.owner_id.def_id, ctxt.tcx.def_span(id.owner_id)));
} else {
- struct_span_err!(
- ctxt.tcx.sess,
- ctxt.tcx.def_span(id.def_id.to_def_id()),
- E0137,
- "multiple functions with a `#[rustc_main]` attribute"
- )
- .span_label(
- ctxt.tcx.def_span(id.def_id.to_def_id()),
- "additional `#[rustc_main]` function",
- )
- .span_label(ctxt.attr_main_fn.unwrap().1, "first `#[rustc_main]` function")
- .emit();
+ ctxt.tcx.sess.emit_err(MultipleRustcMain {
+ span: ctxt.tcx.def_span(id.owner_id.to_def_id()),
+ first: ctxt.attr_main_fn.unwrap().1,
+ additional: ctxt.tcx.def_span(id.owner_id.to_def_id()),
+ });
}
}
EntryPointType::Start => {
+ if let Some(span) = attr_span_by_symbol(ctxt, id, sym::unix_sigpipe) {
+ ctxt.tcx.sess.emit_err(AttrOnlyOnMain { span, attr: sym::unix_sigpipe });
+ }
if ctxt.start_fn.is_none() {
- ctxt.start_fn = Some((id.def_id, ctxt.tcx.def_span(id.def_id.to_def_id())));
+ ctxt.start_fn = Some((id.owner_id.def_id, ctxt.tcx.def_span(id.owner_id)));
} else {
- struct_span_err!(
- ctxt.tcx.sess,
- ctxt.tcx.def_span(id.def_id.to_def_id()),
- E0138,
- "multiple `start` functions"
- )
- .span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here")
- .span_label(ctxt.tcx.def_span(id.def_id.to_def_id()), "multiple `start` functions")
- .emit();
+ ctxt.tcx.sess.emit_err(MultipleStartFunctions {
+ span: ctxt.tcx.def_span(id.owner_id),
+ labeled: ctxt.tcx.def_span(id.owner_id.to_def_id()),
+ previous: ctxt.start_fn.unwrap().1,
+ });
}
}
}
@@ -136,18 +135,14 @@ fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) -> Option<(DefId, EntryFnType)> {
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))
+ } else if let Some((local_def_id, _)) = visitor.attr_main_fn {
+ let def_id = local_def_id.to_def_id();
+ Some((def_id, EntryFnType::Main { sigpipe: sigpipe(tcx, def_id) }))
} else {
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",
- )
- .emit();
+ tcx.sess.emit_err(ExternMain { span: tcx.def_span(def_id) });
return None;
}
@@ -161,13 +156,34 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) -> Option<(DefId,
)
.emit();
}
- return Some((def_id, EntryFnType::Main));
+ return Some((def_id, EntryFnType::Main { sigpipe: sigpipe(tcx, def_id) }));
}
no_main_err(tcx, visitor);
None
}
}
+fn sigpipe(tcx: TyCtxt<'_>, def_id: DefId) -> u8 {
+ if let Some(attr) = tcx.get_attr(def_id, sym::unix_sigpipe) {
+ match (attr.value_str(), attr.meta_item_list()) {
+ (Some(sym::inherit), None) => sigpipe::INHERIT,
+ (Some(sym::sig_ign), None) => sigpipe::SIG_IGN,
+ (Some(sym::sig_dfl), None) => sigpipe::SIG_DFL,
+ (_, Some(_)) => {
+ // Keep going so that `fn emit_malformed_attribute()` can print
+ // an excellent error message
+ sigpipe::DEFAULT
+ }
+ _ => {
+ tcx.sess.emit_err(UnixSigpipeValues { span: attr.span });
+ sigpipe::DEFAULT
+ }
+ }
+ } else {
+ sigpipe::DEFAULT
+ }
+}
+
fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) {
let sp = tcx.def_span(CRATE_DEF_ID);
if *tcx.sess.parse_sess.reached_eof.borrow() {
@@ -178,52 +194,29 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) {
}
// There is no main function.
- let mut err = struct_span_err!(
- tcx.sess,
- DUMMY_SP,
- E0601,
- "`main` function not found in crate `{}`",
- tcx.crate_name(LOCAL_CRATE)
- );
- let filename = &tcx.sess.local_crate_source_file;
- let note = if !visitor.non_main_fns.is_empty() {
- for &span in &visitor.non_main_fns {
- 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");
- err.help("consider moving the `main` function definitions");
- // There were some functions named `main` though. Try to give the user a hint.
- format!(
- "the main function must be defined at the crate level{}",
- filename.as_ref().map(|f| format!(" (in `{}`)", f.display())).unwrap_or_default()
- )
- } 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")
- };
+ let mut has_filename = true;
+ let filename = tcx.sess.local_crate_source_file.clone().unwrap_or_else(|| {
+ has_filename = false;
+ Default::default()
+ });
+ let main_def_opt = tcx.resolutions(()).main_def;
+ let diagnostic_id = error_code!(E0601);
+ let add_teach_note = tcx.sess.teach(&diagnostic_id);
// 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.
- 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(), &note);
- } else {
- err.note(&note);
- }
-
- 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");
- }
-
- if tcx.sess.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/",
- );
- }
- err.emit();
+ let file_empty = !tcx.sess.source_map().lookup_line(sp.hi()).is_ok();
+
+ tcx.sess.emit_err(NoMainErr {
+ sp,
+ crate_name: tcx.crate_name(LOCAL_CRATE),
+ has_filename,
+ filename,
+ file_empty,
+ non_main_fns: visitor.non_main_fns.clone(),
+ main_def_opt,
+ add_teach_note,
+ });
}
pub fn provide(providers: &mut Providers) {