@@ -57,7 +57,7 @@ const ADD_ATTR: &str =
57
57
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items" ;
58
58
59
59
impl < ' 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 ) {
61
61
let impl_def_id = item. owner_id ;
62
62
if let Some ( def_id) = def_id. as_local ( ) {
63
63
// Add the implementation to the mapping from implementation to base
@@ -76,12 +76,12 @@ impl<'tcx> InherentCollect<'tcx> {
76
76
if !self . tcx . has_attr ( def_id, sym:: rustc_has_incoherent_inherent_impls) {
77
77
struct_span_err ! (
78
78
self . tcx. sess,
79
- item . span,
79
+ span,
80
80
E0390 ,
81
81
"cannot define inherent `impl` for a type outside of the crate where the type is defined" ,
82
82
)
83
83
. help ( INTO_DEFINING_CRATE )
84
- . span_help ( item . span , ADD_ATTR_TO_TY )
84
+ . span_help ( span, ADD_ATTR_TO_TY )
85
85
. emit ( ) ;
86
86
return ;
87
87
}
@@ -93,12 +93,12 @@ impl<'tcx> InherentCollect<'tcx> {
93
93
{
94
94
struct_span_err ! (
95
95
self . tcx. sess,
96
- item . span,
96
+ span,
97
97
E0390 ,
98
98
"cannot define inherent `impl` for a type outside of the crate where the type is defined" ,
99
99
)
100
100
. 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 )
102
102
. emit ( ) ;
103
103
return ;
104
104
}
@@ -112,12 +112,12 @@ impl<'tcx> InherentCollect<'tcx> {
112
112
} else {
113
113
struct_span_err ! (
114
114
self . tcx. sess,
115
- item . span,
115
+ span,
116
116
E0116 ,
117
117
"cannot define inherent `impl` for a type outside of the crate \
118
118
where the type is defined"
119
119
)
120
- . span_label ( item . span , "impl for type defined outside of crate." )
120
+ . span_label ( span, "impl for type defined outside of crate." )
121
121
. note ( "define and implement a trait or new type instead" )
122
122
. emit ( ) ;
123
123
}
@@ -182,29 +182,30 @@ impl<'tcx> InherentCollect<'tcx> {
182
182
}
183
183
184
184
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 {
186
187
return ;
187
188
} ;
188
189
189
190
let self_ty = self . tcx . type_of ( item. owner_id ) ;
190
191
match * self_ty. kind ( ) {
191
192
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 ) ;
193
194
}
194
195
ty:: Foreign ( did) => {
195
- self . check_def_id ( item, self_ty, did) ;
196
+ self . check_def_id ( item, self_ty, did, impl_span ) ;
196
197
}
197
198
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 ) ;
199
200
}
200
201
ty:: Dynamic ( ..) => {
201
202
struct_span_err ! (
202
203
self . tcx. sess,
203
- ty . span ,
204
+ impl_span ,
204
205
E0785 ,
205
206
"cannot define inherent `impl` for a dyn auto trait"
206
207
)
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" )
208
209
. note ( "define and implement a new trait or type instead" )
209
210
. emit ( ) ;
210
211
}
@@ -221,17 +222,17 @@ impl<'tcx> InherentCollect<'tcx> {
221
222
| ty:: Never
222
223
| ty:: FnPtr ( _)
223
224
| 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 )
225
226
}
226
227
ty:: Alias ( ..) | ty:: Param ( _) => {
227
228
let mut err = struct_span_err ! (
228
229
self . tcx. sess,
229
- ty . span ,
230
+ impl_span ,
230
231
E0118 ,
231
232
"no nominal type found for inherent implementation"
232
233
) ;
233
234
234
- err. span_label ( ty . span , "impl requires a nominal type" )
235
+ err. span_label ( impl_span , "impl requires a nominal type" )
235
236
. note ( "either implement a trait on it or create a newtype to wrap it instead" ) ;
236
237
237
238
err. emit ( ) ;
0 commit comments