-
Notifications
You must be signed in to change notification settings - Fork 6.3k
Fix incorrect shadow warning for variables in non-overlapping scopes #16379
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -260,6 +260,9 @@ void NameAndTypeResolver::warnHomonymDeclarations() const | |
| for (Declaration const* outerDeclaration: outerDeclarations) | ||
| { | ||
| solAssert(outerDeclaration, ""); | ||
| if (auto const* varDecl = dynamic_cast<VariableDeclaration const*>(outerDeclaration)) | ||
| if (varDecl->isLocalVariable() && outerDeclaration->location().start >= innerLocation->start) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don't think it's a good idea for semantics to be dependent on exact locations in the source file. Locations are just debug information and might be unavailable or imprecise. It's easy to mess with them e.g. in the AST import mode. We must have some more direct way to determine the order of declarations without locations. Though I see that this is not a new thing we already have code that does this, e.g. in |
||
| continue; | ||
| if (dynamic_cast<MagicVariableDeclaration const*>(outerDeclaration)) | ||
| magicShadowed = true; | ||
| else if (!outerDeclaration->isVisibleInContract()) | ||
|
|
||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This PR needs more tests, both for the cases that should now be fixed and the ones where we want to ensure we still have the warning:
I posted some of those in #16190 (comment), #16190 (comment) and #16190 (comment). But please also look at the existing test we have for shadowing and see if there might be some other gaps in our coverage.
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some of those cases it would also be good to have equivalent semantic tests. For example in the constant case from #16190 (comment) we would be interested not only whether the analysis produces the warning but also that the code generator follows the same logic and uses the right constant in calculations inside and outside of the contract: contract C {
uint256 constant X = 0x1234;
function getInnerX() internal returns (uint) { return X; }
function f() public returns (uint, uint) { return (getInnerX(), getOuterX()); }
}
uint256 constant X = 0x5678;
function getOuterX() returns (uint) { return X; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The PR needs a changelog entry.