metallang / metal

Metal compiler and toolchain.
MIT License
3 stars 1 forks source link

(Tracking Issue) Inferred Lifetimes #82

Open VincentRPS opened 1 day ago

VincentRPS commented 1 day ago

Supersedes #61 for memory management.

High-level Conceptual Detail

The Metal compiler will do its best to at most cost infer the lifetime of all types. Inferring and processing lifetimes would be happening around the second or third pass of the AST as it should be after types and statements are parsed.

Inferring lifetimes should go in some direction like this:

Now let's try to give a real life example, such as this:

def get_module_name(module: &Module): String {
    return _inner_get_module_name(module);
}

Or maybe a more complex example, this one translating Rust code from the Metal codebase:

struct PathBuilder {
    inner: std.Vec<std.Cow<str>>;

    def push(&mut self, content: &str) {
        self.inner.push(Cow::new(content));
    }
}

This is how the compiler should view this:

Deferred RC

Metal is aiming to cover as much use cases as possible, if not all, by purely inferring lifetimes. And the fallback will instead be manual lifetimes only.

On cases where lifetimes can't be inferred, we can either error and force the user to file and issue/change their code, or fallback to a Deferred RC garbage collector which will handle variables with no inferrable lifetimes.

Ideally too we can have manual lifetime definition as to minimize the cases where the Deferred RC collector would have to be used

linear[bot] commented 1 day ago

MET-12 (Tracking Issue) Inferred Lifetimes + Deferred RC

VincentRPS commented 1 day ago

On the topic of why Deferred RC specifically, it allows us to keep Metal 100% pauseless even in the case of not being able to infer a lifetime. If Metal detects a cyclic reference it should error and tell the user to use a Weakref or to manually define the lifetime.