forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#57352 - arielb1:no-manual-markers, r=nikoma…
…tsakis forbid manually impl'ing one of an object type's marker traits This shouldn't break compatibility for crates that do not use `feature(optin_builtin_traits)`, because as the test shows, it is only possible to impl a marker trait for a trait object in the crate the marker trait is defined in, which must define `feature(optin_builtin_traits)`. Fixes rust-lang#56934. r? @nikomatsakis
- Loading branch information
Showing
5 changed files
with
145 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/test/ui/coherence/coherence-impl-trait-for-marker-trait-negative.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![feature(optin_builtin_traits)] | ||
|
||
// Test for issue #56934 - that it is impossible to redundantly | ||
// implement an auto-trait for a trait object type that contains it. | ||
|
||
// Negative impl variant. | ||
|
||
auto trait Marker1 {} | ||
auto trait Marker2 {} | ||
|
||
trait Object: Marker1 {} | ||
|
||
// A supertrait marker is illegal... | ||
impl !Marker1 for dyn Object + Marker2 { } //~ ERROR E0371 | ||
// ...and also a direct component. | ||
impl !Marker2 for dyn Object + Marker2 { } //~ ERROR E0371 | ||
|
||
// But implementing a marker if it is not present is OK. | ||
impl !Marker2 for dyn Object {} // OK | ||
|
||
// A non-principal trait-object type is orphan even in its crate. | ||
impl !Send for dyn Marker2 {} //~ ERROR E0117 | ||
|
||
// And impl'ing a remote marker for a local trait object is forbidden | ||
// by one of these special orphan-like rules. | ||
impl !Send for dyn Object {} //~ ERROR E0321 | ||
impl !Send for dyn Object + Marker2 {} //~ ERROR E0321 | ||
|
||
fn main() { } |
37 changes: 37 additions & 0 deletions
37
src/test/ui/coherence/coherence-impl-trait-for-marker-trait-negative.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
error[E0371]: the object type `(dyn Object + Marker2 + 'static)` automatically implements the trait `Marker1` | ||
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:14:1 | ||
| | ||
LL | impl !Marker1 for dyn Object + Marker2 { } //~ ERROR E0371 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Object + Marker2 + 'static)` automatically implements trait `Marker1` | ||
|
||
error[E0371]: the object type `(dyn Object + Marker2 + 'static)` automatically implements the trait `Marker2` | ||
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:16:1 | ||
| | ||
LL | impl !Marker2 for dyn Object + Marker2 { } //~ ERROR E0371 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Object + Marker2 + 'static)` automatically implements trait `Marker2` | ||
|
||
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types | ||
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:22:1 | ||
| | ||
LL | impl !Send for dyn Marker2 {} //~ ERROR E0117 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use types inside crate | ||
| | ||
= note: the impl does not reference only types defined in this crate | ||
= note: define and implement a trait or new type instead | ||
|
||
error[E0321]: cross-crate traits with a default impl, like `std::marker::Send`, can only be implemented for a struct/enum type, not `(dyn Object + 'static)` | ||
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:26:1 | ||
| | ||
LL | impl !Send for dyn Object {} //~ ERROR E0321 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type | ||
|
||
error[E0321]: cross-crate traits with a default impl, like `std::marker::Send`, can only be implemented for a struct/enum type, not `(dyn Object + Marker2 + 'static)` | ||
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:27:1 | ||
| | ||
LL | impl !Send for dyn Object + Marker2 {} //~ ERROR E0321 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
Some errors occurred: E0117, E0321, E0371. | ||
For more information about an error, try `rustc --explain E0117`. |
29 changes: 29 additions & 0 deletions
29
src/test/ui/coherence/coherence-impl-trait-for-marker-trait-positive.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#![feature(optin_builtin_traits)] | ||
|
||
// Test for issue #56934 - that it is impossible to redundantly | ||
// implement an auto-trait for a trait object type that contains it. | ||
|
||
// Positive impl variant. | ||
|
||
auto trait Marker1 {} | ||
auto trait Marker2 {} | ||
|
||
trait Object: Marker1 {} | ||
|
||
// A supertrait marker is illegal... | ||
impl Marker1 for dyn Object + Marker2 { } //~ ERROR E0371 | ||
// ...and also a direct component. | ||
impl Marker2 for dyn Object + Marker2 { } //~ ERROR E0371 | ||
|
||
// But implementing a marker if it is not present is OK. | ||
impl Marker2 for dyn Object {} // OK | ||
|
||
// A non-principal trait-object type is orphan even in its crate. | ||
unsafe impl Send for dyn Marker2 {} //~ ERROR E0117 | ||
|
||
// And impl'ing a remote marker for a local trait object is forbidden | ||
// by one of these special orphan-like rules. | ||
unsafe impl Send for dyn Object {} //~ ERROR E0321 | ||
unsafe impl Send for dyn Object + Marker2 {} //~ ERROR E0321 | ||
|
||
fn main() { } |
37 changes: 37 additions & 0 deletions
37
src/test/ui/coherence/coherence-impl-trait-for-marker-trait-positive.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
error[E0371]: the object type `(dyn Object + Marker2 + 'static)` automatically implements the trait `Marker1` | ||
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:14:1 | ||
| | ||
LL | impl Marker1 for dyn Object + Marker2 { } //~ ERROR E0371 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Object + Marker2 + 'static)` automatically implements trait `Marker1` | ||
|
||
error[E0371]: the object type `(dyn Object + Marker2 + 'static)` automatically implements the trait `Marker2` | ||
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:16:1 | ||
| | ||
LL | impl Marker2 for dyn Object + Marker2 { } //~ ERROR E0371 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Object + Marker2 + 'static)` automatically implements trait `Marker2` | ||
|
||
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types | ||
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:22:1 | ||
| | ||
LL | unsafe impl Send for dyn Marker2 {} //~ ERROR E0117 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use types inside crate | ||
| | ||
= note: the impl does not reference only types defined in this crate | ||
= note: define and implement a trait or new type instead | ||
|
||
error[E0321]: cross-crate traits with a default impl, like `std::marker::Send`, can only be implemented for a struct/enum type, not `(dyn Object + 'static)` | ||
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:26:1 | ||
| | ||
LL | unsafe impl Send for dyn Object {} //~ ERROR E0321 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type | ||
|
||
error[E0321]: cross-crate traits with a default impl, like `std::marker::Send`, can only be implemented for a struct/enum type, not `(dyn Object + Marker2 + 'static)` | ||
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:27:1 | ||
| | ||
LL | unsafe impl Send for dyn Object + Marker2 {} //~ ERROR E0321 | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type | ||
|
||
error: aborting due to 5 previous errors | ||
|
||
Some errors occurred: E0117, E0321, E0371. | ||
For more information about an error, try `rustc --explain E0117`. |