]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_passes/src/entry.rs
Auto merge of #94609 - esp-rs:esp-idf-stat-type-fixes, r=Mark-Simulacrum
[rust.git] / compiler / rustc_passes / src / entry.rs
index f5040a373c29637b1c72a243762b73ce58a76993..db083d0453bc05d7cc9a6924a3a3e3ca845eca8c 100644 (file)
@@ -1,21 +1,18 @@
 use rustc_ast::entry::EntryPointType;
 use rustc_errors::struct_span_err;
-use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE};
+use rustc_hir::def_id::{DefId, LocalDefId, CRATE_DEF_ID, LOCAL_CRATE};
 use rustc_hir::itemlikevisit::ItemLikeVisitor;
 use rustc_hir::{ForeignItem, ImplItem, Item, ItemKind, Node, TraitItem, CRATE_HIR_ID};
-use rustc_middle::hir::map::Map;
 use rustc_middle::ty::query::Providers;
-use rustc_middle::ty::TyCtxt;
+use rustc_middle::ty::{DefIdTree, TyCtxt};
 use rustc_session::config::{CrateType, EntryFnType};
 use rustc_session::parse::feature_err;
 use rustc_session::Session;
 use rustc_span::symbol::sym;
 use rustc_span::{Span, DUMMY_SP};
 
-struct EntryContext<'a, 'tcx> {
-    session: &'a Session,
-
-    map: Map<'tcx>,
+struct EntryContext<'tcx> {
+    tcx: TyCtxt<'tcx>,
 
     /// The function that has attribute named `main`.
     attr_main_fn: Option<(LocalDefId, Span)>,
@@ -28,10 +25,9 @@ struct EntryContext<'a, 'tcx> {
     non_main_fns: Vec<Span>,
 }
 
-impl<'a, 'tcx> ItemLikeVisitor<'tcx> for EntryContext<'a, 'tcx> {
+impl<'tcx> ItemLikeVisitor<'tcx> for EntryContext<'tcx> {
     fn visit_item(&mut self, item: &'tcx Item<'tcx>) {
-        let def_key = self.map.def_key(item.def_id);
-        let at_root = def_key.parent == Some(CRATE_DEF_INDEX);
+        let at_root = self.tcx.local_parent(item.def_id) == Some(CRATE_DEF_ID);
         find_item(item, self, at_root);
     }
 
@@ -60,13 +56,8 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> {
         return None;
     }
 
-    let mut ctxt = EntryContext {
-        session: tcx.sess,
-        map: tcx.hir(),
-        attr_main_fn: None,
-        start_fn: None,
-        non_main_fns: Vec::new(),
-    };
+    let mut ctxt =
+        EntryContext { tcx, attr_main_fn: None, start_fn: None, non_main_fns: Vec::new() };
 
     tcx.hir().visit_all_item_likes(&mut ctxt);
 
@@ -75,11 +66,11 @@ fn entry_fn(tcx: TyCtxt<'_>, (): ()) -> Option<(DefId, EntryFnType)> {
 
 // Beware, this is duplicated in `librustc_builtin_macros/test_harness.rs`
 // (with `ast::Item`), so make sure to keep them in sync.
-fn entry_point_type(ctxt: &EntryContext<'_, '_>, item: &Item<'_>, at_root: bool) -> EntryPointType {
-    let attrs = ctxt.map.attrs(item.hir_id());
-    if ctxt.session.contains_name(attrs, sym::start) {
+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) {
         EntryPointType::Start
-    } else if ctxt.session.contains_name(attrs, sym::rustc_main) {
+    } else if ctxt.tcx.sess.contains_name(attrs, sym::rustc_main) {
         EntryPointType::MainAttr
     } else if item.ident.name == sym::main {
         if at_root {
@@ -98,16 +89,16 @@ fn throw_attr_err(sess: &Session, span: Span, attr: &str) {
         .emit();
 }
 
-fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
+fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_>, at_root: bool) {
     match entry_point_type(ctxt, item, at_root) {
         EntryPointType::None => (),
         _ if !matches!(item.kind, ItemKind::Fn(..)) => {
-            let attrs = ctxt.map.attrs(item.hir_id());
-            if let Some(attr) = ctxt.session.find_by_name(attrs, sym::start) {
-                throw_attr_err(&ctxt.session, attr.span, "start");
+            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");
             }
-            if let Some(attr) = ctxt.session.find_by_name(attrs, sym::rustc_main) {
-                throw_attr_err(&ctxt.session, attr.span, "rustc_main");
+            if let Some(attr) = ctxt.tcx.sess.find_by_name(attrs, sym::rustc_main) {
+                throw_attr_err(&ctxt.tcx.sess, attr.span, "rustc_main");
             }
         }
         EntryPointType::MainNamed => (),
@@ -119,7 +110,7 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
                 ctxt.attr_main_fn = Some((item.def_id, item.span));
             } else {
                 struct_span_err!(
-                    ctxt.session,
+                    ctxt.tcx.sess,
                     item.span,
                     E0137,
                     "multiple functions with a `#[main]` attribute"
@@ -133,7 +124,7 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
             if ctxt.start_fn.is_none() {
                 ctxt.start_fn = Some((item.def_id, item.span));
             } else {
-                struct_span_err!(ctxt.session, item.span, E0138, "multiple `start` functions")
+                struct_span_err!(ctxt.tcx.sess, item.span, E0138, "multiple `start` functions")
                     .span_label(ctxt.start_fn.unwrap().1, "previous `#[start]` function here")
                     .span_label(item.span, "multiple `start` functions")
                     .emit();
@@ -142,46 +133,42 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
     }
 }
 
-fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(DefId, EntryFnType)> {
+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(main_def) = tcx.resolutions(()).main_def {
-            if 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() {
-                    if 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();
-                        return None;
-                    }
-                }
-
-                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",
+        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();
-                }
-                return Some((def_id, EntryFnType::Main));
+                return None;
             }
+
+            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));
         }
         no_main_err(tcx, visitor);
         None
     }
 }
 
-fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
+fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_>) {
     let sp = tcx.def_span(CRATE_DEF_ID);
     if *tcx.sess.parse_sess.reached_eof.borrow() {
         // There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about
@@ -225,11 +212,9 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
         err.note(&note);
     }
 
-    if let Some(main_def) = tcx.resolutions(()).main_def {
-        if 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 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()) {