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

chore: improve error message for unassigned structs #3312

Merged

Conversation

tserg
Copy link
Collaborator

@tserg tserg commented Mar 6, 2023

What I did

Unassigned structs currently run into an error while raising an exception:

struct Foo:
    a: uint256

@external
def foo():
    Foo({a: 1})
AttributeError: 'TYPE_T' object has no attribute '_id'

This is due to TYPE_T not having an _id attribute:

if (
return_value
and not isinstance(fn_type, MemberFunctionT)
and not isinstance(fn_type, ContractFunctionT)
):
raise StructureException(
f"Function '{fn_type._id}' cannot be called without assigning the result", node
)
self.expr_visitor.visit(node.value)

Even if we remove ._id, the error message is still not precise because it is a struct creation and not a function call:

vyper.exceptions.StructureException: Function 'type(Foo declaration object)' cannot be called without assigning the result
  contract "contracts/struct_assign.vy:8", function "foo", line 8:4 
       7 def foo():
  ---> 8     Foo({a: 1})
  -----------^
       9

How I did it

Added a new branch to catch unassigned structs.

How to verify it

See test.

Commit message

chore: improve error message for unassigned structs

Description for the changelog

Improve error message for unassigned structs

Cute Animal Picture

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

@codecov-commenter
Copy link

codecov-commenter commented Mar 6, 2023

Codecov Report

Merging #3312 (300259b) into master (8ebabc5) will increase coverage by 0.00%.
The diff coverage is 100.00%.

📣 This organization is not using Codecov’s GitHub App Integration. We recommend you install it so Codecov can continue to function properly for your repositories. Learn more

@@           Coverage Diff           @@
##           master    #3312   +/-   ##
=======================================
  Coverage   88.89%   88.89%           
=======================================
  Files          84       84           
  Lines       10600    10602    +2     
  Branches     2213     2214    +1     
=======================================
+ Hits         9423     9425    +2     
  Misses        769      769           
  Partials      408      408           
Impacted Files Coverage Δ
vyper/semantics/analysis/local.py 91.36% <100.00%> (+0.05%) ⬆️

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@tserg
Copy link
Collaborator Author

tserg commented Mar 6, 2023

Note - this now catches builtin functions that have not been folded:

if (
return_value
and not isinstance(fn_type, MemberFunctionT)
and not isinstance(fn_type, ContractFunctionT)
):
raise StructureException(
f"Function '{fn_type._id}' cannot be called without assigning the result", node
)

@tserg tserg marked this pull request as ready for review March 6, 2023 10:09
@charles-cooper charles-cooper merged commit cb00d37 into vyperlang:master Mar 6, 2023
@tserg tserg deleted the fix/struct_declaration_without_assign branch March 7, 2023 03:51
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

4 participants