Skip to content

Commit

Permalink
refactor: fix IRnode.from_list
Browse files Browse the repository at this point in the history
IRnode.from_list() currently has a footgun where it modifies metadata on
the input. this commit copies the input to be slightly safer
  • Loading branch information
charles-cooper committed Sep 21, 2023
1 parent 79303fc commit 624c865
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions vyper/codegen/ir_node.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import contextlib
import copy
import re
from enum import Enum, auto
from functools import cached_property
Expand Down Expand Up @@ -181,12 +182,14 @@ def __init__(
source_pos: Optional[Tuple[int, int]] = None,
annotation: Optional[str] = None,
error_msg: Optional[str] = None,
encoding: Encoding = None,
mutable: bool = True,
add_gas_estimate: int = 0,
encoding: Encoding = Encoding.VYPER,
):
if args is None:
args = []
if encoding is None:
encoding = Encoding.VYPER

self.value = value
self.args = args
Expand Down Expand Up @@ -596,26 +599,30 @@ def from_list(
error_msg: Optional[str] = None,
mutable: bool = True,
add_gas_estimate: int = 0,
encoding: Encoding = Encoding.VYPER,
encoding: Encoding = None,
) -> "IRnode":
if isinstance(typ, str):
raise CompilerPanic(f"Expected type, not string: {typ}")

if isinstance(obj, IRnode):
ret = copy.copy(obj)
# note: this modify-and-returnclause is a little weird since
# the input gets modified. CC 20191121.
if typ is not None:
obj.typ = typ
if obj.source_pos is None:
obj.source_pos = source_pos
if obj.location is None:
obj.location = location
if obj.encoding is None:
obj.encoding = encoding
if obj.error_msg is None:
obj.error_msg = error_msg

return obj
ret.typ = typ
if source_pos is not None:
ret.source_pos = source_pos
if location is not None:
ret.location = location
if encoding is not None:
ret.encoding = encoding
if error_msg is not None:
ret.error_msg = error_msg
if add_gas_estimate != 0:
ret.add_gas_estimate = add_gas_estimate

return ret

elif not isinstance(obj, list):
return cls(
obj,
Expand Down

0 comments on commit 624c865

Please sign in to comment.