-
Notifications
You must be signed in to change notification settings - Fork 13.3k
NLL suggests removing mutability from a variable, doing so causes a compiler error #47279
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
Comments
Without NLL, the original code has both an error and the warning:
|
I am going to try fix this. |
Status update: Understanding the two error pastes, I don't think this a NLL specific problem as the warning is produced in non-NLL case also. I am looking into code how the used_mut is computed in borrowck. |
This is the part of code where the error message is coming from rust/src/librustc_borrowck/borrowck/unused.rs Lines 73 to 90 in 8d3e93b
I am bit confused by the comment here rust/src/librustc_borrowck/borrowck/unused.rs Lines 57 to 60 in 8d3e93b
@nikomatsakis can you guide me a bit here. Also is there any way I can debug into rust source code and see what's happening there? |
@Pulkit07 ok, a few things First, you are correct that the code which issues Ah, well, I bet I see what is happening. I suspect that the old AST borrowck is reporting an error (which gets silenced, because we are in NLL mode). As a result, it exits early, which then in turn causes the set of "used (That is, the way this code works, the borrowck creates a set of which I feel like the best way to handle this may indeed be to update the MIR-based borrowck to compute this set, and to have the lint (when in NLL mode) use the set produced by the MIR-based borrowck. |
The MIR-based code is probably somewhat simpler. I think we would want to modify this function: rust/src/librustc_mir/borrow_check/mod.rs Lines 1378 to 1383 in 8d3e93b
The idea would be that whenever we are doing a mutation, and the mutation is allowed because the local variable is declared Lines 441 to 446 in 8d3e93b
and in particular this field will be true: Line 453 in 8d3e93b
We would then have to use the Lines 483 to 484 in 8d3e93b
We might need to thread a bit more information here, I'm not sure. |
I am trying to implement a function for MirBorrowckCtxt which can return us a set of user defined mutable variables. The set can then be used to check whether all the variables are used mutably or not. I am trying to implement the first step right now which is a function to return the set of user defined mutable variables. Here is the paste of the diff: https://gist.github.com/Pulkit07/e6254a3c0f37f82bfec48a512071d1f2 How can I convert a LocalDecl to a Place type? The Place enum is used everywhere but here I have LocalDecl type. cc: @nikomatsakis |
As a nit, I'm not sure it's worth actually building a set -- maybe return a fn user_declared_mutable_variables(&self) -> impl Iterator<Item = Local> + 'cx {
// the 'cx here is the lifetime of the `mir`
self.mir.local_decls.iter_enumerated()
.filter_map(|(local, local_decl)| {
if local_decl.is_user_declared && /* is mutable */ { Some(local) } else { None }
})
} If you want to construct the corresponding Line 1236 in da569fa
|
@Pulkit07 how goes? just checking in here =) |
I'm clearing @spastorino because he's busy working on other things just now and I think this would be a good issue for someone to take up. The goal here is to rework the way that the I added some mentoring instructions here -- basically instructions on how to gather up the set of cc @rust-lang/wg-compiler-nll |
I will pick this issue up and work on it. |
Allow MIR borrowck to catch unused mutable locals Fixes #47279. r? @nikomatsakis
UPDATE: Mentoring instructions below.
This code compiles, but with a warning:
Removing the
mut
causes a compiler error:The text was updated successfully, but these errors were encountered: