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

fix[lang]: stateless modules should not be initialized #4347

Open
wants to merge 12 commits into
base: master
Choose a base branch
from

Conversation

sandbubbles
Copy link
Collaborator

What I did

Forbid initializes on stateless modules as defined in #4050.

How I did it

Define modules with storage variables, transient variables or with uses and initializes as stateful.
When using initializes on stateless module, raise exception.

How to verify it

Commit message

Commit message for the final, squashed PR. (Optional, but reviewers will appreciate it! Please see our commit message style guide for what we would ideally like to see in a commit message.)

Description for the changelog

Cute Animal Picture

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

Comment on lines 741 to 754
def is_stateless(module: vy_ast.Module):
"""
Determine whether a module is stateless by examining its top-level declarations.
A module has state if it contains storage variables, transient variables, or
immutables, or if it includes a "uses" or "initializes" declaration.
"""
for i in module.body:
if isinstance(i, (vy_ast.InitializesDecl, vy_ast.UsesDecl)):
return False
if isinstance(i, vy_ast.VariableDecl) and not i.is_constant:
return False
if isinstance(i, vy_ast.FunctionDef) and i.name == "__init__":
return False
return True
Copy link
Member

Choose a reason for hiding this comment

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

i think this belongs better on ModuleT

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Okay, moved it.

@@ -409,6 +410,10 @@ def visit_InitializesDecl(self, node):
module_info = get_expr_info(module_ref).module_info
if module_info is None:
raise StructureException("Not a module!", module_ref)
if is_stateless(module_info.module_node):
Copy link
Member

Choose a reason for hiding this comment

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

maybe should we also have this restriction on UsesDecl -- @cyberthirst wdyt?

@sandbubbles sandbubbles marked this pull request as ready for review November 9, 2024 10:19
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.

2 participants