Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions sway-core/src/language/ty/declaration/declaration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,7 @@ impl DisplayWithEngines for TyDecl {
match mutability {
VariableMutability::Mutable => builder.push_str("mut"),
VariableMutability::RefMutable => builder.push_str("ref mut"),
VariableMutability::RefImmutable => builder.push_str("ref"),
VariableMutability::Immutable => {}
}
builder.push_str(name.as_str());
Expand Down Expand Up @@ -441,6 +442,7 @@ impl DebugWithEngines for TyDecl {
match mutability {
VariableMutability::Mutable => builder.push_str("mut"),
VariableMutability::RefMutable => builder.push_str("ref mut"),
VariableMutability::RefImmutable => builder.push_str("ref"),
VariableMutability::Immutable => {}
}
builder.push_str(name.as_str());
Expand Down
6 changes: 5 additions & 1 deletion sway-core/src/language/ty/variable_mutability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,19 @@ pub enum VariableMutability {
Mutable,
// referenceable + mutable
RefMutable,
// referenceable + immutable
RefImmutable,
// immutable
#[default]
Immutable,
}

impl VariableMutability {
pub fn new_from_ref_mut(is_reference: bool, is_mutable: bool) -> VariableMutability {
if is_reference {
if is_reference && is_mutable {
VariableMutability::RefMutable
} else if is_reference {
VariableMutability::RefImmutable
} else if is_mutable {
VariableMutability::Mutable
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,9 +216,11 @@ fn unify_arguments_and_parameters(
}

// check for matching mutability
let param_mutability =
ty::VariableMutability::new_from_ref_mut(param.is_reference, param.is_mutable);
if arg.gather_mutability().is_immutable() && param_mutability.is_mutable() {
// Only ref mut params require mutable args.
if arg.gather_mutability().is_immutable()
&& param.is_reference
&& param.is_mutable
{
handler.emit_err(CompileError::ImmutableArgumentToMutableParameter {
span: arg.span.clone(),
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = "ref_without_mut_not_assignable"
source = "member"
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
name = "ref_without_mut_not_assignable"
authors = ["Fuel Labs <contact@fuel.sh>"]
entry = "main.sw"
license = "Apache-2.0"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
script;

fn ref_immutable(ref x: u64) {
x = 10;
}

fn ref_mut_immutable_arg(ref mut _x: u64) { }

fn main() -> u64 {
let y = 42;

ref_immutable(y);

ref_mut_immutable_arg(y);

y
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
category = "fail"

# ref without mut: parameter should be immutable inside the function body
# check: $()x = 10;
# nextln: $()cannot be assigned to, because it is an immutable variable

# ref mut with immutable argument: should still require mutable arg
# check: $()ref_mut_immutable_arg(y);
# nextln: $()Cannot pass immutable argument to mutable parameter
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[[package]]
name = "ref_without_mut"
source = "member"
dependencies = ["std"]

[[package]]
name = "std"
source = "path+from-root-9C22439F46EC1A75"
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "ref_without_mut"
entry = "main.sw"
implicit-std = false

[dependencies]
std = { path = "../../../../reduced_std_libs/sway-lib-std-core" }
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
script;

struct S {
value: u64,
}

impl S {
fn method(self, ref _x: u64) -> u64 {
_x
}

fn associated_fn(ref _x: u64) -> u64 {
_x
}
}

fn ref_pass(ref _x: u64) -> u64 {
_x
}

fn main() -> u64 {
let x = 42;

let a = ref_pass(x);
let b = S { value: 1 }.method(x);
let c = S::associated_fn(x);

if a == 42 && b == 42 && c == 42 {
42
} else {
0
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
category = "run"
expected_result = { action = "return", value = 42 }
expected_result_new_encoding = { action = "return_data", value = "000000000000002A" }