-
-
Notifications
You must be signed in to change notification settings - Fork 810
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: constructor context for internal functions (#3388)
this commit fixes two related issues with initcode generation: - nested internal functions called from the constructor would cause a compiler panic - internal functions called from the constructor would not read/write from the correct immutables space the relevant examples reproducing each issue are in the tests. this commit fixes the issue by - not trying to traverse the call graph to figure out which internal functions to include in the initcode. instead, all internal functions are included, and we rely on the dead code eliminator to remove unused functions - adding a "constructor" flag to the codegen, so we can distinguish between internal calls which are being generated to include in initcode or runtime code.
- Loading branch information
1 parent
1c8349e
commit c202c4e
Showing
9 changed files
with
203 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from vyper.compiler.phases import CompilerData | ||
|
||
|
||
def test_dead_code_eliminator(): | ||
code = """ | ||
s: uint256 | ||
@internal | ||
def foo(): | ||
self.s = 1 | ||
@internal | ||
def qux(): | ||
self.s = 2 | ||
@external | ||
def bar(): | ||
self.foo() | ||
@external | ||
def __init__(): | ||
self.qux() | ||
""" | ||
|
||
c = CompilerData(code, no_optimize=True) | ||
initcode_asm = [i for i in c.assembly if not isinstance(i, list)] | ||
runtime_asm = c.assembly_runtime | ||
|
||
foo_label = "_sym_internal_foo___" | ||
qux_label = "_sym_internal_qux___" | ||
|
||
# all the labels should be in all the unoptimized asms | ||
for s in (foo_label, qux_label): | ||
assert s in initcode_asm | ||
assert s in runtime_asm | ||
|
||
c = CompilerData(code, no_optimize=False) | ||
initcode_asm = [i for i in c.assembly if not isinstance(i, list)] | ||
runtime_asm = c.assembly_runtime | ||
|
||
# qux should not be in runtime code | ||
for instr in runtime_asm: | ||
if isinstance(instr, str): | ||
assert not instr.startswith(qux_label), instr | ||
|
||
# foo should not be in initcode asm | ||
for instr in initcode_asm: | ||
if isinstance(instr, str): | ||
assert not instr.startswith(foo_label), instr |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters