Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix ref mut parameter checking to apply to all function parameters. #4442

Merged
merged 1 commit into from
Apr 15, 2023
Merged
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
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?