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

Warn about shadowing state variables #973

Closed
1 task
Denton-L opened this issue Aug 29, 2016 · 5 comments
Closed
1 task

Warn about shadowing state variables #973

Denton-L opened this issue Aug 29, 2016 · 5 comments

Comments

@Denton-L
Copy link
Contributor

Denton-L commented Aug 29, 2016

Unsolved questions:

  • in which situations should warnings be emitted?

We have to keep in mind that we want to have identifiers at global level at some point, but I think it is still fine because we have qualified import statements.


The following is valid Solidity code:

contract Example {
    uint n = 2;

    function test() constant returns (uint) {
        uint n = 1;
        return n;// Will return 1
    }
}

I propose that the compiler should not allow function-level variables to be named identically to contract-level variables.

In other languages, such as Java, this would be valid code because you can differentiate between fields and local variables using the this keyword. In Solidity, however, this is an issue because this has a different purpose and accessing this.* will spawn an external call, which would probably not be expected behaviour.

In addition to this, this behaviour is confusing for people who are starting out with the language.

@redsquirrel
Copy link
Contributor

👍🏼

@chriseth chriseth changed the title Prevent Contract-Level and Function-Level Variable Name Collisions Warn about overwriting identifiers Aug 30, 2016
@chriseth chriseth added this to the 2-safety-warnings milestone Aug 30, 2016
@chriseth chriseth added the soon label Aug 30, 2016
@ethers
Copy link
Member

ethers commented Nov 20, 2016

this behaviour is confusing for people who are starting out with the language.

+100

Another example using a "named return" related to #718

contract Example {
    uint n = 2;

    function test() constant returns (uint n) {
        return n; // Will return 0
    }
}

And another:

contract Example {
    uint n = 2;

    function test() constant returns (uint n) {
        n = 1;
        return n; // Will return 1
    }
}

One more:

contract Example {
    uint n = 2;
    uint x = 3;

    function test() constant returns (uint x) {
        uint n = 4;
        return n+x; // Will return 4
    }
}

Fortunately this doesn't compile:

contract Example {
    uint n = 2;

    function test() constant returns (uint n) {
        uint n = 3;
        return n; // does not compile
    }
}

@ghost
Copy link

ghost commented Feb 6, 2017

Got burned by this. Lost 20 minutes or so. Added a global variable to a contract I'd not been working on for a few days and forgot that that variable name was already used inside a function.

@axic
Copy link
Member

axic commented Jun 30, 2017

I think we should add a warning for shadowing "globals".

@axic axic changed the title Warn about overwriting identifiers Warn about shadowing state variables Jul 11, 2017
@chriseth
Copy link
Contributor

This is implemented now.

axic pushed a commit that referenced this issue Nov 20, 2018
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

5 participants