Skip to content

Commit

Permalink
Fixes error when dependency variable is used as function parameter. (F…
Browse files Browse the repository at this point in the history
…uelLabs#3494)

When a dependency variable was passed into a function as first parameter
such as `ContractId::from(contract_c::CONTRACT_ID);` an error was thrown
by `type_check_method_application` while it tried to resolve the
variable as a local one.

The solution was to only resolve the variable if the function is
expecting a mutable parameter.

Closes FuelLabs#3406

Co-authored-by: João Matos <[email protected]>
  • Loading branch information
esdrubal and tritao authored Dec 2, 2022
1 parent 12aff78 commit ef47e69
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -205,33 +205,35 @@ pub(crate) fn type_check_method_application(
Some(ty::TyFunctionParameter { is_mutable, .. }),
) = (args_buf.get(0), method.parameters.get(0))
{
let unknown_decl = check!(
ctx.namespace.resolve_symbol(name).cloned(),
return err(warnings, errors),
warnings,
errors
);
if *is_mutable {
let unknown_decl = check!(
ctx.namespace.resolve_symbol(name).cloned(),
return err(warnings, errors),
warnings,
errors
);

let is_decl_mutable = match unknown_decl {
ty::TyDeclaration::ConstantDeclaration(_) => false,
_ => {
let variable_decl = check!(
unknown_decl.expect_variable().cloned(),
return err(warnings, errors),
warnings,
errors
);
variable_decl.mutability.is_mutable()
}
};
let is_decl_mutable = match unknown_decl {
ty::TyDeclaration::ConstantDeclaration(_) => false,
_ => {
let variable_decl = check!(
unknown_decl.expect_variable().cloned(),
return err(warnings, errors),
warnings,
errors
);
variable_decl.mutability.is_mutable()
}
};

if !is_decl_mutable && *is_mutable {
errors.push(CompileError::MethodRequiresMutableSelf {
method_name: method_name_binding.inner.easy_name(),
variable_name: name.clone(),
span,
});
return err(warnings, errors);
if !is_decl_mutable {
errors.push(CompileError::MethodRequiresMutableSelf {
method_name: method_name_binding.inner.easy_name(),
variable_name: name.clone(),
span,
});
return err(warnings, errors);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,7 @@ abi MyContract {
impl MyContract for Contract {
fn test_function() {
let CONTRACT_B = CONTRACT_B_ID;
let CONTRACT_C = CONTRACT_C_ID;
let contract_b_id = ContractId::from(CONTRACT_B);
let contract_c_id = ContractId::from(CONTRACT_C);
let contract_c_id = ContractId::from(contract_c::CONTRACT_ID);
}
}

0 comments on commit ef47e69

Please sign in to comment.