Skip to content
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

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

Closed
estebank opened this issue Jan 18, 2017 · 0 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@estebank
Copy link
Contributor

estebank commented Jan 18, 2017

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');
   |         ^^^^^^
@sfackler sfackler added the A-diagnostics Area: Messages for errors, warnings, and lints label Jan 18, 2017
@steveklabnik steveklabnik added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Mar 9, 2017
bors added a commit that referenced this issue Jul 6, 2017
Make suggestion include the line number

When there're more than one suggestions in the same diagnostic, they are
displayed in their own block, instead of inline. In order to reduce
confusion, those blocks now display the line number.

New output:

```
error[E0308]: mismatched types
  --> ../../src/test/ui/block-result/unexpected-return-on-unit.rs:19:5
   |
19 |     foo()
   |     ^^^^^ expected (), found usize
   |
   = note: expected type `()`
              found type `usize`
help: did you mean to add a semicolon here?
   |
19 |     foo();
   |          ^
help: possibly return type missing here?
   |
18 | fn bar() -> usize {
   |          ^^^^^^^^

error: aborting due to previous error(s)
```

Fix #39152.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants