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

Allow array index to be uint256 #1479

Merged
merged 4 commits into from
Jun 21, 2019

Conversation

charles-cooper
Copy link
Member

@charles-cooper charles-cooper commented Jun 12, 2019

What I did

Fix #1478 and #1483

TODO:

  • tests

How I did it

This goes back to commit 4448705.
Before that, the type of the index was inferred. After that, it changed
to be the default numeric type in the language, num (now int128).

Array index types should generally be uint256.

How to verify it

Check IR of following:

xs: uint256[10]
# runtime bounds check
@public
def signed_bounds_check(ix: int128) -> uint256: 
  return self.xs[ix]

# allow uint256 index
@public
def uint256_index(ix: uint256) -> uint256: 
  return self.xs[ix]

Check this throws compile-time exception:

xs: uint256[10]
# compile-time failure
@public
def compile_time_bounds_check() -> uint256: 
  return self.xs[10]

Description for the changelog

  • Allow array index to be uint256 or int128
  • Fix runtime bounds checks for signed indices
  • Convert runtime bounds checks into compile-time check for literal
    indices.

Cute Animal Picture

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

This goes back to commit 4448705.
Before that, the type of the index was inferred. After that, it changed
to be the default numeric type in the language, `num` (now `int128`).

Array index types should generally be uint256.
- Fix runtime bounds checks for signed indices
- Convert runtime bounds checks into compile-time check for literal
  indices.
@charles-cooper
Copy link
Member Author

Per discussion in #1477, I added support for both int128 and uint256 array indexes. I also fixed #1483 and converted the runtime out of bounds access check into a compile-time check in the case that the index is a literal.

@jacqueswww
Copy link
Contributor

LGTM

Copy link
Contributor

@jacqueswww jacqueswww left a comment

Choose a reason for hiding this comment

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

Add tests and we can merge :)

and also compile-time out-of-bounds check
@charles-cooper charles-cooper changed the title WIP: Allow array index to be uint256 Allow array index to be uint256 Jun 19, 2019
@jacqueswww jacqueswww merged commit 34ad5a5 into vyperlang:master Jun 21, 2019
@charles-cooper charles-cooper deleted the array_index branch September 24, 2019 11:49
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.

uint256 doesn't work as array index
2 participants