-
-
Notifications
You must be signed in to change notification settings - Fork 804
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: rewrite typechecker journal to handle nested commits (#3375)
this commit fixes a bug which was introduced in 66930fd. when the typechecker enters a nested loop, it can typecheck the inner loop (committing it), and result in invalid state if the outer loop fails to typecheck. this commit implements a checkpointing system in the typechecker state committer so that it can handle nested changes. it also cleans up the data structure used so that there is a single entry point and users of the data structure do not need to think about implementation details. one drawback of this approach is that it *only* handles changes to the metadata dict. non-idempotent changes to the AST during typechecking (such as the use case we are using them for - caching) should then be restricted to changes to the metadata dict so that they can register automatically with the node metadata journal.
- Loading branch information
1 parent
0c7066c
commit 7c3cf61
Showing
6 changed files
with
128 additions
and
37 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
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,80 @@ | ||
import contextlib | ||
from typing import Any | ||
|
||
from vyper.exceptions import VyperException | ||
|
||
|
||
# a commit/rollback scheme for metadata caching. in the case that an | ||
# exception is thrown and caught during type checking (currently, only | ||
# during for loop iterator variable type inference), we can roll back | ||
# any state updates due to type checking. | ||
# this is implemented as a stack of changesets, because we need to | ||
# handle nested rollbacks in the case of nested for loops | ||
class _NodeMetadataJournal: | ||
_NOT_FOUND = object() | ||
|
||
def __init__(self): | ||
self._node_updates: list[dict[tuple[int, str, Any], NodeMetadata]] = [] | ||
|
||
def register_update(self, metadata, k): | ||
prev = metadata.get(k, self._NOT_FOUND) | ||
self._node_updates[-1][(id(metadata), k)] = (metadata, prev) | ||
|
||
@contextlib.contextmanager | ||
def enter(self): | ||
self._node_updates.append({}) | ||
try: | ||
yield | ||
except VyperException as e: | ||
# note: would be better to only catch typechecker exceptions here. | ||
self._rollback_inner() | ||
raise e from e | ||
else: | ||
self._commit_inner() | ||
|
||
def _rollback_inner(self): | ||
for (_, k), (metadata, prev) in self._node_updates[-1].items(): | ||
if prev is self._NOT_FOUND: | ||
metadata.pop(k, None) | ||
else: | ||
metadata[k] = prev | ||
self._pop_inner() | ||
|
||
def _commit_inner(self): | ||
inner = self._pop_inner() | ||
|
||
if len(self._node_updates) == 0: | ||
return | ||
|
||
outer = self._node_updates[-1] | ||
|
||
# register with previous frame in case inner gets commited | ||
# but outer needs to be rolled back | ||
for (_, k), (metadata, prev) in inner.items(): | ||
if (id(metadata), k) not in outer: | ||
outer[(id(metadata), k)] = (metadata, prev) | ||
|
||
def _pop_inner(self): | ||
return self._node_updates.pop() | ||
|
||
|
||
class NodeMetadata(dict): | ||
""" | ||
A data structure which allows for journaling. | ||
""" | ||
|
||
_JOURNAL: _NodeMetadataJournal = _NodeMetadataJournal() | ||
|
||
def __setitem__(self, k, v): | ||
# if we are in a context where we need to journal, add | ||
# this to the changeset. | ||
if len(self._JOURNAL._node_updates) != 0: | ||
self._JOURNAL.register_update(self, k) | ||
|
||
super().__setitem__(k, v) | ||
|
||
@classmethod | ||
@contextlib.contextmanager | ||
def enter_typechecker_speculation(cls): | ||
with cls._JOURNAL.enter(): | ||
yield |
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