-
Notifications
You must be signed in to change notification settings - Fork 78
Description
In Rust you can get unnamed statics from constant promotion: in an expression like &42, the compiler actually generates a static item initialized to 42, so that you can borrow it. However, these statics are given a compiler-generated name in the MIR. This makes it hard to refer to them from SAW without manually inspecting the MIR to find the generated name.
Many SAW scripts have employed a workaround to describe functions which output such static items, by using mir_alloc instead of mir_static:
pub fn f() -> &'static u32 {
&42
}let f_spec = do {
mir_execute_func [];
x <- mir_alloc mir_u32;
mir_return x;
};
However, mir_alloc is not meant for this purpose, as when used in the post-state of a spec, it describes that the function performs a memory allocation, which borrowing a static item is not. Indeed, as #2665 describes, using mir_alloc for static items can lead to unsoundness, so after this issue is fixed in #2678, this workaround will no longer be allowed.
In #2678 (comment) and subsequent replies, @RyanGlScott and I discuss ways to make it easier to refer to these unnamed static items in situations like this. Currently we are thinking of something like mir_static_of : MIRModule -> String -> MIRType -> MIRValue -> MIRValue (name is tentative), where mir_static_of mod prefix ty init would return a reference to the static in MIR module mod whose identifier starts with prefix, has type ty, and has initializer value init. The prefix can be used to disambiguate promoted statics from different crates by crate name, since promoted statics are only interned within the same crate. If there are multiple such statics (or none), an error is thrown.
Perhaps a less confusing name for this would be something like mir_static_from_value.