mirror of
https://github.com/FuelLabs/sway.git
synced 2025-08-07 20:28:43 +00:00

## Description This PR makes struct fields private by default and introduces explicit public struct fields: ``` pub struct Struct { pub public_field: u8, private_field: u8, } ``` Private fields can be accessed only withing the module in which their struct is declared. Error messages are properly orchestrated so that no conflicting or duplicated messages are emitted. Since the change is a breaking change, relevant suggestion on how to fix the particular issue are given. To avoid an abrupt breaking change, the errors are temporarily turned into warnings. These warnings will become errors in the upcoming versions of Sway. The demo section below demonstrate how the errors will look like, and how a one warning equivalent looks now. Standard library structs like `Vec` or `String`, are adapted where needed by adding the `pub` keyword to the fields that are accessed outside of the struct declaration module. Some of these fields should very likely remain public, but some, like e.g., `RawBytes::ptr` or `Vec::buf` should be private. Adjusting the standard library to properly utilize private fields is out of scope of this PR and will be done separately. I expect breaking changes in the STD once we start modeling structs keeping encapsulation in mind. In addition, the PR: - migrates [annotate_snippets](https://github.com/rust-lang/annotate-snippets-rs) crate to its latest version (breaking changes) - removes a redundant non-existing field error and orchestrates non-existing field error with privacy errors - replaces an invalid and misleading error when accessing fields on storage elements that are not structs Closes #4692. ## Known limitations There is an issue in displaying multi-line code snippets, which is independent of these changes, but wasn't apparent before. The issue is visible in the demo section below, where the struct bodies are sometimes not fully shown, and they should be. This issue is reported in #5499 and will be solved in a separate PR. ## Demo (an excerpt 😄) ### Private struct field is inaccessible   ### Struct cannot be instantiated   ### Struct pattern must ignore inaccessible private fields  ### Struct pattern has missing fields  ### Errors temporarily turned into warnings  ## Checklist - [x] I have linked to any relevant issues. - [x] I have commented my code, particularly in hard-to-understand areas. - [x] I have updated the documentation where relevant (API docs, the reference, and the Sway book). - [x] I have added tests that prove my fix is effective or that my feature works. - [x] I have added (or requested a maintainer to add) the necessary `Breaking*` or `New Feature` labels where relevant. - [x] I have done my best to ensure that my PR adheres to [the Fuel Labs Code Review Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md). - [x] I have requested a review from the relevant team or maintainers.
528 B
528 B
The Sway Reference
Setup
If you wish contribute to this reference: