Skip to content

Arc is invariant in its type parameter #29037

@bkoropoff

Description

@bkoropoff
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() {}

Activity

bluss

bluss commented on Oct 15, 2015

@bluss
Member

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

apasel422

apasel422 commented on Oct 15, 2015

@apasel422
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

arielb1 commented on Oct 15, 2015

@arielb1
Contributor

@apasel422

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

apasel422

apasel422 commented on Oct 16, 2015

@apasel422
Contributor

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

apasel422

apasel422 commented on Oct 16, 2015

@apasel422
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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @arielb1@bkoropoff@bluss@apasel422

        Issue actions

          Arc is invariant in its type parameter · Issue #29037 · rust-lang/rust