Skip to content
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

Arc is invariant in its type parameter #29037

Closed
bkoropoff opened this issue Oct 14, 2015 · 5 comments
Closed

Arc is invariant in its type parameter #29037

bkoropoff opened this issue Oct 14, 2015 · 5 comments

Comments

@bkoropoff
Copy link
Contributor

I would expect it to be covariant. This seems to be an unintentional side-effect of using *mut internally.

Code

Playground

use std::sync::Arc;

fn works<'r>(x: Box<&'static str>) -> Box<&'r str> {
    x
}

fn also_works<'r,'w>(x: &'w &'static str) -> &'w &'r str {
    x
}

fn breaks<'r>(x: Arc<&'static str>) -> Arc<&'r str> {
    x
}

fn main() {}
@bluss
Copy link
Member

bluss commented Oct 15, 2015

@gankro's Raw Pointer Cleanup seems to already touch upon this.

@apasel422
Copy link
Contributor

I'm working on implementing Shared per @gankro's Raw Pointer Cleanup pre-RFC, but I'll point out that by naively changing ArcInner to use *const T instead of *mut T, we get RFC 1214 warnings in libstd/thread/mod:283:

../rust/src/libstd/thread/mod.rs:283:27: 283:32 warning: the parameter type `T` may not live long enough [E0310]
../rust/src/libstd/thread/mod.rs:283             *their_packet.get() = Some(try_result.map(|()| {
                                                               ^~~~~
../rust/src/libstd/thread/mod.rs:283:27: 283:32 help: run `rustc --explain E0310` to see a detailed explanation
../rust/src/libstd/thread/mod.rs:283:27: 283:32 help: consider adding an explicit lifetime bound `T: 'static`...
../rust/src/libstd/thread/mod.rs:283:27: 283:32 note: this warning results from recent bug fixes and clarifications; it will become a HARD ERROR in the next release. See RFC 1214 for details.
../rust/src/libstd/thread/mod.rs:283             *their_packet.get() = Some(try_result.map(|()| {
                                                               ^~~~~
../rust/src/libstd/thread/mod.rs:283:27: 283:32 note: ...so that the reference type `&alloc::arc::Arc<core::cell::UnsafeCell<core::option::Option<core::result::Result<T, Box<core::any::Any + Send>>>>>` does not outlive the data it points at
../rust/src/libstd/thread/mod.rs:283             *their_packet.get() = Some(try_result.map(|()| {
                                                               ^~~~~
../rust/src/libstd/thread/mod.rs:283:27: 283:32 warning: the parameter type `T` may not live long enough [E0310]
../rust/src/libstd/thread/mod.rs:283             *their_packet.get() = Some(try_result.map(|()| {
                                                               ^~~~~
../rust/src/libstd/thread/mod.rs:283:27: 283:32 help: run `rustc --explain E0310` to see a detailed explanation
../rust/src/libstd/thread/mod.rs:283:27: 283:32 help: consider adding an explicit lifetime bound `T: 'static`...
../rust/src/libstd/thread/mod.rs:283:27: 283:32 note: this warning results from recent bug fixes and clarifications; it will become a HARD ERROR in the next release. See RFC 1214 for details.
../rust/src/libstd/thread/mod.rs:283             *their_packet.get() = Some(try_result.map(|()| {
                                                               ^~~~~
../rust/src/libstd/thread/mod.rs:283:27: 283:32 note: ...so that the reference type `&core::cell::UnsafeCell<core::option::Option<core::result::Result<T, Box<core::any::Any + Send>>>>` does not outlive the data it points at
../rust/src/libstd/thread/mod.rs:283             *their_packet.get() = Some(try_result.map(|()| {
                                                               ^~~~~

These may or may not be spurious.

@arielb1
Copy link
Contributor

arielb1 commented Oct 15, 2015

@apasel422

That method has been giving me trouble, so probably spurious.

@apasel422
Copy link
Contributor

@arielb1 Should I file that as a separate RFC 1214 bug?

@apasel422
Copy link
Contributor

For what it's worth, Rc is also invariant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants