Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lifetimes #4

Open
DGriffin91 opened this issue Dec 20, 2021 · 0 comments
Open

Lifetimes #4

DGriffin91 opened this issue Dec 20, 2021 · 0 comments

Comments

@DGriffin91
Copy link
Owner

DGriffin91 commented Dec 20, 2021

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
        }
    }
}
@DGriffin91 DGriffin91 mentioned this issue Dec 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant