@@ -2,7 +2,7 @@ use rustc_ast::entry::EntryPointType;
22use rustc_errors:: struct_span_err;
33use rustc_hir:: def_id:: { DefId , CRATE_DEF_INDEX , LOCAL_CRATE } ;
44use 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 } ;
66use rustc_middle:: hir:: map:: Map ;
77use rustc_middle:: ty:: query:: Providers ;
88use 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 (
0 commit comments