vyper icon indicating copy to clipboard operation
vyper copied to clipboard

bug: incorrect typing for builtin functions whose return type depends on its arguments' types

Open tserg opened this issue 2 years ago • 2 comments

Version Information

  • vyper Version (output of vyper --version): https://github.com/vyperlang/vyper/commit/9e3b9a2b8ae55aa83b5450080f750be15f819de7
  • OS: linux
  • Python Version (output of python --version): 3.10.4

What's your issue about?

Builtin functions whose return type depends on some of its argument's type can be incorrectly typed resulting in the compiler exiting with a TypeMismatch.

To trigger this behavior, call the builtin function with arguments such that:

  1. at least one argument is not constant (as the call would be folded otherwise)
  2. get_possible_types_from_node should return multiple potential types for this arguments on which the return type of the builtin function depends.

Affected builtins:

  • min and max: a: uint256 = min(1 if True else 2, 1)
  • all unsafe builtins: a: uint256 = unsafe_add(1 if True else 2, 1)
  • shift (deprecated as of v0.3.8): a: uint256 = shift(-1, 1 if True else 2)
  • uint2str: f: String[12] = uint2str(1 if True else 2)

h/t @trocher

How can it be fixed?

Fill this in if you know how to fix it.

tserg avatar Jul 19 '23 09:07 tserg

Other example:

@external
@payable
def foo():
    x:uint256 = 1
    a:uint256 = min([1,2][x], 1)
vyper.exceptions.TypeMismatch: Given reference has type int8, expected uint256

(Also for reference still an issue as of 0.3.10)

trocher avatar Nov 08 '23 13:11 trocher

note: several of these are resolved as of v0.4.0b1 (b53a020c74857de33cf4fd1a6d24a398539758b1) but some still will not compile.

charles-cooper avatar Feb 27 '24 16:02 charles-cooper