Skip to content

Make suggestion include the line number when the suggestion span is ambiguous #39152

Description

@estebank

Given a diagnostic builder using span_labels 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_suggestions (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 one span_labels 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');
   |         ^^^^^^

Activity

  1. added
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    on Mar 9, 2017
  2. added a commit that references this issue on Jul 6, 2017
    d2ebb12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions