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

Return statements check #1384

Merged
merged 11 commits into from
Apr 12, 2019

Conversation

jacqueswww
Copy link
Contributor

@jacqueswww jacqueswww commented Apr 5, 2019

What I did

Fixes #590, and indirectly fixes #1383 .

Special thanks to @yzhang90 this is working really well 💯 👌

How I did it

See discussion in #590.

How to verify it

Check tests.

Description for the changelog

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@jacqueswww jacqueswww requested a review from fubuloubu April 5, 2019 22:09
@jacqueswww jacqueswww changed the title Return statement check Return statements check Apr 5, 2019
@jacqueswww
Copy link
Contributor Author

@charles-cooper have a look, I think this looks pretty decent! :)

tests/parser/exceptions/test_constancy_exception.py Outdated Show resolved Hide resolved
tests/parser/syntax/test_unbalanced_return.py Show resolved Hide resolved
tests/parser/syntax/test_unbalanced_return.py Outdated Show resolved Hide resolved
vyper/parser/parser_utils.py Outdated Show resolved Hide resolved
@jacqueswww
Copy link
Contributor Author

@fubuloubu @charles-cooper I have fixed all above comments, I think it's ready to merge. Dead code discussion goes to an issue instead.

@@ -709,6 +712,23 @@ def make_setter(left, right, location, pos, in_function_call=False):
raise Exception("Invalid type for setters")


def is_return_from_function(node: Union[ast.AST, List[Any]]) -> bool:
is_selfdesctruct = (
Copy link
Member

Choose a reason for hiding this comment

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

is_selfdesctruct -> is_selfdestruct

@charles-cooper
Copy link
Member

charles-cooper commented Apr 8, 2019

Could we add a couple tests for the single exit checker? Here's a couple examples which should fail the single exit checker

def test1() : 
  return
  return
def test2() : 
  return
  selfdestruct(msg.sender)
def test3() -> int128 : 
  if block.number % 2 == 1 : 
    return 1
  else : 
    raise "even block number"
  return 0

@charles-cooper
Copy link
Member

Although at this point, the single exit checker looks so much like a dead code checker that we could easily strengthen it to catch more dead code conditions, e.g.

def foo() : 
  return 1
  self.x += 1

I believe the single exit checker only needs to be modified to read

def check_return_body(self, node, node_list):
    for i, n in enumerate(node_list) : 
        if is_return_from_function(n) and i + 1 < len(node_list) : 
            raise StructureException(
                f'Code cannot be reached after return, raise or selfdestruct',
                node
            )

Other kinds of dead code checking are still out of scope (especially involving branches, e.g. the following)

if False : 
  <body>

@jacqueswww
Copy link
Contributor Author

@fubuloubu OK I think all of the above with additional steps as highlighted by @charles-cooper, I will add them :)

@fubuloubu
Copy link
Member

I resolved all my comments. It seems like the test cases will be rehashed in #1390, which is not ideal to me but it's okay.

@jacqueswww
Copy link
Contributor Author

jacqueswww commented Apr 11, 2019

@charles-cooper @fubuloubu I added the check for succeeding code after exit statements,

@private
def valid_address(sender: address) -> bool:
    selfdestruct(sender)
    a: address = sender

Will fail.

I leave the check for type checking of selfdestruct & "return None" for #1390. :)

@charles-cooper
Copy link
Member

charles-cooper commented Apr 11, 2019

Awesome! I think the only thing left which is bugging me is the variable named is_selfdesctruct ;)

@jacqueswww
Copy link
Contributor Author

Perfect, will fix then merge hehe.

@jacqueswww jacqueswww merged commit 73abd4b into vyperlang:master Apr 12, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
3 participants