forbid empty impls for types with incoherent impls
This commit is contained in:
parent
dc184b4e17
commit
ba0ecbdcd4
6 changed files with 228 additions and 5 deletions
|
|
@ -122,6 +122,8 @@ impl<'tcx> ItemLikeVisitor<'_> for InherentCollect<'tcx> {
|
|||
const INTO_CORE: &str = "consider moving this inherent impl into `core` if possible";
|
||||
const INTO_DEFINING_CRATE: &str =
|
||||
"consider moving this inherent impl into the crate defining the type if possible";
|
||||
const ADD_ATTR_TO_TY: &str = "alternatively add `#[rustc_has_incoherent_inherent_impls]` to the type \
|
||||
and `#[rustc_allow_incoherent_impl]` to the relevant impl items";
|
||||
const ADD_ATTR: &str =
|
||||
"alternatively add `#[rustc_allow_incoherent_impl]` to the relevant impl items";
|
||||
|
||||
|
|
@ -137,13 +139,28 @@ impl<'tcx> InherentCollect<'tcx> {
|
|||
return;
|
||||
}
|
||||
|
||||
if self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) {
|
||||
let hir::ItemKind::Impl(hir::Impl { items, .. }) = item.kind else {
|
||||
if self.tcx.features().rustc_attrs {
|
||||
let hir::ItemKind::Impl(&hir::Impl { items, .. }) = item.kind else {
|
||||
bug!("expected `impl` item: {:?}", item);
|
||||
};
|
||||
|
||||
for item in items {
|
||||
if !self.tcx.has_attr(item.id.def_id.to_def_id(), sym::rustc_allow_incoherent_impl)
|
||||
if !self.tcx.has_attr(def_id, sym::rustc_has_incoherent_inherent_impls) {
|
||||
struct_span_err!(
|
||||
self.tcx.sess,
|
||||
item.span,
|
||||
E0390,
|
||||
"cannot define inherent `impl` for a type outside of crate where the type is defined",
|
||||
)
|
||||
.help(INTO_DEFINING_CRATE)
|
||||
.span_help(item.span, ADD_ATTR_TO_TY)
|
||||
.emit();
|
||||
return;
|
||||
}
|
||||
|
||||
for impl_item in items {
|
||||
if !self
|
||||
.tcx
|
||||
.has_attr(impl_item.id.def_id.to_def_id(), sym::rustc_allow_incoherent_impl)
|
||||
{
|
||||
struct_span_err!(
|
||||
self.tcx.sess,
|
||||
|
|
@ -152,7 +169,7 @@ impl<'tcx> InherentCollect<'tcx> {
|
|||
"cannot define inherent `impl` for a type outside of crate where the type is defined",
|
||||
)
|
||||
.help(INTO_DEFINING_CRATE)
|
||||
.span_help(item.span, ADD_ATTR)
|
||||
.span_help(impl_item.span, ADD_ATTR)
|
||||
.emit();
|
||||
return;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,9 @@
|
|||
#![feature(rustc_attrs)]
|
||||
|
||||
#[rustc_has_incoherent_inherent_impls]
|
||||
pub struct StructWithAttr;
|
||||
pub struct StructNoAttr;
|
||||
|
||||
#[rustc_has_incoherent_inherent_impls]
|
||||
pub enum EnumWithAttr {}
|
||||
pub enum EnumNoAttr {}
|
||||
|
|
@ -0,0 +1,34 @@
|
|||
// aux-build:extern-crate.rs
|
||||
#![feature(rustc_attrs)]
|
||||
extern crate extern_crate;
|
||||
|
||||
impl extern_crate::StructWithAttr { //~ ERROR
|
||||
fn foo() {}
|
||||
}
|
||||
impl extern_crate::StructWithAttr {
|
||||
#[rustc_allow_incoherent_impl]
|
||||
fn bar() {}
|
||||
}
|
||||
impl extern_crate::StructNoAttr { //~ ERROR
|
||||
fn foo() {}
|
||||
}
|
||||
impl extern_crate::StructNoAttr { //~ ERROR
|
||||
#[rustc_allow_incoherent_impl]
|
||||
fn bar() {}
|
||||
}
|
||||
impl extern_crate::EnumWithAttr { //~ ERROR
|
||||
fn foo() {}
|
||||
}
|
||||
impl extern_crate::EnumWithAttr {
|
||||
#[rustc_allow_incoherent_impl]
|
||||
fn bar() {}
|
||||
}
|
||||
impl extern_crate::EnumNoAttr { //~ ERROR
|
||||
fn foo() {}
|
||||
}
|
||||
impl extern_crate::EnumNoAttr { //~ ERROR
|
||||
#[rustc_allow_incoherent_impl]
|
||||
fn bar() {}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,105 @@
|
|||
error[E0390]: cannot define inherent `impl` for a type outside of crate where the type is defined
|
||||
--> $DIR/needs-has-incoherent-impls.rs:5:1
|
||||
|
|
||||
LL | / impl extern_crate::StructWithAttr {
|
||||
LL | | fn foo() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= 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:6:5
|
||||
|
|
||||
LL | fn foo() {}
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error[E0390]: cannot define inherent `impl` for a type outside of crate where the type is defined
|
||||
--> $DIR/needs-has-incoherent-impls.rs:12:1
|
||||
|
|
||||
LL | / impl extern_crate::StructNoAttr {
|
||||
LL | | fn foo() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= 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:12:1
|
||||
|
|
||||
LL | / impl extern_crate::StructNoAttr {
|
||||
LL | | fn foo() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
||||
error[E0390]: cannot define inherent `impl` for a type outside of crate where the type is defined
|
||||
--> $DIR/needs-has-incoherent-impls.rs:15:1
|
||||
|
|
||||
LL | / impl extern_crate::StructNoAttr {
|
||||
LL | | #[rustc_allow_incoherent_impl]
|
||||
LL | | fn bar() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= 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:15:1
|
||||
|
|
||||
LL | / impl extern_crate::StructNoAttr {
|
||||
LL | | #[rustc_allow_incoherent_impl]
|
||||
LL | | fn bar() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
||||
error[E0390]: cannot define inherent `impl` for a type outside of crate where the type is defined
|
||||
--> $DIR/needs-has-incoherent-impls.rs:19:1
|
||||
|
|
||||
LL | / impl extern_crate::EnumWithAttr {
|
||||
LL | | fn foo() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= 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:20:5
|
||||
|
|
||||
LL | fn foo() {}
|
||||
| ^^^^^^^^^^^
|
||||
|
||||
error[E0390]: cannot define inherent `impl` for a type outside of crate where the type is defined
|
||||
--> $DIR/needs-has-incoherent-impls.rs:26:1
|
||||
|
|
||||
LL | / impl extern_crate::EnumNoAttr {
|
||||
LL | | fn foo() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= 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:26:1
|
||||
|
|
||||
LL | / impl extern_crate::EnumNoAttr {
|
||||
LL | | fn foo() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
||||
error[E0390]: cannot define inherent `impl` for a type outside of crate where the type is defined
|
||||
--> $DIR/needs-has-incoherent-impls.rs:29:1
|
||||
|
|
||||
LL | / impl extern_crate::EnumNoAttr {
|
||||
LL | | #[rustc_allow_incoherent_impl]
|
||||
LL | | fn bar() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
|
||||
= 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:29:1
|
||||
|
|
||||
LL | / impl extern_crate::EnumNoAttr {
|
||||
LL | | #[rustc_allow_incoherent_impl]
|
||||
LL | | fn bar() {}
|
||||
LL | | }
|
||||
| |_^
|
||||
|
||||
error: aborting due to 6 previous errors
|
||||
|
||||
For more information about this error, try `rustc --explain E0390`.
|
||||
14
src/test/ui/incoherent-inherent-impls/no-attr-empty-impl.rs
Normal file
14
src/test/ui/incoherent-inherent-impls/no-attr-empty-impl.rs
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
// aux-build:extern-crate.rs
|
||||
extern crate extern_crate;
|
||||
|
||||
impl extern_crate::StructWithAttr {} //~ ERROR
|
||||
|
||||
impl extern_crate::StructNoAttr {} //~ ERROR
|
||||
|
||||
impl extern_crate::EnumWithAttr {} //~ ERROR
|
||||
|
||||
impl extern_crate::EnumNoAttr {} //~ ERROR
|
||||
|
||||
impl f32 {} //~ ERROR
|
||||
|
||||
fn main() {}
|
||||
|
|
@ -0,0 +1,44 @@
|
|||
error[E0116]: cannot define inherent `impl` for a type outside of the crate where the type is defined
|
||||
--> $DIR/no-attr-empty-impl.rs:4:1
|
||||
|
|
||||
LL | impl extern_crate::StructWithAttr {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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:6:1
|
||||
|
|
||||
LL | impl extern_crate::StructNoAttr {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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:8:1
|
||||
|
|
||||
LL | impl extern_crate::EnumWithAttr {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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::EnumNoAttr {}
|
||||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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:12:6
|
||||
|
|
||||
LL | impl f32 {}
|
||||
| ^^^
|
||||
|
|
||||
= help: consider using an extension trait instead
|
||||
|
||||
error: aborting due to 5 previous errors
|
||||
|
||||
Some errors have detailed explanations: E0116, E0390.
|
||||
For more information about an error, try `rustc --explain E0116`.
|
||||
Loading…
Add table
Add a link
Reference in a new issue