1- use rustc_ast:: entry:: EntryPointType ;
1+ use rustc_ast:: { entry:: EntryPointType , Attribute } ;
22use rustc_errors:: struct_span_err;
33use rustc_hir:: def:: DefKind ;
44use rustc_hir:: def_id:: { DefId , LocalDefId , CRATE_DEF_ID , LOCAL_CRATE } ;
@@ -7,9 +7,8 @@ use rustc_middle::ty::query::Providers;
77use rustc_middle:: ty:: { DefIdTree , TyCtxt } ;
88use rustc_session:: config:: { CrateType , EntryFnType } ;
99use rustc_session:: parse:: feature_err;
10- use rustc_session:: Session ;
1110use rustc_span:: symbol:: sym;
12- use rustc_span:: { Span , DUMMY_SP } ;
11+ use rustc_span:: { Span , Symbol , DUMMY_SP } ;
1312
1413struct EntryContext < ' tcx > {
1514 tcx : TyCtxt < ' tcx > ,
@@ -72,9 +71,16 @@ fn entry_point_type(ctxt: &EntryContext<'_>, id: ItemId, at_root: bool) -> Entry
7271 }
7372}
7473
75- fn throw_attr_err ( sess : & Session , span : Span , attr : & str ) {
76- sess. struct_span_err ( span, & format ! ( "`{}` attribute can only be used on functions" , attr) )
77- . emit ( ) ;
74+ fn err_if_attr_found ( ctxt : & EntryContext < ' _ > , attrs : & [ Attribute ] , sym : Symbol ) {
75+ if let Some ( attr) = ctxt. tcx . sess . find_by_name ( attrs, sym) {
76+ ctxt. tcx
77+ . sess
78+ . struct_span_err (
79+ attr. span ,
80+ & format ! ( "`{}` attribute can only be used on functions" , sym. as_str( ) ) ,
81+ )
82+ . emit ( ) ;
83+ }
7884}
7985
8086fn find_item ( id : ItemId , ctxt : & mut EntryContext < ' _ > ) {
@@ -84,12 +90,8 @@ fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
8490 EntryPointType :: None => ( ) ,
8591 _ if !matches ! ( ctxt. tcx. def_kind( id. def_id) , DefKind :: Fn ) => {
8692 let attrs = ctxt. tcx . hir ( ) . attrs ( id. hir_id ( ) ) ;
87- if let Some ( attr) = ctxt. tcx . sess . find_by_name ( attrs, sym:: start) {
88- throw_attr_err ( & ctxt. tcx . sess , attr. span , "start" ) ;
89- }
90- if let Some ( attr) = ctxt. tcx . sess . find_by_name ( attrs, sym:: rustc_main) {
91- throw_attr_err ( & ctxt. tcx . sess , attr. span , "rustc_main" ) ;
92- }
93+ err_if_attr_found ( ctxt, attrs, sym:: start) ;
94+ err_if_attr_found ( ctxt, attrs, sym:: rustc_main) ;
9395 }
9496 EntryPointType :: MainNamed => ( ) ,
9597 EntryPointType :: OtherMain => {
0 commit comments