]> git.lizzy.rs Git - rust.git/blobdiff - compiler/rustc_passes/src/entry.rs
Auto merge of #92361 - vacuus:doctest-run-test-out-lines, r=CraftSpider
[rust.git] / compiler / rustc_passes / src / entry.rs
index fdabe41dafaedff61466dea6036b6be20c155163..5a1373ad1a218dc94c0987cc3a6b2023ca0e4811 100644 (file)
@@ -148,33 +148,29 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De
     } 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
@@ -218,18 +214,16 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
     // 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.lo()).is_ok() {
-        err.set_span(sp);
-        err.span_label(sp, &note);
+    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 {
-        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()) {