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

Introduce dereference operator for references #2982

Merged
merged 21 commits into from
Jan 10, 2024

Conversation

darkdrag00nv2
Copy link
Contributor

@darkdrag00nv2 darkdrag00nv2 commented Dec 16, 2023

Closes #2803

Description


  • 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

@darkdrag00nv2
Copy link
Contributor Author

Need to cover a few more types in the interpreter tests.

@darkdrag00nv2 darkdrag00nv2 marked this pull request as ready for review December 17, 2023 17:30
runtime/sema/type.go Outdated Show resolved Hide resolved
runtime/sema/type.go Outdated Show resolved Hide resolved
runtime/sema/type.go Outdated Show resolved Hide resolved
runtime/tests/interpreter/reference_test.go Outdated Show resolved Hide resolved
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.

Nice!

runtime/sema/errors.go Outdated Show resolved Hide resolved
runtime/interpreter/value.go Outdated Show resolved Hide resolved
@SupunS SupunS self-assigned this Dec 19, 2023
@turbolent
Copy link
Member

CI is reporting data races: https://github.com/onflow/cadence/actions/runs/7240009930/job/19722511487?pr=2982

Copy link

codecov bot commented Dec 25, 2023

Codecov Report

Attention: 20 lines in your changes are missing coverage. Please review.

Comparison is base (77fc728) 80.15% compared to head (4736011) 80.00%.
Report is 110 commits behind head on master.

❗ Current head 4736011 differs from pull request most recent head 129d7c5. Consider uploading reports for the commit 129d7c5 to get more accurate results

Files Patch % Lines
runtime/sema/check_unary_expression.go 55.88% 14 Missing and 1 partial ⚠️
runtime/interpreter/value.go 72.72% 3 Missing ⚠️
runtime/interpreter/interpreter_expression.go 86.66% 1 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master    #2982      +/-   ##
==========================================
- Coverage   80.15%   80.00%   -0.16%     
==========================================
  Files         348      356       +8     
  Lines       81989    83037    +1048     
==========================================
+ Hits        65716    66431     +715     
- Misses      13952    14247     +295     
- Partials     2321     2359      +38     
Flag Coverage Δ
unittests 80.00% <78.72%> (-0.16%) ⬇️

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

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? 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.

Looks good, great work!

In general I'm still not sure if this should be a magic function on references, or rather something else, like an operator.

For the functionality to be a magic function, the PR should also handle the case where a composite declares a member named the same as the magic function, as it seems like the current implementation shadows / makes it inaccessible. We might want to forbid declaring members which have that magic name. An operator would not have that problem.

runtime/sema/type.go Outdated Show resolved Hide resolved
@turbolent
Copy link
Member

We might want to use an operator here instead of / in addition to the method, e.g. the unary prefix operator *, like in Rust, Go, C, C++, etc.

@darkdrag00nv2
Copy link
Contributor Author

Sure, will update to remove the function and just have the operator.

@turbolent
Copy link
Member

@darkdrag00nv2 Not sure if you started yet, but if not: I'm half-way done and could finish and push it up here

@darkdrag00nv2
Copy link
Contributor Author

@turbolent Haven't started yet. You can push it. Thank you!

@turbolent turbolent changed the title Introduce dereference on the Reference type Introduce dereference operator for references Jan 9, 2024
@turbolent
Copy link
Member

Refactored the dereference function to the prefix * operator.

@onflow/cadence could you please have a look?

Copy link
Contributor

@dsainati1 dsainati1 left a comment

Choose a reason for hiding this comment

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

Now that dereference isn't a member function, it would be nice for * to have simple/clean behavior with respect to optional references. I.e. if we have a &Int? type, it would be nice for the * operator on that type to produce a Int?. Previously we could do this with ?. chaining, but with the change to an operator we lose this functionality. Can we add this (and tests for it)?

@turbolent
Copy link
Member

turbolent commented Jan 9, 2024

@dsainati1 Would this simply be *(optRef!)? Do you mean that * on an optional ? of a reference type (&T?) should return T? ?

@dsainati1
Copy link
Contributor

Yeah, it would be nice for the * operator to implicitly "lift" itself to operate on optional values as well as normal ones.

@turbolent
Copy link
Member

turbolent commented Jan 9, 2024

@dsainati1 Sounds good! Let's get that added in a follow-up PR. Could you please open a feature request issue for it?

runtime/parser/expression_test.go Outdated Show resolved Hide resolved
runtime/sema/type.go Outdated Show resolved Hide resolved
@turbolent turbolent requested review from SupunS and dsainati1 January 9, 2024 22:48
Copy link
Member

@SupunS SupunS left a comment

Choose a reason for hiding this comment

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

LGTM!

@turbolent turbolent enabled auto-merge January 10, 2024 21:32
@turbolent turbolent merged commit 24f277e into onflow:master Jan 10, 2024
10 of 12 checks passed
@darkdrag00nv2 darkdrag00nv2 deleted the copy_from_reference branch January 11, 2024 17:06
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.

Need a way get a dereferenced copy from a reference
4 participants