Skip to content

Lifetimes #4

Open
Open
@DGriffin91

Description

Ideas regarding lifetimes in Sarus

There would be 2 lifetimes: inside and outside

Both values and variables can be marked with these lifetimes

In the compiler:

enum Lifetime {
    Inside,
    Outside(arg: u64),
}
struct Arg {
    ...
    // args can have their lifetime tied input args
    // this number is the input arg that the lifetime is tied to
    pub lifetime: Option<Lifetime>,
}

Sarus examples:
[i64;100] is an array of i64, [..] returns a slice from 0 to len

fn some_func() -> (b: [u8]) {
    a = [0;100] //inside, copy
    b = a[..] //inside, not copy
    // ERROR b lifetime is inside, and b is not copy
    // consider declaring fn as inline
}

When a function returns or mutates something that derives its lifetime from an arg, that is shown in the funcdef.

fn some_func(a: [u8]) -> (b: [u8]) {
    b = a[5..10] //inherit arg a, not copy
}
fn other_func() {
    arr = [0;100] //inside, copy
    a = arr[..] //inside, not copy

    //fn some_func says return 0 lifetime is the same as arg 0
    b = some_func(a) //inside, not copy
}
fn mutates_an_arg(a: Stuff) {
    b = [0;100][..] //inside, not copy
    a.s = b
    //ERROR b lifetime is inside, and b is not copy
    //OR perhaps in caller's function b is marked as no longer usable after this call
}
fn mutates_an_arg(a: [i64], b: Stuff) {
    b.sl = a //arg b inherit arg a, not copy
}
fn other_func(arr: [i64;100]) {
    s = Stuff { //outside, not copy
        sl: arr[..] //outside, not copy
    }

    a = [0;100][..] //inside, not copy

    //fn mutates_an_arg says it will make arg 1 lifetime match arg 0
    mutates_an_arg(a, s) //inside, not copy
    //arg 0 is inside, so now s is marked with inside
}

Need to also account of lifetime implications in loops. The scope of a loop will need to be handled similarly to a function call.

fn main() -> () {
    a = [0;10000][..]
    i = 0 while i < 5 {i+=1} : {
        if i == 0 {
            b = [2;10000] //(while)inner, copy
            a = b[..] //TODO compiler error
        }
    }
}

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions