vyper icon indicating copy to clipboard operation
vyper copied to clipboard

feat: allow constant and immutable variables to be declared public

Open benber86 opened this issue 3 years ago • 2 comments

What I did

I gave a first try to #2903 after encountering the same issue and discussing it on Discord. This will make it possible to declare immutable and constant variables as public.

This is still a WIP as:

  • When creating public getters, I'm lumping the immutable variables along with other state variables as attributes of self, but this should probably be done separately.
  • The public getter for constants currently just returns a value, so this will not work for lists or mappings.
  • This only allows for the declaration of public(immutable(x)) or public(constant(x)) not immutable(public(x)) or constant(public(x))

How I did it

  • Check if public nodes have immutable or constant children to set is_immutable or is_constant accordingly
  • Update the getter creation function to handle immutable and constant variables
  • Add logic to handle immutable public variables in codegen

How to verify it

  • Run tests/parser/syntax/test_public.py

Commit message

Commit message for the final, squashed PR. (Optional, but reviewers will appreciate it! Please see our commit message style guide for what we would ideally like to see in a commit message.)

Description for the changelog

Cute Animal Picture

image

benber86 avatar Aug 04 '22 05:08 benber86

Codecov Report

Merging #3024 (fab45f2) into master (cb41608) will increase coverage by 0.04%. The diff coverage is 91.42%.

@@            Coverage Diff             @@
##           master    #3024      +/-   ##
==========================================
+ Coverage   88.30%   88.34%   +0.04%     
==========================================
  Files          97       98       +1     
  Lines       10941    11021      +80     
  Branches     2587     2605      +18     
==========================================
+ Hits         9661     9737      +76     
- Misses        831      833       +2     
- Partials      449      451       +2     
Impacted Files Coverage Δ
vyper/semantics/types/utils.py 90.22% <ø> (ø)
vyper/semantics/types/function.py 87.16% <50.00%> (+0.04%) :arrow_up:
vyper/codegen/global_context.py 74.07% <75.00%> (-0.72%) :arrow_down:
vyper/ast/expansion.py 95.00% <100.00%> (+1.06%) :arrow_up:
vyper/ast/folding.py 92.62% <100.00%> (ø)
vyper/ast/nodes.py 93.22% <100.00%> (+0.04%) :arrow_up:
vyper/semantics/types/user/interface.py 83.19% <100.00%> (ø)
vyper/semantics/validation/data_positions.py 91.39% <100.00%> (ø)
vyper/semantics/validation/module.py 85.49% <100.00%> (-0.37%) :arrow_down:
vyper/__init__.py 58.82% <0.00%> (-17.65%) :arrow_down:
... and 14 more

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

codecov-commenter avatar Aug 04 '22 14:08 codecov-commenter

by the way, i think that only handling public(immutable(...)) and not immutable(public(...)) is a feature, not a bug

charles-cooper avatar Aug 05 '22 13:08 charles-cooper

Nice

jarivers082461 avatar Sep 02 '22 03:09 jarivers082461

@benber86 i cleaned up the logic for extracting the public/immutable/constant annotations a little bit

charles-cooper avatar Sep 08 '22 21:09 charles-cooper

This pull request introduces 2 alerts when merging 062ffb8f85ae2ab3c3d9b880f4b80654021bb5f8 into 915d430525ab6d83b11d45754b5bc6cc1f8f49f9 - view on LGTM.com

new alerts:

  • 1 for Unused local variable
  • 1 for Unused import

lgtm-com[bot] avatar Sep 08 '22 22:09 lgtm-com[bot]