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

Fix Value.Type() returning Type referencing nil ptr for eight external values #2388

Merged
merged 5 commits into from
Mar 22, 2023

Conversation

fxamacker
Copy link
Member

Changes include:

  • Changed type of Dictionary.DictionaryType from Type to *DictionaryType.
  • Changed Type() to check and return nil Type, instead of Type referencing nil pointer for these external values:
    • Dictionary
    • Struct
    • Resource
    • Attachment
    • Event
    • Contract
    • Enum
    • Function

Closes #2387


  • Targeted PR against master branch
  • Linked to Github issue with discussion and accepted design OR link to spec that describes this work
  • Code follows the standards mentioned here
  • Updated relevant documentation
  • Re-reviewed Files changed in the Github PR explorer
  • Added appropriate labels

- changed some external value's Type() to check and return nil Type,
  instead of Type referencing nil pointer, such as nil *ResourceType.
- changed type of Dictionary.DictionaryType from Type to *DictionaryType.
@fxamacker fxamacker added Bug Something isn't working Improvement labels Mar 15, 2023
@fxamacker fxamacker self-assigned this Mar 15, 2023
@github-actions
Copy link

github-actions bot commented Mar 15, 2023

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 2e80137
The command for i in {1..N}; do go test ./... -run=XXX -bench=. -benchmem -shuffle=on; done was used.
Bench tests were run a total of 7 times on each branch.

Collapsed results for better readability

old.txtnew.txt
time/opdelta
CheckContractInterfaceFungibleTokenConformance-2119µs ± 0%116µs ± 0%~(p=1.000 n=1+1)
ContractInterfaceFungibleToken-239.9µs ± 0%40.5µs ± 0%~(p=1.000 n=1+1)
ExportType/composite_type-2360ns ± 0%383ns ± 0%~(p=1.000 n=1+1)
ExportType/simple_type-257.7ns ± 0%58.1ns ± 0%~(p=1.000 n=1+1)
InterpretRecursionFib-22.59ms ± 0%2.26ms ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_interpreter-21.18µs ± 0%1.19µs ± 0%~(p=1.000 n=1+1)
NewInterpreter/new_sub-interpreter-2631ns ± 0%627ns ± 0%~(p=1.000 n=1+1)
ParseArray-28.16ms ± 0%8.47ms ± 0%~(p=1.000 n=1+1)
ParseDeploy/byte_array-212.6ms ± 0%12.7ms ± 0%~(p=1.000 n=1+1)
ParseDeploy/decode_hex-21.26ms ± 0%1.27ms ± 0%~(p=1.000 n=1+1)
ParseFungibleToken/With_memory_metering-2199µs ± 0%201µs ± 0%~(p=1.000 n=1+1)
ParseFungibleToken/Without_memory_metering-2150µs ± 0%149µs ± 0%~(p=1.000 n=1+1)
ParseInfix-26.96µs ± 0%7.28µs ± 0%~(p=1.000 n=1+1)
QualifiedIdentifierCreation/One_level-22.42ns ± 0%2.36ns ± 0%~(p=1.000 n=1+1)
QualifiedIdentifierCreation/Three_levels-2156ns ± 0%143ns ± 0%~(p=1.000 n=1+1)
RuntimeResourceDictionaryValues-25.24ms ± 0%5.88ms ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-27.01µs ± 0%7.10µs ± 0%~(p=1.000 n=1+1)
SuperTypeInference/arrays-2336ns ± 0%322ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/composites-2136ns ± 0%134ns ± 0%~(p=1.000 n=1+1)
SuperTypeInference/integers-288.9ns ± 0%91.0ns ± 0%~(p=1.000 n=1+1)
ValueIsSubtypeOfSemaType-297.4ns ± 0%98.5ns ± 0%~(p=1.000 n=1+1)
 
alloc/opdelta
CheckContractInterfaceFungibleTokenConformance-248.9kB ± 0%48.9kB ± 0%~(all equal)
ContractInterfaceFungibleToken-223.2kB ± 0%23.2kB ± 0%~(all equal)
ExportType/composite_type-2136B ± 0%136B ± 0%~(all equal)
ExportType/simple_type-20.00B 0.00B ~(all equal)
InterpretRecursionFib-21.00MB ± 0%1.00MB ± 0%~(all equal)
NewInterpreter/new_interpreter-2768B ± 0%768B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-2200B ± 0%200B ± 0%~(all equal)
ParseArray-22.72MB ± 0%2.73MB ± 0%~(p=1.000 n=1+1)
ParseDeploy/byte_array-24.27MB ± 0%4.09MB ± 0%~(p=1.000 n=1+1)
ParseDeploy/decode_hex-2214kB ± 0%214kB ± 0%~(p=1.000 n=1+1)
ParseFungibleToken/With_memory_metering-228.9kB ± 0%28.9kB ± 0%~(p=1.000 n=1+1)
ParseFungibleToken/Without_memory_metering-228.9kB ± 0%28.9kB ± 0%~(p=1.000 n=1+1)
ParseInfix-21.91kB ± 0%1.92kB ± 0%~(p=1.000 n=1+1)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
RuntimeResourceDictionaryValues-22.29MB ± 0%2.29MB ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-22.70kB ± 0%2.70kB ± 0%~(all equal)
SuperTypeInference/arrays-296.0B ± 0%96.0B ± 0%~(all equal)
SuperTypeInference/composites-20.00B 0.00B ~(all equal)
SuperTypeInference/integers-20.00B 0.00B ~(all equal)
ValueIsSubtypeOfSemaType-248.0B ± 0%48.0B ± 0%~(all equal)
 
allocs/opdelta
CheckContractInterfaceFungibleTokenConformance-2806 ± 0%806 ± 0%~(all equal)
ContractInterfaceFungibleToken-2370 ± 0%370 ± 0%~(all equal)
ExportType/composite_type-23.00 ± 0%3.00 ± 0%~(all equal)
ExportType/simple_type-20.00 0.00 ~(all equal)
InterpretRecursionFib-218.9k ± 0%18.9k ± 0%~(all equal)
NewInterpreter/new_interpreter-213.0 ± 0%13.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-24.00 ± 0%4.00 ± 0%~(all equal)
ParseArray-259.6k ± 0%59.6k ± 0%~(p=1.000 n=1+1)
ParseDeploy/byte_array-289.4k ± 0%89.4k ± 0%~(p=1.000 n=1+1)
ParseDeploy/decode_hex-263.0 ± 0%63.0 ± 0%~(all equal)
ParseFungibleToken/With_memory_metering-2768 ± 0%768 ± 0%~(all equal)
ParseFungibleToken/Without_memory_metering-2768 ± 0%768 ± 0%~(all equal)
ParseInfix-248.0 ± 0%48.0 ± 0%~(all equal)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
RuntimeResourceDictionaryValues-236.9k ± 0%36.9k ± 0%~(p=1.000 n=1+1)
RuntimeScriptNoop-243.0 ± 0%43.0 ± 0%~(all equal)
SuperTypeInference/arrays-23.00 ± 0%3.00 ± 0%~(all equal)
SuperTypeInference/composites-20.00 0.00 ~(all equal)
SuperTypeInference/integers-20.00 0.00 ~(all equal)
ValueIsSubtypeOfSemaType-21.00 ± 0%1.00 ± 0%~(all equal)
 

@codecov
Copy link

codecov bot commented Mar 15, 2023

Codecov Report

Merging #2388 (f45e8d2) into master (2e80137) will increase coverage by 0.02%.
The diff coverage is 87.50%.

❗ Current head f45e8d2 differs from pull request most recent head 7a6b490. Consider uploading reports for the commit 7a6b490 to get more accurate results

@@            Coverage Diff             @@
##           master    #2388      +/-   ##
==========================================
+ Coverage   78.52%   78.55%   +0.02%     
==========================================
  Files         316      316              
  Lines       68561    68596      +35     
==========================================
+ Hits        53837    53885      +48     
+ Misses      12925    12911      -14     
- Partials     1799     1800       +1     
Flag Coverage Δ
unittests 78.55% <87.50%> (+0.02%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
runtime/format/capability.go 100.00% <ø> (+31.25%) ⬆️
values.go 75.39% <87.50%> (+1.62%) ⬆️

... and 1 file with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Copy link
Member

@turbolent turbolent left a comment

Choose a reason for hiding this comment

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

Thanks for fixing this!

Unfortunately, introducing typed nils is too easy.

We should maybe add tests that avoid regressions for these functions, and in addition, prevent incorrect implementations in the future (e.g. by creating an empty instance of each Type implementation, and then, as each nested type is automatically nil, the a call to Type() should be equal to the untyped nil (== nil).

Let me try adding that.

values.go Show resolved Hide resolved
values_test.go Outdated Show resolved Hide resolved
@turbolent
Copy link
Member

turbolent commented Mar 22, 2023

Refactored the existing tests and unified them with similar tests for String(), as well as added further missing test cases (e.g. Function and Character) in fcb2519.

@fxamacker @SupunS Could you please have a look?

values.go Outdated Show resolved Hide resolved
@turbolent turbolent requested a review from SupunS March 22, 2023 17:51
@turbolent
Copy link
Member

@dsainati1 @SupunS Could you please have a(nother) look? 🙏 This would unblock Faye on #2364

@turbolent turbolent enabled auto-merge March 22, 2023 18:09
@turbolent turbolent merged commit 6ac267c into master Mar 22, 2023
@turbolent turbolent deleted the fxamacker/fix-nil-ptr-to-type branch March 22, 2023 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug Something isn't working Improvement
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Value.Type() can return a reference to a nil pointer
3 participants