-
Notifications
You must be signed in to change notification settings - Fork 13.3k
Add help message for missing IndexMut
impl
#52788
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
Merged
bors
merged 3 commits into
rust-lang:master
from
LukasKalbertodt:improve-index-mut-error
Aug 10, 2018
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error[E0596]: cannot borrow data in a `&` reference as mutable | ||
--> $DIR/index-mut-help.rs:21:5 | ||
| | ||
LL | map["peter"].clear(); //~ ERROR | ||
| ^^^^^^^^^^^^ cannot borrow as mutable | ||
|
||
error[E0594]: cannot assign to data in a `&` reference | ||
--> $DIR/index-mut-help.rs:22:5 | ||
| | ||
LL | map["peter"] = "0".to_string(); //~ ERROR | ||
| ^^^^^^^^^^^^ cannot assign | ||
|
||
error[E0596]: cannot borrow data in a `&` reference as mutable | ||
--> $DIR/index-mut-help.rs:23:13 | ||
| | ||
LL | let _ = &mut map["peter"]; //~ ERROR | ||
| ^^^^^^^^^^^^^^^^^ cannot borrow as mutable | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors occurred: E0594, E0596. | ||
For more information about an error, try `rustc --explain E0594`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// Copyright 2018 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. | ||
|
||
// When mutably indexing a type that implements `Index` but not `IndexMut`, a | ||
// special 'help' message is added to the output. | ||
|
||
|
||
fn main() { | ||
use std::collections::HashMap; | ||
|
||
let mut map = HashMap::new(); | ||
map.insert("peter", "23".to_string()); | ||
|
||
map["peter"].clear(); //~ ERROR | ||
map["peter"] = "0".to_string(); //~ ERROR | ||
let _ = &mut map["peter"]; //~ ERROR | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
error[E0596]: cannot borrow immutable indexed content as mutable | ||
--> $DIR/index-mut-help.rs:21:5 | ||
| | ||
LL | map["peter"].clear(); //~ ERROR | ||
| ^^^^^^^^^^^^ cannot borrow as mutable | ||
| | ||
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>` | ||
|
||
error[E0594]: cannot assign to immutable indexed content | ||
--> $DIR/index-mut-help.rs:22:5 | ||
| | ||
LL | map["peter"] = "0".to_string(); //~ ERROR | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable | ||
| | ||
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>` | ||
|
||
error[E0596]: cannot borrow immutable indexed content as mutable | ||
--> $DIR/index-mut-help.rs:23:18 | ||
| | ||
LL | let _ = &mut map["peter"]; //~ ERROR | ||
| ^^^^^^^^^^^^ cannot borrow as mutable | ||
| | ||
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>` | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
Some errors occurred: E0594, E0596. | ||
For more information about an error, try `rustc --explain E0594`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you also add the message to the NLL errors? Eventually we'll switch over and lose the new note otherwise.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to do that in the past couple of hours and now I really don't know how to proceed.
So I'm pretty sure I have to add my code to
report_mutability_error()
inlibrustc_mir/borrow_check/mutability_errors.rs
. But I don't know how I can get the information whether this error was caused by an index operation. I have access to the following things:access_place
andthe_place_err
: these were my biggest hope of getting information about the expression causing the error. Sadly, in all three cases mentioned in this thread, they areProjectionElem::Deref { base: Place::Local }
which doesn't help me. I would have expected one of them to be aProjectionElem::Index
.span
anderror_access
: not helpful AFAICTlocation
: it's only a reference to the enclosing basic block, right? So not really helpful eitherself.mir_def_id
: this is aDefId
and thus references a definition of an item, right? So I guess it references the enclosing function. So not useful either.self.{other_fields}
: AFAICT most of them are global structures and nothing pointing to the expression in question. I don't think anything is helpful here.So I'm stuck and can't invest a lot more time into this. Could someone help me to get this done fairly quickly? If you people in this thread don't know about this stuff, could you ping someone who does know the code? Or tell me where I should go and ask about this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ProjectionElem::Index
is only for simple arrays and slices. Everything else goes through theIndexMut
/Index
trait methods. I'm assuming rustc decides onIndex
due to the lack ofIndexMut
and then tries to take a mutable reference to the result of the method call (which is where yourDeref
comes from:&mut Index::index(&map, "peter")
)cc @rust-lang/wg-compiler-nll any ideas how to do this in the MIR borrowchecker? Right now rustc (with the nll feature gate active) produces
which actually is less informative than the message of old borrowck even before this PR:
cannot borrow immutable indexed content as mutable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the thing to do would be to look at the
&
reference and try to figure out where it came from. It'll probably be a temporary here, which is a good indicator that we should try to get more info about it. We'd see it as the result of anIndex
call -- that is probably a good signal for us to insert this advice, since unless the call toIndex
was manually written by the user, it must imply thatIndexMut
does not exist.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
maybe worth filing a bug for it?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your advice! Would it be possible to create a bug about this and then merge this PR as is? I don't think I will have time to work on this anytime soon. Then someone else can work on adding the same advice to the NLL version.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Filed #53228 to follow up.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks :)