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 index [] #5530

Merged
merged 4 commits into from
Jan 31, 2024

Conversation

ironcev
Copy link
Member

@ironcev ironcev commented Jan 30, 2024

Description

This PR implements index operator [] for references. The overall effort related to references is tracked in #5063.

[] is defined for references using this recursive definition: <reference>[<index>] := (*<reference>)[<index>].

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

let array = [1, 2, 3];

let r = &&&array;

assert((***r)[0] == r[0]);
let r = &&&[ &&[1, 2, 3], &&[4, 5, 6] ];
assert(r[0][2] == 3);
assert(r[1][0] == 4);

Additionally, the PR fixes two previously existing issues (see below screenshots):

  • the misleading error message on type not implementing the "index" method when indexing a non-indexable type.
  • the broken error span and expression text when indexing a non-indexable type in reassignments.

Before:
No method named index found for type

Not an indexable expression - Before

After:
Type is not indexable

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 Jan 30, 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 Jan 30, 2024
@ironcev ironcev requested review from a team January 31, 2024 00:10
Copy link
Contributor

@IGI-111 IGI-111 left a comment

Choose a reason for hiding this comment

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

Though we'll want to formalize it eventually with something like Deref it's appropriate to hardcode it for builtin types at this time.

@IGI-111 IGI-111 requested a review from a team January 31, 2024 11:30
@ironcev
Copy link
Member Author

ironcev commented Jan 31, 2024

Though we'll want to formalize it eventually with something like Deref it's appropriate to hardcode it for builtin types at this time.

Yes, agree. In this case it will be Deref and Index traits. Once we introduce them, extending the current solution will be straightforward. The sam will be with the upcoming . dereferencing operator for field and element access and method calls.

@ironcev ironcev enabled auto-merge (squash) January 31, 2024 14:35
@ironcev ironcev merged commit 6f9aab8 into master Jan 31, 2024
34 checks passed
@ironcev ironcev deleted the ironcev/dereferencing-operator-indexing branch January 31, 2024 14:56
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.

3 participants