Tighter spans for bad inherent impl types

This commit is contained in:
Michael Goulet 2023-02-11 23:17:38 +00:00
parent 0b439b119b
commit e20f6ff1dc
12 changed files with 74 additions and 110 deletions

View File

@ -57,7 +57,7 @@ const ADD_ATTR: &str =
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items";
impl<'tcx> InherentCollect<'tcx> {
fn check_def_id(&mut self, item: &hir::Item<'_>, self_ty: Ty<'tcx>, def_id: DefId) {
fn check_def_id(&mut self, item: &hir::Item<'_>, self_ty: Ty<'tcx>, def_id: DefId, span: Span) {
let impl_def_id = item.owner_id;
if let Some(def_id) = def_id.as_local() {
// Add the implementation to the mapping from implementation to base
@ -76,12 +76,12 @@ impl<'tcx> InherentCollect<'tcx> {
if !self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) {
struct_span_err!(
self.tcx.sess,
item.span,
span,
E0390,
"cannot define inherent `impl` for a type outside of the crate where the type is defined",
)
.help(INTO_DEFINING_CRATE)
.span_help(item.span, ADD_ATTR_TO_TY)
.span_help(span, ADD_ATTR_TO_TY)
.emit();
return;
}
@ -93,12 +93,12 @@ impl<'tcx> InherentCollect<'tcx> {
{
struct_span_err!(
self.tcx.sess,
item.span,
span,
E0390,
"cannot define inherent `impl` for a type outside of the crate where the type is defined",
)
.help(INTO_DEFINING_CRATE)
.span_help(impl_item.span, ADD_ATTR)
.span_help(self.tcx.hir().span(impl_item.id.hir_id()), ADD_ATTR)
.emit();
return;
}
@ -112,12 +112,12 @@ impl<'tcx> InherentCollect<'tcx> {
} else {
struct_span_err!(
self.tcx.sess,
item.span,
span,
E0116,
"cannot define inherent `impl` for a type outside of the crate \
where the type is defined"
)
.span_label(item.span, "impl for type defined outside of crate.")
.span_label(span, "impl for type defined outside of crate.")
.note("define and implement a trait or new type instead")
.emit();
}
@ -182,29 +182,30 @@ impl<'tcx> InherentCollect<'tcx> {
}
let item = self.tcx.hir().item(id);
let hir::ItemKind::Impl(hir::Impl { of_trait: None, self_ty: ty, items, .. }) = item.kind else {
let impl_span = self.tcx.hir().span(id.hir_id());
let hir::ItemKind::Impl(hir::Impl { of_trait: None, items, .. }) = item.kind else {
return;
};
let self_ty = self.tcx.type_of(item.owner_id);
match *self_ty.kind() {
ty::Adt(def, _) => {
self.check_def_id(item, self_ty, def.did());
self.check_def_id(item, self_ty, def.did(), impl_span);
}
ty::Foreign(did) => {
self.check_def_id(item, self_ty, did);
self.check_def_id(item, self_ty, did, impl_span);
}
ty::Dynamic(data, ..) if data.principal_def_id().is_some() => {
self.check_def_id(item, self_ty, data.principal_def_id().unwrap());
self.check_def_id(item, self_ty, data.principal_def_id().unwrap(), impl_span);
}
ty::Dynamic(..) => {
struct_span_err!(
self.tcx.sess,
ty.span,
impl_span,
E0785,
"cannot define inherent `impl` for a dyn auto trait"
)
.span_label(ty.span, "impl requires at least one non-auto trait")
.span_label(impl_span, "impl requires at least one non-auto trait")
.note("define and implement a new trait or type instead")
.emit();
}
@ -221,17 +222,17 @@ impl<'tcx> InherentCollect<'tcx> {
| ty::Never
| ty::FnPtr(_)
| ty::Tuple(..) => {
self.check_primitive_impl(item.owner_id.def_id, self_ty, items, ty.span)
self.check_primitive_impl(item.owner_id.def_id, self_ty, items, impl_span)
}
ty::Alias(..) | ty::Param(_) => {
let mut err = struct_span_err!(
self.tcx.sess,
ty.span,
impl_span,
E0118,
"no nominal type found for inherent implementation"
);
err.span_label(ty.span, "impl requires a nominal type")
err.span_label(impl_span, "impl requires a nominal type")
.note("either implement a trait on it or create a newtype to wrap it instead");
err.emit();

View File

@ -1,16 +1,16 @@
error[E0785]: cannot define inherent `impl` for a dyn auto trait
--> $DIR/issue-85026.rs:5:6
--> $DIR/issue-85026.rs:5:1
|
LL | impl dyn AutoTrait {}
| ^^^^^^^^^^^^^ impl requires at least one non-auto trait
| ^^^^^^^^^^^^^^^^^^ impl requires at least one non-auto trait
|
= note: define and implement a new trait or type instead
error[E0785]: cannot define inherent `impl` for a dyn auto trait
--> $DIR/issue-85026.rs:8:6
--> $DIR/issue-85026.rs:8:1
|
LL | impl dyn Unpin {}
| ^^^^^^^^^ impl requires at least one non-auto trait
| ^^^^^^^^^^^^^^ impl requires at least one non-auto trait
|
= note: define and implement a new trait or type instead

View File

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

View File

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

View File

@ -1,8 +1,8 @@
error[E0118]: no nominal type found for inherent implementation
--> $DIR/E0118.rs:1:9
--> $DIR/E0118.rs:1:1
|
LL | impl<T> T {
| ^ impl requires a nominal type
| ^^^^^^^^^ impl requires a nominal type
|
= note: either implement a trait on it or create a newtype to wrap it instead

View File

@ -1,16 +1,16 @@
error[E0390]: cannot define inherent `impl` for primitive types
--> $DIR/E0390.rs:5:6
--> $DIR/E0390.rs:5:1
|
LL | impl *mut Foo {}
| ^^^^^^^^
| ^^^^^^^^^^^^^
|
= help: consider using an extension trait instead
error[E0390]: cannot define inherent `impl` for primitive types
--> $DIR/E0390.rs:7:6
--> $DIR/E0390.rs:7:1
|
LL | impl fn(Foo) {}
| ^^^^^^^
| ^^^^^^^^^^^^
|
= help: consider using an extension trait instead

View File

@ -303,10 +303,10 @@ LL | fn in_method_generic_param_default<T = impl Debug>(_: T) {}
= note: for more information, see issue #36887 <https://github.com/rust-lang/rust/issues/36887>
error[E0118]: no nominal type found for inherent implementation
--> $DIR/where-allowed.rs:233:23
--> $DIR/where-allowed.rs:233:1
|
LL | impl <T = impl Debug> T {}
| ^ impl requires a nominal type
| ^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
|
= note: either implement a trait on it or create a newtype to wrap it instead

View File

@ -1,114 +1,80 @@
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $DIR/needs-has-incoherent-impls.rs:5:1
|
LL | / impl extern_crate::StructWithAttr {
LL | |
LL | | fn foo() {}
LL | | }
| |_^
LL | impl extern_crate::StructWithAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider moving this inherent impl into the crate defining the type if possible
help: alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items
--> $DIR/needs-has-incoherent-impls.rs:7:5
|
LL | fn foo() {}
| ^^^^^^^^^^^
| ^^^^^^^^
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $DIR/needs-has-incoherent-impls.rs:13:1
|
LL | / impl extern_crate::StructNoAttr {
LL | |
LL | | fn foo() {}
LL | | }
| |_^
LL | impl extern_crate::StructNoAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider moving this inherent impl into the crate defining the type if possible
help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
--> $DIR/needs-has-incoherent-impls.rs:13:1
|
LL | / impl extern_crate::StructNoAttr {
LL | |
LL | | fn foo() {}
LL | | }
| |_^
LL | impl extern_crate::StructNoAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $DIR/needs-has-incoherent-impls.rs:17:1
|
LL | / impl extern_crate::StructNoAttr {
LL | |
LL | | #[rustc_allow_incoherent_impl]
LL | | fn bar() {}
LL | | }
| |_^
LL | impl extern_crate::StructNoAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider moving this inherent impl into the crate defining the type if possible
help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
--> $DIR/needs-has-incoherent-impls.rs:17:1
|
LL | / impl extern_crate::StructNoAttr {
LL | |
LL | | #[rustc_allow_incoherent_impl]
LL | | fn bar() {}
LL | | }
| |_^
LL | impl extern_crate::StructNoAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $DIR/needs-has-incoherent-impls.rs:22:1
|
LL | / impl extern_crate::EnumWithAttr {
LL | |
LL | | fn foo() {}
LL | | }
| |_^
LL | impl extern_crate::EnumWithAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider moving this inherent impl into the crate defining the type if possible
help: alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items
--> $DIR/needs-has-incoherent-impls.rs:24:5
|
LL | fn foo() {}
| ^^^^^^^^^^^
| ^^^^^^^^
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $DIR/needs-has-incoherent-impls.rs:30:1
|
LL | / impl extern_crate::EnumNoAttr {
LL | |
LL | | fn foo() {}
LL | | }
| |_^
LL | impl extern_crate::EnumNoAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider moving this inherent impl into the crate defining the type if possible
help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
--> $DIR/needs-has-incoherent-impls.rs:30:1
|
LL | / impl extern_crate::EnumNoAttr {
LL | |
LL | | fn foo() {}
LL | | }
| |_^
LL | impl extern_crate::EnumNoAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error[E0390]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $DIR/needs-has-incoherent-impls.rs:34:1
|
LL | / impl extern_crate::EnumNoAttr {
LL | |
LL | | #[rustc_allow_incoherent_impl]
LL | | fn bar() {}
LL | | }
| |_^
LL | impl extern_crate::EnumNoAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= help: consider moving this inherent impl into the crate defining the type if possible
help: alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type and `#[rustc_allow_incoherent_impl]` to the relevant impl items
--> $DIR/needs-has-incoherent-impls.rs:34:1
|
LL | / impl extern_crate::EnumNoAttr {
LL | |
LL | | #[rustc_allow_incoherent_impl]
LL | | fn bar() {}
LL | | }
| |_^
LL | impl extern_crate::EnumNoAttr {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 6 previous errors

View File

@ -2,7 +2,7 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher
--> $DIR/no-attr-empty-impl.rs:4:1
|
LL | impl extern_crate::StructWithAttr {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
|
= note: define and implement a trait or new type instead
@ -10,7 +10,7 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher
--> $DIR/no-attr-empty-impl.rs:7:1
|
LL | impl extern_crate::StructNoAttr {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
|
= note: define and implement a trait or new type instead
@ -18,7 +18,7 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher
--> $DIR/no-attr-empty-impl.rs:10:1
|
LL | impl extern_crate::EnumWithAttr {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
|
= note: define and implement a trait or new type instead
@ -26,15 +26,15 @@ error[E0116]: cannot define inherent `impl` for a type outside of the crate wher
--> $DIR/no-attr-empty-impl.rs:13:1
|
LL | impl extern_crate::EnumNoAttr {}
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
|
= note: define and implement a trait or new type instead
error[E0390]: cannot define inherent `impl` for primitive types
--> $DIR/no-attr-empty-impl.rs:16:6
--> $DIR/no-attr-empty-impl.rs:16:1
|
LL | impl f32 {}
| ^^^
| ^^^^^^^^
|
= help: consider using an extension trait instead

View File

@ -1,32 +1,32 @@
error[E0390]: cannot define inherent `impl` for primitive types
--> $DIR/kinds-of-primitive-impl.rs:1:6
--> $DIR/kinds-of-primitive-impl.rs:1:1
|
LL | impl u8 {
| ^^
| ^^^^^^^
|
= help: consider using an extension trait instead
error[E0390]: cannot define inherent `impl` for primitive types
--> $DIR/kinds-of-primitive-impl.rs:6:6
--> $DIR/kinds-of-primitive-impl.rs:6:1
|
LL | impl str {
| ^^^
| ^^^^^^^^
|
= help: consider using an extension trait instead
error[E0390]: cannot define inherent `impl` for primitive types
--> $DIR/kinds-of-primitive-impl.rs:12:6
--> $DIR/kinds-of-primitive-impl.rs:12:1
|
LL | impl char {
| ^^^^
| ^^^^^^^^^
|
= help: consider using an extension trait instead
error[E0390]: cannot define inherent `impl` for primitive types
--> $DIR/kinds-of-primitive-impl.rs:21:6
--> $DIR/kinds-of-primitive-impl.rs:21:1
|
LL | impl &MyType {
| ^^^^^^^
| ^^^^^^^^^^^^
|
= help: consider using an extension trait instead
= note: you could also try moving the reference to uses of `MyType` (such as `self`) within the implementation

View File

@ -1,16 +1,16 @@
error[E0118]: no nominal type found for inherent implementation
--> $DIR/private-in-public-ill-formed.rs:14:10
--> $DIR/private-in-public-ill-formed.rs:14:5
|
LL | impl <Priv as PrivTr>::AssocAlias {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
|
= note: either implement a trait on it or create a newtype to wrap it instead
error[E0118]: no nominal type found for inherent implementation
--> $DIR/private-in-public-ill-formed.rs:31:10
--> $DIR/private-in-public-ill-formed.rs:31:5
|
LL | impl <Priv as PrivTr>::AssocAlias {
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl requires a nominal type
|
= note: either implement a trait on it or create a newtype to wrap it instead

View File

@ -1,11 +1,8 @@
error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $DIR/trait-or-new-type-instead.rs:1:1
|
LL | / impl<T> Option<T> {
LL | |
LL | | pub fn foo(&self) { }
LL | | }
| |_^ impl for type defined outside of crate.
LL | impl<T> Option<T> {
| ^^^^^^^^^^^^^^^^^ impl for type defined outside of crate.
|
= note: define and implement a trait or new type instead