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

Dereferencing operator . on struct field access #5538

Merged
merged 6 commits into from
Feb 4, 2024

Conversation

ironcev
Copy link
Member

@ironcev ironcev commented Feb 1, 2024

Description

This PR implements struct field access operator . for references. The overall effort related to references is tracked in #5063.

. is defined for references to structs using this recursive definition: <reference>.<field name> := (*<reference>).<field name>.

This eliminates the need for the dereferencing operator * when working with references to structs:

let s = Struct { x: 0 };

let r = &&&s;

assert((***r).x == r.x);
let r = &&&Struct { r_a: &&A { x: 1 }, &B { y: 2 } ];
assert(r.r_a.x == 1);
assert(r.r_b.y == 2);

Additionally, the PR adds a Diagnostic for the StorageFieldDoesNotExist error and aligns it with the StructFieldDoesNotExist error.

Demo

Field access requires a struct - On expression)

Field access requires a struct - Storage variable

Storage field does not exist

Checklist

  • I have linked to any relevant issues.
  • I have commented my code, particularly in hard-to-understand areas.
  • I have updated the documentation where relevant (API docs, the reference, and the Sway book).
  • I have added tests that prove my fix is effective or that my feature works.
  • I have added (or requested a maintainer to add) the necessary Breaking* or New Feature labels where relevant.
  • I have done my best to ensure that my PR adheres to the Fuel Labs Code Review Standards.
  • I have requested a review from the relevant team or maintainers.

@ironcev ironcev self-assigned this Feb 1, 2024
@ironcev ironcev added compiler General compiler. Should eventually become more specific as the issue is triaged language feature Core language features visible to end users compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen labels Feb 1, 2024
@ironcev ironcev marked this pull request as ready for review February 2, 2024 00:04
@ironcev ironcev requested review from a team February 2, 2024 00:04
@ironcev ironcev enabled auto-merge (squash) February 2, 2024 00:05
tritao
tritao previously approved these changes Feb 2, 2024
Copy link
Contributor

@tritao tritao 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! Just some minor nits.

@ironcev ironcev requested review from tritao and a team February 4, 2024 18:54
@ironcev ironcev merged commit 575d2bb into master Feb 4, 2024
37 checks passed
@ironcev ironcev deleted the ironcev/dereferencing-struct-field-access branch February 4, 2024 22:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
compiler: frontend Everything to do with type checking, control flow analysis, and everything between parsing and IRgen compiler General compiler. Should eventually become more specific as the issue is triaged language feature Core language features visible to end users
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants