Skip to content

Commit 24abef3

Browse files
Add and update tests for IndexMut help message
1 parent 764d472 commit 24abef3

File tree

4 files changed

+76
-0
lines changed

4 files changed

+76
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
error[E0596]: cannot borrow data in a `&` reference as mutable
2+
--> $DIR/index-mut-help.rs:21:5
3+
|
4+
LL | map["peter"].clear(); //~ ERROR
5+
| ^^^^^^^^^^^^ cannot borrow as mutable
6+
7+
error[E0594]: cannot assign to data in a `&` reference
8+
--> $DIR/index-mut-help.rs:22:5
9+
|
10+
LL | map["peter"] = "0".to_string(); //~ ERROR
11+
| ^^^^^^^^^^^^ cannot assign
12+
13+
error[E0596]: cannot borrow data in a `&` reference as mutable
14+
--> $DIR/index-mut-help.rs:23:13
15+
|
16+
LL | let _ = &mut map["peter"]; //~ ERROR
17+
| ^^^^^^^^^^^^^^^^^ cannot borrow as mutable
18+
19+
error: aborting due to 3 previous errors
20+
21+
Some errors occurred: E0594, E0596.
22+
For more information about an error, try `rustc --explain E0594`.
+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// When mutably indexing a type that implements `Index` but not `IndexMut`, a
12+
// special 'help' message is added to the output.
13+
14+
15+
fn main() {
16+
use std::collections::HashMap;
17+
18+
let mut map = HashMap::new();
19+
map.insert("peter", "23".to_string());
20+
21+
map["peter"].clear(); //~ ERROR
22+
map["peter"] = "0".to_string(); //~ ERROR
23+
let _ = &mut map["peter"]; //~ ERROR
24+
}
+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
error[E0596]: cannot borrow immutable indexed content as mutable
2+
--> $DIR/index-mut-help.rs:21:5
3+
|
4+
LL | map["peter"].clear(); //~ ERROR
5+
| ^^^^^^^^^^^^ cannot borrow as mutable
6+
|
7+
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>`
8+
9+
error[E0594]: cannot assign to immutable indexed content
10+
--> $DIR/index-mut-help.rs:22:5
11+
|
12+
LL | map["peter"] = "0".to_string(); //~ ERROR
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
14+
|
15+
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>`
16+
17+
error[E0596]: cannot borrow immutable indexed content as mutable
18+
--> $DIR/index-mut-help.rs:23:18
19+
|
20+
LL | let _ = &mut map["peter"]; //~ ERROR
21+
| ^^^^^^^^^^^^ cannot borrow as mutable
22+
|
23+
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<&str, std::string::String>`
24+
25+
error: aborting due to 3 previous errors
26+
27+
Some errors occurred: E0594, E0596.
28+
For more information about an error, try `rustc --explain E0594`.

src/test/ui/issue-41726.stderr

+2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ error[E0596]: cannot borrow immutable indexed content as mutable
33
|
44
LL | things[src.as_str()].sort(); //~ ERROR cannot borrow immutable
55
| ^^^^^^^^^^^^^^^^^^^^ cannot borrow as mutable
6+
|
7+
= help: trait `IndexMut` is required to modify indexed content, but it is not implemented for `std::collections::HashMap<std::string::String, std::vec::Vec<std::string::String>>`
68

79
error: aborting due to previous error
810

0 commit comments

Comments
 (0)