Skip to content

Deprecate the var keyword in SyntaxChecker#3425

Merged
chriseth merged 3 commits intoargotorg:developfrom
jevogel:3301
Feb 13, 2018
Merged

Deprecate the var keyword in SyntaxChecker#3425
chriseth merged 3 commits intoargotorg:developfrom
jevogel:3301

Conversation

@jevogel
Copy link
Contributor

@jevogel jevogel commented Jan 23, 2018

Fix #3301 by extending libsolidity/analysis/SyntaxChecker with a visitor for VariableDeclaration where the typeName is empty (means "var"). The compiler emits a warning for such instances, unless the experimental mode V050 is turned on, in which case it emits an error. This addition causes 7 errors in SolidityTests due to their use of the var keyword.

@axic
Copy link
Contributor

axic commented Jan 24, 2018

Thank you!

Can you please add test cases (in test/libsolidity/SolidityNameAndTypeChecker) and update the change log?

@jevogel
Copy link
Contributor Author

jevogel commented Jan 24, 2018

Sure. Should I do anything about the existing test cases that are failing due to their use of var?

@axic
Copy link
Contributor

axic commented Jan 24, 2018

Please fix them :)

The ones which are specifically made to test var should use CHECK_MULTI_WARNING or something similar and must keep the var keyword.

In cases it is used by mistake, it should be replaced with a proper type (probably there aren't any cases of this).

@chriseth
Copy link
Contributor

I wonder whether it might be better to already do that at parsing time, but I guess the problem is that we do not yet have access to the pragma at that point - is that correct?

@axic
Copy link
Contributor

axic commented Jan 24, 2018

Yes, pragma is evaluated later. But this should be fine.

@jevogel
Copy link
Contributor Author

jevogel commented Jan 24, 2018

I edited all test cases that were failing to check for the var deprecated warning. However, it looks like some of these might require modification to replace var in them because otherwise the test will not be checking what it should.

If you could take a look at the changes and let me know which test cases should be reworked, I can take a shot at it.

contract B is A { }
)";
// Error (TypeError): Definition of base has to precede definition of derived contract
// Warning: Use of var keyword is deprecated
Copy link
Contributor

Choose a reason for hiding this comment

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

Why did you put this 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.

Not sure how that got in there, but I'll get rid of it.

}
}
)";
CHECK_WARNING(sourceCode, "uint8, which can hold values between 0 and 255");
Copy link
Contributor

Choose a reason for hiding this comment

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

It should be checked that this error message is present until we disallow var completely.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Can you clarify? CHECK_WARNING_ALLOW_MULTI seems to only allow the first warning to be checked, so if var is used in this test it will cause the first warning and it will not be possible to check for the size warning. Is there a way to check both?

Copy link
Contributor

Choose a reason for hiding this comment

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

I fear someone has to extend the macro...

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 could look into this within the next week or so. Do you want to file it as a separate issue?

}
}
)";
CHECK_WARNING(text, "\"callcode\" has been deprecated in favour of \"delegatecall\"");
Copy link
Contributor

Choose a reason for hiding this comment

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

You can replace var with bool in this example.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Changing var to bool here gives me the message

Type function () payable returns (bool) is not implicitly convertible to expected type bool.

Copy link
Contributor

Choose a reason for hiding this comment

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

You might be able to go with just function f() pure public { address(0x12).callcode; } for now.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, then probably best to just remove the variable.

@@ -4838,7 +4850,10 @@ BOOST_AUTO_TEST_CASE(warn_about_callcode)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

same here

}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
CHECK_WARNING(text, "Use of var keyword is deprecated");
Copy link
Contributor

Choose a reason for hiding this comment

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

function pure external g = this.h should work

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Looks like the next block below tests this exactly, so I'll just remove this block.

@@ -6817,7 +6832,7 @@ BOOST_AUTO_TEST_CASE(function_types_sig)
}
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 don't modify the test by adding a type for i.

}
}
)";
CHECK_WARNING(text, "\"sha3\" has been deprecated in favour of \"keccak256\"");
Copy link
Contributor

Choose a reason for hiding this comment

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

Please replace var by bytes32

// Error (TypeError):
// Function declared as pure, but this expression (potentially) reads from the environment or state and thus requires "view"
CHECK_WARNING_ALLOW_MULTI(
"contract C { function f() pure public { var x = " + x + "; x; } }",
Copy link
Contributor

Choose a reason for hiding this comment

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

This is a tricky one. Perhaps you could turn this vector of expressions into a vector of pairs of expressions and their type, but it might not work for all of them because some types do not have a name.

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 took a stab at this but ran into some problems. Maybe you can spot the root cause? gist.

function() payable public {}
}
)";
CHECK_SUCCESS_NO_WARNINGS(text);
Copy link
Contributor

Choose a reason for hiding this comment

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

Please replace var by the proper types here.

@jevogel
Copy link
Contributor Author

jevogel commented Jan 24, 2018

Made some progress on fixing the tests, but ran into some problems with the ViewPureChecker. Here are the errors I'm getting when running the tests: gist.

@jevogel
Copy link
Contributor Author

jevogel commented Feb 2, 2018

I am a git noob and inadvertently polluted this pull request with a ton of commits from develop. Should I just close this PR and start over?

@axic
Copy link
Contributor

axic commented Feb 2, 2018

Please don't close. Just run git rebase upstream/develop if upstream is the upstream.

Assuming you're local git copy has origin as your repo, then do the following:

$ git remote add upstream https://github.com/ethereum/solidity
$ git fetch upstream
$ get rebase upstream/develop

@jevogel
Copy link
Contributor Author

jevogel commented Feb 2, 2018

I did the rebase but all of the commits are still showing up in the history of this PR. Is that a problem?

@axic
Copy link
Contributor

axic commented Feb 2, 2018

It seems to be correct for me. Refresh github a few times, sometimes it can take a while.

@jevogel
Copy link
Contributor Author

jevogel commented Feb 2, 2018

Awesome, that worked. Thanks for your patience!

@chriseth
Copy link
Contributor

chriseth commented Feb 9, 2018

Started #3479 to allow searching in a set of errors.

@chriseth
Copy link
Contributor

All tests should work now, but we still need to take a look at the expectations:

Some tests now search for "var is deprecated", although what they actually should do is check that there is no other warning apart from "var is deprecated".

We should change the 'multi-error' feature such that we list all errors/warnings that occur (i.e. the exact same set), and not only search for one to be present.

{
CHECK_ERROR(
"contract C { function f() pure public { var x = " + x + "; x; } }",
"contract C { function f() pure public { " + x + "; } }",
Copy link
Contributor

Choose a reason for hiding this comment

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

The code was initially like that to avoid the "statement does not have an effect" warning, but it seems to be no problem.

@chriseth
Copy link
Contributor

Rebased onto the new multi-error testing framework. Will adjust the expectations tomorrow.

@axic
Copy link
Contributor

axic commented Feb 12, 2018

@chriseth was it intended to drop all the other changes it had? I assume all of that was obsoleted by the new framework.

@chriseth
Copy link
Contributor

Yes, it was intended, I would rather restart adding the expectations instead of going back and forth in the previous commits.

@axic
Copy link
Contributor

axic commented Feb 13, 2018

Made the bear minimum changes required in tests. Many other tests could be cleaned up too.

@chriseth chriseth merged commit 729c6a9 into argotorg:develop Feb 13, 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

Successfully merging this pull request may close these issues.

4 participants