As an example, ordinarily on Rust this does what it looks like it does:
let mut lyrics = "Never was".to_owned();
lyrics += " a cornflake girl";
With cfg(no_global_oom_handling) this can't work, most importantly because the second line is an arbitrary concatenation and therefore allocates, but there is no possible way to signal if it fails.Giving this up doesn’t seem very impactful and I’d be happy to do so to enable actually useful features.
I mean, they did warn you: No Global OOM Handling. So, if this mustn't fail, and it might not succeed, therefore it has to be eliminated, a String under cfg(no_global_oom_handling) can't push_str() and it can't reserve() and it can't do a lot of things. Their definitions are conditionally removed from the type.
I didn't check, it's possible "something".to_owned() is similarly forbidden, seems like that would allocate memory.
Basically if you live in a world where allocating memory for strings, vectors, and other growable types feels extravagant, cfg(no_global_oom_handling) is for you, and if not then maybe you should re-evaluate why you are worried about allocation failures when you are wasting precious heap memory on such data structures.
Thanks for the clarification. I had totally missed the point.
How hard would it be for a text editor to highlight all lines that do or might allocate? Can they be known statically?
fn maybe_alloc() -> Vec<usize> {
if collatz_conjecture_is_true() {
vec![42]
} else {
vec![]
}
}
fn main() {
let v = maybe_alloc(); // Does this allocate?
}But I feel like a static analyzer could clearly identify “this line can allocate” just by knowing what language features can allocate. And for third party library methods “does this function have anything inside of it that could allocate?”
You can't know for sure if all of those places are reachable in a running program, but that's not necessary to solve this problem.