@@ -57,7 +57,7 @@ const ADD_ATTR: &str =
5757 "alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items" ;
5858
5959impl < ' tcx > InherentCollect < ' tcx > {
60- fn check_def_id ( & mut self , item : & hir:: Item < ' _ > , self_ty : Ty < ' tcx > , def_id : DefId ) {
60+ fn check_def_id ( & mut self , item : & hir:: Item < ' _ > , self_ty : Ty < ' tcx > , def_id : DefId , span : Span ) {
6161 let impl_def_id = item. owner_id ;
6262 if let Some ( def_id) = def_id. as_local ( ) {
6363 // Add the implementation to the mapping from implementation to base
@@ -76,12 +76,12 @@ impl<'tcx> InherentCollect<'tcx> {
7676 if !self . tcx . has_attr ( def_id, sym:: rustc_has_incoherent_inherent_impls) {
7777 struct_span_err ! (
7878 self . tcx. sess,
79- item . span,
79+ span,
8080 E0390 ,
8181 "cannot define inherent `impl` for a type outside of the crate where the type is defined" ,
8282 )
8383 . help ( INTO_DEFINING_CRATE )
84- . span_help ( item . span , ADD_ATTR_TO_TY )
84+ . span_help ( span, ADD_ATTR_TO_TY )
8585 . emit ( ) ;
8686 return ;
8787 }
@@ -93,12 +93,12 @@ impl<'tcx> InherentCollect<'tcx> {
9393 {
9494 struct_span_err ! (
9595 self . tcx. sess,
96- item . span,
96+ span,
9797 E0390 ,
9898 "cannot define inherent `impl` for a type outside of the crate where the type is defined" ,
9999 )
100100 . help ( INTO_DEFINING_CRATE )
101- . span_help ( impl_item . span , ADD_ATTR )
101+ . span_help ( self . tcx . hir ( ) . span ( impl_item . id . hir_id ( ) ) , ADD_ATTR )
102102 . emit ( ) ;
103103 return ;
104104 }
@@ -112,12 +112,12 @@ impl<'tcx> InherentCollect<'tcx> {
112112 } else {
113113 struct_span_err ! (
114114 self . tcx. sess,
115- item . span,
115+ span,
116116 E0116 ,
117117 "cannot define inherent `impl` for a type outside of the crate \
118118 where the type is defined"
119119 )
120- . span_label ( item . span , "impl for type defined outside of crate." )
120+ . span_label ( span, "impl for type defined outside of crate." )
121121 . note ( "define and implement a trait or new type instead" )
122122 . emit ( ) ;
123123 }
@@ -182,29 +182,30 @@ impl<'tcx> InherentCollect<'tcx> {
182182 }
183183
184184 let item = self . tcx . hir ( ) . item ( id) ;
185- let hir:: ItemKind :: Impl ( hir:: Impl { of_trait : None , self_ty : ty, items, .. } ) = item. kind else {
185+ let impl_span = self . tcx . hir ( ) . span ( id. hir_id ( ) ) ;
186+ let hir:: ItemKind :: Impl ( hir:: Impl { of_trait : None , items, .. } ) = item. kind else {
186187 return ;
187188 } ;
188189
189190 let self_ty = self . tcx . type_of ( item. owner_id ) ;
190191 match * self_ty. kind ( ) {
191192 ty:: Adt ( def, _) => {
192- self . check_def_id ( item, self_ty, def. did ( ) ) ;
193+ self . check_def_id ( item, self_ty, def. did ( ) , impl_span ) ;
193194 }
194195 ty:: Foreign ( did) => {
195- self . check_def_id ( item, self_ty, did) ;
196+ self . check_def_id ( item, self_ty, did, impl_span ) ;
196197 }
197198 ty:: Dynamic ( data, ..) if data. principal_def_id ( ) . is_some ( ) => {
198- self . check_def_id ( item, self_ty, data. principal_def_id ( ) . unwrap ( ) ) ;
199+ self . check_def_id ( item, self_ty, data. principal_def_id ( ) . unwrap ( ) , impl_span ) ;
199200 }
200201 ty:: Dynamic ( ..) => {
201202 struct_span_err ! (
202203 self . tcx. sess,
203- ty . span ,
204+ impl_span ,
204205 E0785 ,
205206 "cannot define inherent `impl` for a dyn auto trait"
206207 )
207- . span_label ( ty . span , "impl requires at least one non-auto trait" )
208+ . span_label ( impl_span , "impl requires at least one non-auto trait" )
208209 . note ( "define and implement a new trait or type instead" )
209210 . emit ( ) ;
210211 }
@@ -221,17 +222,17 @@ impl<'tcx> InherentCollect<'tcx> {
221222 | ty:: Never
222223 | ty:: FnPtr ( _)
223224 | ty:: Tuple ( ..) => {
224- self . check_primitive_impl ( item. owner_id . def_id , self_ty, items, ty . span )
225+ self . check_primitive_impl ( item. owner_id . def_id , self_ty, items, impl_span )
225226 }
226227 ty:: Alias ( ..) | ty:: Param ( _) => {
227228 let mut err = struct_span_err ! (
228229 self . tcx. sess,
229- ty . span ,
230+ impl_span ,
230231 E0118 ,
231232 "no nominal type found for inherent implementation"
232233 ) ;
233234
234- err. span_label ( ty . span , "impl requires a nominal type" )
235+ err. span_label ( impl_span , "impl requires a nominal type" )
235236 . note ( "either implement a trait on it or create a newtype to wrap it instead" ) ;
236237
237238 err. emit ( ) ;
0 commit comments