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

lint for shadowing live variables #124

Open
nikomatsakis opened this issue Feb 11, 2022 · 0 comments
Open

lint for shadowing live variables #124

nikomatsakis opened this issue Feb 11, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@nikomatsakis
Copy link
Member

At least as currently implemented, Dada distinguishes declaring a new variable (p = expr) from re-assigning the value of an existing variable (p := expr). This can easily be confusing. Consider this program:

fn main() {
    n = 0
    while n < 3 {
        n = n + 1
    }
}

This program will never terminate because the n = n + 1 declares a new variable.

We should either get rid of the idea of := or have a lint that detects when you shadow a live variable (variable may be accessed again later on). Therefore the above code would warn. This code would not:

async fn main() {
    n = 0
    print(n).await
    n = 1
    print(n).await
}

and neither would this

async fn main() {
    n = 0
    print(n).await
    if n > 0 {
        n = 5
        print(n).await
    }
    n := 1 # ignores current value of `n`
    print(n).await
}

but this would:

async fn main() {
    n = 0
    print(n).await
    if n > 0 {
        n = 5
        print(n).await
    }
    n += 1 # uses current value of `n`
    print(n).await
}

I would do this analysis on the validated tree.

@nikomatsakis nikomatsakis added the enhancement New feature or request label Feb 11, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant