Skip to content

Send/Sync additional trait requirements cannot be automatically relaxed #45716

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
dhardy opened this issue Nov 2, 2017 · 3 comments
Closed
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.

Comments

@dhardy
Copy link
Contributor

dhardy commented Nov 2, 2017

The following code fails:

#[derive(Debug)]
struct Error {
    cause: Option<Box<std::error::Error + Send + Sync>>,
}

impl Error {
    fn cause(&self) -> Option<&::std::error::Error> {
       self.cause.as_ref().map(|e| e.as_ref())
    }
}

fn main() {}

Error:

error[E0308]: mismatched types
 --> sendsyncerror.rs:8:9
  |
8 |         self.cause.as_ref().map(|e| e.as_ref())
  |         ^^^^^^^^^^^^^^^^^^^^^^^ expected trait `std::error::Error`, found trait `std::error::Error + std::marker::Sync + std::marker::Send`
  |
  = note: expected type `std::option::Option<&std::error::Error>`
             found type `std::option::Option<&std::error::Error + std::marker::Sync + std::marker::Send>`

error: aborting due to previous error

Surely automatic type coercion is possible here?

@kennytm
Copy link
Member

kennytm commented Nov 2, 2017

See #18737 for a previous discussion that removed this kind of coercion/implicit-casting.

@kennytm kennytm added A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue. labels Nov 2, 2017
@dhardy
Copy link
Contributor Author

dhardy commented Nov 2, 2017

Ok, fair enough. After sorting out the lifetime stuff the cast works. I guess a general solution is not likely to happen soon then.

@arielb1 arielb1 closed this as completed Nov 2, 2017
@arielb1
Copy link
Contributor

arielb1 commented Nov 2, 2017

This (subtyping is restricted to lifetimes) is an intentional design decision, and changing it would require an RFC.

@dhardy dhardy changed the title Sync/Sync additional trait requirements cannot be automatically relaxed Send/Sync additional trait requirements cannot be automatically relaxed Nov 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-lang Relevant to the language team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants