@@ -363,20 +363,28 @@ def to_decimal(expr, arg, out_typ):
363
363
raise CompilerPanic ("unreachable" )
364
364
365
365
366
- @_input_types (IntegerT , DecimalT , BytesM_T , AddressT , BytesT , BoolT )
366
+ @_input_types (IntegerT , DecimalT , BytesM_T , AddressT , BytesT , StringT , BoolT )
367
367
def to_bytes_m (expr , arg , out_typ ):
368
368
_check_bytes (expr , arg , out_typ , max_bytes_allowed = out_typ .m )
369
369
370
- if isinstance (arg .typ , BytesT ):
371
- bytes_val = LOAD (bytes_data_ptr (arg ))
372
-
373
- # zero out any dirty bytes (which can happen in the last
374
- # word of a bytearray)
375
- len_ = get_bytearray_length (arg )
376
- num_zero_bits = IRnode .from_list (["mul" , ["sub" , 32 , len_ ], 8 ])
377
- with num_zero_bits .cache_when_complex ("bits" ) as (b , num_zero_bits ):
378
- arg = shl (num_zero_bits , shr (num_zero_bits , bytes_val ))
379
- arg = b .resolve (arg )
370
+ if isinstance (arg .typ , _BytestringT ):
371
+ # handle literal bytestrings first
372
+ if isinstance (expr , vy_ast .Constant ) and arg .typ .length <= out_typ .m :
373
+ arg = int (expr .value .encode ("utf-8" ).hex (), 16 )
374
+
375
+ elif isinstance (arg .typ , BytesT ):
376
+ bytes_val = LOAD (bytes_data_ptr (arg ))
377
+
378
+ # zero out any dirty bytes (which can happen in the last
379
+ # word of a bytearray)
380
+ len_ = get_bytearray_length (arg )
381
+ num_zero_bits = IRnode .from_list (["mul" , ["sub" , 32 , len_ ], 8 ])
382
+ with num_zero_bits .cache_when_complex ("bits" ) as (b , num_zero_bits ):
383
+ arg = shl (num_zero_bits , shr (num_zero_bits , bytes_val ))
384
+ arg = b .resolve (arg )
385
+
386
+ else :
387
+ _FAIL (arg .typ , out_typ , expr )
380
388
381
389
elif is_bytes_m_type (arg .typ ):
382
390
# clamp if it's a downcast
0 commit comments