Closed
Description
Given a diagnostic builder using span_label
s with the following output:
error: cannot borrow immutable borrowed content `*self.s` as mutable
--> $DIR/issue-38147-3.rs:17:9
|
12 | s: &'a String
| ------------- this field should be `&mut`
...|
16 | fn f(&self) {
| ----- use `&mut self` here to make mutable
17 | self.s.push('x');
| ^^^^^^
If it is changed to use span_suggestion
s (as I believe is the intention going forward), I see a couple of problems:
error: cannot borrow immutable borrowed content `*self.s` as mutable
--> $DIR/issue-38147-3.rs:17:9
|
12 | s: &'a String
| ------------- this field should be `&mut`
...
17 | self.s.push('x');
| ^^^^^^
|
help: try making this a mutable borrow
| fn f(&mut self) {
The span being replaced in the suggestion is not part of the main body of the diagnostic and the line number where the change is suggested is not present. Because of this, it obscures where the suggested code should be written. This is exacerbated by the out of order presentation.
There're a few possible solutions:
- If the
span_suggestion
contains a span not in the main body or there're more than onespan_label
s in the diagnostic, show the line number - Change the suggestion helps to appear inline with the original code above it:
error: cannot borrow immutable borrowed content `*self.s` as mutable
--> $DIR/issue-38147-3.rs:17:9
|
12 | s: &'a String
| ------------- this field should be `&mut`
...
16 | fn f(&self) {
| ----- try making this a mutable borrow as follows:
| fn f(&mut self) {
| ---------
17 | self.s.push('x');
| ^^^^^^
- Change the suggestion helps to appear inline:
error: cannot borrow immutable borrowed content `*self.s` as mutable
--> $DIR/issue-38147-3.rs:17:9
|
12 | s: &'a String
| ------------- this field should be `&mut`
...
16 | fn f(&mut self) {
| --------- try making this a mutable borrow
17 | self.s.push('x');
| ^^^^^^