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

Replace IsSubType check with IsSameTypeKind check where necessary #1032

Merged
merged 6 commits into from
Nov 24, 2021

Conversation

SupunS
Copy link
Member

@SupunS SupunS commented Jun 25, 2021

Closes #1031

Description

  • Introduce a method to check whether a given type 'belongs' to the same kind as a given type.
    • Same as IsSubType, but doesn't treat Never as 'not' belongs to the same kind.
  • Replace the usages where strictly 'same kind types' check is needed.

  • 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

@SupunS SupunS added the Bugfix label Jun 25, 2021
@SupunS SupunS self-assigned this Jun 25, 2021
@SupunS SupunS requested a review from turbolent as a code owner June 25, 2021 07:37
@SupunS SupunS changed the title Replace IsSubType check with IsSameKind check where necessary Replace IsSubType check with IsSameTypeKind check where necessary Jun 25, 2021
@codecov-commenter
Copy link

codecov-commenter commented Jun 25, 2021

Codecov Report

Merging #1032 (ff2304b) into master (424f814) will increase coverage by 0.00%.
The diff coverage is 88.88%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1032   +/-   ##
=======================================
  Coverage   77.28%   77.29%           
=======================================
  Files         274      274           
  Lines       35414    35401   -13     
=======================================
- Hits        27371    27362    -9     
+ Misses       6958     6955    -3     
+ Partials     1085     1084    -1     
Flag Coverage Δ
unittests 77.29% <88.88%> (+<0.01%) ⬆️

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

Impacted Files Coverage Δ
runtime/sema/checker.go 89.62% <20.00%> (+0.28%) ⬆️
runtime/literal.go 86.36% <100.00%> (ø)
runtime/sema/check_binary_expression.go 88.05% <100.00%> (ø)
runtime/sema/check_composite_declaration.go 95.98% <100.00%> (ø)
runtime/sema/check_dictionary_expression.go 100.00% <100.00%> (ø)
runtime/sema/check_event_declaration.go 94.87% <100.00%> (ø)
runtime/sema/check_expression.go 100.00% <100.00%> (ø)
runtime/sema/check_transaction_declaration.go 81.81% <100.00%> (ø)
runtime/sema/check_unary_expression.go 88.37% <100.00%> (ø)
runtime/sema/check_while.go 100.00% <100.00%> (ø)
... and 1 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 424f814...ff2304b. Read the comment docs.

@SupunS SupunS force-pushed the supun/never-type-checking branch from 1f9a426 to 1709c9f Compare June 25, 2021 08:12
@SupunS SupunS force-pushed the supun/never-type-checking branch 2 times, most recently from 65c2749 to c090eef Compare November 9, 2021 16:00
@SupunS SupunS requested a review from dsainati1 November 9, 2021 18:41
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.

Great work! Thank you for fixing this! 👏

Do we have a way of auditing the rest of the code for cases where IsSameTypeKind should be used instead of IsSubType? Also, maybe we can document a guideline for future code?

@SupunS SupunS force-pushed the supun/never-type-checking branch from c090eef to 0de4ee2 Compare November 23, 2021 01:56
@SupunS SupunS force-pushed the supun/never-type-checking branch from 0de4ee2 to e6be268 Compare November 23, 2021 01:56
@github-actions
Copy link

github-actions bot commented Nov 23, 2021

Cadence Benchstat comparison

This branch with compared with the base branch onflow:master commit 424f814
The command go test ./... -run=XXX -bench=. -shuffle=on -count N was used.
Bench tests were run a total of 7 times on each branch.

Results

old.txtnew.txt
time/opdelta
NewInterpreter/new_interpreter-21.35µs ± 2%1.42µs ± 4%+5.29%(p=0.005 n=6+7)
RuntimeStorageWriteCached-2185µs ±35%161µs ± 5%~(p=0.128 n=7+7)
RuntimeResourceDictionaryValues-218.1ms ± 1%18.7ms ± 6%~(p=0.234 n=6+7)
RuntimeFungibleTokenTransfer-21.37ms ± 2%1.60ms ±23%~(p=0.053 n=7+7)
ParseArray-225.6ms ± 2%25.4ms ± 3%~(p=0.456 n=7+7)
ParseFungibleToken-2521µs ± 1%538µs ± 7%~(p=0.053 n=7+7)
ParseInfix-227.4µs ± 2%27.4µs ± 2%~(p=0.818 n=6+6)
ParseDeploy/byte_array-239.1ms ± 4%39.2ms ± 2%~(p=0.710 n=7+7)
ParseDeploy/decode_hex-21.63ms ± 1%1.63ms ± 1%~(p=0.535 n=7+7)
CheckContractInterfaceFungibleTokenConformance-2197µs ± 8%201µs ± 4%~(p=0.456 n=7+7)
ContractInterfaceFungibleToken-258.8µs ±10%56.7µs ± 6%~(p=0.259 n=7+7)
NewInterpreter/new_sub-interpreter-22.47µs ± 3%2.49µs ± 4%~(p=0.456 n=7+7)
InterpretRecursionFib-23.12ms ± 0%3.23ms ±13%~(p=0.268 n=5+7)
QualifiedIdentifierCreation/One_level-23.73ns ± 2%3.61ns ± 2%−3.10%(p=0.010 n=7+7)
QualifiedIdentifierCreation/Three_levels-2195ns ± 3%188ns ± 3%−3.66%(p=0.002 n=7+7)
 
alloc/opdelta
RuntimeStorageWriteCached-283.7kB ± 0%83.7kB ± 0%~(p=0.106 n=6+7)
RuntimeResourceDictionaryValues-24.34MB ± 0%4.34MB ± 0%~(p=0.366 n=6+7)
QualifiedIdentifierCreation/One_level-20.00B 0.00B ~(all equal)
QualifiedIdentifierCreation/Three_levels-264.0B ± 0%64.0B ± 0%~(all equal)
ContractInterfaceFungibleToken-226.7kB ± 0%26.7kB ± 0%~(all equal)
NewInterpreter/new_interpreter-2680B ± 0%680B ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-21.06kB ± 0%1.06kB ± 0%~(all equal)
InterpretRecursionFib-21.21MB ± 0%1.21MB ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-266.4kB ± 0%66.4kB ± 0%−0.00%(p=0.008 n=6+7)
RuntimeFungibleTokenTransfer-2233kB ± 0%233kB ± 0%−0.02%(p=0.035 n=7+7)
 
allocs/opdelta
RuntimeStorageWriteCached-21.42k ± 0%1.42k ± 0%~(all equal)
RuntimeResourceDictionaryValues-2108k ± 0%108k ± 0%~(p=0.110 n=7+7)
RuntimeFungibleTokenTransfer-24.42k ± 0%4.42k ± 0%~(p=0.070 n=7+7)
QualifiedIdentifierCreation/One_level-20.00 0.00 ~(all equal)
QualifiedIdentifierCreation/Three_levels-22.00 ± 0%2.00 ± 0%~(all equal)
CheckContractInterfaceFungibleTokenConformance-21.07k ± 0%1.07k ± 0%~(all equal)
ContractInterfaceFungibleToken-2458 ± 0%458 ± 0%~(all equal)
NewInterpreter/new_interpreter-211.0 ± 0%11.0 ± 0%~(all equal)
NewInterpreter/new_sub-interpreter-231.0 ± 0%31.0 ± 0%~(all equal)
InterpretRecursionFib-225.0k ± 0%25.0k ± 0%~(all equal)
 

@SupunS
Copy link
Member Author

SupunS commented Nov 23, 2021

Do we have a way of auditing the rest of the code for cases where IsSameTypeKind should be used instead of IsSubType?

I searched all the usages and replaced them where necessary. Unfortunately, this has to be done manually, can't think of a way to automate this/ audit with a test case. The general rule is:

  • IsSubType() - To check the assignability. e.g: Is argument type T is a sub-type of parameter type R. This is the more frequent use-case.
  • IsSameTypeKind() - To check if a type strictly belongs to a certain category. e.g: Is the expression type T is any of the integer types, but nothing else. Another way to check is, asking the question "if the subType is Never, should the check still pass?". A common code-smell for potential incorrect usage is, using IsSubType() method with a constant/pre-defined superType. e.g: IsSubType(<<someType>>, FixedPointType)

Also, maybe we can document a guideline for future code?

I had already added some comments to the newly added mehtod. Also added a note on the old IsSameType() method for the usage: 1b324ec

@turbolent
Copy link
Member

@SupunS Nice! Could we keep this comment/guideline somewhere in the codebase? Maybe as a comment somewhere, or in docs?

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.

Great work! 👏

Comment on lines +4711 to +4721
// However, to check if a type *strictly* belongs to a certain category, then consider
// using `IsSameTypeKind` method. e.g: "Is type `T` an Integer type?". Using this method
// for the later use-case may produce incorrect results.
// * IsSubType() - To check the assignability. e.g: Is argument type T is a sub-type
// of parameter type R. This is the more frequent use-case.
// * IsSameTypeKind() - To check if a type strictly belongs to a certain category. e.g: Is the
// expression type T is any of the integer types, but nothing else.
// Another way to check is, asking the question of "if the subType is Never,
// should the check still pass?". A common code-smell for potential incorrect
// usage is, using IsSubType() method with a constant/pre-defined superType.
// e.g: IsSubType(<<someType>>, FixedPointType)
Copy link
Member

Choose a reason for hiding this comment

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

✨ 👏

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

'Never' typed expressions get skipped from certain type-checks
3 participants