Skip to content

Commit

Permalink
Disallow assigning to fields of non-mutable structs (FuelLabs#1420)
Browse files Browse the repository at this point in the history
* Disallow assigning to fields of non-mutable structs

* Add test for assigning to field of non-mutable struct

* fix broken test case that was failing with mutability check

* disable broken test blocked on FuelLabs#1188

* disable impl_self_reassignment test

This test was failing by trying to assign to a non-mut variable. We need
`&mut self` methods before it can be re-enabled.

Co-authored-by: Alex Hansen <[email protected]>
  • Loading branch information
canndrew and sezna authored May 19, 2022
1 parent 6a974d2 commit 13467d9
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 1 deletion.
24 changes: 24 additions & 0 deletions sway-core/src/semantic_analysis/ast_node/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -961,6 +961,30 @@ fn reassignment(

match expr {
Expression::VariableExpression { name, .. } => {
match namespace.clone().resolve_symbol(&name).value {
Some(TypedDeclaration::VariableDeclaration(
TypedVariableDeclaration { is_mutable, .. },
)) => {
if !is_mutable.is_mutable() {
errors.push(CompileError::AssignmentToNonMutable {
name: name.clone(),
});
}
}
Some(other) => {
errors.push(CompileError::ReassignmentToNonVariable {
name: name.clone(),
kind: other.friendly_name(),
span,
});
return err(warnings, errors);
}
None => {
errors
.push(CompileError::UnknownVariable { var_name: name });
return err(warnings, errors);
}
}
names_vec.push(ReassignmentLhs {
name,
r#type: type_checked.return_type,
Expand Down
7 changes: 7 additions & 0 deletions test/src/e2e_vm_tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -293,10 +293,16 @@ pub fn run(filter_regex: Option<regex::Regex>) {
"should_pass/language/contract_caller_as_type",
ProgramState::Return(42),
),
/*
* This test is disabled because in order to work correctly it requires that we implement
* `&mut self` methods.
*
* See: #1188
(
"should_pass/language/self_impl_reassignment",
ProgramState::Return(1),
),
*/
(
"should_pass/language/import_trailing_comma",
ProgramState::Return(0),
Expand Down Expand Up @@ -455,6 +461,7 @@ pub fn run(filter_regex: Option<regex::Regex>) {
"should_fail/double_underscore_var",
"should_fail/double_underscore_struct",
"should_fail/double_underscore_enum",
"should_fail/assign_to_field_of_non_mutable_struct",
"should_fail/abi_method_signature_mismatch",
"should_fail/trait_method_signature_mismatch",
"should_fail/impure_read_calls_impure_write",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'assign_to_field_of_non_mutable_struct'
dependencies = []
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <[email protected]>"]
license = "Apache-2.0"
name = "assign_to_field_of_non_mutable_struct"
entry = "main.sw"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
script;

struct S {
x: u64,
}

fn wow() {
let thing: S = S { x: 0 };
thing.x = 23;
}

Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct S {
}

fn main() -> bool {
let s = S {
let mut s = S {
t0: W {
t5: 5,
t6: T6 {
Expand Down

0 comments on commit 13467d9

Please sign in to comment.