Skip to content

Commit

Permalink
forbid empty impls for types with incoherent impls
Browse files Browse the repository at this point in the history
  • Loading branch information
lcnr committed May 5, 2022
1 parent dc184b4 commit ba0ecbd
Show file tree
Hide file tree
Showing 6 changed files with 228 additions and 5 deletions.
27 changes: 22 additions & 5 deletions compiler/rustc_typeck/src/coherence/inherent_impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand All @@ -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,
Expand All @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {}
Original file line number Diff line number Diff line change
@@ -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() {}
Original file line number Diff line number Diff line change
@@ -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 changes: 14 additions & 0 deletions src/test/ui/incoherent-inherent-impls/no-attr-empty-impl.rs
Original file line number Diff line number Diff line change
@@ -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() {}
44 changes: 44 additions & 0 deletions src/test/ui/incoherent-inherent-impls/no-attr-empty-impl.stderr
Original file line number Diff line number Diff line change
@@ -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`.

0 comments on commit ba0ecbd

Please sign in to comment.