Skip to content

Commit 5fa44c4

Browse files
authored
Rollup merge of rust-lang#57352 - arielb1:no-manual-markers, r=nikomatsakis
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
2 parents c4b8735 + d38a59f commit 5fa44c4

5 files changed

+145
-4
lines changed

src/librustc_typeck/coherence/mod.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -171,13 +171,23 @@ fn check_impl_overlap<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeI
171171
// This is something like impl Trait1 for Trait2. Illegal
172172
// if Trait1 is a supertrait of Trait2 or Trait2 is not object safe.
173173

174-
if let Some(principal_def_id) = data.principal_def_id() {
175-
if !tcx.is_object_safe(principal_def_id) {
174+
let component_def_ids = data.iter().flat_map(|predicate| {
175+
match predicate.skip_binder() {
176+
ty::ExistentialPredicate::Trait(tr) => Some(tr.def_id),
177+
ty::ExistentialPredicate::AutoTrait(def_id) => Some(*def_id),
178+
// An associated type projection necessarily comes with
179+
// an additional `Trait` requirement.
180+
ty::ExistentialPredicate::Projection(..) => None,
181+
}
182+
});
183+
184+
for component_def_id in component_def_ids {
185+
if !tcx.is_object_safe(component_def_id) {
176186
// This is an error, but it will be reported by wfcheck. Ignore it here.
177187
// This is tested by `coherence-impl-trait-for-trait-object-safe.rs`.
178188
} else {
179189
let mut supertrait_def_ids =
180-
traits::supertrait_def_ids(tcx, principal_def_id);
190+
traits::supertrait_def_ids(tcx, component_def_id);
181191
if supertrait_def_ids.any(|d| d == trait_def_id) {
182192
let sp = tcx.sess.source_map().def_span(tcx.span_of_impl(impl_def_id).unwrap());
183193
struct_span_err!(tcx.sess,
@@ -193,6 +203,5 @@ fn check_impl_overlap<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>, node_id: ast::NodeI
193203
}
194204
}
195205
}
196-
// FIXME: also check auto-trait def-ids? (e.g. `impl Sync for Foo+Sync`)?
197206
}
198207
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![feature(optin_builtin_traits)]
2+
3+
// Test for issue #56934 - that it is impossible to redundantly
4+
// implement an auto-trait for a trait object type that contains it.
5+
6+
// Negative impl variant.
7+
8+
auto trait Marker1 {}
9+
auto trait Marker2 {}
10+
11+
trait Object: Marker1 {}
12+
13+
// A supertrait marker is illegal...
14+
impl !Marker1 for dyn Object + Marker2 { } //~ ERROR E0371
15+
// ...and also a direct component.
16+
impl !Marker2 for dyn Object + Marker2 { } //~ ERROR E0371
17+
18+
// But implementing a marker if it is not present is OK.
19+
impl !Marker2 for dyn Object {} // OK
20+
21+
// A non-principal trait-object type is orphan even in its crate.
22+
impl !Send for dyn Marker2 {} //~ ERROR E0117
23+
24+
// And impl'ing a remote marker for a local trait object is forbidden
25+
// by one of these special orphan-like rules.
26+
impl !Send for dyn Object {} //~ ERROR E0321
27+
impl !Send for dyn Object + Marker2 {} //~ ERROR E0321
28+
29+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0371]: the object type `(dyn Object + Marker2 + 'static)` automatically implements the trait `Marker1`
2+
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:14:1
3+
|
4+
LL | impl !Marker1 for dyn Object + Marker2 { } //~ ERROR E0371
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Object + Marker2 + 'static)` automatically implements trait `Marker1`
6+
7+
error[E0371]: the object type `(dyn Object + Marker2 + 'static)` automatically implements the trait `Marker2`
8+
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:16:1
9+
|
10+
LL | impl !Marker2 for dyn Object + Marker2 { } //~ ERROR E0371
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Object + Marker2 + 'static)` automatically implements trait `Marker2`
12+
13+
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
14+
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:22:1
15+
|
16+
LL | impl !Send for dyn Marker2 {} //~ ERROR E0117
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use types inside crate
18+
|
19+
= note: the impl does not reference only types defined in this crate
20+
= note: define and implement a trait or new type instead
21+
22+
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)`
23+
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:26:1
24+
|
25+
LL | impl !Send for dyn Object {} //~ ERROR E0321
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type
27+
28+
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)`
29+
--> $DIR/coherence-impl-trait-for-marker-trait-negative.rs:27:1
30+
|
31+
LL | impl !Send for dyn Object + Marker2 {} //~ ERROR E0321
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type
33+
34+
error: aborting due to 5 previous errors
35+
36+
Some errors occurred: E0117, E0321, E0371.
37+
For more information about an error, try `rustc --explain E0117`.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#![feature(optin_builtin_traits)]
2+
3+
// Test for issue #56934 - that it is impossible to redundantly
4+
// implement an auto-trait for a trait object type that contains it.
5+
6+
// Positive impl variant.
7+
8+
auto trait Marker1 {}
9+
auto trait Marker2 {}
10+
11+
trait Object: Marker1 {}
12+
13+
// A supertrait marker is illegal...
14+
impl Marker1 for dyn Object + Marker2 { } //~ ERROR E0371
15+
// ...and also a direct component.
16+
impl Marker2 for dyn Object + Marker2 { } //~ ERROR E0371
17+
18+
// But implementing a marker if it is not present is OK.
19+
impl Marker2 for dyn Object {} // OK
20+
21+
// A non-principal trait-object type is orphan even in its crate.
22+
unsafe impl Send for dyn Marker2 {} //~ ERROR E0117
23+
24+
// And impl'ing a remote marker for a local trait object is forbidden
25+
// by one of these special orphan-like rules.
26+
unsafe impl Send for dyn Object {} //~ ERROR E0321
27+
unsafe impl Send for dyn Object + Marker2 {} //~ ERROR E0321
28+
29+
fn main() { }
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
error[E0371]: the object type `(dyn Object + Marker2 + 'static)` automatically implements the trait `Marker1`
2+
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:14:1
3+
|
4+
LL | impl Marker1 for dyn Object + Marker2 { } //~ ERROR E0371
5+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Object + Marker2 + 'static)` automatically implements trait `Marker1`
6+
7+
error[E0371]: the object type `(dyn Object + Marker2 + 'static)` automatically implements the trait `Marker2`
8+
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:16:1
9+
|
10+
LL | impl Marker2 for dyn Object + Marker2 { } //~ ERROR E0371
11+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `(dyn Object + Marker2 + 'static)` automatically implements trait `Marker2`
12+
13+
error[E0117]: only traits defined in the current crate can be implemented for arbitrary types
14+
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:22:1
15+
|
16+
LL | unsafe impl Send for dyn Marker2 {} //~ ERROR E0117
17+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ impl doesn't use types inside crate
18+
|
19+
= note: the impl does not reference only types defined in this crate
20+
= note: define and implement a trait or new type instead
21+
22+
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)`
23+
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:26:1
24+
|
25+
LL | unsafe impl Send for dyn Object {} //~ ERROR E0321
26+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type
27+
28+
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)`
29+
--> $DIR/coherence-impl-trait-for-marker-trait-positive.rs:27:1
30+
|
31+
LL | unsafe impl Send for dyn Object + Marker2 {} //~ ERROR E0321
32+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ can't implement cross-crate trait with a default impl for non-struct/enum type
33+
34+
error: aborting due to 5 previous errors
35+
36+
Some errors occurred: E0117, E0321, E0371.
37+
For more information about an error, try `rustc --explain E0117`.

0 commit comments

Comments
 (0)