-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
Add references to Sway #5406
Add references to Sway #5406
Conversation
What about ABI methods - are there any special considerations we've taken into account for those? (i.e. no mutable references) |
Good question! I am also asking it in one of the open todos: The current proposal goes for forbidding references altogether on the boundaries. But if we support Having some good examples for use cases where Long story short, current proposal bans references on the boundaries, but we will re-discuss it. Any concrete examples in dapps that would be helpful to understand the use cases are appreciated. |
sway-core/src/semantic_analysis/ast_node/expression/typed_expression.rs
Outdated
Show resolved
Hide resolved
I left some comments but I am in favor of merging this and avoid merge/rebase issues. |
I am also curious to know what would happen to references to locals being returned
|
The locals will be placed on heap and the returned reference will contain a heap pointer. This is meant by "Allocating values on the heap when using references" in #5063. Knowing we never free heap allocations this is fine and is a first step. BDW we discussed garbage collection at the offsite and there should not be anything in the reference concept what will stop us one day from implementing garbage collection if we want. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So cool to see this finally land in sway. Awesome work!
Description
This PR brings references to the language. The overall effort is tracked in #5063.
In the below description, when talking about references we mean
&T
references - references to immutable values.&mut T
will be implemented in upcoming PRs.The PR implements the following features:
*
operator, etc.&return;
let mut r_x = &x
&T
works as expected.*
operator.*&return;
Known limitations:
&&a
emits error and must be written as& &a
. Extending parser to support, e.g., arbitrary&&...&&a
will be done in upcoming PRs.On the IR level, references are pointers stored as
u64
and do not exist as a separate concept. They play well with all the existing IR optimizations.Since
core::ops::Eq
and__addr_of()
are still not implemented for references, checking for equality in tests is done by converting references toraw_ptr
s and checking raw pointers. This is essentially fine and will one day be turned into converting references to typed pointers which are tests we will anyhow want to have.Next steps are listed in #5063.
The documentation will be done in a separate PR and provided separately once feature is considered ready for public usage.
There are several todos in code marked as
TODO-IG
. They will be resolved in upcoming PRs.Demo
For extensive demos of implemented features, see the tests in this PR.
An example of a semantic check:
Checklist
Breaking*
orNew Feature
labels where relevant.