@@ -17,17 +17,17 @@ struct EntryContext<'a, 'tcx> {
1717 map : Map < ' tcx > ,
1818
1919 /// The top-level function called `main`.
20- main_fn : Option < ( HirId , Span ) > ,
20+ main_fn : Option < HirId > ,
2121
2222 /// The function that has attribute named `main`.
23- attr_main_fn : Option < ( HirId , Span ) > ,
23+ attr_main_fn : Option < HirId > ,
2424
2525 /// The function that has the attribute 'start' on it.
26- start_fn : Option < ( HirId , Span ) > ,
26+ start_fn : Option < HirId > ,
2727
2828 /// The functions that one might think are `main` but aren't, e.g.
2929 /// main functions not defined at the top level. For diagnostics.
30- non_main_fns : Vec < ( HirId , Span ) > ,
30+ non_main_fns : Vec < HirId > ,
3131}
3232
3333impl < ' a , ' tcx > ItemLikeVisitor < ' tcx > for EntryContext < ' a , ' tcx > {
@@ -117,37 +117,42 @@ fn find_item(item: &Item<'_>, ctxt: &mut EntryContext<'_, '_>, at_root: bool) {
117117 }
118118 EntryPointType :: MainNamed => {
119119 if ctxt. main_fn . is_none ( ) {
120- ctxt. main_fn = Some ( ( item. hir_id ( ) , item . span ) ) ;
120+ ctxt. main_fn = Some ( item. hir_id ( ) ) ;
121121 } else {
122- struct_span_err ! ( ctxt. session, item. span, E0136 , "multiple `main` functions" )
122+ let item_span = ctxt. map . span_with_body ( item. hir_id ( ) ) ;
123+ struct_span_err ! ( ctxt. session, item_span, E0136 , "multiple `main` functions" )
123124 . emit ( ) ;
124125 }
125126 }
126127 EntryPointType :: OtherMain => {
127- ctxt. non_main_fns . push ( ( item. hir_id ( ) , item . span ) ) ;
128+ ctxt. non_main_fns . push ( item. hir_id ( ) ) ;
128129 }
129130 EntryPointType :: MainAttr => {
130131 if ctxt. attr_main_fn . is_none ( ) {
131- ctxt. attr_main_fn = Some ( ( item. hir_id ( ) , item . span ) ) ;
132+ ctxt. attr_main_fn = Some ( item. hir_id ( ) ) ;
132133 } else {
134+ let item_span = ctxt. map . span_with_body ( item. hir_id ( ) ) ;
135+ let old_span = ctxt. map . span_with_body ( ctxt. attr_main_fn . unwrap ( ) ) ;
133136 struct_span_err ! (
134137 ctxt. session,
135- item . span ,
138+ item_span ,
136139 E0137 ,
137140 "multiple functions with a `#[main]` attribute"
138141 )
139- . span_label ( item . span , "additional `#[main]` function" )
140- . span_label ( ctxt . attr_main_fn . unwrap ( ) . 1 , "first `#[main]` function" )
142+ . span_label ( item_span , "additional `#[main]` function" )
143+ . span_label ( old_span , "first `#[main]` function" )
141144 . emit ( ) ;
142145 }
143146 }
144147 EntryPointType :: Start => {
145148 if ctxt. start_fn . is_none ( ) {
146- ctxt. start_fn = Some ( ( item. hir_id ( ) , item . span ) ) ;
149+ ctxt. start_fn = Some ( item. hir_id ( ) ) ;
147150 } else {
148- struct_span_err ! ( ctxt. session, item. span, E0138 , "multiple `start` functions" )
149- . span_label ( ctxt. start_fn . unwrap ( ) . 1 , "previous `#[start]` function here" )
150- . span_label ( item. span , "multiple `start` functions" )
151+ let item_span = ctxt. map . span_with_body ( item. hir_id ( ) ) ;
152+ let old_span = ctxt. map . span_with_body ( ctxt. start_fn . unwrap ( ) ) ;
153+ struct_span_err ! ( ctxt. session, item_span, E0138 , "multiple `start` functions" )
154+ . span_label ( old_span, "previous `#[start]` function here" )
155+ . span_label ( item_span, "multiple `start` functions" )
151156 . emit ( ) ;
152157 }
153158 }
@@ -158,11 +163,11 @@ fn configure_main(
158163 tcx : TyCtxt < ' _ > ,
159164 visitor : & EntryContext < ' _ , ' _ > ,
160165) -> Option < ( LocalDefId , EntryFnType ) > {
161- if let Some ( ( hir_id, _ ) ) = visitor. start_fn {
166+ if let Some ( hir_id) = visitor. start_fn {
162167 Some ( ( tcx. hir ( ) . local_def_id ( hir_id) , EntryFnType :: Start ) )
163- } else if let Some ( ( hir_id, _ ) ) = visitor. attr_main_fn {
168+ } else if let Some ( hir_id) = visitor. attr_main_fn {
164169 Some ( ( tcx. hir ( ) . local_def_id ( hir_id) , EntryFnType :: Main ) )
165- } else if let Some ( ( hir_id, _ ) ) = visitor. main_fn {
170+ } else if let Some ( hir_id) = visitor. main_fn {
166171 Some ( ( tcx. hir ( ) . local_def_id ( hir_id) , EntryFnType :: Main ) )
167172 } else {
168173 no_main_err ( tcx, visitor) ;
@@ -171,7 +176,7 @@ fn configure_main(
171176}
172177
173178fn no_main_err ( tcx : TyCtxt < ' _ > , visitor : & EntryContext < ' _ , ' _ > ) {
174- let sp = tcx. hir ( ) . span ( CRATE_HIR_ID ) ;
179+ let sp = tcx. hir ( ) . span_with_body ( CRATE_HIR_ID ) ;
175180 if * tcx. sess . parse_sess . reached_eof . borrow ( ) {
176181 // There's an unclosed brace that made the parser reach `Eof`, we shouldn't complain about
177182 // the missing `fn main()` then as it might have been hidden inside an unclosed block.
@@ -189,7 +194,8 @@ fn no_main_err(tcx: TyCtxt<'_>, visitor: &EntryContext<'_, '_>) {
189194 ) ;
190195 let filename = & tcx. sess . local_crate_source_file ;
191196 let note = if !visitor. non_main_fns . is_empty ( ) {
192- for & ( _, span) in & visitor. non_main_fns {
197+ for & hir_id in & visitor. non_main_fns {
198+ let span = tcx. hir ( ) . span_with_body ( hir_id) ;
193199 err. span_note ( span, "here is a function named `main`" ) ;
194200 }
195201 err. note ( "you have one or more functions named `main` not defined at the crate level" ) ;
0 commit comments