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

Bindings that shadow static items need to be renamed #92

Open
jameysharp opened this Issue Nov 13, 2016 · 0 comments

Comments

Projects
None yet
1 participant
@jameysharp
Copy link
Owner

jameysharp commented Nov 13, 2016

In Rust, when a static variable is in scope, its name must not be used in the pattern that binds a function parameter or let-bound variable. So in this example, both f and g are disallowed:

static mut noexec : i32;
fn f(mut noexec : bool) { }
fn g() {
    let mut noexec : bool;
}

In C, on the other hand, it's perfectly legal for parameters and variable declarations to shadow globals. Even though enough people consider it bad style that GCC has a warning available about it, that probably shouldn't stop us from putting some effort into generating legal Rust for code like this.

The easiest thing to do, probably, is to rename any variable declaration that shadows another that's in scope, regardless of whether the shadowing is harmful.

That's a little tricky to do correctly, because C allows multiple declarations of the same symbol within the same scope as long as they have the same type, and they all get merged. It's only when an identifier is reused within a nested scope that it shadows the outer declaration.

It's also tricky because we should probably detect when an outer declaration occurs after the block containing the declaration that shadows it, since Rust doesn't care what order items are declared in.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment