Pre-RFC: Associated statics

Thanks for taking the time to write this! Here are some questions & notes I have to aid in making progress...

This section feels thin; before filing it against rust-lang/rfcs I'd encourage you to collect more motivation, especially real world use cases.

I'd like to see some discussion on why given this restriction it's OK to have associated trait statics at all given that the first implicit parameter of a trait makes for an implicitly generic static you may end up in:

// crate A:
// ------------------------
pub trait Foo { static Bar: usize }

pub struct Alpha;
pub struct Beta;

impl Foo for Alpha { static Bar: usize = 42; }
impl Foo for Beta { static Bar: usize = 24; }

// crate B
// ------------------------
pub fn takes_foo<T: Foo>() { ... }

In particular, some reasoning around how we can guarantee that no matter where <X as Foo>::Bar is being projected, it always end up to the same memory address (which is something unsafe { ... } should be able to assume).

You've discussed the static semantics of your proposed changes, but not the dynamic/operational semantics. So even if it might be obvious, I'd like a discussion in this section of the proposal on the dynamic semantics of all the bits and pieces. You don't have to specify it formally, but it should be clear what is meant.

I'd also move out any rationale for assorted choices in the reference to the section on... well.. the rationale.. :wink:

In all discussions hitherto about generic statics it was always clear that each monomorphization would get its own memory location... why else would you have a generic static? And furthermore, given associated types you may run into:

trait Foo {
    type Bar;
    static Baz: Self::Bar;
}

struct Gamma;
struct Delta;

impl Foo for Gamma {
    type Bar = u8;
    static Foo = 42;
}

impl Foo for Delta {
    type Bar = bool;
    static Foo = false;
}

Since if you'd have u8 and bool at the same memory location, type preservation is directly violated, and you'd get unsoundness.

Speaking of associated types; I think you'll need to consider impl specialization + associated types and whether when generics are used it's reasonable to expect that <X as Foo>::Bar always refers to the same memory address.

Some discussion on statics in C++ might be useful here.

That's fair and I would be OK with experimenting with associated statics on nightly. However, I wouldn't be inclined to stabilize associated statics without first having at the very least an accepted design + implementation on nightly for associated statics in generic implementations. I have this constraint to ensure that when we're done with associated statics, what comes out is coherent and not a patchwork of inconsistencies.

I think the main hurdle for generic statics are dylibs (cc @hanna-kruppe, How about "generic global variables"? - #8 by hanna-kruppe, Generic-type-dependent static data - #7 by hanna-kruppe, Generic-type-dependent static data - #7 by hanna-kruppe, Discord). We might want to kill them off entirely; but we should consider whether we are OK with preventing future better dynamic linking models (might also interact with hotloading a la erlang?).

3 Likes