-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #119338 - compiler-errors:upcast-plus-autos, r=lcnr
Consider principal trait ref's auto-trait super-traits in dyn upcasting Given traits like: ```rust trait Subtrait: Supertrait + Send {} trait Supertrait {} ``` We should be able to upcast `dyn Subtrait` to `dyn Supertrait + Send`. This is not currently possible, because when upcasting, we look at the list of auto traits in the object type (`dyn Subtrait`, which has no auto traits in its bounds) and compare them to the target's auto traits (`dyn Supertrait + Send`, which has `Send` in its bound). Since the target has auto traits that are not present in the source, the upcasting fails. This is overly restrictive, since `dyn Subtrait` will always implement `Send` via its built-in object impl. I propose to loosen this restriction here. r? types --- ### ~~Aside: Fix this in astconv instead?~~ ### edit: This causes too many failures. See #119825 (comment) We may also fix this by by automatically elaborating all auto-trait supertraits during `AstConv::conv_object_ty_poly_trait_ref`. That is, we can make it so that `dyn Subtrait` is elaborated into the same type of `dyn Subtrait + Send`. I'm open to considering this solution instead, but it would break coherence in the following example: ```rust trait Foo: Send {} trait Bar {} impl Bar for dyn Foo {} impl Bar for dyn Foo + Send {} //~^ This would begin to be an overlapping impl. ```
- Loading branch information
Showing
4 changed files
with
93 additions
and
52 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
14 changes: 14 additions & 0 deletions
14
tests/ui/traits/trait-upcasting/add-supertrait-auto-traits.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,14 @@ | ||
// check-pass | ||
// revisions: current next | ||
//[next] compile-flags: -Znext-solver | ||
|
||
#![feature(trait_upcasting)] | ||
|
||
trait Target {} | ||
trait Source: Send + Target {} | ||
|
||
fn upcast(x: &dyn Source) -> &(dyn Target + Send) { x } | ||
|
||
fn same(x: &dyn Source) -> &(dyn Source + Send) { x } | ||
|
||
fn main() {} |