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

Raise clearer exception when using a yet undeclared variable in a type annotation #3215

Merged

Conversation

benber86
Copy link
Contributor

@benber86 benber86 commented Jan 4, 2023

What I did

As per #3214, trying to reference a yet undeclared constant variable in the type annotation of another variable's declaration results in a fairly unhelpful error message: vyper.exceptions.UndeclaredDefinition: 'self' has not been declared.
This fix ensures that a more detailed exception is raised.

How I did it

The exception is raised when trying to check whether the variable is a storage variable by checking if its name is in self.namespace["self"].typ.members.
However this check can happen before self is added to the namespace. When the constant variable is referenced in a type annotation, the check will happen during folding and before validation, so there will be no self key in namespace, leading to the previous exception being raised.
A simple check on whether the key is present suffices to prevent this exception from being raised and to instead raise another detailed UndeclaredDefinition exception containing the undeclared variable's name and line number of the error.

How to verify it

Compiling the following contract:

PRECISIONS: constant(uint256[N_COINS]) = [
    1000000000000,
    10000000000,
    1,
]
N_COINS: constant(int128) = 3

Now raises the following exception:

vyper.exceptions.UndeclaredDefinition: 'N_COINS' has not been declared. 
  contract "***.vy:1", line 1:29 
  ---> 1 PRECISIONS: constant(uint256[N_COINS]) = [
  ------------------------------------^
       2     1000000000000,

Commit message

Raise clearer exception when using a yet undeclared variable in a type annotation

Description for the changelog

Cute Animal Picture

image

@benber86 benber86 changed the title Raise clearer exception when using a storage variable as a subscript before its declaration Raise clearer exception when using a storage variable in a type annotation before its declaration Jan 4, 2023
@benber86 benber86 changed the title Raise clearer exception when using a storage variable in a type annotation before its declaration Raise clearer exception when using a yet undeclared variable in a type annotation Jan 4, 2023
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.

None yet

3 participants