Skip to content

Commit 5f3d360

Browse files
authored
Rollup merge of #107942 - compiler-errors:tighter-inherent-impl-bad-spans, r=Nilstrieb
Tighter spans for bad inherent `impl` self types Self-explanatory
2 parents 5c94f4a + e20f6ff commit 5f3d360

File tree

12 files changed

+74
-110
lines changed

12 files changed

+74
-110
lines changed

compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs

+17-16
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const ADD_ATTR: &str =
5757
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items";
5858

5959
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) {
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();

tests/ui/coherence/issue-85026.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
error[E0785]: cannot define inherent `impl` for a dyn auto trait
2-
--> $DIR/issue-85026.rs:5:6
2+
--> $DIR/issue-85026.rs:5:1
33
|
44
LL | impl dyn AutoTrait {}
5-
| ^^^^^^^^^^^^^ impl requires at least one non-auto trait
5+
| ^^^^^^^^^^^^^^^^^^ impl requires at least one non-auto trait
66
|
77
= note: define and implement a new trait or type instead
88

99
error[E0785]: cannot define inherent `impl` for a dyn auto trait
10-
--> $DIR/issue-85026.rs:8:6
10+
--> $DIR/issue-85026.rs:8:1
1111
|
1212
LL | impl dyn Unpin {}
13-
| ^^^^^^^^^ impl requires at least one non-auto trait
13+
| ^^^^^^^^^^^^^^ impl requires at least one non-auto trait
1414
|
1515
= note: define and implement a new trait or type instead
1616

tests/ui/const-generics/wrong-normalization.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0118]: no nominal type found for inherent implementation
2-
--> $DIR/wrong-normalization.rs:16:6
2+
--> $DIR/wrong-normalization.rs:16:1
33
|
44
LL | impl <I8<{i8::MIN}> as Identity>::Identity {
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
66
|
77
= note: either implement a trait on it or create a newtype to wrap it instead
88

tests/ui/error-codes/E0116.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher
22
--> $DIR/E0116.rs:1:1
33
|
44
LL | impl Vec<u8> {}
5-
| ^^^^^^^^^^^^^^^ impl for type defined outside of crate.
5+
| ^^^^^^^^^^^^ impl for type defined outside of crate.
66
|
77
= note: define and implement a trait or new type instead
88

tests/ui/error-codes/E0118.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
error[E0118]: no nominal type found for inherent implementation
2-
--> $DIR/E0118.rs:1:9
2+
--> $DIR/E0118.rs:1:1
33
|
44
LL | impl<T> T {
5-
| ^ impl requires a nominal type
5+
| ^^^^^^^^^ impl requires a nominal type
66
|
77
= note: either implement a trait on it or create a newtype to wrap it instead
88

tests/ui/error-codes/E0390.stderr

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
error[E0390]: cannot define inherent `impl` for primitive types
2-
--> $DIR/E0390.rs:5:6
2+
--> $DIR/E0390.rs:5:1
33
|
44
LL | impl *mut Foo {}
5-
| ^^^^^^^^
5+
| ^^^^^^^^^^^^^
66
|
77
= help: consider using an extension trait instead
88

99
error[E0390]: cannot define inherent `impl` for primitive types
10-
--> $DIR/E0390.rs:7:6
10+
--> $DIR/E0390.rs:7:1
1111
|
1212
LL | impl fn(Foo) {}
13-
| ^^^^^^^
13+
| ^^^^^^^^^^^^
1414
|
1515
= help: consider using an extension trait instead
1616

tests/ui/impl-trait/where-allowed.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -303,10 +303,10 @@ LL | fn in_method_generic_param_default<T = impl Debug>(_: T) {}
303303
= note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
304304

305305
error[E0118]: no nominal type found for inherent implementation
306-
--> $DIR/where-allowed.rs:233:23
306+
--> $DIR/where-allowed.rs:233:1
307307
|
308308
LL | impl <T = impl Debug> T {}
309-
| ^ impl requires a nominal type
309+
| ^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
310310
|
311311
= note: either implement a trait on it or create a newtype to wrap it instead
312312

tests/ui/incoherent-inherent-impls/needs-has-incoherent-impls.stderr

+22-56
Original file line numberDiff line numberDiff line change
@@ -1,114 +1,80 @@
11
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
22
--> $DIR/needs-has-incoherent-impls.rs:5:1
33
|
4-
LL | / impl extern_crate::StructWithAttr {
5-
LL | |
6-
LL | | fn foo() {}
7-
LL | | }
8-
| |_^
4+
LL | impl extern_crate::StructWithAttr {
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
96
|
107
= help: consider moving this inherent impl into the crate defining the type if possible
118
help: alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items
129
--> $DIR/needs-has-incoherent-impls.rs:7:5
1310
|
1411
LL | fn foo() {}
15-
| ^^^^^^^^^^^
12+
| ^^^^^^^^
1613

1714
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
1815
--> $DIR/needs-has-incoherent-impls.rs:13:1
1916
|
20-
LL | / impl extern_crate::StructNoAttr {
21-
LL | |
22-
LL | | fn foo() {}
23-
LL | | }
24-
| |_^
17+
LL | impl extern_crate::StructNoAttr {
18+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2519
|
2620
= help: consider moving this inherent impl into the crate defining the type if possible
2721
help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
2822
--> $DIR/needs-has-incoherent-impls.rs:13:1
2923
|
30-
LL | / impl extern_crate::StructNoAttr {
31-
LL | |
32-
LL | | fn foo() {}
33-
LL | | }
34-
| |_^
24+
LL | impl extern_crate::StructNoAttr {
25+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
3526

3627
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
3728
--> $DIR/needs-has-incoherent-impls.rs:17:1
3829
|
39-
LL | / impl extern_crate::StructNoAttr {
40-
LL | |
41-
LL | | #[rustc_allow_incoherent_impl]
42-
LL | | fn bar() {}
43-
LL | | }
44-
| |_^
30+
LL | impl extern_crate::StructNoAttr {
31+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4532
|
4633
= help: consider moving this inherent impl into the crate defining the type if possible
4734
help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
4835
--> $DIR/needs-has-incoherent-impls.rs:17:1
4936
|
50-
LL | / impl extern_crate::StructNoAttr {
51-
LL | |
52-
LL | | #[rustc_allow_incoherent_impl]
53-
LL | | fn bar() {}
54-
LL | | }
55-
| |_^
37+
LL | impl extern_crate::StructNoAttr {
38+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
5639

5740
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
5841
--> $DIR/needs-has-incoherent-impls.rs:22:1
5942
|
60-
LL | / impl extern_crate::EnumWithAttr {
61-
LL | |
62-
LL | | fn foo() {}
63-
LL | | }
64-
| |_^
43+
LL | impl extern_crate::EnumWithAttr {
44+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
6545
|
6646
= help: consider moving this inherent impl into the crate defining the type if possible
6747
help: alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items
6848
--> $DIR/needs-has-incoherent-impls.rs:24:5
6949
|
7050
LL | fn foo() {}
71-
| ^^^^^^^^^^^
51+
| ^^^^^^^^
7252

7353
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
7454
--> $DIR/needs-has-incoherent-impls.rs:30:1
7555
|
76-
LL | / impl extern_crate::EnumNoAttr {
77-
LL | |
78-
LL | | fn foo() {}
79-
LL | | }
80-
| |_^
56+
LL | impl extern_crate::EnumNoAttr {
57+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8158
|
8259
= help: consider moving this inherent impl into the crate defining the type if possible
8360
help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
8461
--> $DIR/needs-has-incoherent-impls.rs:30:1
8562
|
86-
LL | / impl extern_crate::EnumNoAttr {
87-
LL | |
88-
LL | | fn foo() {}
89-
LL | | }
90-
| |_^
63+
LL | impl extern_crate::EnumNoAttr {
64+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
9165

9266
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
9367
--> $DIR/needs-has-incoherent-impls.rs:34:1
9468
|
95-
LL | / impl extern_crate::EnumNoAttr {
96-
LL | |
97-
LL | | #[rustc_allow_incoherent_impl]
98-
LL | | fn bar() {}
99-
LL | | }
100-
| |_^
69+
LL | impl extern_crate::EnumNoAttr {
70+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10171
|
10272
= help: consider moving this inherent impl into the crate defining the type if possible
10373
help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
10474
--> $DIR/needs-has-incoherent-impls.rs:34:1
10575
|
106-
LL | / impl extern_crate::EnumNoAttr {
107-
LL | |
108-
LL | | #[rustc_allow_incoherent_impl]
109-
LL | | fn bar() {}
110-
LL | | }
111-
| |_^
76+
LL | impl extern_crate::EnumNoAttr {
77+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
11278

11379
error: aborting due to 6 previous errors
11480

tests/ui/incoherent-inherent-impls/no-attr-empty-impl.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,39 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher
22
--> $DIR/no-attr-empty-impl.rs:4:1
33
|
44
LL | impl extern_crate::StructWithAttr {}
5-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
66
|
77
= note: define and implement a trait or new type instead
88

99
error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
1010
--> $DIR/no-attr-empty-impl.rs:7:1
1111
|
1212
LL | impl extern_crate::StructNoAttr {}
13-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
1414
|
1515
= note: define and implement a trait or new type instead
1616

1717
error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
1818
--> $DIR/no-attr-empty-impl.rs:10:1
1919
|
2020
LL | impl extern_crate::EnumWithAttr {}
21-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
21+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
2222
|
2323
= note: define and implement a trait or new type instead
2424

2525
error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
2626
--> $DIR/no-attr-empty-impl.rs:13:1
2727
|
2828
LL | impl extern_crate::EnumNoAttr {}
29-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
29+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
3030
|
3131
= note: define and implement a trait or new type instead
3232

3333
error[E0390]: cannot define inherent `impl` for primitive types
34-
--> $DIR/no-attr-empty-impl.rs:16:6
34+
--> $DIR/no-attr-empty-impl.rs:16:1
3535
|
3636
LL | impl f32 {}
37-
| ^^^
37+
| ^^^^^^^^
3838
|
3939
= help: consider using an extension trait instead
4040

0 commit comments

Comments
 (0)