umka-lang icon indicating copy to clipboard operation
umka-lang copied to clipboard

Allow weak pointers pointing to stack or global storage

Open vtereshkov opened this issue 2 years ago • 0 comments

Weak pointers are now only allowed for heap-allocated data:

fn f() {
        a := 42
        wp := weak ^int(&a)
        if p := ^int(wp); p != null {
                printf(repr(p^) + '\n')
        } else {
                printf("NULL\n")
        }
}

fn g() {
        q := new(int, 42)
        wp := weak ^int(q)
        if p := ^int(wp); p != null {
                printf(repr(p^) + '\n')
        } else {
                printf("NULL\n")
        }
}

fn main() {
        f()        // NULL 
        g()        // 42
}

From the language specification:

If the weak pointer does not point to a valid dynamically allocated memory block, the result of this conversion is null.

Related issue: #274

vtereshkov avatar Apr 27 '23 12:04 vtereshkov