-
-
Notifications
You must be signed in to change notification settings - Fork 801
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
base: master
Are you sure you want to change the base?
fix[lang]: stateless modules should not be initialized #4347
Conversation
vyper/semantics/analysis/utils.py
Outdated
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 |
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.
i think this belongs better on ModuleT
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.
Okay, moved it.
vyper/semantics/analysis/module.py
Outdated
@@ -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): |
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.
maybe should we also have this restriction on UsesDecl
-- @cyberthirst wdyt?
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
andinitializes
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