nikomatsakis / context-capabilities-initiative

https://nikomatsakis.github.io/context-capabilities-initiative/
Apache License 2.0
12 stars 1 forks source link

How do we model globals? #2

Open yoshuawuyts opened 2 years ago

yoshuawuyts commented 2 years ago

Right now we don't have a design yet for how to model globals. We might expect Allocator to be a context capability, but right now that has to be set through #[global_allocator]. It'd be great if we could model that too using context capabilities.

Examples

Current

This is how to declare a global allocator today (_example using wee-alloc_):

// Use `wee_alloc` as the global allocator.
#[global_allocator]
static ALLOC: wee_alloc::WeeAlloc = wee_alloc::WeeAlloc::INIT;

fn main() {
    let v = vec![1, 2, 3];
}

Proposed

What I'm wondering is whether we'd be able to replace the attribute using with in global position instead:

// Use `wee_alloc` as the global allocator.
with std::alloc::Allocator = wee_alloc::WeAlloc::INIT;

fn main() {
    let v = vec![1, 2, 3];
}

This would be equivalent to doing:

fn main() {
    with std::alloc::Allocator = wee_alloc::WeAlloc::INIT {
        let v = vec![1, 2, 3];
    }
}

Open Questions

yoshuawuyts commented 2 years ago

Something I forgot to add: but I believe there is a connection between platform triples and which capabilities are exposed by default. For example wasm32-unknown-unknown will not ship with either an allocator or async runtime, but a (potential) wasm32-browser-wasi platform could be expected to provide both.

tmandry commented 2 years ago

For globals like this we may want to make them un-overridable for soundness, e.g. using the final keyword.

cc @joshtriplett