Skip to content

Commit f8ac8fd

Browse files
committed
Auto merge of #86190 - asquared31415:extern-main-86110-fix, r=varkor
Fix ICE when `main` is declared in an `extern` block Changes in #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 #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 #86110
2 parents 1034282 + 9b2ba6d commit f8ac8fd

File tree

5 files changed

+32
-2
lines changed

5 files changed

+32
-2
lines changed

compiler/rustc_ast/src/entry.rs

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
#[derive(Debug)]
12
pub enum EntryPointType {
23
None,
34
MainNamed,

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ pub struct ResolverOutputs {
128128
pub main_def: Option<MainDefinition>,
129129
}
130130

131-
#[derive(Clone, Copy)]
131+
#[derive(Clone, Copy, Debug)]
132132
pub struct MainDefinition {
133133
pub res: Res<ast::NodeId>,
134134
pub is_import: bool,

compiler/rustc_passes/src/entry.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use rustc_ast::entry::EntryPointType;
22
use rustc_errors::struct_span_err;
33
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
44
use rustc_hir::itemlikevisit::ItemLikeVisitor;
5-
use rustc_hir::{ForeignItem, HirId, ImplItem, Item, ItemKind, TraitItem, CRATE_HIR_ID};
5+
use rustc_hir::{ForeignItem, HirId, ImplItem, Item, ItemKind, Node, TraitItem, CRATE_HIR_ID};
66
use rustc_middle::hir::map::Map;
77
use rustc_middle::ty::query::Providers;
88
use rustc_middle::ty::TyCtxt;
@@ -148,6 +148,20 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De
148148
} else if let Some((hir_id, _)) = visitor.attr_main_fn {
149149
Some((tcx.hir().local_def_id(hir_id).to_def_id(), EntryFnType::Main))
150150
} else if let Some(def_id) = tcx.main_def.and_then(|main_def| main_def.opt_fn_def_id()) {
151+
// non-local main imports are handled below
152+
if def_id.is_local() {
153+
let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local());
154+
if matches!(tcx.hir().find(hir_id), Some(Node::ForeignItem(_))) {
155+
tcx.sess
156+
.struct_span_err(
157+
tcx.hir().span(hir_id),
158+
"the `main` function cannot be declared in an `extern` block",
159+
)
160+
.emit();
161+
return None;
162+
}
163+
}
164+
151165
if tcx.main_def.unwrap().is_import && !tcx.features().imported_main {
152166
let span = tcx.main_def.unwrap().span;
153167
feature_err(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// missing and missing2 exist to make sure that the error only happens on a `main` declaration
2+
extern "C" {
3+
fn missing();
4+
fn main();
5+
//~^ the `main` function cannot be declared in an `extern` block
6+
fn missing2();
7+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: the `main` function cannot be declared in an `extern` block
2+
--> $DIR/extern-main-issue-86110.rs:4:5
3+
|
4+
LL | fn main();
5+
| ^^^^^^^^^^
6+
7+
error: aborting due to previous error
8+

0 commit comments

Comments
 (0)