@@ -2,7 +2,7 @@ use rustc_ast::entry::EntryPointType;
2
2
use rustc_errors:: struct_span_err;
3
3
use rustc_hir:: def_id:: { DefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
4
4
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 } ;
6
6
use rustc_middle:: hir:: map:: Map ;
7
7
use rustc_middle:: ty:: query:: Providers ;
8
8
use rustc_middle:: ty:: TyCtxt ;
@@ -148,6 +148,20 @@ fn configure_main(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) -> Option<(De
148
148
} else if let Some ( ( hir_id, _) ) = visitor. attr_main_fn {
149
149
Some ( ( tcx. hir ( ) . local_def_id ( hir_id) . to_def_id ( ) , EntryFnType :: Main ) )
150
150
} 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
+
151
165
if tcx. main_def . unwrap ( ) . is_import && !tcx. features ( ) . imported_main {
152
166
let span = tcx. main_def . unwrap ( ) . span ;
153
167
feature_err (
0 commit comments