Skip to content

Allow marker traits as additional traits on trait objects. #89061

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

Closed
wants to merge 1 commit into from
Closed
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
19 changes: 13 additions & 6 deletions compiler/rustc_typeck/src/astconv/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1297,27 +1297,34 @@ impl<'o, 'tcx> dyn AstConv<'tcx> + 'o {
}
}

// Expand trait aliases recursively and check that only one regular (non-auto) trait
// Expand trait aliases recursively and check that only one regular (non-auto or marker) trait
// is used and no 'maybe' bounds are used.
let expanded_traits =
traits::expand_trait_aliases(tcx, bounds.trait_bounds.iter().map(|&(a, b, _)| (a, b)));
let (mut auto_traits, regular_traits): (Vec<_>, Vec<_>) =
expanded_traits.partition(|i| tcx.trait_is_auto(i.trait_ref().def_id()));
let (mut auto_traits, regular_traits): (Vec<_>, Vec<_>) = expanded_traits.partition(|i| {
let trait_def = tcx.trait_def(i.trait_ref().def_id());

trait_def.has_auto_impl || trait_def.is_marker
});
if regular_traits.len() > 1 {
let first_trait = &regular_traits[0];
let additional_trait = &regular_traits[1];
let mut err = struct_span_err!(
tcx.sess,
additional_trait.bottom().1,
E0225,
"only auto traits can be used as additional traits in a trait object"
"only auto and marker traits can be used as additional traits in a trait object"
);
additional_trait.label_with_exp_info(
&mut err,
"additional non-auto trait",
"additional non-auto or marker trait",
"additional use",
);
first_trait.label_with_exp_info(&mut err, "first non-auto trait", "first use");
first_trait.label_with_exp_info(
&mut err,
"first non-auto or marker trait",
"first use",
);
err.help(&format!(
"consider creating a new trait with all of these as supertraits and using that \
trait here instead: `trait NewTrait: {} {{}}`",
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/associated-types/issue-22560.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@ LL | type Test = dyn Add + Sub;
|
= note: because of the default `Self` reference, type parameters must be specified on object types

error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/issue-22560.rs:9:23
|
LL | type Test = dyn Add + Sub;
| --- ^^^ additional non-auto trait
| --- ^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<[type error]> + Sub<[type error]> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
Expand Down
10 changes: 5 additions & 5 deletions src/test/ui/associated-types/missing-associated-types.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::ops::{Add, Sub, Mul, Div};
use std::ops::{Add, Div, Mul, Sub};
trait X<Rhs>: Mul<Rhs> + Div<Rhs> {}
trait Y<Rhs>: Div<Rhs, Output = Rhs> {
type A;
Expand All @@ -10,16 +10,16 @@ trait Z<Rhs>: Div<Rhs> {
trait Fine<Rhs>: Div<Rhs, Output = Rhs> {}

type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object
//~| ERROR the value of the associated types
type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object
//~| ERROR the value of the associated types
type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object
//~| ERROR the value of the associated types
type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object
//~| ERROR the value of the associated types
type Bal<Rhs> = dyn X<Rhs>;
//~^ ERROR the value of the associated types
Expand Down
24 changes: 12 additions & 12 deletions src/test/ui/associated-types/missing-associated-types.stderr
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/missing-associated-types.rs:12:32
|
LL | type Foo<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs>;
| -------- ^^^^^^^^ additional non-auto trait
| -------- ^^^^^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<Rhs> + Sub<Rhs> + X<Rhs> + Y<Rhs> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
Expand All @@ -27,13 +27,13 @@ help: specify the associated types
LL | type Foo<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + X<Rhs, Output = Type> + Y<Rhs, A = Type>;
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/missing-associated-types.rs:15:32
|
LL | type Bar<Rhs> = dyn Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs>;
| -------- ^^^^^^^^ additional non-auto trait
| -------- ^^^^^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<Rhs> + Sub<Rhs> + X<Rhs> + Z<Rhs> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
Expand Down Expand Up @@ -63,13 +63,13 @@ help: specify the associated types
LL | type Bar<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + X<Rhs> + Z<Rhs, A = Type, B = Type, Output = Type>;
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/missing-associated-types.rs:18:32
|
LL | type Baz<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Y<Rhs>;
| -------- ^^^^^^^^ additional non-auto trait
| -------- ^^^^^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<Rhs> + Sub<Rhs> + Y<Rhs> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
Expand All @@ -91,13 +91,13 @@ help: specify the associated types
LL | type Baz<Rhs> = dyn Add<Rhs, Output = Type> + Sub<Rhs, Output = Type> + Y<Rhs, A = Type>;
| ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~

error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/missing-associated-types.rs:21:32
|
LL | type Bat<Rhs> = dyn Add<Rhs> + Sub<Rhs> + Fine<Rhs>;
| -------- ^^^^^^^^ additional non-auto trait
| -------- ^^^^^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Add<Rhs> + Sub<Rhs> + Fine<Rhs> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
Expand Down
4 changes: 2 additions & 2 deletions src/test/ui/error-codes/E0225.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ trait Foo = std::io::Read + std::io::Write;

fn main() {
let _: Box<dyn std::io::Read + std::io::Write>;
//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225]
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object [E0225]
let _: Box<dyn Foo>;
//~^ ERROR only auto traits can be used as additional traits in a trait object [E0225]
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object [E0225]
}
12 changes: 6 additions & 6 deletions src/test/ui/error-codes/E0225.stderr
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/E0225.rs:6:36
|
LL | let _: Box<dyn std::io::Read + std::io::Write>;
| ------------- ^^^^^^^^^^^^^^ additional non-auto trait
| ------------- ^^^^^^^^^^^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: std::io::Read + std::io::Write {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/E0225.rs:8:20
|
LL | trait Foo = std::io::Read + std::io::Write;
| ------------- -------------- additional non-auto trait
| ------------- -------------- additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
...
LL | let _: Box<dyn Foo>;
| ^^^
Expand Down
8 changes: 5 additions & 3 deletions src/test/ui/issues/issue-32963.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ use std::mem;

trait Misc {}

fn size_of_copy<T: Copy+?Sized>() -> usize { mem::size_of::<T>() }
fn size_of_copy<T: Copy + ?Sized>() -> usize {
mem::size_of::<T>()
}

fn main() {
size_of_copy::<dyn Misc + Copy>();
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~| ERROR only auto traits can be used as additional traits in a trait object
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object
//~| ERROR only auto and marker traits can be used as additional traits in a trait object
//~| ERROR the trait bound `dyn Misc: Copy` is not satisfied
}
20 changes: 10 additions & 10 deletions src/test/ui/issues/issue-32963.stderr
Original file line number Diff line number Diff line change
@@ -1,35 +1,35 @@
error[E0225]: only auto traits can be used as additional traits in a trait object
--> $DIR/issue-32963.rs:8:31
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/issue-32963.rs:10:31
|
LL | size_of_copy::<dyn Misc + Copy>();
| ---- ^^^^ additional non-auto trait
| ---- ^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Misc + Copy {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0225]: only auto traits can be used as additional traits in a trait object
--> $DIR/issue-32963.rs:8:31
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/issue-32963.rs:10:31
|
LL | size_of_copy::<dyn Misc + Copy>();
| ---- ^^^^ additional non-auto trait
| ---- ^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Misc + Copy {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>

error[E0277]: the trait bound `dyn Misc: Copy` is not satisfied
--> $DIR/issue-32963.rs:8:5
--> $DIR/issue-32963.rs:10:5
|
LL | size_of_copy::<dyn Misc + Copy>();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `Copy` is not implemented for `dyn Misc`
|
note: required by a bound in `size_of_copy`
--> $DIR/issue-32963.rs:5:20
|
LL | fn size_of_copy<T: Copy+?Sized>() -> usize { mem::size_of::<T>() }
LL | fn size_of_copy<T: Copy + ?Sized>() -> usize {
| ^^^^ required by this bound in `size_of_copy`

error: aborting due to 3 previous errors
Expand Down
11 changes: 11 additions & 0 deletions src/test/ui/marker_trait_attr/marker-trait-on-trait-object.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// check-pass

#![feature(marker_trait_attr)]

trait NonMarker {}
#[marker]
trait Marker {}

fn main() {
let _: &(dyn NonMarker + Marker);
}
4 changes: 2 additions & 2 deletions src/test/ui/parser/trait-object-delimiters.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// edition:2018

fn foo1(_: &dyn Drop + AsRef<str>) {} //~ ERROR ambiguous `+` in a type
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object

fn foo2(_: &dyn (Drop + AsRef<str>)) {} //~ ERROR incorrect braces around trait bounds

Expand All @@ -12,6 +12,6 @@ fn foo3(_: &dyn {Drop + AsRef<str>}) {} //~ ERROR expected parameter name, found
fn foo4(_: &dyn <Drop + AsRef<str>>) {} //~ ERROR expected identifier, found `<`

fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {} //~ ERROR invalid `dyn` keyword
//~^ ERROR only auto traits can be used as additional traits in a trait object
//~^ ERROR only auto and marker traits can be used as additional traits in a trait object

fn main() {}
12 changes: 6 additions & 6 deletions src/test/ui/parser/trait-object-delimiters.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,13 @@ LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {}
|
= help: `dyn` is only needed at the start of a trait `+`-separated list

error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/trait-object-delimiters.rs:3:24
|
LL | fn foo1(_: &dyn Drop + AsRef<str>) {}
| ---- ^^^^^^^^^^ additional non-auto trait
| ---- ^^^^^^^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Drop + AsRef<str> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
Expand All @@ -61,13 +61,13 @@ error[E0224]: at least one trait is required for an object type
LL | fn foo3(_: &dyn {Drop + AsRef<str>}) {}
| ^^^

error[E0225]: only auto traits can be used as additional traits in a trait object
error[E0225]: only auto and marker traits can be used as additional traits in a trait object
--> $DIR/trait-object-delimiters.rs:14:29
|
LL | fn foo5(_: &(dyn Drop + dyn AsRef<str>)) {}
| ---- ^^^^^^^^^^ additional non-auto trait
| ---- ^^^^^^^^^^ additional non-auto or marker trait
| |
| first non-auto trait
| first non-auto or marker trait
|
= help: consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: Drop + AsRef<str> {}`
= note: auto-traits like `Send` and `Sync` are traits that have special properties; for more information on them, visit <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>
Expand Down
6 changes: 3 additions & 3 deletions src/test/ui/parser/trait-object-trait-parens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ fn f<T: (Copy) + (?Sized) + (for<'a> Trait<'a>)>() {}
fn main() {
let _: Box<(Obj) + (?Sized) + (for<'a> Trait<'a>)>;
//~^ ERROR `?Trait` is not permitted in trait object types
//~| ERROR only auto traits can be used as additional traits
//~| ERROR only auto and marker traits can be used as additional traits
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| WARN this is accepted in the current edition
let _: Box<?Sized + (for<'a> Trait<'a>) + (Obj)>;
//~^ ERROR `?Trait` is not permitted in trait object types
//~| ERROR only auto traits can be used as additional traits
//~| ERROR only auto and marker traits can be used as additional traits
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| WARN this is accepted in the current edition
let _: Box<for<'a> Trait<'a> + (Obj) + (?Sized)>;
//~^ ERROR `?Trait` is not permitted in trait object types
//~| ERROR only auto traits can be used as additional traits
//~| ERROR only auto and marker traits can be used as additional traits
//~| WARN trait objects without an explicit `dyn` are deprecated
//~| WARN this is accepted in the current edition
}
Loading