zig icon indicating copy to clipboard operation
zig copied to clipboard

Container level declarations: "static" instead of "global"

Open Lking03x opened this issue 1 year ago • 2 comments

Zig Version

0.12.0

Steps to Reproduce and Observed Output

Snippets from 0.12 release note

const ptr: *const u32 = ptr: {
    var x: u32 = 123;
    break :ptr &x;
};
comptime {
    _ = ptr;
}

error: global variable contains reference to comptime var

Expected Output

error: static variable contains reference to comptime var

The variable is properly namespaced and associated to a struct (akin to classes). The meaning of static is more appropriate since it doesn't infer that zig has actual global variable (except errors)

"A global variable is a variable with global scope, meaning that it is visible (hence accessible) throughout the program, unless shadowed. (...) In compiled languages, global variables are generally static variables, whose extent (lifetime) is the entire runtime of the program..." wiki

"A static variable is a variable that has been allocated "statically", meaning that its lifetime (or "extent") is the entire run of the program. This is in contrast to shorter-lived automatic variables, whose storage is stack allocated and deallocated on the call stack; and in contrast to objects, whose storage is dynamically allocated and deallocated in heap memory." wiki

Lking03x avatar Apr 25 '24 20:04 Lking03x

I don't think the term "global" is wrong here, however I do agree that "static" is a term currently used more specifically for the exact concept we're trying to refer to here. I personally think it's unfortunate that the word "static" also has meanings close to "immutable"/"constant" (adjective meanings (1), (4), and (2), kind of, on wiktionary).

The question is whether we want Zig to stick with "global" to make that terminology more prevalent (if others prefer it like I do), or instead use the currently more common term, even if there's room for mistaking it for "immutable".

Note: There's separately also room to improve the error message to read "constant" instead of "variable" if it pertains to a const declaration like in the example, instead of a var declaration.

rohlem avatar Apr 25 '24 20:04 rohlem

error: namespace variable contains reference to comptime var

or

error: container-level variable contains reference to comptime var

or

error: runtime value references comptime var

expikr avatar Apr 28 '24 05:04 expikr