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

feat[lang]: add linearization check for initializers #4038

Draft
wants to merge 17 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion tests/functional/syntax/modules/test_initializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,10 @@ def foo():

@deploy
def __init__():
lib2.__init__()
# demonstrate we can call lib1.__init__ through lib2.lib1
# (not sure this should be allowed, really.
lib2.lib1.__init__()
lib2.__init__()
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "lib2.vy": lib2})

Expand Down Expand Up @@ -238,6 +238,52 @@ def __init__():
assert compile_code(main, input_bundle=input_bundle) is not None


def test_initialize_wrong_order(make_input_bundle):
lib1 = """
counter: uint256

@deploy
def __init__():
pass
"""
lib2 = """
import lib1

uses: lib1

counter: uint256

@deploy
def __init__():
pass

@internal
def foo():
lib1.counter += 1
"""
main = """
import lib1
import lib2

initializes: lib2[lib1 := lib1]
initializes: lib1

@deploy
def __init__():
lib2.__init__()
lib1.__init__()
"""
input_bundle = make_input_bundle({"lib1.vy": lib1, "lib2.vy": lib2})

with pytest.raises(InitializerException) as e:
assert compile_code(main, input_bundle=input_bundle) is not None

expected = "Tried to initialize `lib2`, but it depends on `lib1`, "
expected += "which has not been initialized yet."
assert e.value._message == expected
assert e.value._hint == "call `lib1.__init__()` before `lib2.__init__()`."


def test_imported_as_different_names(make_input_bundle):
lib1 = """
counter: uint256
Expand Down
16 changes: 12 additions & 4 deletions vyper/semantics/analysis/module.py
Original file line number Diff line number Diff line change
Expand Up @@ -331,10 +331,7 @@ def validate_initialized_modules(self):
# don't call `__init__()` for modules which don't have
# `__init__()` function
for m in should_initialize.copy():
for f in m.functions.values():
if f.is_constructor:
break
else:
if not any(f.is_constructor for f in m.functions.values()):
del should_initialize[m]

init_calls = []
Expand Down Expand Up @@ -375,6 +372,17 @@ def validate_initialized_modules(self):
hint += "as a top-level statement to your contract"
raise InitializerException(msg, call_node.func, hint=hint)

initializer_info = should_initialize[initialized_module.module_t]
for dep_info in initializer_info.dependencies: # type: ModuleInfo
dep_t = dep_info.module_t
if dep_t not in seen_initializers and dep_t.init_function is not None:
msg = f"Tried to initialize `{initialized_module.alias}`, "
msg += f"but it depends on `{dep_info.alias}`, which has not "
msg += "been initialized yet."
hint = f"call `{dep_info.alias}.__init__()` before "
hint += f"`{initialized_module.alias}.__init__()`."
raise InitializerException(msg, call_node.func, hint=hint)

del should_initialize[initialized_module.module_t]
seen_initializers[initialized_module.module_t] = call_node.func

Expand Down
Loading