-
-
Notifications
You must be signed in to change notification settings - Fork 887
Folding Not Supported in Certain Locations #4190
Copy link
Copy link
Closed
Labels
Description
Version Information
- vyper Version (output of
vyper --versionOR linkable commit hash vyperlang/vyper@): b43ffac
Issue Description
Several Vyper constructs require or can be optimized if constant values
are provided. To achieve this, AST nodes can be folded to constants by
the compiler, however, the following locations do not support folding:
-
lengthinslice(x, start, length)whenxismsg.dataor
address.code:isinstance(parent.args[2], vy_ast.Int)in
_validate_msg_data_attribute()isinstance(parent.args[2], vy_ast.Int)in
_validate_address_code()
-
lengthinslice(x, start, length):if length_literal is not Nonecondition in
Slice.fetch_call_return()
-
indexinvariable[index]when the variable is an array.isinstance(node, vy_ast.Int)in
_SequenceT.validate_index_type()
-
indexinvariable[index]when the variable is a tuple.not isinstance(node, vy_ast.Int)in
TupleT.validate_index_type()
-
xoryinx ** y:left, right = _get_lr()inNumericT.validate_numeric_op()
-
xinconvert(x, T)_convert._to_int(),_convert.to_decimal()and
_convert._cast_bytestring()does not check ifexprhas a
folded value.
-
topicinraw_log(topic, data).not isinstance(node.args[0], vy_ast.List)in
RawLog.infer_arg_types()
POC
The following example demonstrates the different issues.
@external
def foo():
x: Bytes[32] = slice(msg.data, 0, 31 + 1) # StructureException@external
def foo(tuple: (uint256,uint256)) -> uint256:
return tuple[0+1] # InvalidTypek: constant(Bytes[3]) = b'aaa'
@external
def foo():
a: Bytes[2] = convert (k, Bytes[2]) # compiles instead of failing
b: Bytes[2] = convert (b'aaa', Bytes[2]) # TypeMismatcha: constant(uint256) = 12
@external
def foo():
# The following check is inserted by the compiler although it is not necessary
# [assert, [iszero, [shr, 128, 12 <12>]]],
b: uint128 = convert (a, uint128)topic: constant(bytes32) = 0x1212121212121210212801291212121212121210121212121212121212121212
@external
def foo():
raw_log([[topic]][0], b'') # InvalidTypeReactions are currently unavailable