diff options
Diffstat (limited to 'src/test/ui/object-safety')
5 files changed, 129 insertions, 0 deletions
diff --git a/src/test/ui/object-safety/issue-102762.rs b/src/test/ui/object-safety/issue-102762.rs new file mode 100644 index 000000000..4f4c36345 --- /dev/null +++ b/src/test/ui/object-safety/issue-102762.rs @@ -0,0 +1,26 @@ +// compile-flags: --crate-type=lib +// This test checks that the `where_clauses_object_safety` lint does not cause +// other object safety *hard errors* to be suppressed, because we currently +// only emit one object safety error per trait... + +use std::future::Future; +use std::pin::Pin; + +pub trait Fetcher: Send + Sync { + fn get<'a>(self: &'a Box<Self>) -> Pin<Box<dyn Future<Output = Vec<u8>> + 'a>> + where + Self: Sync, + { + todo!() + } +} + +fn fetcher() -> Box<dyn Fetcher> { + //~^ ERROR the trait `Fetcher` cannot be made into an object + todo!() +} + +pub fn foo() { + let fetcher = fetcher(); + let _ = fetcher.get(); +} diff --git a/src/test/ui/object-safety/issue-102762.stderr b/src/test/ui/object-safety/issue-102762.stderr new file mode 100644 index 000000000..5041ebe77 --- /dev/null +++ b/src/test/ui/object-safety/issue-102762.stderr @@ -0,0 +1,20 @@ +error[E0038]: the trait `Fetcher` cannot be made into an object + --> $DIR/issue-102762.rs:18:21 + | +LL | fn get<'a>(self: &'a Box<Self>) -> Pin<Box<dyn Future<Output = Vec<u8>> + 'a>> + | ------------- help: consider changing method `get`'s `self` parameter to be `&self`: `&Self` +... +LL | fn fetcher() -> Box<dyn Fetcher> { + | ^^^^^^^^^^^ `Fetcher` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> + --> $DIR/issue-102762.rs:10:22 + | +LL | pub trait Fetcher: Send + Sync { + | ------- this trait cannot be made into an object... +LL | fn get<'a>(self: &'a Box<Self>) -> Pin<Box<dyn Future<Output = Vec<u8>> + 'a>> + | ^^^^^^^^^^^^^ ...because method `get`'s `self` parameter cannot be dispatched on + +error: aborting due to previous error + +For more information about this error, try `rustc --explain E0038`. diff --git a/src/test/ui/object-safety/issue-102933.rs b/src/test/ui/object-safety/issue-102933.rs new file mode 100644 index 000000000..843391cff --- /dev/null +++ b/src/test/ui/object-safety/issue-102933.rs @@ -0,0 +1,25 @@ +// check-pass + +use std::future::Future; + +pub trait Service { + type Response; + type Future: Future<Output = Self::Response>; +} + +pub trait A1: Service<Response = i32> {} + +pub trait A2: Service<Future = Box<dyn Future<Output = i32>>> + A1 { + fn foo(&self) {} +} + +pub trait B1: Service<Future = Box<dyn Future<Output = i32>>> {} + +pub trait B2: Service<Response = i32> + B1 { + fn foo(&self) {} +} + +fn main() { + let x: &dyn A2 = todo!(); + let x: &dyn B2 = todo!(); +} diff --git a/src/test/ui/object-safety/object-safety-supertrait-mentions-GAT.rs b/src/test/ui/object-safety/object-safety-supertrait-mentions-GAT.rs new file mode 100644 index 000000000..14e00d2ef --- /dev/null +++ b/src/test/ui/object-safety/object-safety-supertrait-mentions-GAT.rs @@ -0,0 +1,15 @@ +//~ ERROR the parameter type `Self` may not live long enough + +trait GatTrait { + type Gat<'a> + where + Self: 'a; +} + +trait SuperTrait<T>: for<'a> GatTrait<Gat<'a> = T> { + fn c(&self) -> dyn SuperTrait<T>; + //~^ ERROR associated item referring to unboxed trait object for its own trait + //~| ERROR the trait `SuperTrait` cannot be made into an object +} + +fn main() {} diff --git a/src/test/ui/object-safety/object-safety-supertrait-mentions-GAT.stderr b/src/test/ui/object-safety/object-safety-supertrait-mentions-GAT.stderr new file mode 100644 index 000000000..c1aaad31e --- /dev/null +++ b/src/test/ui/object-safety/object-safety-supertrait-mentions-GAT.stderr @@ -0,0 +1,43 @@ +error[E0311]: the parameter type `Self` may not live long enough + | + = help: consider adding an explicit lifetime bound `Self: 'a`... + = note: ...so that the type `Self` will meet its required lifetime bounds... +note: ...that is required by this bound + --> $DIR/object-safety-supertrait-mentions-GAT.rs:9:39 + | +LL | trait SuperTrait<T>: for<'a> GatTrait<Gat<'a> = T> { + | ^^^^^^^^^^^ + +error: associated item referring to unboxed trait object for its own trait + --> $DIR/object-safety-supertrait-mentions-GAT.rs:10:20 + | +LL | trait SuperTrait<T>: for<'a> GatTrait<Gat<'a> = T> { + | ---------- in this trait +LL | fn c(&self) -> dyn SuperTrait<T>; + | ^^^^^^^^^^^^^^^^^ + | +help: you might have meant to use `Self` to refer to the implementing type + | +LL | fn c(&self) -> Self; + | ~~~~ + +error[E0038]: the trait `SuperTrait` cannot be made into an object + --> $DIR/object-safety-supertrait-mentions-GAT.rs:10:20 + | +LL | fn c(&self) -> dyn SuperTrait<T>; + | ^^^^^^^^^^^^^^^^^ `SuperTrait` cannot be made into an object + | +note: for a trait to be "object safe" it needs to allow building a vtable to allow the call to be resolvable dynamically; for more information visit <https://doc.rust-lang.org/reference/items/traits.html#object-safety> + --> $DIR/object-safety-supertrait-mentions-GAT.rs:4:10 + | +LL | type Gat<'a> + | ^^^ ...because it contains the generic associated type `Gat` +... +LL | trait SuperTrait<T>: for<'a> GatTrait<Gat<'a> = T> { + | ---------- this trait cannot be made into an object... + = help: consider moving `Gat` to another trait + +error: aborting due to 3 previous errors + +Some errors have detailed explanations: E0038, E0311. +For more information about an error, try `rustc --explain E0038`. |