You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
public inline fun pass_mut_ref<T>(v: &mut vector<T>, action: |&mut T|&mut T): &mut T {
action(vector::borrow_mut(v, 0))
}
public fun consume_mut_freeze() {
let v = vector[1, 2, 3];
let r = pass_mut_ref(&mut v, |x| (freeze(x)));
}
yields the error message:
error: the function takes `&mut` but `&` was provided
┌─ tests/checking/typing/lambda_widen_result.move:43:38
│
43 │ let r = pass_mut_ref(&mut v, |x| (freeze(x)));
│ ^^^^^^^^^^^^^^^
Which is very confusing. The problem here is that
The type of parameter action is |&mut T|&mut T.
The value provided is a lambda of ambiguous type, either |&mut T|&T or |&T|&T.
So the type checker is trying to unify |&mut T|&T with |&mut T|&mut T and finds that the
return type &T is not compatible with &mut T.
But:
saying that "the function takes &mut" is wrong: it takes a function value with result type &mut.
No & was provided: rather, a function value with result type & was provided.
BTW, the type checker probably tries both parameter types and gets one right.
For the parameter type of the function-valued parameter, it's difficult to engineer a similar situation without PR #14254, which adds type declarations for lambda parameters.
But changing the test case to
public fun consume_mut_mut() {
let v = vector[1, 2, 3];
let r = pass_mut_ref(&mut v, |a, b| a);
}
leads to the more reasonable error message:
error: cannot pass `|(_, _)|_` to a function which expects argument of type `|&mut integer|&mut integer`
┌─ tests/checking/typing/lambda_widen_result.move:28:38
│
28 │ let r = pass_mut_ref(&mut v, |a, b| a);
│ ^^^^^^^^
which would be better for the result case.
The text was updated successfully, but these errors were encountered:
@rahxephon89 and @wrwg this is Urgent because I will need this cleaned up for more general-purpose lambda parameters. I've spent a few minutes looking at the code, and hope one of you can give some insight into how to fix it.
🐛 Bug
yields the error message:
Which is very confusing. The problem here is that
action
is|&mut T|&mut T
.|&mut T|&T
or|&T|&T
.|&mut T|&T
with|&mut T|&mut T
and finds that thereturn type
&T
is not compatible with&mut T
.&mut
" is wrong: it takes a function value with result type&mut
.&
was provided: rather, a function value with result type&
was provided.For the parameter type of the function-valued parameter, it's difficult to engineer a similar situation without PR #14254, which adds type declarations for lambda parameters.
But changing the test case to
leads to the more reasonable error message:
which would be better for the result case.
The text was updated successfully, but these errors were encountered: