-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Account for object safety when suggesting
Box<dyn Trait>
- Loading branch information
Showing
10 changed files
with
202 additions
and
19 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
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
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
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
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
35 changes: 35 additions & 0 deletions
35
src/test/ui/impl-trait/object-unsafe-trait-in-return-position-dyn-trait.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,35 @@ | ||
#![allow(bare_trait_objects)] | ||
trait NotObjectSafe { | ||
fn foo() -> Self; | ||
} | ||
|
||
struct A; | ||
struct B; | ||
|
||
impl NotObjectSafe for A { | ||
fn foo() -> Self { | ||
A | ||
} | ||
} | ||
|
||
impl NotObjectSafe for B { | ||
fn foo() -> Self { | ||
B | ||
} | ||
} | ||
|
||
fn car() -> dyn NotObjectSafe { //~ ERROR the trait `NotObjectSafe` cannot be made into an object | ||
if true { | ||
return A; | ||
} | ||
B | ||
} | ||
|
||
fn cat() -> Box<dyn NotObjectSafe> { //~ ERROR the trait `NotObjectSafe` cannot be made into an | ||
if true { | ||
return Box::new(A); | ||
} | ||
Box::new(B) | ||
} | ||
|
||
fn main() {} |
21 changes: 21 additions & 0 deletions
21
src/test/ui/impl-trait/object-unsafe-trait-in-return-position-dyn-trait.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,21 @@ | ||
error[E0038]: the trait `NotObjectSafe` cannot be made into an object | ||
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:21:1 | ||
| | ||
LL | fn foo() -> Self; | ||
| --- associated function `foo` has no `self` parameter | ||
... | ||
LL | fn car() -> dyn NotObjectSafe { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object | ||
|
||
error[E0038]: the trait `NotObjectSafe` cannot be made into an object | ||
--> $DIR/object-unsafe-trait-in-return-position-dyn-trait.rs:28:1 | ||
| | ||
LL | fn foo() -> Self; | ||
| --- associated function `foo` has no `self` parameter | ||
... | ||
LL | fn cat() -> Box<dyn NotObjectSafe> { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ the trait `NotObjectSafe` cannot be made into an object | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0038`. |
46 changes: 46 additions & 0 deletions
46
src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.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,46 @@ | ||
trait NotObjectSafe { | ||
fn foo() -> Self; | ||
} | ||
|
||
trait ObjectSafe { | ||
fn bar(&self); | ||
} | ||
|
||
struct A; | ||
struct B; | ||
|
||
impl NotObjectSafe for A { | ||
fn foo() -> Self { | ||
A | ||
} | ||
} | ||
|
||
impl NotObjectSafe for B { | ||
fn foo() -> Self { | ||
B | ||
} | ||
} | ||
|
||
impl ObjectSafe for A { | ||
fn bar(&self) {} | ||
} | ||
|
||
impl ObjectSafe for B { | ||
fn bar(&self) {} | ||
} | ||
|
||
fn can() -> impl NotObjectSafe { | ||
if true { | ||
return A; | ||
} | ||
B //~ ERROR mismatched types | ||
} | ||
|
||
fn cat() -> impl ObjectSafe { | ||
if true { | ||
return A; | ||
} | ||
B //~ ERROR mismatched types | ||
} | ||
|
||
fn main() {} |
39 changes: 39 additions & 0 deletions
39
src/test/ui/impl-trait/object-unsafe-trait-in-return-position-impl-trait.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,39 @@ | ||
error[E0308]: mismatched types | ||
--> $DIR/object-unsafe-trait-in-return-position-impl-trait.rs:36:5 | ||
| | ||
LL | fn can() -> impl NotObjectSafe { | ||
| ------------------ expected because this return type... | ||
LL | if true { | ||
LL | return A; | ||
| - ...is found to be `A` here | ||
LL | } | ||
LL | B | ||
| ^ expected struct `A`, found struct `B` | ||
| | ||
= note: to return `impl Trait`, all returned values must be of the same type | ||
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> | ||
= help: if the trait `NotObjectSafe` were object safe, you could return a boxed trait object | ||
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types> | ||
= help: alternatively, create a new `enum` with a variant for each returned type | ||
|
||
error[E0308]: mismatched types | ||
--> $DIR/object-unsafe-trait-in-return-position-impl-trait.rs:43:5 | ||
| | ||
LL | fn cat() -> impl ObjectSafe { | ||
| --------------- expected because this return type... | ||
LL | if true { | ||
LL | return A; | ||
| - ...is found to be `A` here | ||
LL | } | ||
LL | B | ||
| ^ expected struct `A`, found struct `B` | ||
| | ||
= note: to return `impl Trait`, all returned values must be of the same type | ||
= note: for information on `impl Trait`, see <https://doc.rust-lang.org/book/ch10-02-traits.html#returning-types-that-implement-traits> | ||
= help: you can instead return a boxed trait object using `Box<dyn ObjectSafe>` | ||
= note: for information on trait objects, see <https://doc.rust-lang.org/book/ch17-02-trait-objects.html#using-trait-objects-that-allow-for-values-of-different-types> | ||
= help: alternatively, create a new `enum` with a variant for each returned type | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0308`. |
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