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
{{ message }}
Repository navigation
MIR borrowck: AST error message refers to kind of item that can't be borrowed #46629
I think the mc code sometimes generates ugly error messages - it should be called on the lvalue that causes the mutability error, not on the full lvalue - but you can use it as a reference to improve the errors.
Note: if you do this correctly, you should also be able to reroute closure errors into things like E0387 - "cannot assign to data in a captured outer variable in an Fn closure" instead of the standard "cannot borrow immutable item as mutable". See e.g. the borrow-immutable-upvar-mutation test:
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT// file at the top-level directory of this distribution and at// http://rust-lang.org/COPYRIGHT.//// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your// option. This file may not be copied, modified, or distributed// except according to those terms.#![feature(unboxed_closures)]// Tests that we can't assign to or mutably borrow upvars from `Fn`// closures (issue #17780)fnset(x:&mutusize){*x = 5;}fnto_fn<A,F:Fn<A>>(f:F) -> F{ f }fnto_fn_mut<A,F:FnMut<A>>(f:F) -> F{ f }fnmain(){// By-ref captures{letmut x = 0;let _f = to_fn(|| x = 42);//~ ERROR cannot assignletmut y = 0;let _g = to_fn(|| set(&mut y));//~ ERROR cannot borrowletmut z = 0;let _h = to_fn_mut(|| {set(&mut z);to_fn(|| z = 42);});//~ ERROR cannot assign}// By-value captures{letmut x = 0;let _f = to_fn(move || x = 42);//~ ERROR cannot assignletmut y = 0;let _g = to_fn(move || set(&mut y));//~ ERROR cannot borrowletmut z = 0;let _h = to_fn_mut(move || {set(&mut z);to_fn(move || z = 42);});//~ ERROR cannot assign}}
Activity
arielb1 commentedon Dec 10, 2017
I think the mc code sometimes generates ugly error messages - it should be called on the lvalue that causes the mutability error, not on the full lvalue - but you can use it as a reference to improve the errors.
arielb1 commentedon Dec 10, 2017
Note: if you do this correctly, you should also be able to reroute closure errors into things like E0387 - "cannot assign to data in a captured outer variable in an
Fnclosure" instead of the standard "cannot borrow immutable item as mutable". See e.g. the borrow-immutable-upvar-mutation test:arielb1 commentedon Dec 10, 2017
In that case, the error message we emit is especially terrible, because it is not obvious that the problem is with the closure:
nikomatsakis commentedon Apr 3, 2018
Semi-dup of #47388
nikomatsakis commentedon Jul 3, 2018
It seems like we still say things like "immutable item". This doesn't seem great. Tagging for release candidate.
nikomatsakis commentedon Jul 3, 2018
nikomatsakis commentedon Jul 3, 2018
Honestly, I think I would prefer to see this phrased rather differently. Something like:
"cannot borrow
xmutably, as it is not declared as mutable"Auto merge of #52405 - matthewjasper:mutability-errors, r=pnkfelix