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

Warning: This declaration shadows an existing declaration. #2391

Closed
vittominacori opened this issue Oct 20, 2020 · 7 comments
Closed

Warning: This declaration shadows an existing declaration. #2391

vittominacori opened this issue Oct 20, 2020 · 7 comments

Comments

@vittominacori
Copy link
Contributor

There are lot of Warning: This declaration shadows an existing declaration..

Is there a way to silence these warnings.

I'm using solc 0.7.4

Compilation warnings: 
contracts/token/ERC20/ERC20.sol:57:18: Warning: This declaration shadows an existing declaration.
    constructor (string memory name, string memory symbol) {
                 ^----------------^
contracts/token/ERC20/ERC20.sol:66:5: The shadowed declaration is here:
    function name() public view returns (string memory) {
    ^ (Relevant source part starts here and spans across multiple lines).

@nventuro
Copy link
Contributor

nventuro commented Oct 21, 2020

Solidity 0.7.4 now properly diagnoses shadowing when it clashes with variables or functions that are only declared later on in the file.

This leads to a massive number of warnings, since Contracts has sort of relied on this and used careful ordering to avoid warnings. A compilation of Contracts itself yields 250 lines of warnings. Pretty much every user of the library gets a few of these when using 0.7.4.

@ongrid
Copy link

ongrid commented Oct 23, 2020

I confirm the issue with solc 0.7.4

pragma solidity ^0.7.0;

import "../openzeppelin-contracts/contracts/access/AccessControl.sol";
import "../openzeppelin-contracts/contracts/token/ERC20/ERC20.sol";
import "../openzeppelin-contracts/contracts/token/ERC20/ERC20Burnable.sol";
import "../openzeppelin-contracts/contracts/GSN/Context.sol";

contract Token is AccessControl, ERC20Burnable {
    bytes32 public constant MINTER_ROLE = keccak256("MINTER_ROLE");

    constructor() ERC20("Aura Token", "TKN") {
        _setupRole(MINTER_ROLE, _msgSender());
    }

    function mint(address to, uint256 amount) public virtual {
        require(hasRole(MINTER_ROLE, _msgSender()), "Need MINTER_ROLE");
        _mint(to, amount);
    }
}
$ npx oz compile --solc-version 0.7.3
✓ Compiled contracts with solc 0.7.3 (commit.9bfce1f6)

$ npx oz compile --solc-version 0.7.4
✓ Compiled contracts with solc 0.7.4 (commit.3f05b770)
Compilation warnings: 
openzeppelin-contracts/contracts/token/ERC20/ERC20.sol:57:18: Warning: This declaration shadows an existing declaration.
    constructor (string memory name, string memory symbol) {
                 ^----------------^
openzeppelin-contracts/contracts/token/ERC20/ERC20.sol:66:5: The shadowed declaration is here:
    function name() public view returns (string memory) {
    ^ (Relevant source part starts here and spans across multiple lines).

openzeppelin-contracts/contracts/token/ERC20/ERC20.sol:57:38: Warning: This declaration shadows an existing declaration.
    constructor (string memory name, string memory symbol) {
                                     ^------------------^
openzeppelin-contracts/contracts/token/ERC20/ERC20.sol:74:5: The shadowed declaration is here:
    function symbol() public view returns (string memory) {
    ^ (Relevant source part starts here and spans across multiple lines).

@frangio
Copy link
Contributor

frangio commented Oct 23, 2020

If anyone would like to provide a PR for this it would be really appreciated. I won't be able to tackle this until next week.

@frangio
Copy link
Contributor

frangio commented Oct 27, 2020

As a follow up to the number of warnings this actually produces, I'd like to clarify there is only 14 warnings due to variable shadowing. In any case, we're working on this in #2396 and it will be released as soon as possible.

frangio pushed a commit that referenced this issue Oct 28, 2020
* Fix unnamed return variable warning

This commit fixes warnings thrown by the solc 0.7.4 compiler:
"Warning: Unnamed return variable can remain unassigned. Add an explicit
return with value to all non-reverting code paths or name the variable."

* Fix function state mutability warning

This commit fixes warnings thrown by the solc 0.7.4 compiler:
"Warning: Function state mutability can be restricted to pure"

* Fix shadows an existing declaration warning

This commit fixes warnings thrown by the solc 0.7.4 compiler:
"Warning: This declaration shadows an existing declaration."

1. Arguments by default are not underscored.
2. If the name isn't available due to shadowing, use prefix underscore.
3. If prefix underscore isn't available due to shadowing, use suffix underscore.
@frangio
Copy link
Contributor

frangio commented Oct 28, 2020

Fixed by @zemse in #2396. Thank you!

Released now in @openzeppelin/contracts@3.2.2-solc-0.7.

@frangio frangio closed this as completed Oct 28, 2020
@0xKarl98
Copy link

why my solc 0.8.15 still got the same error ?

@frangio
Copy link
Contributor

frangio commented Feb 27, 2023

@0xKarl98 Open a new issue.

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