Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add min and max value builtins #2935

Merged
merged 22 commits into from
Jul 26, 2022

Conversation

tserg
Copy link
Collaborator

@tserg tserg commented Jun 23, 2022

What I did

Fix #1923.

How I did it

Add min_value and max_value builtin functions.

How to verify it

See tests.

Commit message

feat: add min_value and max_value builtins

Add min_value and max_value builtins for bounds of numeric types.
Remove builtin constants MAX_INT128, MIN_INT128, MIN_DECIMAL,
MAX_DECIMAL and MAX_UINT256.

Description for the changelog

Add min_value and max_value builtin functions.

Cute Animal Picture

Put a link to a cute animal picture inside the parenthesis-->

@codecov-commenter
Copy link

codecov-commenter commented Jun 23, 2022

Codecov Report

Merging #2935 (83f87ed) into master (4b3b636) will decrease coverage by 1.53%.
The diff coverage is 97.05%.

@@            Coverage Diff             @@
##           master    #2935      +/-   ##
==========================================
- Coverage   88.10%   86.56%   -1.54%     
==========================================
  Files          97       97              
  Lines       10877    10923      +46     
  Branches     2574     2522      -52     
==========================================
- Hits         9583     9456     -127     
- Misses        838      995     +157     
- Partials      456      472      +16     
Impacted Files Coverage Δ
vyper/semantics/namespace.py 97.29% <ø> (ø)
vyper/builtin_functions/functions.py 86.61% <96.55%> (-3.64%) ⬇️
vyper/ast/folding.py 92.62% <100.00%> (+1.02%) ⬆️
vyper/builtin_functions/convert.py 67.02% <0.00%> (-23.58%) ⬇️
vyper/codegen/arithmetic.py 54.87% <0.00%> (-20.88%) ⬇️
vyper/semantics/types/value/numeric.py 79.31% <0.00%> (-5.75%) ⬇️
vyper/ast/validation.py 75.00% <0.00%> (-4.17%) ⬇️
vyper/utils.py 80.86% <0.00%> (-3.83%) ⬇️
vyper/ir/s_expressions.py 91.17% <0.00%> (-2.95%) ⬇️
... and 7 more

Help us with your feedback. Take ten seconds to tell us how you rate us.

Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks nice! i might rename def _eval_fn() to def _eval().

also i think we want to be sure not to actually remove the old constants from the language. We can deprecate them but we should keep them in for a few releases to avoid breaking user code.

vyper/builtin_functions/functions.py Show resolved Hide resolved
vyper/builtin_functions/functions.py Outdated Show resolved Hide resolved
@@ -93,11 +93,6 @@ Name Type Value
================= ================ ==============================================
``ZERO_ADDRESS`` ``address`` ``0x0000000000000000000000000000000000000000``
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i mean we might as well get rid of ZERO_ADDRESS and EMPTY_BYTES32 while we are at it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can support min_value(bytesN) as well since Vyper now has all bytesN types.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well that's just empty(bytesN). actually i think for types T where min_value(T) == empty(T), we should block min_value(T) and suggest empty(T) instead (on the principle that there should be one obvious way to do things).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh right, so empty() is already sufficient for ZERO_ADDRESS and EMPTY_BYTESN.

in that case, the only types where min_value(T) == empty(T) are unsigned integers only though. hence, I think supporting min_value for signed integers but not unsigned integers would be more confusing than having min_value and empty for unsigned integers.

@tserg
Copy link
Collaborator Author

tserg commented Jun 27, 2022

To discuss replacing min_value and max_value with lower_bound and upper_bound / typemin and typemax.

@tserg
Copy link
Collaborator Author

tserg commented Jul 25, 2022

Notes from meeting on 25 July: proceed with min_value and max_value

@charles-cooper
Copy link
Member

meeting notes:
use min_value and max_value (and include min_value for unsigned integers)

@tserg
Copy link
Collaborator Author

tserg commented Jul 25, 2022

Do we need to support decimal?

@charles-cooper
Copy link
Member

Do we need to support decimal?

yes, that would be good!

@tserg tserg marked this pull request as ready for review July 26, 2022 00:43
Copy link
Member

@charles-cooper charles-cooper left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looking quite good. couple small things


if isinstance(input_type, IntegerAbstractType):
(lo, hi) = int_bounds(input_type._is_signed, input_type._bits)
val = self._eval(lo, hi)
Copy link
Member

@charles-cooper charles-cooper Jul 26, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it's nice to use this _eval function. dispatching to min/max is a bit magical though. for instance, if the signature of int_bounds ever changes, you would expect this function to break actually - and it wouldn't!

i would say this would be slightly clearer with two functions, _eval_decimal and _eval_int. each takes the input type and returns a value. so this block would look more like

        if isinstance(input_type, IntegerAbstractType):
            val = self._eval_int(input_type)

and similar for the decimal case.

@charles-cooper charles-cooper enabled auto-merge (squash) July 26, 2022 18:27
@charles-cooper charles-cooper enabled auto-merge (squash) July 26, 2022 18:29
@charles-cooper charles-cooper merged commit 5713bd4 into vyperlang:master Jul 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

VIP: Add min and max type
3 participants