Skip to content

C99/C++ scoping rules#3476

Merged
chriseth merged 10 commits intodevelopfrom
scoping
Feb 27, 2018
Merged

C99/C++ scoping rules#3476
chriseth merged 10 commits intodevelopfrom
scoping

Conversation

@chriseth
Copy link
Contributor

@chriseth chriseth commented Feb 9, 2018

Fixes #1679 and #2495.

Still to do:

@chriseth
Copy link
Contributor Author

What should be the semantics of the following snippet? With the current scoping, it will create an error, but only a warning with the new scoping, because in the new scoping, it is shadowing and not redeclaring. With the current implementation, though, the assignment (*) will assign to the second variable, not the first. I guess at least that last behaviour should be fixed.

uint x = 3;
{
  x = 2; // (*)
  uint x = 8;
}

@axic
Copy link
Contributor

axic commented Feb 15, 2018

It is debatable whether we want to support shadowing.

With the current implementation, though, the assignment (*) will assign to the second variable, not the first.

That sounds like a bug.

@chriseth
Copy link
Contributor Author

Yes, I hope I just fixed it.

@chriseth chriseth force-pushed the scoping branch 4 times, most recently from 81b3d6e to 3e8ab71 Compare February 15, 2018 14:46
@chriseth
Copy link
Contributor Author

Sorry, changed it again because a similar functionality is needed in another PR.

This is ready for review.

{
x = 3; // This should still assign to the outer variable
uint x;
x = 4; // This should assign to the new one
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps another test could do a return x here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added some more weird cases.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you forgot to push.

pragma experimental "v0.5.0";
contract test {
function f() public {
uint a = a;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need a test for non-0.5.0 for this case? Perhaps it is covered somewhere.

}
}
)";
CHECK_WARNING(text, "Experimental features");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will need to rebase and fix this if #3600 is merged first.

contract test {
function f() pure public {
for (uint x = 0; x < 10; x ++)
x = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The only difference in this test compared to scoping_for is the lack of curly brackets. Is that intended?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that is exactly the intention.

char const* text = R"(
pragma experimental "v0.5.0";
contract B {
function f() mod(x) pure public { uint x = 7; }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was this working before?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure, I can add a test.


These rules are already introduced now as an experimental feature.

As a consequence, the following examples will compile without warnings (apart from
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe worth mentioning this is not compiling currently due to variable redeclaration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argh, weird, that should not happen.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah sorry, I used the wrong solc version, everything is fine.

@@ -90,6 +94,7 @@ class ReferencesResolver: private ASTConstVisitor
std::vector<ParameterList const*> m_returnParameters;
bool const m_resolveInsideCode;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What exactly is this setting?

Copy link
Contributor Author

@chriseth chriseth Feb 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reference resolution happens in two stages: In the first, all declarations at contract level are registered, but nothing inside code. In the second, code is also examined. This is necessary since at contract level, any declaration can appear anywhere, and for some parts - I don't remember anymore what exactly - it is important at this stage whether an identifier is a type name or e.g. a function, but only inside code.

x = 7;
{
x = 3;
uint x = x; // This should read from the outer and assign to the inner
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is super convoluted :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I take that as a compliment :)
It will generate a shadowing warning, so I think we do not need to make it more strict.

@chriseth chriseth merged commit 908b46e into develop Feb 27, 2018
@axic axic deleted the scoping branch February 28, 2018 15:48
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

Successfully merging this pull request may close these issues.

3 participants

Comments