1
1
use rustc_ast:: entry:: EntryPointType ;
2
2
use rustc_errors:: struct_span_err;
3
- use rustc_hir:: def_id:: { DefId , CRATE_DEF_ID , CRATE_DEF_INDEX , LOCAL_CRATE } ;
3
+ use rustc_hir:: def_id:: { DefId , LocalDefId , CRATE_DEF_ID , CRATE_DEF_INDEX , LOCAL_CRATE } ;
4
4
use rustc_hir:: itemlikevisit:: ItemLikeVisitor ;
5
- use rustc_hir:: { ForeignItem , HirId , ImplItem , Item , ItemKind , Node , TraitItem , CRATE_HIR_ID } ;
5
+ use rustc_hir:: { ForeignItem , 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 ;
@@ -18,14 +18,14 @@ struct EntryContext<'a, 'tcx> {
18
18
map : Map < ' tcx > ,
19
19
20
20
/// The function that has attribute named `main`.
21
- attr_main_fn : Option < ( HirId , Span ) > ,
21
+ attr_main_fn : Option < ( LocalDefId , Span ) > ,
22
22
23
23
/// The function that has the attribute 'start' on it.
24
- start_fn : Option < ( HirId , Span ) > ,
24
+ start_fn : Option < ( LocalDefId , Span ) > ,
25
25
26
26
/// The functions that one might think are `main` but aren't, e.g.
27
27
/// main functions not defined at the top level. For diagnostics.
28
- non_main_fns : Vec < ( HirId , Span ) > ,
28
+ non_main_fns : Vec < Span > ,
29
29
}
30
30
31
31
impl < ' a , ' tcx > ItemLikeVisitor < ' tcx > for EntryContext < ' a , ' tcx > {
@@ -112,11 +112,11 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
112
112
}
113
113
EntryPointType :: MainNamed => ( ) ,
114
114
EntryPointType :: OtherMain => {
115
- ctxt. non_main_fns . push ( ( item. hir_id ( ) , item . span ) ) ;
115
+ ctxt. non_main_fns . push ( item. span ) ;
116
116
}
117
117
EntryPointType :: MainAttr => {
118
118
if ctxt. attr_main_fn . is_none ( ) {
119
- ctxt. attr_main_fn = Some ( ( item. hir_id ( ) , item. span ) ) ;
119
+ ctxt. attr_main_fn = Some ( ( item. def_id , item. span ) ) ;
120
120
} else {
121
121
struct_span_err ! (
122
122
ctxt. session,
@@ -131,7 +131,7 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
131
131
}
132
132
EntryPointType :: Start => {
133
133
if ctxt. start_fn . is_none ( ) {
134
- ctxt. start_fn = Some ( ( item. hir_id ( ) , item. span ) ) ;
134
+ ctxt. start_fn = Some ( ( item. def_id , item. span ) ) ;
135
135
} else {
136
136
struct_span_err ! ( ctxt. session, item. span, E0138 , "multiple `start` functions" )
137
137
. span_label ( ctxt. start_fn . unwrap ( ) . 1 , "previous `#[start]` function here" )
@@ -143,16 +143,16 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
143
143
}
144
144
145
145
fn configure_main ( tcx : TyCtxt < ' _ > , visitor : & EntryContext < ' _ , ' _ > ) -> Option < ( DefId , EntryFnType ) > {
146
- if let Some ( ( hir_id , _) ) = visitor. start_fn {
147
- Some ( ( tcx . hir ( ) . local_def_id ( hir_id ) . to_def_id ( ) , EntryFnType :: Start ) )
148
- } else if let Some ( ( hir_id , _) ) = visitor. attr_main_fn {
149
- Some ( ( tcx . hir ( ) . local_def_id ( hir_id ) . to_def_id ( ) , EntryFnType :: Main ) )
146
+ if let Some ( ( def_id , _) ) = visitor. start_fn {
147
+ Some ( ( def_id . to_def_id ( ) , EntryFnType :: Start ) )
148
+ } else if let Some ( ( def_id , _) ) = visitor. attr_main_fn {
149
+ Some ( ( def_id . to_def_id ( ) , EntryFnType :: Main ) )
150
150
} else {
151
151
if let Some ( main_def) = tcx. resolutions ( ( ) ) . main_def {
152
152
if let Some ( def_id) = main_def. opt_fn_def_id ( ) {
153
153
// non-local main imports are handled below
154
- if def_id. is_local ( ) {
155
- let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id. expect_local ( ) ) ;
154
+ if let Some ( def_id) = def_id . as_local ( ) {
155
+ let hir_id = tcx. hir ( ) . local_def_id_to_hir_id ( def_id) ;
156
156
if matches ! ( tcx. hir( ) . find( hir_id) , Some ( Node :: ForeignItem ( _) ) ) {
157
157
tcx. sess
158
158
. struct_span_err (
@@ -201,7 +201,7 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
201
201
) ;
202
202
let filename = & tcx. sess . local_crate_source_file ;
203
203
let note = if !visitor. non_main_fns . is_empty ( ) {
204
- for & ( _ , span) in & visitor. non_main_fns {
204
+ for & span in & visitor. non_main_fns {
205
205
err. span_note ( span, "here is a function named `main`" ) ;
206
206
}
207
207
err. note ( "you have one or more functions named `main` not defined at the crate level" ) ;
0 commit comments