Skip to content

Commit d839fba

Browse files
committed
Refactor (step 3).
1 parent b47446d commit d839fba

File tree

50 files changed

+637
-1034
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+637
-1034
lines changed

docs/book/src/basics/variables.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ let mut foo = 5;
3030
foo = 6;
3131
```
3232

33-
Now, `foo` is mutable, and the reassignment to the number `6` is valid. That is, we are allowed to _mutate_ the variable `foo` to change its value.
33+
Now, `foo` is mutable, and the assignment to the number `6` is valid. That is, we are allowed to _mutate_ the variable `foo` to change its value.
3434

3535
When assigning to a mutable variable, the right-hand side of the assignment is evaluated before the left-hand side. In the below example, the mutable variable `i` will first be increased and the resulting value of `1` will be stored to `array[1]`, thus resulting in `array` being changed to `[0, 1, 0]`.
3636

docs/internals.md

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -190,15 +190,13 @@ pub struct ParseModule {
190190
```rust
191191
/// Represents the various structures that constitute a Sway program.
192192
pub enum AstNodeContent {
193-
/// A statement of the form `use foo::bar;` or `use ::foo::bar;`
194-
UseStatement(UseStatement),
193+
/// Any standalone statement (currently `let`, `use`, or `mod`).
194+
Statement(Statement),
195195
/// Any type of declaration, of which there are quite a few. See [Declaration] for more details
196196
/// on the possible variants.
197197
Declaration(Declaration),
198198
/// Any type of expression, of which there are quite a few. See [Expression] for more details.
199199
Expression(Expression),
200-
/// A statement of the form `mod foo::bar;` which imports/includes another source file.
201-
IncludeStatement(IncludeStatement),
202200
...
203201
}
204202
```

docs/reference/src/code/language/variables/src/lib.sw

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,14 @@ fn immutable() {
2525
// ANCHOR_END: immutable
2626
}
2727

28-
fn reassignment() {
29-
// ANCHOR: reassignment
28+
fn assignment() {
29+
// ANCHOR: assignment
3030
// Set `foo` to take the value of `5` and the default `u64` type
3131
let foo = 5;
3232

3333
// Reassign `foo` to be a `str` with the value of `Fuel`
3434
let foo = "Fuel";
35-
// ANCHOR_END: reassignment
35+
// ANCHOR_END: assignment
3636
}
3737

3838
fn shadowing() {

docs/reference/src/documentation/language/variables/variable-shadowing.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# Shadowing
22

3-
When looking at the [let](let.md) variable we've seen that the value can be changed through the use of the `mut` keyword. We can take this a couple steps further through [reassignment](#reassignment) and [variable shadowing](#variable-shadowing). Note that shadowing applies only to variables. [Constants](const.md) cannot be shadowed.
3+
When looking at the [let](let.md) variable we've seen that the value can be changed through the use of the `mut` keyword. We can take this a couple steps further through [assignment](#assignment) and [variable shadowing](#variable-shadowing). Note that shadowing applies only to variables. [Constants](const.md) cannot be shadowed.
44

5-
## Reassignment
5+
## Assignment
66

77
We can redefine the type and value of a variable by instantiating a new version after the first declaration.
88

99
```sway
10-
{{#include ../../../code/language/variables/src/lib.sw:reassignment}}
10+
{{#include ../../../code/language/variables/src/lib.sw:assignment}}
1111
```
1212

1313
## Variable Shadowing

forc-plugins/forc-migrate/src/migrations/error_type.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use sway_ast::{
1414
};
1515
use sway_core::language::{
1616
ty::{
17-
TyExpression, TyExpressionVariant, TyFunctionDecl, TyReassignmentTarget, TyStructDecl,
17+
TyAssignmentTarget, TyExpression, TyExpressionVariant, TyFunctionDecl, TyStructDecl,
1818
TyVariableDecl,
1919
},
2020
CallPathType,
@@ -312,12 +312,12 @@ fn rename_existing_panic_identifiers_to_r_panic_step(
312312
Ok(InvalidateTypedElement::No)
313313
}
314314

315-
fn visit_reassignment(
315+
fn visit_assignment(
316316
&mut self,
317317
ctx: &VisitingContext,
318-
_lexed_op: &mut expr::ReassignmentOp,
318+
_lexed_op: &mut expr::AssignmentOp,
319319
lexed_lhs: &mut Assignable,
320-
_ty_lhs: Option<&TyReassignmentTarget>,
320+
_ty_lhs: Option<&TyAssignmentTarget>,
321321
_lexed_rhs: &mut Expr,
322322
_ty_rhs: Option<&TyExpression>,
323323
output: &mut Vec<Occurrence>,
@@ -326,7 +326,7 @@ fn rename_existing_panic_identifiers_to_r_panic_step(
326326
// - Variable names, e.g., `let panic = 42;`
327327
// - Single field access, e.g., `let x.panic = 42;`
328328
// But occurrences in, e.g., `foo[panic].x = 42;` will not be renamed.
329-
// Full traversal of reassignments' LHS will be done as a part of migration
329+
// Full traversal of assignments' LHS will be done as a part of migration
330330
// infrastructure in the future.
331331

332332
// We report the occurrences only if it is not a dry-run.

forc-plugins/forc-migrate/src/visiting/mod.rs

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use std::sync::Arc;
99
use duplicate::duplicate_item;
1010
use sway_ast::{
1111
assignable::ElementAccess,
12-
expr::{LoopControlFlow, ReassignmentOp, ReassignmentOpVariant},
12+
expr::{AssignmentOp, AssignmentOpVariant, LoopControlFlow},
1313
keywords::*,
1414
AsmBlock, Assignable, Braces, CodeBlockContents, Expr, ExprArrayDescriptor, ExprStructField,
1515
ExprTupleDescriptor, IfCondition, IfExpr, Intrinsic, ItemAbi, ItemFn, ItemImpl, ItemImplItem,
@@ -21,9 +21,9 @@ use sway_core::{
2121
language::{
2222
lexed::LexedModule,
2323
ty::{
24-
TyAbiDecl, TyAstNodeContent, TyCodeBlock, TyDecl, TyExpression, TyExpressionVariant,
25-
TyFunctionDecl, TyImplSelfOrTrait, TyIntrinsicFunctionKind, TyModule,
26-
TyReassignmentTarget, TySideEffect, TySideEffectVariant, TyStorageDecl, TyStorageField,
24+
TyAbiDecl, TyAssignmentTarget, TyAstNodeContent, TyCodeBlock, TyDecl, TyExpression,
25+
TyExpressionVariant, TyFunctionDecl, TyImplSelfOrTrait, TyIntrinsicFunctionKind,
26+
TyModule, TySideEffect, TySideEffectVariant, TyStorageDecl, TyStorageField,
2727
TyStructDecl, TyTraitDecl, TyTraitItem, TyUseStatement, TyVariableDecl,
2828
},
2929
CallPath,
@@ -256,12 +256,12 @@ pub(crate) trait __TreesVisitor<O> {
256256
Ok(InvalidateTypedElement::No)
257257
}
258258
#[allow(clippy::too_many_arguments)]
259-
fn visit_reassignment(
259+
fn visit_assignment(
260260
&mut self,
261261
ctx: &VisitingContext,
262-
lexed_op: __ref_type([ReassignmentOp]),
262+
lexed_op: __ref_type([AssignmentOp]),
263263
lexed_lhs: __ref_type([Assignable]),
264-
ty_lhs: Option<&TyReassignmentTarget>,
264+
ty_lhs: Option<&TyAssignmentTarget>,
265265
lexed_rhs: __ref_type([Expr]),
266266
ty_rhs: Option<&TyExpression>,
267267
output: &mut Vec<O>,
@@ -1676,28 +1676,25 @@ impl __ProgramVisitor {
16761676
output,
16771677
)?;
16781678
}
1679-
Expr::Reassignment {
1679+
Expr::Assignment {
16801680
assignable,
1681-
reassignment_op,
1681+
assignment_op,
16821682
expr,
16831683
} => {
1684-
let ty_reassignment = ty_expr
1684+
let ty_assignment = ty_expr
16851685
.map(|ty_expr| match &ty_expr.expression {
1686-
TyExpressionVariant::Reassignment(ty_reassignment) => {
1687-
Ok(ty_reassignment.as_ref())
1686+
TyExpressionVariant::Assignment(ty_assignment) => {
1687+
Ok(ty_assignment.as_ref())
16881688
}
1689-
_ => bail!(invalid_ty_expression_variant(
1690-
"Reassignment",
1691-
"Reassignment"
1692-
)),
1689+
_ => bail!(invalid_ty_expression_variant("Assignment", "Assignment")),
16931690
})
16941691
.transpose()?;
1695-
let ty_lhs = ty_reassignment.map(|ty_reassignment| &ty_reassignment.lhs);
1696-
let ty_rhs = ty_reassignment.map(|ty_reassignment| &ty_reassignment.rhs);
1692+
let ty_lhs = ty_assignment.map(|ty_assignment| &ty_assignment.lhs);
1693+
let ty_rhs = ty_assignment.map(|ty_assignment| &ty_assignment.rhs);
16971694

1698-
let (ty_lhs, ty_rhs) = match visitor.visit_reassignment(
1695+
let (ty_lhs, ty_rhs) = match visitor.visit_assignment(
16991696
ctx,
1700-
reassignment_op,
1697+
assignment_op,
17011698
assignable,
17021699
ty_lhs,
17031700
expr.__as_ref(),
@@ -1714,7 +1711,7 @@ impl __ProgramVisitor {
17141711
fn visit_element_access<V, O>(
17151712
ctx: &VisitingContext,
17161713
element_access: __ref_type([ElementAccess]),
1717-
_ty_element_access: Option<&TyReassignmentTarget>,
1714+
_ty_element_access: Option<&TyAssignmentTarget>,
17181715
visitor: &mut V,
17191716
output: &mut Vec<O>,
17201717
) -> Result<()>
@@ -1724,7 +1721,7 @@ impl __ProgramVisitor {
17241721
match element_access {
17251722
ElementAccess::Var(_base_ident) => {}
17261723
ElementAccess::Index { target, arg } => {
1727-
// TODO: Implement extracting typed `reassignment LHS`.
1724+
// TODO: Implement extracting typed `assignment LHS`.
17281725
let ty_target = None;
17291726
visit_element_access(
17301727
ctx,
@@ -1764,27 +1761,27 @@ impl __ProgramVisitor {
17641761
star_token: _,
17651762
expr,
17661763
} => {
1767-
// TODO: Implement extracting typed `reassignment LHS`.
1764+
// TODO: Implement extracting typed `assignment LHS`.
17681765
let ty_expr = None;
17691766
Self::visit_expr(ctx, expr.__as_ref(), ty_expr, visitor, output)?;
17701767
}
17711768
}
17721769

17731770
// Visit RHS.
1774-
match reassignment_op.variant {
1775-
ReassignmentOpVariant::Equals => {
1771+
match assignment_op.variant {
1772+
AssignmentOpVariant::Equals => {
17761773
Self::visit_expr(ctx, expr, ty_rhs, visitor, output)?;
17771774
}
17781775
_ => {
1779-
// TODO: Implement extracting typed `ty_expr` when visiting `compound reassignments`.
1776+
// TODO: Implement extracting typed `ty_expr` when visiting `compound assignments`.
17801777
// We need to properly handle the desugaring.
17811778
// E.g., `x += func(1, 2);`
17821779
// will be desugared into `x = add(x, func(1, 2));`
17831780
// When visiting the RHS in the lexed tree, we need to skip the
17841781
// operator method call in the typed tree, and provide the
17851782
// typed arguments instead.
17861783
// To provide visiting without losing the information about compound
1787-
// reassignment, we will need to have a dedicated `visit_reassignment`
1784+
// assignment, we will need to have a dedicated `visit_assignment`
17881785
// method.
17891786
Self::visit_expr(ctx, expr, None, visitor, output)?;
17901787
}

sway-ast/src/expr/mod.rs

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ pub enum Expr {
186186
double_pipe_token: DoublePipeToken,
187187
rhs: Box<Expr>,
188188
},
189-
Reassignment {
189+
Assignment {
190190
assignable: Assignable,
191-
reassignment_op: ReassignmentOp,
191+
assignment_op: AssignmentOp,
192192
expr: Box<Expr>,
193193
},
194194
Break {
@@ -283,7 +283,7 @@ impl Spanned for Expr {
283283
Expr::GreaterThanEq { lhs, rhs, .. } => Span::join(lhs.span(), &rhs.span()),
284284
Expr::LogicalAnd { lhs, rhs, .. } => Span::join(lhs.span(), &rhs.span()),
285285
Expr::LogicalOr { lhs, rhs, .. } => Span::join(lhs.span(), &rhs.span()),
286-
Expr::Reassignment {
286+
Expr::Assignment {
287287
assignable, expr, ..
288288
} => Span::join(assignable.span(), &expr.span()),
289289
Expr::Break { break_token } => break_token.span(),
@@ -293,13 +293,13 @@ impl Spanned for Expr {
293293
}
294294

295295
#[derive(Clone, Debug, Serialize)]
296-
pub struct ReassignmentOp {
297-
pub variant: ReassignmentOpVariant,
296+
pub struct AssignmentOp {
297+
pub variant: AssignmentOpVariant,
298298
pub span: Span,
299299
}
300300

301301
#[derive(Clone, Debug, Serialize)]
302-
pub enum ReassignmentOpVariant {
302+
pub enum AssignmentOpVariant {
303303
Equals,
304304
AddEquals,
305305
SubEquals,
@@ -309,28 +309,28 @@ pub enum ReassignmentOpVariant {
309309
ShrEquals,
310310
}
311311

312-
impl ReassignmentOpVariant {
312+
impl AssignmentOpVariant {
313313
pub fn std_name(&self) -> &'static str {
314314
match self {
315-
ReassignmentOpVariant::Equals => "eq",
316-
ReassignmentOpVariant::AddEquals => "add",
317-
ReassignmentOpVariant::SubEquals => "subtract",
318-
ReassignmentOpVariant::MulEquals => "multiply",
319-
ReassignmentOpVariant::DivEquals => "divide",
320-
ReassignmentOpVariant::ShlEquals => "lsh",
321-
ReassignmentOpVariant::ShrEquals => "rsh",
315+
AssignmentOpVariant::Equals => "eq",
316+
AssignmentOpVariant::AddEquals => "add",
317+
AssignmentOpVariant::SubEquals => "subtract",
318+
AssignmentOpVariant::MulEquals => "multiply",
319+
AssignmentOpVariant::DivEquals => "divide",
320+
AssignmentOpVariant::ShlEquals => "lsh",
321+
AssignmentOpVariant::ShrEquals => "rsh",
322322
}
323323
}
324324

325325
pub fn as_str(&self) -> &'static str {
326326
match self {
327-
ReassignmentOpVariant::Equals => EqToken::AS_STR,
328-
ReassignmentOpVariant::AddEquals => AddEqToken::AS_STR,
329-
ReassignmentOpVariant::SubEquals => SubEqToken::AS_STR,
330-
ReassignmentOpVariant::MulEquals => StarEqToken::AS_STR,
331-
ReassignmentOpVariant::DivEquals => DivEqToken::AS_STR,
332-
ReassignmentOpVariant::ShlEquals => ShlEqToken::AS_STR,
333-
ReassignmentOpVariant::ShrEquals => ShrEqToken::AS_STR,
327+
AssignmentOpVariant::Equals => EqToken::AS_STR,
328+
AssignmentOpVariant::AddEquals => AddEqToken::AS_STR,
329+
AssignmentOpVariant::SubEquals => SubEqToken::AS_STR,
330+
AssignmentOpVariant::MulEquals => StarEqToken::AS_STR,
331+
AssignmentOpVariant::DivEquals => DivEqToken::AS_STR,
332+
AssignmentOpVariant::ShlEquals => ShlEqToken::AS_STR,
333+
AssignmentOpVariant::ShrEquals => ShrEqToken::AS_STR,
334334
}
335335
}
336336
}
@@ -611,7 +611,7 @@ impl Expr {
611611
| Expr::GreaterThanEq { .. }
612612
| Expr::LogicalAnd { .. }
613613
| Expr::LogicalOr { .. }
614-
| Expr::Reassignment { .. }
614+
| Expr::Assignment { .. }
615615
| Expr::Break { .. }
616616
| Expr::Continue { .. } => false,
617617
}
@@ -663,7 +663,7 @@ impl Expr {
663663
Expr::GreaterThanEq { .. } => "greater than or equal operation",
664664
Expr::LogicalAnd { .. } => "logical and",
665665
Expr::LogicalOr { .. } => "logical or",
666-
Expr::Reassignment { .. } => "reassignment",
666+
Expr::Assignment { .. } => "assignment",
667667
Expr::Break { .. } => "break",
668668
Expr::Continue { .. } => "continue",
669669
}

sway-core/src/control_flow_analysis/analyze_return_paths.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,9 @@ fn connect_node<'eng: 'cfg, 'cfg>(
199199
}
200200
Ok(NodeConnection::NextStep(Some(entry_node)))
201201
}
202+
ty::TyStatement::Use(_) | ty::TyStatement::Mod(_) => {
203+
Ok(NodeConnection::NextStep(leaf_opt))
204+
}
202205
},
203206
ty::TyAstNodeContent::SideEffect(_) => Ok(NodeConnection::NextStep(leaf_opt)),
204207
ty::TyAstNodeContent::Declaration(decl) => Ok(NodeConnection::NextStep(

0 commit comments

Comments
 (0)