mirror of
https://github.com/FuelLabs/sway.git
synced 2025-08-12 14:48:35 +00:00
![]() ## Description This PR implements dereferencing in reassignment targets, as defined in [references](https://github.com/FuelLabs/sway-rfcs/blob/ironcev/amend-references/files/0010-references.sw). The overall effort related to references is tracked in https://github.com/FuelLabs/sway/issues/5063. Up to now, it was only possible to read the values references refer to. This PR allows values to be written over `&mut` references. In other words, any `*<expression that results in &mut>`is now an l-values and can be used in left-hand sides (LHS) of assignments. In most of the cases, the `<expression>` will very likely be just a reference variable, but any expression that results in `&mut` is allowed and supported. E.g.; ```Sway *mut_ref_to_u64 = 42; *if condition { &mut x } else { &mut y } = 42; *max_mut(&mut x, &mut y) = 42; ``` Additionally, the PR: - fixes #5736 by properly replacing decls in reassignment LHSs. - fixes #5737 by properly substituting types in reassignment LHSs. - fixes #5920 by properly connecting expressions in array indices to the DCA graph. - fixes #5922 by type-cheking the array indices in reassignment LHSs and forcing them to be `u64`. - improves misplaced and misleading error messages when assigning to constants and other items (see demo below). - improves error message when assigning to immutable variables by pointing to variable declaration. - improves error message when expression cannot be assigned to by pointing to the problematic part of the expression. Since Sway is more restrictive here then Rust, the restrictions, without being explained, could cause confusion. That's why special attention was given to point to the exact issue and explain what is actually supported in Sway (see demo below). - reduces number of expected warnings in references E2E tests that were previously set high because of DCA issues that are fixed in the meantime. The PR also brings additional analysis and checks to IR optimizations. Among other things, it explicitly points out the cases in which a potential usage of a symbol cannot be deterministically confirmed or denied. In this PR properly reacting for such cases is done in some optimizations. Rolling it out fully will be done in a follow up PR #5924. More advanced escape analysis will be done later on, as a part of allocating values on the heap in case of referencing. This PR implements only dereferencing in LHS. Support for referenced elements in the element based access (without dereferencing) will be done in a separate step as an ongoing work on implementing #5063. Closes #5736, #5737, #5920, #5922. ## Demo Before: ![Assignment to constant - Before]( |
||
---|---|---|
.. | ||
src | ||
theme | ||
.gitignore | ||
book.toml | ||
README.md |
The Sway Reference
Setup
If you wish contribute to this reference: