Skip to content

Tighter spans for bad inherent impl self types #107942

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 17 additions & 16 deletions compiler/rustc_hir_analysis/src/coherence/inherent_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand All @@ -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();
}
Expand Down Expand Up @@ -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();
}
Expand All @@ -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();
Expand Down
8 changes: 4 additions & 4 deletions tests/ui/coherence/issue-85026.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/const-generics/wrong-normalization.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
2 changes: 1 addition & 1 deletion tests/ui/error-codes/E0116.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/error-codes/E0118.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
8 changes: 4 additions & 4 deletions tests/ui/error-codes/E0390.stderr
Original file line number Diff line number Diff line change
@@ -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

Expand Down
4 changes: 2 additions & 2 deletions tests/ui/impl-trait/where-allowed.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
@@ -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

Expand Down
12 changes: 6 additions & 6 deletions tests/ui/incoherent-inherent-impls/no-attr-empty-impl.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,39 @@ 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

error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $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

error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $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

error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
--> $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

Expand Down
Loading