From 09712bad96d3c8a4bbb52226c387163faf0f1610 Mon Sep 17 00:00:00 2001 From: Charles Cooper Date: Thu, 21 Sep 2023 11:35:55 -0400 Subject: [PATCH] refactor: fix IRnode.from_list IRnode.from_list() currently has a footgun where it modifies metadata on the input. this commit copies the input to be slightly safer --- vyper/codegen/ir_node.py | 32 +++++++++++++++++++------------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/vyper/codegen/ir_node.py b/vyper/codegen/ir_node.py index b1a71021c8..40c4606784 100644 --- a/vyper/codegen/ir_node.py +++ b/vyper/codegen/ir_node.py @@ -188,12 +188,14 @@ def __init__( error_msg: Optional[str] = None, mutable: bool = True, add_gas_estimate: int = 0, - encoding: Encoding = Encoding.VYPER, + encoding: Encoding = None, is_self_call: bool = False, passthrough_metadata: dict[str, Any] = None, ): if args is None: args = [] + if encoding is None: + encoding = Encoding.VYPER self.value = value self.args = args @@ -610,26 +612,30 @@ def from_list( add_gas_estimate: int = 0, is_self_call: bool = False, passthrough_metadata: dict[str, Any] = None, - 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,