Skip to content

Commit

Permalink
Auto merge of rust-lang#86190 - asquared31415:extern-main-86110-fix, …
Browse files Browse the repository at this point in the history
…r=varkor

Fix ICE when `main` is declared in an `extern` block

Changes in rust-lang#84401 to implement `imported_main` changed how the crate entry point is found, and a declared `main` in an `extern` block was detected erroneously.  This was causing the ICE described in rust-lang#86110.

This PR adds a check for this case and emits an error instead.  Previously a `main` declaration in an `extern` block was not detected as an entry point at all, so emitting an error shouldn't break anything that worked previously.  In 1.52.1 stable this is demonstrated, with a `` `main` function not found`` error.

Fixes rust-lang#86110
  • Loading branch information
bors committed Jul 1, 2021
2 parents 1034282 + 9b2ba6d commit f8ac8fd
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 2 deletions.
1 change: 1 addition & 0 deletions compiler/rustc_ast/src/entry.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#[derive(Debug)]
pub enum EntryPointType {
None,
MainNamed,
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ pub struct ResolverOutputs {
pub main_def: Option<MainDefinition>,
}

#[derive(Clone, Copy)]
#[derive(Clone, Copy, Debug)]
pub struct MainDefinition {
pub res: Res<ast::NodeId>,
pub is_import: bool,
Expand Down
16 changes: 15 additions & 1 deletion compiler/rustc_passes/src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use rustc_ast::entry::EntryPointType;
use rustc_errors::struct_span_err;
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
use rustc_hir::itemlikevisit::ItemLikeVisitor;
use rustc_hir::{ForeignItem, HirId, ImplItem, Item, ItemKind, TraitItem, CRATE_HIR_ID};
use rustc_hir::{ForeignItem, HirId, 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;
Expand Down Expand Up @@ -148,6 +148,20 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De
} else if let Some((hir_id, _)) = visitor.attr_main_fn {
Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Main))
} else if let Some(def_id) = tcx.main_def.and_then(|main_def| main_def.opt_fn_def_id()) {
// non-local main imports are handled below
if def_id.is_local() {
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
if matches!(tcx.hir().find(hir_id), Some(Node::ForeignItem(_))) {
tcx.sess
.struct_span_err(
tcx.hir().span(hir_id),
"the `main` function cannot be declared in an `extern` block",
)
.emit();
return None;
}
}

if tcx.main_def.unwrap().is_import && !tcx.features().imported_main {
let span = tcx.main_def.unwrap().span;
feature_err(
Expand Down
7 changes: 7 additions & 0 deletions src/test/ui/extern/extern-main-issue-86110.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// missing and missing2 exist to make sure that the error only happens on a `main` declaration
extern "C" {
fn missing();
fn main();
//~^ the `main` function cannot be declared in an `extern` block
fn missing2();
}
8 changes: 8 additions & 0 deletions src/test/ui/extern/extern-main-issue-86110.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
error: the `main` function cannot be declared in an `extern` block
--> $DIR/extern-main-issue-86110.rs:4:5
|
LL | fn main();
| ^^^^^^^^^^

error: aborting due to previous error

0 comments on commit f8ac8fd

Please sign in to comment.