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: metadata journal can rollback incorrectly #3569

Merged
merged 5 commits into from
Sep 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
82 changes: 82 additions & 0 deletions tests/ast/test_metadata_journal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
from vyper.ast.metadata import NodeMetadata
from vyper.exceptions import VyperException


def test_metadata_journal_basic():
m = NodeMetadata()

m["x"] = 1
assert m["x"] == 1


def test_metadata_journal_commit():
m = NodeMetadata()

with m.enter_typechecker_speculation():
m["x"] = 1

assert m["x"] == 1


def test_metadata_journal_exception():
m = NodeMetadata()

m["x"] = 1
try:
with m.enter_typechecker_speculation():
m["x"] = 2
m["x"] = 3

assert m["x"] == 3
raise VyperException("dummy exception")

except VyperException:
pass

# rollback upon exception
assert m["x"] == 1


def test_metadata_journal_rollback_inner():
m = NodeMetadata()

m["x"] = 1
with m.enter_typechecker_speculation():
m["x"] = 2

try:
with m.enter_typechecker_speculation():
m["x"] = 3
m["x"] = 4 # test multiple writes

assert m["x"] == 4
raise VyperException("dummy exception")

except VyperException:
pass

assert m["x"] == 2


def test_metadata_journal_rollback_outer():
m = NodeMetadata()

m["x"] = 1
try:
with m.enter_typechecker_speculation():
m["x"] = 2

with m.enter_typechecker_speculation():
m["x"] = 3
m["x"] = 4 # test multiple writes

assert m["x"] == 4

m["x"] = 5

raise VyperException("dummy exception")

except VyperException:
pass

assert m["x"] == 1
5 changes: 4 additions & 1 deletion vyper/ast/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,11 @@ def __init__(self):
self._node_updates: list[dict[tuple[int, str, Any], NodeMetadata]] = []

def register_update(self, metadata, k):
KEY = (id(metadata), k)
if KEY in self._node_updates[-1]:
return
prev = metadata.get(k, self._NOT_FOUND)
self._node_updates[-1][(id(metadata), k)] = (metadata, prev)
self._node_updates[-1][KEY] = (metadata, prev)

@contextlib.contextmanager
def enter(self):
Expand Down