fix: type check for constant arrays #2580
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
What I did
Fix type checker issue in #2534 and #2130.
The type checking for
Subscript
VyperNode type currently assumes that an exact type can be derived for itsvalue
. Ordinarily, this does not raise an issue for array variables as thevalue
of theSubscript
node in this instance would be aName
node type, from which an exact type can be determined.However, where the array is a constant, the
value
of theSubscript
node is now aList
node. When deriving the type for aList
node, a list of possible types are returned instead of a specific type (e.g. an array of [0, 1, 2] will return a list ofuint8
,int128
,int256
anduint256
). As a result, aStructureException
of 'Ambiguous type' will be thrown when there is more than one possible type for an array.How I did it
Relax the assumption that an exact type can be derived for a Subscript node where its
value
is aList
node:vyper/semantics/validation/annotation.py
, thevisit_Subscript
function is amended to get all possible types wherevalue
is aList
node. If only one type is returned, it will be assigned as the base type. If more than one value is returned, we iterate through the list of possible types and return the type that matches thetype_
argument supplied to this function.vyper/semantics/validation/utils.py
, thetypes_from_Subscript
function is amended to return all possible types wherevalue
is aList
node.How to verify it
See test cases.
Description for the changelog
Fix type checker issue for constant/literal arrays
Cute Animal Picture