1
- use rustc_ast:: entry:: EntryPointType ;
1
+ use rustc_ast:: { entry:: EntryPointType , Attribute } ;
2
2
use rustc_errors:: struct_span_err;
3
3
use rustc_hir:: def:: DefKind ;
4
4
use rustc_hir:: def_id:: { DefId , LocalDefId , CRATE_DEF_ID , LOCAL_CRATE } ;
@@ -7,9 +7,8 @@ use rustc_middle::ty::query::Providers;
7
7
use rustc_middle:: ty:: { DefIdTree , TyCtxt } ;
8
8
use rustc_session:: config:: { CrateType , EntryFnType } ;
9
9
use rustc_session:: parse:: feature_err;
10
- use rustc_session:: Session ;
11
10
use rustc_span:: symbol:: sym;
12
- use rustc_span:: { Span , DUMMY_SP } ;
11
+ use rustc_span:: { Span , Symbol , DUMMY_SP } ;
13
12
14
13
struct EntryContext < ' tcx > {
15
14
tcx : TyCtxt < ' tcx > ,
@@ -72,9 +71,16 @@ fn entry_point_type(ctxt: &EntryContext<'_>, id: ItemId, at_root: bool) -> Entry
72
71
}
73
72
}
74
73
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
+ }
78
84
}
79
85
80
86
fn find_item ( id : ItemId , ctxt : & mut EntryContext < ' _ > ) {
@@ -84,12 +90,8 @@ fn find_item(id: ItemId, ctxt: &mut EntryContext<'_>) {
84
90
EntryPointType :: None => ( ) ,
85
91
_ if !matches ! ( ctxt. tcx. def_kind( id. def_id) , DefKind :: Fn ) => {
86
92
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) ;
93
95
}
94
96
EntryPointType :: MainNamed => ( ) ,
95
97
EntryPointType :: OtherMain => {
0 commit comments