Skip to content

Commit

Permalink
Fix ref mut parameter checking to apply to all function parameters.
Browse files Browse the repository at this point in the history
Closes #4438.
  • Loading branch information
tritao committed Apr 14, 2023
1 parent 0531854 commit 5bceb32
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl ty::TyFunctionDecl {
let mut new_parameters = vec![];
for parameter in parameters.into_iter() {
new_parameters.push(check!(
ty::TyFunctionParameter::type_check(ctx.by_ref(), parameter, is_method),
ty::TyFunctionParameter::type_check(ctx.by_ref(), parameter),
continue,
warnings,
errors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ impl ty::TyFunctionParameter {
pub(crate) fn type_check(
mut ctx: TypeCheckContext,
parameter: FunctionParameter,
is_from_method: bool,
) -> CompileResult<Self> {
let mut warnings = vec![];
let mut errors = vec![];
Expand Down Expand Up @@ -41,15 +40,13 @@ impl ty::TyFunctionParameter {
errors,
);

if !is_from_method {
let mutability = ty::VariableMutability::new_from_ref_mut(is_reference, is_mutable);
if mutability == ty::VariableMutability::Mutable {
errors.push(CompileError::MutableParameterNotSupported {
param_name: name.clone(),
span: name.span(),
});
return err(warnings, errors);
}
let mutability = ty::VariableMutability::new_from_ref_mut(is_reference, is_mutable);
if mutability == ty::VariableMutability::Mutable {
errors.push(CompileError::MutableParameterNotSupported {
param_name: name.clone(),
span: name.span(),
});
return err(warnings, errors);
}

let typed_parameter = ty::TyFunctionParameter {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,31 @@ category = "fail"

# check: $()error
# check: $()fn test_function1(ref mut p1: u64) {
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation.

# check: $()error
# check: $()fn test_function5(ref p5: u64) {
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation.

# check: $()error
# check: $()fn test_function6(mut p6: u64) {
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation
# nextln: $()This parameter was declared as mutable, which is not supported yet, did you mean to use ref mut?

#### Trait

# check: $()error
# check: $()fn check_function1(ref mut q1: u64) {
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation.

# check: $()error
# check: $()fn check_function2(mut q2: u64);
# check: $()fn check_function2(mut q2: u64) {
# nextln: $()This parameter was declared as mutable, which is not supported yet, did you mean to use ref mut?

# check: $()error
# check: $()fn check_function5(ref q5: u64) {
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation.

# check: $()error
# check: $()fn check_function6(mut q6: u64) {
# nextln: $()Parameter reference type or mutability mismatch between the trait function declaration and its implementation
# nextln: $()This parameter was declared as mutable, which is not supported yet, did you mean to use ref mut?

Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[[package]]
name = 'mutable_fn_args_impl_self'
source = 'member'
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[project]
authors = ["Fuel Labs <contact@fuel.sh>"]
license = "Apache-2.0"
name = "mutable_fn_args_impl_self"
entry = "main.sw"
implicit-std = false
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
[
{
"attributes": null,
"inputs": [],
"name": "main",
"outputs": [
{
"components": null,
"name": "",
"type": "bool",
"typeArguments": null
}
],
"type": "function"
}
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
script;

struct S {}

impl S {
fn foo(mut self) {}
fn bar(self, mut data: u64) {}
}

fn main() {}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
category = "fail"

# mutable (non-ref) primitive parameters are not supported yet...

# check: foo(mut self) {}
# check: $()This parameter was declared as mutable, which is not supported yet, did you mean to use ref mut?

# check: fn bar(self, mut data: u64) {}
# check: $()This parameter was declared as mutable, which is not supported yet, did you mean to use ref mut?

0 comments on commit 5bceb32

Please sign in to comment.